-
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
Navigation Block: Use dom.focus for focus control #57362
Conversation
This pull request has changed or added PHP files. Please confirm whether these changes need to be synced to WordPress Core, and therefore featured in the next release of WordPress. If so, it is recommended to create a new Trac ticket and submit a pull request to the WordPress Core Github repository soon after this pull request is merged. If you're unsure, you can always ask for help in the #core-editor channel in WordPress Slack. Thank you! ❤️ View changed files❔ lib/compat/wordpress-6.5/class-wp-navigation-block-renderer.php |
Size Change: -52 B (0%) Total Size: 1.69 MB
ℹ️ View Unchanged
|
// Not Tested: overlay menu traps focus | ||
// Test: overlay menu traps focus | ||
await pageUtils.pressKeys( 'Tab', { times: 2, delay: 50 } ); | ||
await expect( closeMenuButton ).toBeFocused(); | ||
await pageUtils.pressKeys( 'Shift+Tab', { times: 2, delay: 50 } ); | ||
await expect( overlayMenuFirstElement ).toBeFocused(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't know why this test wasn't done in Safari, but let's try it and see if it works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the dom
package could significantly increase the bundle size (for a front-end script). Using plain JS might be better for the moment.
Thank you for teaching me. Certainly, I think that it can be modified with a simple code without using this package, so I will try it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Look good to me, both the code and the fix 🙂
@@ -428,27 +428,29 @@ private static function get_responsive_container_markup( $attributes, $inner_blo | |||
$responsive_dialog_directives = ''; | |||
$close_button_directives = ''; | |||
if ( $should_load_view_script ) { | |||
$open_button_directives = ' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked and these changes are still included in the WP_Navigation_Block_Renderer
which has now been moved to the block-library package and thus will be automatically synced to Core as part of the packages sync process.
✅ I updated the PHP Sync Tracking Issue for WP 6.5 to note this PR no longer requires a backport for WP 6.5. Why? Because the code was moved into the block-library package in #57979 and thus should be automatically handled by the packages sync process. |
Fixes #57359
What?
This PR avoids using
querySelector
/querySelectorAll
and usesfocus
from the@wordpress/dom
package for focus control in the navigation dialog. This fixes a focus error when the Navigation block has a search block, as reported in #57359.Why?
The following two things are expected in the Navigation block dialog:
Regarding the first point, it tries to find focusable elements in the dialog starting from the element with the
.wp-block-navigation-item
class. However, if it is just a Search block, there is no element with this class, so focus will fail and you will get a console error.How?
The@wordpress/dom
package has afocus
object that finds focusable elements. I used this and replacedquerySelector
/querySelectorAll
. This should always focus the appropriate first element, no matter what blocks the navigation block contains.Update: To avoid the increase in bundle size, I fixed the logic of a problem (searching for the first focusing element in the dialog) without using the
@wordpress/dom
package.
I also moved the
focusFirstElement
callback to the content wrapper div (.wp-block-navigation__responsive-container-content
) to accurately locate the first element (not including the close button) when the dialog opens.Before:
After:
Testing Instructions for Keyboard
I'm a Windows user, and I've confirmed it works in Chrome and Firefox browsers. I would be happy if you could confirm that it works as expected on Mac OS.
Tab
key to focus on the navigation button and pressEnter
.Screenshots or screencast
27ab37f5125e56c5a469012622c43858.mp4