Improve the Scrollable component accessibility
Description
Browsers have different native behaviors when it comes to elements made scrollable with CSS overflow:
- Firefox: the scrollable element is focusable.
- Webkit-based browsers (Chrome, Safari, Opera, etc.): the scrollable element is not focusable.
For keyboard users, it is essential to be able to scroll a scrollable element to see all its content. When the element contains focusable elements, more or less users are able to make the element scroll by tabbing through the focusable elements. However, when the scrollable element does not contain focusable elements, there's no way for keyboard users to scroll. See for example the Keyboard shortcuts modal: https://github.com/WordPress/gutenberg/issues/41500
As said above, the underlying issue comes from different browsers behavior. Regardless, we should make sure keyboard users can always access the content of a Scrollable component.
There's a simple technique to remediate see this note by the Paciello group:
Short note on improving usability of scrollable regions https://www.tpgi.com/short-note-on-improving-usability-of-scrollable-regions/
- Add a
tabindex=0to include the scrollable element in the tab sequence for all browsers. - Add a clear focus style to the scrollable element.
- This allows sighted keyboard users to move focus to the scrollable element and then use the arrow keys to scroll. Other native keyboard shortcuts e.g. Scrollbar, Shift+Scrollbar, Fn+Down arrow/Up Arrow work as well.
- However, at this point, the element would be a silent tab stop for screen reader users. When tabbing to the scrollable element, nothing would be announced.
- To remediate, an appropriate ARIA role and label need to be provided.
The Paciello group recommends to use role=region and an aria-label with a meaningful value. That seems fine to me, with a little drawback: an element with role=region and an aria-label becomes an ARIA landmark. As such, it will be listed by screen readers in the page landmarks. Ideally, landmarks should be used only to mark the main regions of a page. This seems a minor annoyance compared to the greater benefit users will get from being able to scroll.
By fixing the accessibility of the Scrollable component, we could also consider to re-use it in more places to fix issues like #41500.
Worth also mentioning the technique to make scrollable elements accessible is already used in Core, see for example https://core.trac.wordpress.org/ticket/49211
Step-by-step reproduction instructions
- Check the Scrollable component in the Storybook at https://wordpress.github.io/gutenberg/?path=/story/components-experimental-scrollable--default
- Use Firefox and tab through the page: see the Scrollable element receives focus:
- Repeat the test using any other Webkit-based browser, e.g. Chrome.
- See the Scrollable element is not focusable (focus lands on the button within the Scrollable):
Screenshots, screen recording, code snippet
No response
Environment info
No response
Please confirm that you have searched existing issues in the repo.
Yes
Please confirm that you have tested with all plugins deactivated except Gutenberg.
Yes
One more thing: if the Scrollable element is made focusable, there will be the need to check if and how that may impact the focus.focusable and focus.tabbable DOM utilities from the @wordpress/dom package.
This is a great plan and definitely needed. I agree with your statement about the role="region" and aria-label usage. I find silent tab stops mildly disconcerting, particularly in a workspace/editing environment or when focus has shifted, like in the keyboard shortcut modal.
https://github.com/WordPress/gutenberg/pull/47426 is going to introduce some of the logic needed for an accessible scrollable to fix bugs within the Modal component. That logic should be better abstracted here for the Scrollable component. As suggested in https://github.com/WordPress/gutenberg/pull/47426#discussion_r1093386574 a hook would probably be the best approach.
Also, it would be great to have https://github.com/WordPress/gutenberg/pull/41001 in place to see if the accessible Scrollable can use the useResizeObserver hook instead of a custom implementation of ResizeObserver.
After conversation on https://github.com/WordPress/gutenberg/issues/67959 we did discover the default behavior in Chrome changed with the release 130 on October 2024. The new Chrome behavior is closer to the Firefox one bus still not the same behavior.
Source: https://developer.chrome.com/blog/keyboard-focusable-scrollers
Prior to this change, a scroller element can only be tab focused if the tabindex is explicitly set to 0
From Chrome 130, the new default:
allows scrollers without tabindex value set and without any focusable children to be keyboard focusable. This allows users who cannot or choose not to use a mouse to focus content using a keyboard's tab and arrow keys.
Note that this behavior only happens if the scroller has no focusable children. For example, if the scroller already contains a button, then the tab focus will skip the scroller and focus on the button directly.
Emphasis mine:
Due to this rule, the default might not always behave in the most optimal way if such children exist. If you want the scroller element itself to be keyboard focusable in this situation, it is recommended to set a tabindex value of 0 or higher.
A simple codepen for testing: https://codepen.io/afercia/full/Expxgby
It is important to note that even with the new default behavior from Chrome 130, ths second scrollable in the test codepen can't be scrolled with the keyboard.
Because of:
if the scroller already contains a button, then the tab focus will skip the scroller
- Focus goes to the input field in the second scrollable.
- There is no way to scroll the scrollable element to see the full content by using only the keyboard.
Thanks for the thorough write-up! Scrollable has since been deprecated (#83273) in favour of plain CSS, so it won't get the focusable, labelled behaviour described here. Keyboard access to scroll containers is still important. If a specific scroll area in the editor can't be reached with the keyboard today, please open a focused issue for it.