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

+44 -16
View File
@@ -1,5 +1,4 @@
/** 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 {
act,
@@ -16,12 +15,16 @@ import AnnotationsTab from '@/components/editor/tabs/AnnotationsTab'
import EditorTabs from '@/components/editor/EditorTabs'
import HistoryButtons from '@/components/editor/HistoryButtons'
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 { useUndoShortcuts } from '@/editor/useUndoShortcuts'
import { useLocaleStore } from '@/state/localeStore'
import { fr } from '@/i18n/fr'
import type { EditorTabId } from '@/editor/tabs'
import { useEditorNavigation } from '@/editor/useEditorNavigation'
import { stripLeadOf } from '@/editor/annotations/stripLead'
import { initialUi } from '@/editor/annotations/uiState'
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)
})
/** 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() {
const [active, setActive] = useState<EditorTabId>('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
}
const nav = useEditorNavigation('annotations')
return (
<>
<EditorTabs active={active} tier="wide" onSelect={select} />
<KeptPlace visible={active === 'annotations'}>{tab()}</KeptPlace>
<EditorTabs
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)
})
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 () => {
await keyedVertex()
fireEvent.click(screen.getByRole('button', { name: 'Déplacer le sommet' }))