-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow Modal to place focus on first element within contents via new API #54590
Conversation
Size Change: +2.74 kB (0%) Total Size: 1.62 MB
ℹ️ View Unchanged
|
Given that 16.7 is the final release before WP 6.4 I'd really like to land this API so we can fix the Modal focus for the Block Rename feature 🙏 |
IMO, I think it's still worth following up with a PR introducing a fail-safe fallback behaviour, since it just brings peace of mind to consumers of the modal. For example, let's say that:
I'm not saying that this is super urgent, but we should still plan on introducing this fallback behaviour sooner rather than later. |
It's going to be pretty challenging with the current implementation though as it relies on moving the focus ref between elements. Another option could be to |
Flaky tests detected in 1fd06ed. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6259582718
|
A |
Late to the party, sorry. I'm not sure this is going in the right direction.
This is a common misconception and, in a way, now considered an 'old' recommendation. I've been closely following the discussion about initial focus for the native It is very important to note that after long discussion the initial focus mechanism will be changed. If no specified otherwise, the Although the above relates to the native HTML Cc @joedolson @alexsanford @andrewhayward Aside: |
@afercia , The
Thank you for the advice. The |
…PI (#54590) * Add new firstContentElement variation to Modal * Update Story * Update e2e test * Add focus tests * Conditionally add ref * Provide comment to explain unusual code * Remove extra div element * Update docs * Tweak code comment documentation * Add test for firstElement * Refactor tests * Prefer getByRole * Improve README with additional context
I just cherry-picked this PR to the release/16.7 branch to get it included in the next release: 76e5214 |
Lack of knowledge and uncritically adoption of (long established) wrong naming, i guess. |
Warning: Type of PR label error To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.
Read more about Type labels in Gutenberg. |
Added a dev note in the PR description |
What?
Adds new API to
Modal
to allow focus to be placed on first element in the content on mount.Alternative to #54296
Closes #54106
Why?
As discussed in #54106 dialogs (including Modal) should ideally place focus on the first focusable element on mount. Currently all Modals find that the Close button is the first element in the DOM inside the Modal that is focusable and thus it receives focus. A11y feedback has been that this is unhelpful.
For further explanation as to how we arrived at this approach see
#54296 (comment)
How?
Conditionally places the
focusOnMountRef
on the Modal contents<div>
whenfirstContentElement
is passed asfocusOnMount
prop. This ensures the focus is attempted within the Modal's contents.Testing Instructions
Rename
.input
and not onClose
buttonClose
button and constrained tabbing is still active.Testing Instructions for Keyboard
✍️ Dev Note
The
Modal' component's
focusOnMountprop has received an update, and it now accepts a new
"firstContentElement"option. When setting
focusOnMount="firstContentElement", the
Modalcomponent will try to focus on the first tabbable element within the
Modal's content (ie. the markup passed via the
children` prop).This is different from the pre-existing
"firstElement"
option, which causes focus to go to the first element anywhere within theModal
, including its header (usually the close button).Note that it is the responsibility of the consumer to ensure that, when
focusOnMount="firstContentElement"
, there is at least one tabbable element within theModal
's children.