diff --git a/apps/app/e2e/shell/editor-shell-layout.spec.ts b/apps/app/e2e/shell/editor-shell-layout.spec.ts index af9a2122..1ae7222c 100644 --- a/apps/app/e2e/shell/editor-shell-layout.spec.ts +++ b/apps/app/e2e/shell/editor-shell-layout.spec.ts @@ -94,7 +94,8 @@ async function expectSheetWithin(page: Page, viewportWidth: number) { const layer = page.locator('[data-place="citations"]') const box = await layer.boundingBox() expect(box!.width).toBeLessThanOrEqual(34 * REM + 0.5) - expect(box!.width).toBeLessThanOrEqual(viewportWidth) + // Sub-pixel layout rounding (375.00001…): the same tolerance as the line above. + expect(box!.width).toBeLessThanOrEqual(viewportWidth + 0.5) const edges = await page.evaluate(() => { const sheet = document.querySelector('[data-place="citations"]')! const rect = sheet.getBoundingClientRect() diff --git a/apps/app/src/components/editor/useMoreMenu.ts b/apps/app/src/components/editor/useMoreMenu.ts index d14d6172..a3a53875 100644 --- a/apps/app/src/components/editor/useMoreMenu.ts +++ b/apps/app/src/components/editor/useMoreMenu.ts @@ -110,22 +110,33 @@ function useBackRequest(open: boolean, close: () => void) { }, [open, close]) } +const inMenu = (target: EventTarget | null) => + target instanceof Element && target.closest(MENU_PARTS) !== null + /** - * Keyboard focus that leaves the trigger and the list closes « Plus »: the open overlay - * would cover the control the focus reached (WCAG 2.4.11). A focus that goes nowhere in - * the page (the browser's own interface) leaves it open. + * Tab that moves the focus out of the trigger and the list closes « Plus »: the open + * overlay would cover the control the focus reached (WCAG 2.4.11). Only a Tab move counts: + * a focus the page places elsewhere (the canvas, a tap that keeps it there) leaves the + * menu open, so Escape still closes it and returns the focus to « Plus ». A Tab that + * reaches the browser's own interface leaves it open too. */ -function useFocusLeave(open: boolean, close: () => void) { +function useTabLeave(open: boolean, close: () => void) { useEffect(() => { if (!open) return - const onOut = (event: FocusEvent) => { - const from = event.target instanceof Element ? event.target : null - const to = event.relatedTarget - if (!from?.closest(MENU_PARTS) || !(to instanceof Element)) return - if (!to.closest(MENU_PARTS)) close() + let tabbing = false + const onKey = (event: KeyboardEvent) => { + tabbing = event.key === 'Tab' && inMenu(event.target) + } + const onIn = (event: FocusEvent) => { + if (tabbing && !inMenu(event.target)) close() + tabbing = false + } + document.addEventListener('keydown', onKey, true) + document.addEventListener('focusin', onIn, true) + return () => { + document.removeEventListener('keydown', onKey, true) + document.removeEventListener('focusin', onIn, true) } - document.addEventListener('focusout', onOut, true) - return () => document.removeEventListener('focusout', onOut, true) }, [open, close]) } @@ -134,5 +145,5 @@ export function useMoreDismiss(open: boolean, close: () => void) { useMoreEscape(open, close) useOutsidePress(open, close) useBackRequest(open, close) - useFocusLeave(open, close) + useTabLeave(open, close) } diff --git a/apps/app/tests/unit/editor-more-menu.test.tsx b/apps/app/tests/unit/editor-more-menu.test.tsx index c4dfa0f3..70bbaab9 100644 --- a/apps/app/tests/unit/editor-more-menu.test.tsx +++ b/apps/app/tests/unit/editor-more-menu.test.tsx @@ -233,16 +233,28 @@ describe('the platform back request', () => { }) describe('final review: keyboard focus leaving « Plus » (M-8)', () => { - it('closes when the focus moves out of the trigger and the list', async () => { + it('closes when Tab moves the focus out of the trigger and the list', async () => { const { user } = mount() await user.click(more()) - const item = screen.getByRole('button', { name: 'Constats' }) - act(() => item.focus()) - expect(more()).toHaveAttribute('aria-expanded', 'true') + const items = [...list()!.querySelectorAll('button')] act(() => more().focus()) + await user.tab() + expect(items[0]).toHaveFocus() expect(more()).toHaveAttribute('aria-expanded', 'true') - act(() => screen.getByRole('button', { name: 'outside' }).focus()) + act(() => items.at(-1)!.focus()) + await user.tab() + expect(document.activeElement?.closest('#editor-tabs-more')).toBeNull() expect(more()).toHaveAttribute('aria-expanded', 'false') await gone() }) + + it('a focus placed elsewhere by the page keeps it open: Escape closes it and returns the focus', async () => { + const { user } = mount() + await user.click(more()) + act(() => screen.getByRole('button', { name: 'outside' }).focus()) + expect(more()).toHaveAttribute('aria-expanded', 'true') + await user.keyboard('{Escape}') + expect(more()).toHaveAttribute('aria-expanded', 'false') + expect(more()).toHaveFocus() + }) })