fix(editor): Plus Escape focus return, sheet width rounding

This commit is contained in:
bchanot committed 2026-11-13 23:58:59 +01:00
1 parent 021685f6b3
commit 3a7ffa2f8b
3 files changed
+42 -18

No files matched your search

@@ -94,7 +94,8 @@ async function expectSheetWithin(page: Page, viewportWidth: number) {
const layer = page.locator('[data-place="citations"]') const layer = page.locator('[data-place="citations"]')
const box = await layer.boundingBox() const box = await layer.boundingBox()
expect(box!.width).toBeLessThanOrEqual(34 * REM + 0.5) 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 edges = await page.evaluate(() => {
const sheet = document.querySelector('[data-place="citations"]')! const sheet = document.querySelector('[data-place="citations"]')!
const rect = sheet.getBoundingClientRect() const rect = sheet.getBoundingClientRect()
+23 -12
View File
@@ -110,22 +110,33 @@ function useBackRequest(open: boolean, close: () => void) {
}, [open, close]) }, [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 * Tab that moves the focus out of the trigger and the list closes « Plus »: the open
* would cover the control the focus reached (WCAG 2.4.11). A focus that goes nowhere in * overlay would cover the control the focus reached (WCAG 2.4.11). Only a Tab move counts:
* the page (the browser's own interface) leaves it open. * 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(() => { useEffect(() => {
if (!open) return if (!open) return
const onOut = (event: FocusEvent) => { let tabbing = false
const from = event.target instanceof Element ? event.target : null const onKey = (event: KeyboardEvent) => {
const to = event.relatedTarget tabbing = event.key === 'Tab' && inMenu(event.target)
if (!from?.closest(MENU_PARTS) || !(to instanceof Element)) return }
if (!to.closest(MENU_PARTS)) close() 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]) }, [open, close])
} }
@@ -134,5 +145,5 @@ export function useMoreDismiss(open: boolean, close: () => void) {
useMoreEscape(open, close) useMoreEscape(open, close)
useOutsidePress(open, close) useOutsidePress(open, close)
useBackRequest(open, close) useBackRequest(open, close)
useFocusLeave(open, close) useTabLeave(open, close)
} }
+17 -5
View File
@@ -233,16 +233,28 @@ describe('the platform back request', () => {
}) })
describe('final review: keyboard focus leaving « Plus » (M-8)', () => { 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() const { user } = mount()
await user.click(more()) await user.click(more())
const item = screen.getByRole('button', { name: 'Constats' }) const items = [...list()!.querySelectorAll('button')]
act(() => item.focus())
expect(more()).toHaveAttribute('aria-expanded', 'true')
act(() => more().focus()) act(() => more().focus())
await user.tab()
expect(items[0]).toHaveFocus()
expect(more()).toHaveAttribute('aria-expanded', 'true') 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') expect(more()).toHaveAttribute('aria-expanded', 'false')
await gone() 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()
})
}) })