diff --git a/apps/app/e2e/shell/editor-shell-crossing.spec.ts b/apps/app/e2e/shell/editor-shell-crossing.spec.ts index 9964bafa..3d6c6f44 100644 --- a/apps/app/e2e/shell/editor-shell-crossing.spec.ts +++ b/apps/app/e2e/shell/editor-shell-crossing.spec.ts @@ -295,6 +295,21 @@ test.describe('editor shell, width crossing (H)', () => { .not.toBeNull() }) + test('a focused drawing tool hands its focus to the shown tab on a crossing', async ({ + page, + }) => { + await open(page) + await selectTab(page, dict, 'annotations') + const zone = page.getByRole('button', { + name: dict['editor.annotations.tool.zone'], + exact: true, + }) + await expect(zone).toBeEnabled() + await zone.focus() + await cross(page, NARROW) + await expect(active(page)).toHaveAttribute('id', 'editor-tab-annotations') + }) + test('a drag across the crossing commits nothing', async ({ page }) => { await open( page, diff --git a/apps/app/e2e/shell/editor-shell-journeys.spec.ts b/apps/app/e2e/shell/editor-shell-journeys.spec.ts index 0cbd1661..d40ed42d 100644 --- a/apps/app/e2e/shell/editor-shell-journeys.spec.ts +++ b/apps/app/e2e/shell/editor-shell-journeys.spec.ts @@ -129,10 +129,8 @@ test.describe('editor shell journeys (H)', () => { page, }) => { await open(page) - const original = await (async () => { - await selectTab(page, dict, 'general') - return nameField(page).inputValue() - })() + await selectTab(page, dict, 'general') + const original = await nameField(page).inputValue() await commitName(page, 'Nom depuis Général') await unsaved(page, 1) await selectTab(page, dict, 'findings') diff --git a/apps/app/e2e/shell/editor-shell-layout.spec.ts b/apps/app/e2e/shell/editor-shell-layout.spec.ts index e21f9395..16054f0f 100644 --- a/apps/app/e2e/shell/editor-shell-layout.spec.ts +++ b/apps/app/e2e/shell/editor-shell-layout.spec.ts @@ -181,7 +181,9 @@ test.describe('editor shell layout (H)', () => { } await showDrawing(page, dict) await openReferences(page, dict) + await settle(page) await page.evaluate(() => window.scrollTo(0, 0)) + await expect(page.locator('dialog[open]')).toHaveCount(0) await expectAccessible(page, `the open panel at ${width} px`) await closeReferences(page, dict) } diff --git a/apps/app/src/components/editor/HistoryButtons.tsx b/apps/app/src/components/editor/HistoryButtons.tsx index ea1946b1..2d8dfdd8 100644 --- a/apps/app/src/components/editor/HistoryButtons.tsx +++ b/apps/app/src/components/editor/HistoryButtons.tsx @@ -49,7 +49,7 @@ function usePress( if (!pair?.contains(event.relatedTarget as Node | null)) forget() } const onPointerDown = () => { - left.current = asField(document.activeElement) + left.current = asField(document.activeElement) ?? left.current commitFocusedField() } const onFocus = (event: FocusEvent) => { @@ -58,7 +58,7 @@ function usePress( const onClick = () => { const field = left.current forget() - if (field?.getAttribute('aria-invalid') === 'true') { + if (field?.isConnected && field.getAttribute('aria-invalid') === 'true') { field.focus() return } diff --git a/apps/app/src/components/editor/fields/TextField.tsx b/apps/app/src/components/editor/fields/TextField.tsx index 9dd9c9d1..3748fbda 100644 --- a/apps/app/src/components/editor/fields/TextField.tsx +++ b/apps/app/src/components/editor/fields/TextField.tsx @@ -75,7 +75,7 @@ function EditableText({ const commit = () => { const next = trim ? draft.trim() : draft if (next !== draft) setDraft(next) - if (next === value) return + if (next === latest.current.value) return // The unmount cleanup runs before a render brings the new value: it must not commit twice. latest.current.value = next onCommit(next) diff --git a/apps/app/src/editor/focusRequest.ts b/apps/app/src/editor/focusRequest.ts index b57556d5..a29c15fe 100644 --- a/apps/app/src/editor/focusRequest.ts +++ b/apps/app/src/editor/focusRequest.ts @@ -125,15 +125,33 @@ function activeTabButton(): HTMLElement | null { ) } -/** Gives the focus to the shown tab's button: a field that lost the focus must not leave `body`. */ +/** Gives the focus to the shown tab's button: a lost focus must not leave on `body`. */ export function focusActiveTab(): void { activeTabButton()?.focus() } +/** The element holding the focus, when it is something other than `body`. */ +export function heldElement(): HTMLElement | null { + const active = document.activeElement + return active instanceof HTMLElement && active !== document.body + ? active + : null +} + /** - * The editor turned read-only: commits the text being typed; the field that held the focus - * sits in a part that is now hidden, so the focus goes to the shown tab, never to `body`. + * After the render that hides parts: `held` (taken before) that left the page or sits in a + * hidden part hands its focus to the shown tab. An element still visible keeps its place. + */ +export function refocusIfHidden(held: HTMLElement | null): void { + if (held && (!held.isConnected || held.closest('[hidden]'))) focusActiveTab() +} + +/** + * The editor turned read-only: commits the text being typed, then whatever held the focus + * in a now hidden part gives it to the shown tab, never to `body`. */ export function releaseEditFocus(): void { - if (commitFocusedField()) focusActiveTab() + const held = heldElement() + commitFocusedField() + refocusIfHidden(held) } diff --git a/apps/app/src/editor/useViewport.ts b/apps/app/src/editor/useViewport.ts index 3289fd25..dc048f36 100644 --- a/apps/app/src/editor/useViewport.ts +++ b/apps/app/src/editor/useViewport.ts @@ -1,5 +1,5 @@ import { useEffect, useLayoutEffect, useRef, useState } from 'react' -import { commitFocusedField, focusActiveTab } from './focusRequest' +import { commitFocusedField, heldElement, refocusIfHidden } from './focusRequest' import { MIN_EDIT_WIDTH } from './mode' /** The tab lists of board 05: all, the four of 640 px and over, the two below. */ @@ -21,14 +21,15 @@ function crossesEditWidth(from: TabTier, to: TabTier): boolean { export function useTabTier(): TabTier { const [tier, setTier] = useState(() => tierOf(window.innerWidth)) const previous = useRef(tier) - const lostFocus = useRef(false) + const held = useRef(null) useEffect(() => { const update = () => { const next = tierOf(window.innerWidth) // The typed text is committed before the editor turns read-only (or back): a blur // after the hide would be too late, a hidden field keeps no focus event. if (crossesEditWidth(previous.current, next)) { - lostFocus.current = commitFocusedField() + held.current = heldElement() + commitFocusedField() } previous.current = next setTier(next) @@ -37,11 +38,10 @@ export function useTabTier(): TabTier { window.addEventListener('resize', update) return () => window.removeEventListener('resize', update) }, []) - // The tab row of the new tier is rendered: the blurred field's focus goes to the shown tab. + // The new tier is rendered: a focus left in a hidden part goes to the shown tab. useLayoutEffect(() => { - if (!lostFocus.current) return - lostFocus.current = false - focusActiveTab() + refocusIfHidden(held.current) + held.current = null }, [tier]) return tier } diff --git a/apps/app/tests/unit/editor-no-loss.test.tsx b/apps/app/tests/unit/editor-no-loss.test.tsx index 7bd33fa4..8f17f727 100644 --- a/apps/app/tests/unit/editor-no-loss.test.tsx +++ b/apps/app/tests/unit/editor-no-loss.test.tsx @@ -260,6 +260,36 @@ describe('the guards of a width change', () => { expect(document.activeElement).toBe(button('Plus')) }) + it('a focused button of a hidden part hands its focus to the shown tab', async () => { + const user = await mount() + await user.click(tab('Annotations')) + await user.click(button('Modifier les catégories')) + const add = button('Ajouter la catégorie') + act(() => add.focus()) + cross(800) + expect(add.closest('[hidden]')).not.toBeNull() + expect(document.activeElement).toBe(tab('Annotations')) + }) + + it('a focused drawing tool hands its focus to the shown tab', async () => { + const user = await mount() + await waitFor(() => + expect(button('Zone')).toBeEnabled(), + ) + await user.click(button('Zone')) + expect(document.activeElement).toBe(button('Zone')) + cross(800) + expect(document.activeElement).toBe(tab('Annotations')) + }) + + it('a focused header button stays where it is', async () => { + await mount() + const refs = button('Références') + act(() => refs.focus()) + cross(800) + expect(document.activeElement).toBe(refs) + }) + it('a crossing with the focus on no field leaves the focus alone', async () => { const user = await mount() await user.click(tab('Général'))