-
Notifications
You must be signed in to change notification settings - Fork 39
PLANET-8026 Trapped focus within mobile nav menu when open #2853
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
Open
Osong-Michael
wants to merge
3
commits into
main
Choose a base branch
from
main-nav-trap-focus-mobile
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+95
−29
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,13 +5,16 @@ const NAV_DONATE_CLASS = '.nav-donate'; | |
| const NAV_SUBMENU_CLASS = '.nav-submenu'; | ||
| const SITE_LOGO_CLASS = '.site-logo'; | ||
| const NAV_MENU_CLOSE_CLASS = '.nav-menu-close'; | ||
| const MOBILE_NAV_ID = '#nav-main'; | ||
| const PAGE_WRAPPER_ID = '#content'; | ||
| const NAV_MENU_TOGGLE_CLASS = '.nav-menu-toggle'; | ||
|
|
||
| /** | ||
| * Function to handle keyboard accessibility in the navigation menu. | ||
| */ | ||
| export const setupAccessibleNavMenu = () => { | ||
| const mainNav = document.querySelector('#nav-main-desktop'); | ||
| const mobileNav = document.querySelector('#nav-main'); | ||
| const mobileNav = document.querySelector(MOBILE_NAV_ID); | ||
|
|
||
| if (!mainNav && !mobileNav) { | ||
| return; | ||
|
|
@@ -121,26 +124,38 @@ export const setupAccessibleNavMenu = () => { | |
| } | ||
|
|
||
| if (mobileNav) { | ||
| const doc = mobileNav.ownerDocument; | ||
| let lastFocusedElement = null; | ||
| const isMobileMenuOpen = () => mobileNav.classList.contains('open'); | ||
| /** | ||
| * Adds event listeners to create a keyboard trap between the buttons. | ||
| */ | ||
| const addKeyboardTrap = () => { | ||
| const donateBtn = mobileNav.querySelector('.btn-donate'); | ||
| const closeBtn = mobileNav.querySelector(NAV_MENU_CLOSE_CLASS); | ||
| const logo = mobileNav.querySelector(SITE_LOGO_CLASS); | ||
| const focusableSelectors = | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need a variable here? It's only used once |
||
| 'a[href], button:not([disabled]), [tabindex]:not([tabindex="-1"])'; | ||
|
|
||
| closeBtn.addEventListener('keydown', event => { | ||
| if (event.key === 'Tab' && event.shiftKey) { | ||
| event.preventDefault(); | ||
| setTimeout(() => donateBtn.focus(), 5); | ||
| const focusableElements = [...mobileNav.querySelectorAll(focusableSelectors)]; | ||
|
|
||
| if (!focusableElements.length) { | ||
| return; | ||
| } | ||
|
|
||
| const firstEl = focusableElements[0]; | ||
| const lastEl = focusableElements[focusableElements.length - 1]; | ||
|
|
||
| mobileNav.addEventListener('keydown', event => { | ||
| if (event.key !== 'Tab') { | ||
| return; | ||
| } | ||
| if (event.key === 'Tab') { | ||
| setTimeout(() => logo.focus(), 0); | ||
|
|
||
| if (event.shiftKey && doc.activeElement === firstEl) { | ||
| event.preventDefault(); | ||
| lastEl.focus({preventScroll: true}); | ||
| } | ||
| }); | ||
| logo.addEventListener('keydown', event => { | ||
| if (event.key === 'Tab' && event.shiftKey) { | ||
| setTimeout(() => closeBtn.focus(), 0); | ||
|
|
||
| if (!event.shiftKey && doc.activeElement === lastEl) { | ||
| event.preventDefault(); | ||
| firstEl.focus({preventScroll: true}); | ||
| } | ||
| }); | ||
| }; | ||
|
|
@@ -156,10 +171,18 @@ export const setupAccessibleNavMenu = () => { | |
| return; | ||
| } | ||
|
|
||
| hamburgerBtn.addEventListener('keydown', event => { | ||
| if (event.key === 'Enter') { | ||
| setTimeout(() => logo.focus(), 0); | ||
| } | ||
| hamburgerBtn.addEventListener('click', () => { | ||
mleray marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| lastFocusedElement = doc.activeElement; | ||
|
|
||
| // Wait for CSS class to apply | ||
| requestAnimationFrame(() => { | ||
| if (!isMobileMenuOpen()) { | ||
| return; | ||
| } | ||
|
|
||
| syncMobileNavAria(true); | ||
| logo.focus(); | ||
| }); | ||
| }); | ||
| }; | ||
|
|
||
|
|
@@ -168,21 +191,25 @@ export const setupAccessibleNavMenu = () => { | |
| */ | ||
| const focusLogoOnMenuClose = () => { | ||
| const closeBtn = mobileNav.querySelector(NAV_MENU_CLOSE_CLASS); | ||
| const hamburgerLogo = mobileNav.querySelector(SITE_LOGO_CLASS); | ||
| const mainLogo = document.querySelector(`#header ${SITE_LOGO_CLASS}`); | ||
| const toggleBtn = document.querySelector(NAV_MENU_TOGGLE_CLASS); | ||
|
|
||
| if (!mainLogo || !hamburgerLogo || !closeBtn) { | ||
| if (!closeBtn || !toggleBtn) { | ||
| return; | ||
| } | ||
|
|
||
| closeBtn.addEventListener('keydown', event => { | ||
| if (event.key === 'Enter') { | ||
| setTimeout(() => mainLogo.focus(), 0); | ||
| } | ||
| if (event.key === 'Tab' && event.shiftKey) { | ||
| setTimeout(() => hamburgerLogo.focus(), 0); | ||
| } | ||
| closeBtn.addEventListener('click', () => { | ||
| const page = document.querySelector(PAGE_WRAPPER_ID); | ||
| page.removeAttribute('aria-hidden'); | ||
| page.inert = false; | ||
|
|
||
|
|
||
| requestAnimationFrame(() => { | ||
| (lastFocusedElement || toggleBtn).focus(); | ||
| mobileNav.setAttribute('aria-hidden', 'true'); | ||
| toggleBtn.setAttribute('aria-expanded', 'false'); | ||
| }); | ||
| }); | ||
|
|
||
| }; | ||
|
|
||
| addKeyboardTrap(); | ||
|
|
@@ -209,3 +236,36 @@ export const updateNavMenuTabIndex = () => { | |
| ]; | ||
| tabbingItems.forEach(item => item.setAttribute('tabindex', menu.classList.contains('open') ? 0 : -1)); | ||
| }; | ||
|
|
||
| /** | ||
| * Function to update aria attributes for mobile navigation. | ||
| * @param {boolean} isOpen - Whether the mobile navigation is open. | ||
| */ | ||
| const syncMobileNavAria = isOpen => { | ||
| const mobileNav = document.querySelector(MOBILE_NAV_ID); | ||
| const page = document.querySelector(PAGE_WRAPPER_ID); | ||
| const toggleBtn = document.querySelector(NAV_MENU_TOGGLE_CLASS); | ||
|
|
||
| if (!mobileNav || !page || !toggleBtn) { | ||
| return; | ||
| } | ||
|
|
||
| // Mobile menu | ||
| if (isOpen) { | ||
| mobileNav.removeAttribute('aria-hidden'); | ||
| } else { | ||
| mobileNav.setAttribute('aria-hidden', 'true'); | ||
| } | ||
|
|
||
| // Page behind | ||
| if (isOpen) { | ||
| page.setAttribute('aria-hidden', 'true'); | ||
| page.inert = true; | ||
| } else { | ||
| page.removeAttribute('aria-hidden'); | ||
| page.inert = false; | ||
| } | ||
|
|
||
| // Toggle button state | ||
| toggleBtn.setAttribute('aria-expanded', String(isOpen)); | ||
| }; | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.