Last active
March 9, 2026 14:47
-
-
Save patrickhlauke/7624f9a5596bdf75f519b5655e43b486 to your computer and use it in GitHub Desktop.
Scrollable containers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function () { | |
| /* Scroll wrapper | |
| Finds all `.scroll-wrapper` elements, adds a `role="region"` and `aria-label="Scrollable content"` attribute, | |
| and sets up a resize observer to add a `tabindex="0"` attribute if the table is scrollable. | |
| Note: Chrome and Firefox now automatically make elements with `overflow` keyboard-focusable, so the script | |
| here is mostly redundant (though it does add nice `role` and `aria-label`); Safari, however, does not, | |
| so it's still needed. | |
| */ | |
| const scrollWrapperResizeObserver = new ResizeObserver((entries) => { | |
| for (const entry of entries) { | |
| if (entry.target.scrollHeight > entry.target.clientHeight || entry.target.scrollWidth > entry.target.clientWidth) { | |
| entry.target.setAttribute('tabindex','0'); | |
| } else { | |
| entry.target.removeAttribute('tabindex'); | |
| } | |
| } | |
| }); | |
| window.addEventListener('DOMContentLoaded', (event) => { | |
| const scrollers = document.querySelectorAll('.scroll-wrapper'); | |
| scrollers.forEach((scrollWrapper) => { | |
| if (!scrollWrapper.hasAttribute('role')) { | |
| scrollWrapper.setAttribute('role','region'); | |
| } | |
| if (!scrollWrapper.hasAttribute('aria-label')) { | |
| scrollWrapper.setAttribute('aria-label','Scrollable content'); | |
| } | |
| scrollWrapperResizeObserver.observe(scrollWrapper); | |
| }); | |
| }); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment