1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/legacy/focusPreventScroll.js

43 lines
1.3 KiB
JavaScript
Raw Normal View History

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);
}
};
}
})();
}