2020-07-19 16:15:11 +02:00
|
|
|
// TODO: Move to external library (https://github.com/calvellido/focus-options-polyfill)
|
2019-10-19 23:36:59 +03:00
|
|
|
// Polyfill to add support for preventScroll by focus function
|
|
|
|
|
|
|
|
if (HTMLElement.prototype.nativeFocus === undefined) {
|
|
|
|
(function () {
|
|
|
|
var supportsPreventScrollOption = false;
|
|
|
|
try {
|
2020-05-04 12:44:12 +02:00
|
|
|
var focusElem = document.createElement('div');
|
2019-10-19 23:36:59 +03:00
|
|
|
|
2020-05-04 12:44:12 +02:00
|
|
|
focusElem.addEventListener('focus', function(event) {
|
2019-10-19 23:36:59 +03:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
}, true);
|
|
|
|
|
2020-05-04 12:44:12 +02:00
|
|
|
var opts = Object.defineProperty({}, 'preventScroll', {
|
2020-02-25 18:33:38 -05:00
|
|
|
// eslint-disable-next-line getter-return
|
2019-10-19 23:36:59 +03:00
|
|
|
get: function () {
|
|
|
|
supportsPreventScrollOption = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
focusElem.focus(opts);
|
2019-11-27 14:13:16 +03:00
|
|
|
} catch (e) {
|
2020-05-04 12:44:12 +02:00
|
|
|
console.error('error checking preventScroll support');
|
2019-11-27 14:13:16 +03:00
|
|
|
}
|
2019-10-19 23:36:59 +03:00
|
|
|
|
|
|
|
if (!supportsPreventScrollOption) {
|
|
|
|
HTMLElement.prototype.nativeFocus = HTMLElement.prototype.focus;
|
|
|
|
|
|
|
|
HTMLElement.prototype.focus = function(options) {
|
|
|
|
var scrollX = window.scrollX;
|
|
|
|
var scrollY = window.scrollY;
|
|
|
|
|
|
|
|
this.nativeFocus();
|
|
|
|
|
|
|
|
// Restore window scroll if preventScroll
|
|
|
|
if (options && options.preventScroll) {
|
|
|
|
window.scroll(scrollX, scrollY);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
}
|