Skip to content

Commit fa69bee

Browse files
committed
fix(es-app): header menu Tab and focus-out close (anomalyco#229)
1 parent 8798f3f commit fa69bee

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

‎packages/es-app/src/components/header-menu.tsx‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ export const HeaderMenu = (props: { id: string; label: string; title?: string; i
2929
}
3030
document.addEventListener("pointerdown", outside, true)
3131
onCleanup(() => document.removeEventListener("pointerdown", outside, true))
32-
return <span class="header-menu">
32+
return <span class="header-menu" onFocusOut={(event) => {
33+
// Any focus move outside the menu root (Find shortcut, pointer elsewhere) closes it.
34+
const next = event.relatedTarget
35+
if (open() && !(next instanceof Node && event.currentTarget.contains(next))) close(false)
36+
}}>
3337
<button ref={trigger} class="header-icon" aria-label={props.label} title={props.title} aria-haspopup="menu"
3438
aria-expanded={open()} aria-controls={open() ? props.id : undefined}
3539
onClick={() => open() ? close(false) : show(0)}
@@ -54,7 +58,8 @@ export const HeaderMenu = (props: { id: string; label: string; title?: string; i
5458
close(true)
5559
return
5660
}
57-
if (event.key === "Tab") close(false)
61+
// Let native Tab/Shift+Tab continue from the trigger, not from an unmounting item.
62+
if (event.key === "Tab") close(true)
5863
}}>
5964
<For each={props.items}>{(item) =>
6065
<button role="menuitem" tabIndex={-1} onClick={() => { close(true); item.select() }}>{item.label}</button>

‎packages/es-app/test/session-header.browser.mjs‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const run = async (viewport, label, dir = "ltr", lang = "en") => {
1818
const page = await context.newPage()
1919
page.setDefaultTimeout(15_000)
2020
page.on("pageerror", (error) => errors.push(`${label}: ${error.message}`))
21-
await page.goto(preview.laneUrl, { waitUntil: "domcontentloaded" })
21+
await page.goto(preview.laneUrl, { waitUntil: "domcontentloaded", timeout: 60_000 })
2222
const header = page.locator(".topbar")
2323
const editor = page.getByRole("textbox", { name: "Message", exact: true })
2424
await expect(page.locator('[data-find-message="msg_1011"]')).toBeVisible({ timeout: 30_000 })
@@ -89,6 +89,30 @@ const run = async (viewport, label, dir = "ltr", lang = "en") => {
8989
await expect(content).toHaveCount(0)
9090
await expect(menu).toHaveAttribute("aria-expanded", "false")
9191
await expect(menu).toBeFocused()
92+
// Tab and Shift+Tab continue from the trigger and close the menu.
93+
const neighbour = async (shift) => page.evaluate((shift) => {
94+
const trigger = document.querySelector('[aria-label="Session mail"]')
95+
const focusable = [...document.querySelectorAll("button, [href], input, textarea, select, [tabindex]:not([tabindex='-1'])")]
96+
.filter((element) => !element.disabled && element.offsetParent !== null && element.tabIndex >= 0)
97+
return focusable[focusable.indexOf(trigger) + (shift ? -1 : 1)]?.outerHTML.slice(0, 120)
98+
}, shift)
99+
for (const shift of [false, true]) {
100+
await menu.press("Enter")
101+
await expect(content.getByRole("menuitem").first()).toBeFocused()
102+
const expected = await neighbour(shift)
103+
await page.keyboard.press(shift ? "Shift+Tab" : "Tab")
104+
await expect(content).toHaveCount(0)
105+
await expect(menu).toHaveAttribute("aria-expanded", "false")
106+
expect(await page.evaluate(() => document.activeElement?.outerHTML.slice(0, 120))).toBe(expected)
107+
}
108+
// The Find shortcut moves focus out of the open menu, which closes it.
109+
await menu.press("Enter")
110+
await expect(content).toBeVisible()
111+
await page.keyboard.press("Control+f")
112+
await expect(page.getByRole("searchbox", { name: "Find in transcript" })).toBeFocused()
113+
await expect(content).toHaveCount(0)
114+
await page.keyboard.press("Escape")
115+
await expect(page.getByRole("searchbox", { name: "Find in transcript" })).toHaveCount(0)
92116
await menu.click()
93117
await expect(content).toBeVisible()
94118
// The menu sits in the viewport and above the transcript, not behind the header.

0 commit comments

Comments
 (0)