fix(editor): a focus left in a hidden panel counts as lost after a guarded press

This commit is contained in:
bchanot committed 2026-11-19 17:24:32 +01:00
1 parent 7cb1aee7a1
commit f9656a0d9b
3 files changed
+51 -20

No files matched your search

@@ -25,11 +25,14 @@ export function commitFocusedSeries(): void {
/** /**
* After a press that hid or removed the focused handle (Safari focuses no clicked button): * After a press that hid or removed the focused handle (Safari focuses no clicked button):
* the pressed control takes the focus, where the keyboard's press leaves it, never `body`. * the pressed control takes the focus, where the keyboard's press leaves it, never `body`.
* A removed handle drops the focus to `body` at once; a handle in a `hidden` panel keeps it
* until the browser's later fix-up, so a focus inside `[hidden]` counts as lost too.
*/ */
export function focusPressedIfLost(control: HTMLElement): void { export function focusPressedIfLost(control: HTMLElement): void {
const active = document.activeElement const active = document.activeElement
if (active === null || active === document.body) const lost =
control.focus({ preventScroll: true }) active === null || active === document.body || !!active.closest('[hidden]')
if (lost) control.focus({ preventScroll: true })
} }
/** An unstarted mode ends; a pending series that is not the mode's stays. */ /** An unstarted mode ends; a pending series that is not the mode's stays. */
+44 -16
View File
@@ -1,5 +1,4 @@
/** U2 Task 5: vertices, « + », « − », zone move, redraw, paths and the handle cap. */ /** U2 Task 5: vertices, « + », « − », zone move, redraw, paths and the handle cap. */
import { useLayoutEffect, useState } from 'react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { import {
act, act,
@@ -16,12 +15,16 @@ import AnnotationsTab from '@/components/editor/tabs/AnnotationsTab'
import EditorTabs from '@/components/editor/EditorTabs' import EditorTabs from '@/components/editor/EditorTabs'
import HistoryButtons from '@/components/editor/HistoryButtons' import HistoryButtons from '@/components/editor/HistoryButtons'
import KeptPlace from '@/components/editor/KeptPlace' import KeptPlace from '@/components/editor/KeptPlace'
import { answerDrawing, guardDrawing } from '@/editor/annotations/drawingGuard' import {
answerDrawing,
focusPressedIfLost,
guardDrawing,
} from '@/editor/annotations/drawingGuard'
import { useAnnotationWork, resetWork } from '@/editor/annotations/workStore' import { useAnnotationWork, resetWork } from '@/editor/annotations/workStore'
import { useUndoShortcuts } from '@/editor/useUndoShortcuts' import { useUndoShortcuts } from '@/editor/useUndoShortcuts'
import { useLocaleStore } from '@/state/localeStore' import { useLocaleStore } from '@/state/localeStore'
import { fr } from '@/i18n/fr' import { fr } from '@/i18n/fr'
import type { EditorTabId } from '@/editor/tabs' import { useEditorNavigation } from '@/editor/useEditorNavigation'
import { stripLeadOf } from '@/editor/annotations/stripLead' import { stripLeadOf } from '@/editor/annotations/stripLead'
import { initialUi } from '@/editor/annotations/uiState' import { initialUi } from '@/editor/annotations/uiState'
import { drawnText, insert, point } from '../helpers/editorFixtures' import { drawnText, insert, point } from '../helpers/editorFixtures'
@@ -1458,22 +1461,18 @@ describe('Safari: the focus after a press that commits a pending series', () =>
expect(document.activeElement).toBe(row) expect(document.activeElement).toBe(row)
}) })
/** The tab row over the annotations panel, which a browser drops the focus out of when hidden. */ /** The tab row over the annotations panel, hidden with its focus left in place (a browser
* clears the focus of a hidden subtree later, never during the press). */
function TabsOverPanel() { function TabsOverPanel() {
const [active, setActive] = useState<EditorTabId>('annotations') const nav = useEditorNavigation('annotations')
useLayoutEffect(() => {
const held = document.activeElement
if (held instanceof HTMLElement && held.closest('[hidden]')) held.blur()
})
const select = (next: EditorTabId) => {
if (!guardDrawing(() => undefined)) return false
setActive(next)
return true
}
return ( return (
<> <>
<EditorTabs active={active} tier="wide" onSelect={select} /> <EditorTabs
<KeptPlace visible={active === 'annotations'}>{tab()}</KeptPlace> active={nav.state.tab}
tier="wide"
onSelect={nav.selectTab}
/>
<KeptPlace visible={nav.state.tab === 'annotations'}>{tab()}</KeptPlace>
</> </>
) )
} }
@@ -1494,6 +1493,35 @@ describe('Safari: the focus after a press that commits a pending series', () =>
expect(document.activeElement).toBe(target) expect(document.activeElement).toBe(target)
}) })
describe('focusPressedIfLost', () => {
afterEach(() => document.body.replaceChildren())
const pressed = () =>
document.body.appendChild(document.createElement('button'))
function held(hidden: boolean) {
const host = document.body.appendChild(document.createElement('div'))
host.hidden = hidden
const field = host.appendChild(document.createElement('input'))
field.focus()
return field
}
it('takes the focus left inside a hidden panel (the browser clears it later)', () => {
const field = held(true)
expect(document.activeElement).toBe(field)
const control = pressed()
focusPressedIfLost(control)
expect(document.activeElement).toBe(control)
})
it('never steals the focus from a visible field or dialog', () => {
const field = held(false)
focusPressedIfLost(pressed())
expect(document.activeElement).toBe(field)
})
})
it('« Déplacer le sommet » focuses the first arrow', async () => { it('« Déplacer le sommet » focuses the first arrow', async () => {
await keyedVertex() await keyedVertex()
fireEvent.click(screen.getByRole('button', { name: 'Déplacer le sommet' })) fireEvent.click(screen.getByRole('button', { name: 'Déplacer le sommet' }))
+2 -2
View File
@@ -479,8 +479,8 @@ Status: implemented on `feature/v1-2-anatomical-sheets` (lots A to H; Lot I, gat
### U2 · Direct manipulation of points and polygons ### U2 · Direct manipulation of points and polygons
- Status: implemented on `feature/v1-2-anatomical-sheets` (plan `.claude/tasks/plans/2026-11-17-v1-2-6-u2.md`, rev 4, - Status: implemented on `feature/v1-2-anatomical-sheets` (plan `.claude/tasks/plans/2026-11-17-v1-2-6-u2.md`, rev 4,
Tasks 1 to 16 and the final fix rounds done; gates run on the final head, figures in `V1-2-6_UX_TESTS.md`; owner Tasks 1 to 16 and the final fix rounds done; gates ran at f5a6eafc, figures in `V1-2-6_UX_TESTS.md`, and the
trial pending). The boards are `docs/design/v1-2-6-u2/` (A1 to A15 and the three controller reruns them on the final head; owner trial pending). The boards are `docs/design/v1-2-6-u2/` (A1 to A15 and the three
adjustments, validated 2026-11-17). adjustments, validated 2026-11-17).
- Objective: boards 02 to 06, 13 and 31 to 33 of `v1-2-6-u2/` for points and polygons (curve handles excluded, curves - Objective: boards 02 to 06, 13 and 31 to 33 of `v1-2-6-u2/` for points and polygons (curve handles excluded, curves
lot): point move and size slider snapping to the radii of the view with the red minimum mark; vertex drag; « + » per lot): point move and size slider snapping to the radii of the view with the red minimum mark; vertex drag; « + » per