Repository navigation
fix(MobileMenuToggle): trap keyboard focus inside the mobile menu #3911
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
Merged
delucis
merged 10 commits into
withastro:main
from
timothyjordan:fix/mobile-menu-focus-trap-2697
Jul 2, 2026
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cb0c019
fix(MobileMenuToggle): trap keyboard focus inside the mobile menu
timothyjordan 64f379d
fix(MobileMenuToggle): use `inert` to contain focus inside the mobile…
timothyjordan 401c5a8
Tweak changeset
delucis 0c51f84
Refactor menu toggle script
delucis 47d2f98
Revert CSS changes
delucis 7e9e043
Merge branch 'main' into fix/mobile-menu-focus-trap-2697
delucis 31c4d04
Small refactor
delucis 1f00d50
Add E2E tests for the focus trap behaviour
delucis db98cb4
Use getByRole instead of getByLabel in tests
delucis e1b4be9
Test the focus trap is released when the mobile menu is closed
delucis 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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@astrojs/starlight': patch | ||
| --- | ||
|
|
||
| Traps keyboard focus inside the mobile menu while it is open so that Tab and Shift+Tab cycle through the header and sidebar instead of falling through to the page below. |
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 |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import { expect, testFactory } from './test-utils'; | ||
|
|
||
| const test = testFactory('./fixtures/basics/'); | ||
|
|
||
| const mobileViewport = { width: 600, height: 900 }; | ||
|
|
||
| test.describe('mobile menu', () => { | ||
| test('traps Tab focus inside the menu while it is expanded', async ({ | ||
| page, | ||
| getProdServer, | ||
| }) => { | ||
| await page.setViewportSize(mobileViewport); | ||
| const starlight = await getProdServer(); | ||
| await starlight.goto('/headings'); | ||
|
|
||
| const wrapper = page.locator('starlight-menu-button'); | ||
| const toggle = wrapper.locator('button'); | ||
| await toggle.click(); | ||
| await expect(wrapper).toHaveAttribute('aria-expanded', 'true'); | ||
|
|
||
| // Collect the first and last visible focusable in the header `<nav>` using the | ||
| // same selector and visibility filter as the runtime focus-trap. | ||
| const [firstSelector, lastSelector] = await page.evaluate(() => { | ||
| const nav = document.querySelector<HTMLElement>('nav:has(starlight-menu-button)'); | ||
| if (!nav) throw new Error('Could not find the header <nav>.'); | ||
| const focusables = Array.from( | ||
| nav.querySelectorAll<HTMLElement>( | ||
| 'a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])' | ||
| ) | ||
| ).filter((el) => el.offsetParent !== null); | ||
| if (focusables.length < 2) { | ||
| throw new Error('Expected at least two focusable elements in the header nav.'); | ||
| } | ||
| const tagId = (el: HTMLElement) => { | ||
| if (!el.id) el.id = `_focus_test_${Math.random().toString(36).slice(2)}`; | ||
| return `#${CSS.escape(el.id)}`; | ||
| }; | ||
| return [tagId(focusables[0]!), tagId(focusables[focusables.length - 1]!)] as const; | ||
| }); | ||
|
|
||
| // Tab from the last focusable should wrap to the first. | ||
| await page.locator(lastSelector).focus(); | ||
| await page.keyboard.press('Tab'); | ||
| await expect(page.locator(firstSelector)).toBeFocused(); | ||
|
|
||
| // Shift+Tab from the first focusable should wrap to the last. | ||
| await page.locator(firstSelector).focus(); | ||
| await page.keyboard.press('Shift+Tab'); | ||
| await expect(page.locator(lastSelector)).toBeFocused(); | ||
| }); | ||
|
|
||
| test('does not trap Tab focus when the menu is closed', async ({ page, getProdServer }) => { | ||
| await page.setViewportSize(mobileViewport); | ||
| const starlight = await getProdServer(); | ||
| await starlight.goto('/headings'); | ||
|
|
||
| // Menu is closed by default — Tab from the toggle button should proceed | ||
| // to the page content rather than wrap back inside the nav. | ||
| const menuOpen = await page.evaluate(() => | ||
| document.body.hasAttribute('data-mobile-menu-expanded') | ||
| ); | ||
| expect(menuOpen).toBe(false); | ||
|
|
||
| await page.locator('starlight-menu-button button').focus(); | ||
| await page.keyboard.press('Tab'); | ||
|
|
||
| const stillInsideNav = await page.evaluate(() => | ||
| Boolean(document.activeElement?.closest('nav:has(starlight-menu-button)')) | ||
| ); | ||
| expect(stillInsideNav).toBe(false); | ||
| }); | ||
|
|
||
| test('closes on Escape and restores focus to the toggle button', async ({ | ||
| page, | ||
| getProdServer, | ||
| }) => { | ||
| await page.setViewportSize(mobileViewport); | ||
| const starlight = await getProdServer(); | ||
| await starlight.goto('/headings'); | ||
|
|
||
| const wrapper = page.locator('starlight-menu-button'); | ||
| const toggle = wrapper.locator('button'); | ||
| await toggle.click(); | ||
| await expect(wrapper).toHaveAttribute('aria-expanded', 'true'); | ||
|
|
||
| await page.keyboard.press('Escape'); | ||
| await expect(wrapper).toHaveAttribute('aria-expanded', 'false'); | ||
| await expect(toggle).toBeFocused(); | ||
| }); | ||
| }); |
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.