Fix: Escape key dismisses hover-opened submenus (WCAG 2.1 SC 1.4.13)#968
Open
Contributolo wants to merge 4 commits into
Open
Fix: Escape key dismisses hover-opened submenus (WCAG 2.1 SC 1.4.13)#968Contributolo wants to merge 4 commits into
Contributolo wants to merge 4 commits into
Conversation
Add functionality to dismiss hover-opened submenus with Escape key.
Add styles for submenu dismissal on Escape key press.
gilbert-hernandez
requested changes
Apr 28, 2026
| (function () { | ||
| const focusableElementsString = | ||
| 'a[href], area[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), iframe, object, embed, [tabindex="0"], [contenteditable]'; | ||
| const hoveredItems = new Set(); |
Contributor
There was a problem hiding this comment.
Could you please fix the indentation. there is an extra tab.
Author
There was a problem hiding this comment.
Thanks for the review! All three points addressed:
- Fixed the extra tab on
const hoveredItems. - Added the vertical orientation guard at the top of
initHoverEscDismiss. - Scoped the
kb-nav-esc-closerule to.navigation-desktop-orientation-horizontal, matching the existing hover rule scope.
| // Escape key dismissal for hover-opened submenus (WCAG 2.1 SC 1.4.13). | ||
| .menu-item.kb-nav-esc-close > ul.sub-menu { | ||
| display: none !important; | ||
| } |
Contributor
There was a problem hiding this comment.
The new CSS is global but should be limited to the horizontal orientation as hover based submenus only exist on horizontal navigations. It should look more like:
.navigation-desktop-orientation-horizontal .menu-item.kb-nav-esc-close > ul.sub-menu {
display: none !important;
}
Author
There was a problem hiding this comment.
Thanks for the review! All three points addressed:
- Fixed the extra tab on
const hoveredItems. - Added the vertical orientation guard at the top of
initHoverEscDismiss. - Scoped the
kb-nav-esc-closerule to.navigation-desktop-orientation-horizontal, matching the existing hover rule scope.
| * CSS :hover cannot be removed programmatically, so a class-based override | ||
| * is applied and cleaned up when the user re-engages with the menu item. | ||
| */ | ||
| const initHoverEscDismiss = function (nav) { |
Contributor
There was a problem hiding this comment.
Could you also add a guard here to make sure you don't add the listeners to the vertical orientation.
// Hover submenus only exist on horizontal orientation.
if (nav.classList.contains('is-vertical')) {
return;
}
Author
There was a problem hiding this comment.
Thanks for the review! All three points addressed:
- Fixed the extra tab on
const hoveredItems. - Added the vertical orientation guard at the top of
initHoverEscDismiss. - Scoped the
kb-nav-esc-closerule to.navigation-desktop-orientation-horizontal, matching the existing hover rule scope.
…uard - Fix extra tab on hoveredItems declaration - Skip listeners on vertical navigation orientation
Hover-opened submenus only exist in horizontal orientation, matching the existing :hover rule scope.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The Navigation (Adv) Block already handles
Escapefor submenus opened via toggle button click (keyCode === 27inkb-navigation-block.js). However, submenus opened via CSS:hovercannot be dismissed withEscape.The CSS rule responsible:
Since
:hoveris a CSS pseudo-class that cannot be removed via JavaScript, pressingEscapewhile hovering a submenu has no effect.This violates:
Since June 2025, the European Accessibility Act (EAA) makes WCAG 2.1 AA compliance a legal requirement for commercial websites in the EU.
Solution
.menu-item-has-childrenelements viamouseenter/mouseleave.keydownlistener forEscapethat adds akb-nav-esc-closeclass to the deepest hovered menu item..menu-item.kb-nav-esc-close > ul.sub-menu { display: none !important }overrides the:hovervisibility. The!importantis necessary because CSS:hovercannot be programmatically disabled.mouseenter(re-hover) ormouseleave(pointer exits), restoring normal behavior.Escapehandler for toggle-opened submenus (keyboard focus inside submenu) is unchanged; the new global handler skips if focus is already inside asub-menu.Changes
src/assets/js/kb-navigation-block.js: AddedhoveredItemstracking,initHoverEscDismiss(), andhandleHoverEscDismiss().src/blocks/navigation/style.scss: Added.kb-nav-esc-closeoverride rule.Testing
Escape-- submenu closes. Move pointer away and back -- submenu reopens normally.Escape-- submenu closes, focus returns to toggle button (existing behavior, unchanged).Escape-- only the deepest submenu closes.