JavaScript polyfill for focusOptions, an optional property passed to HTMLElement.focus() that contains the preventScroll boolean, which controls whether the browser should prevent a focused element from being scrolled into view.
-
WHATWG Spec
-
MDN Web Docs - focus
-
Chrome/Chromium: Chrome Platform Status issue 734166 - Prevent scrolling in HTMLElement.focus()
-
Edge: Issue 14314565 - Enable ability to prevent scrolling in Element.focus()
-
Firefox: Bug 1374045 - Consider adding support for customizing scrolling behavior with Element.focus
-
Safari: WebKit Bug 178583 - Enable ability to prevent scrolling in Element.focus()
https://bugs.webkit.org/show_bug.cgi?id=178583
This polyfill uses a basic fallback for the document.scrollingElement property, using document.documentElement when not found.
This could suffice in basic cases, but if you need wider and/or specific support you should refer to a polyfill for it:
Also, to overcome its absence if you are executing this polyfill through jsdom, you could place this in your setup:
document.scrollingElement = document.documentElementMore context about this property can be found in:
- https://developer.mozilla.org/en-US/docs/Web/API/Document/scrollingElement
- https://developer.mozilla.org/en-US/docs/Web/HTML/Quirks_Mode_and_Standards_Mode
- https://developer.mozilla.org/en-US/docs/Web/API/Document/documentElement
To check this polyfill you can do:
npm run devto launch a small page that will check browser/polyfill behaviour.