Skip to content

Commit

Permalink
feat(GlobalNav): Focus on menu button when hitting escape in the subm… (
Browse files Browse the repository at this point in the history
#1885)

* feat(GlobalNav): Focus on menu button when hitting escape in the submenu while focused.

* Fixed linting

* Watch the header directly

* Changed keyup to keydown, cleanup

* Removed tab key code checker (unused)
  • Loading branch information
kotva006 authored Feb 15, 2024
1 parent cd5e120 commit 6b8026a
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions assets/ts/app/global-navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,28 @@ export function setup(rootElement: HTMLElement): void {

// Closes veil before navigating to search result
document.addEventListener("autocomplete:selected", closeAllMenus);

// Press Esc within open header should return focus to header
header.addEventListener("keydown", e => {
let activeNavButton: HTMLButtonElement | null | undefined;
const activeNavSection = document.activeElement?.closest(
"[data-nav='desktop-section']"
);
if (activeNavSection) {
const openSectionId = activeNavSection.parentElement!.id;
activeNavButton = document.querySelector<HTMLButtonElement>(
`nav.m-menu--desktop [aria-controls=${openSectionId}]`
);
handleNativeEscapeKeyPress(e, () => {
resetPage();
if (activeNavButton) {
activeNavButton.focus();
// don't bubble up to the rootElement keydown listener
e.stopPropagation();
}
});
}
});
}

export default function setupGlobalNavigation(): void {
Expand Down

0 comments on commit 6b8026a

Please sign in to comment.