Allow Modal to place focus on first element within contents via new API#54590
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 🙏 |
|
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
Modalto 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
focusOnMountRefon the Modal contents<div>whenfirstContentElementis passed asfocusOnMountprop. This ensures the focus is attempted within the Modal's contents.Testing Instructions
Rename.inputand not onClosebuttonClosebutton and constrained tabbing is still active.Testing Instructions for Keyboard
✍️ Dev Note
The
Modal' component'sfocusOnMountprop has received an update, and it now accepts a new"firstContentElement"option. When settingfocusOnMount="firstContentElement", theModalcomponent will try to focus on the first tabbable element within theModal's content (ie. the markup passed via thechildren` 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.