Skip to content

Instantly share code, notes, and snippets.

@MikeIbberson
Created April 18, 2023 14:03
Show Gist options
  • Save MikeIbberson/1dbf387f5b67591a084568da56c8cfd9 to your computer and use it in GitHub Desktop.
Save MikeIbberson/1dbf387f5b67591a084568da56c8cfd9 to your computer and use it in GitHub Desktop.
Safari date inputs
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