2020-08-22 19:10:08 +02:00
|
|
|
// Polyfill to add support for preventScroll by focus function
|
|
|
|
|
|
|
|
if (HTMLElement.prototype.nativeFocus === undefined) {
|
|
|
|
(function () {
|
2020-10-07 21:12:14 +09:00
|
|
|
let supportsPreventScrollOption = false;
|
2020-08-22 19:10:08 +02:00
|
|
|
try {
|
2020-10-07 21:12:14 +09:00
|
|
|
const focusElem = document.createElement('div');
|
2020-08-22 19:10:08 +02:00
|
|
|
|
|
|
|
focusElem.addEventListener('focus', function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
}, true);
|
|
|
|
|
2020-10-07 21:12:14 +09:00
|
|
|
const opts = Object.defineProperty({}, 'preventScroll', {
|
2020-08-22 19:10:08 +02:00
|
|
|
// eslint-disable-next-line getter-return
|
|
|
|
get: function () {
|
|
|
|
supportsPreventScrollOption = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
focusElem.focus(opts);
|
|
|
|
} catch (e) {
|
|
|
|
console.error('error checking preventScroll support');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!supportsPreventScrollOption) {
|
|
|
|
HTMLElement.prototype.nativeFocus = HTMLElement.prototype.focus;
|
|
|
|
|
|
|
|
HTMLElement.prototype.focus = function(options) {
|
2020-10-07 21:12:14 +09:00
|
|
|
const scrollX = window.scrollX;
|
|
|
|
const scrollY = window.scrollY;
|
2020-08-22 19:10:08 +02:00
|
|
|
|
|
|
|
this.nativeFocus();
|
|
|
|
|
|
|
|
// Restore window scroll if preventScroll
|
|
|
|
if (options && options.preventScroll) {
|
|
|
|
window.scroll(scrollX, scrollY);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
}
|