Skip to content
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: Only focus submenu trigger on escape key press #41986

Merged
merged 2 commits into from
Jul 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/block-library/src/navigation-submenu/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ const closeSubmenus = ( element ) => {
.querySelectorAll( '[aria-expanded="true"]' )
.forEach( ( toggle ) => {
toggle.setAttribute( 'aria-expanded', 'false' );
// Always focus the trigger, this becomes especially useful in closing submenus with escape key to ensure focus doesn't get trapped.
toggle.focus();
} );
};

Expand Down Expand Up @@ -57,11 +55,13 @@ document.addEventListener( 'keyup', function ( event ) {
'.wp-block-navigation-submenu'
);
submenuBlocks.forEach( ( block ) => {
if (
! block.contains( event.target ) ||
( block.contains( event.target ) && event.key === 'Escape' )
) {
if ( ! block.contains( event.target ) ) {
closeSubmenus( block );
} else if ( event.key === 'Escape' ) {
const toggle = block.querySelector( '[aria-expanded="true"]' );
closeSubmenus( block );
// Focus the submenu trigger so focus does not get trapped in the closed submenu.
toggle?.focus();
}
} );
} );
12 changes: 6 additions & 6 deletions packages/block-library/src/navigation/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ function closeSubmenus( element ) {
.querySelectorAll( '[aria-expanded="true"]' )
.forEach( function ( toggle ) {
toggle.setAttribute( 'aria-expanded', 'false' );
// Always focus the trigger, this becomes especially useful in closing submenus with escape key to ensure focus doesn't get trapped.
toggle.focus();
} );
}

Expand Down Expand Up @@ -63,11 +61,13 @@ window.addEventListener( 'load', () => {
'.wp-block-navigation-item.has-child'
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see some room for performance improvements here, as this run for any keyup event on the page for example for any Tab key press or even when typing within an input field or textarea. It's part of the existing implementation though so I'll open a separate issue.

submenuBlocks.forEach( function ( block ) {
if (
! block.contains( event.target ) ||
( block.contains( event.target ) && event.key === 'Escape' )
) {
if ( ! block.contains( event.target ) ) {
closeSubmenus( block );
} else if ( event.key === 'Escape' ) {
const toggle = block.querySelector( '[aria-expanded="true"]' );
closeSubmenus( block );
// Focus the submenu trigger so focus does not get trapped in the closed submenu.
toggle?.focus();
}
} );
} );
Expand Down