Created
April 18, 2023 14:03
-
-
Save MikeIbberson/1dbf387f5b67591a084568da56c8cfd9 to your computer and use it in GitHub Desktop.
Safari date inputs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getAppRoot() { | |
return document.getElementById('___gatsby') | |
} | |
function withErrorSuppression(fn) { | |
return function() { | |
try { fn(); } catch (e) {} | |
} | |
} | |
var convertDateInputsOnIos = withErrorSuppression(function() { | |
getAppRoot() | |
.querySelectorAll('[type="date"]') | |
.forEach(node => { | |
node.setAttribute('placeholder', 'YYYY-MM-DD'); | |
node.setAttribute('type', 'text'); | |
}); | |
}); | |
var watchForDomChanges = withErrorSuppression(function() { | |
new MutationObserver(convertDateInputsOnIos).observe( | |
getAppRoot(), { | |
attributes: false, | |
childList: true, | |
subtree: true, | |
}); | |
}); | |
document.addEventListener("DOMContentLoaded", function() { | |
try { | |
if (window.safari !== undefined && window.MutationObserver) { | |
convertDateInputsOnIos(); | |
watchForDomChanges(); | |
console.log('Polyfill for Safari date inputs in effect'); | |
} | |
} catch (e) { | |
console.warn('Failed to polyfill Safari date inputs', e); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment