Skip to content

Commit

Permalink
Remove submenu Escape key handling as it automatically works via popo…
Browse files Browse the repository at this point in the history
…ver.
  • Loading branch information
felixarntz committed Feb 11, 2025
1 parent f8699ed commit 99f032e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 27 deletions.
1 change: 0 additions & 1 deletion packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,6 @@ function block_core_navigation_add_directives_to_submenu( $tags, $block_attribut
$tags->set_attribute( 'data-wp-interactive', 'core/navigation' );
$tags->set_attribute( 'data-wp-context', '{ "submenuOpenedBy": { "click": false, "hover": false, "focus": false }, "type": "submenu", "modal": null }' );
$tags->set_attribute( 'data-wp-on--focusout', 'actions.handleMenuFocusout' );
$tags->set_attribute( 'data-wp-on--keydown', 'actions.handleMenuKeydown' );
$tags->set_attribute( 'style', "anchor-name: --{$anchor_name};" );

// This is a fix for Safari. Without it, Safari doesn't change the active
Expand Down
45 changes: 19 additions & 26 deletions packages/block-library/src/navigation/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,32 +104,25 @@ const { state, actions } = store(
handleMenuKeydown( event ) {
const { type, firstFocusableElement, lastFocusableElement } =
getContext();
if ( state.menuOpenedBy.click ) {
// If Escape close the menu. For the overlay this works out of the box due to [popover].
if ( type === 'submenu' && event?.key === 'Escape' ) {
actions.closeMenu( 'click' );
actions.closeMenu( 'focus' );
return;
}

// Trap focus if it is an overlay (main menu).
if ( type === 'overlay' && event.key === 'Tab' ) {
// If shift + tab it change the direction.
if (
event.shiftKey &&
window.document.activeElement ===
firstFocusableElement
) {
event.preventDefault();
lastFocusableElement.focus();
} else if (
! event.shiftKey &&
window.document.activeElement ===
lastFocusableElement
) {
event.preventDefault();
firstFocusableElement.focus();
}
// Trap focus if it is an overlay (main menu).
if (
state.menuOpenedBy.click &&
type === 'overlay' &&
event.key === 'Tab'
) {
// If shift + tab it change the direction.
if (
event.shiftKey &&
window.document.activeElement === firstFocusableElement
) {
event.preventDefault();
lastFocusableElement.focus();
} else if (
! event.shiftKey &&
window.document.activeElement === lastFocusableElement
) {
event.preventDefault();
firstFocusableElement.focus();
}
}
},
Expand Down

0 comments on commit 99f032e

Please sign in to comment.