diff --git a/apps/app/src/components/editor/EditorTabs.tsx b/apps/app/src/components/editor/EditorTabs.tsx index cfe5d543..21514c99 100644 --- a/apps/app/src/components/editor/EditorTabs.tsx +++ b/apps/app/src/components/editor/EditorTabs.tsx @@ -1,4 +1,6 @@ import { useEffect, useState, type KeyboardEvent } from 'react' +import { flushSync } from 'react-dom' +import { focusPressedIfLost } from '@/editor/annotations/drawingGuard' import { AnimatePresence, motion, @@ -92,6 +94,12 @@ function TabList({ shown, active, onSelect }: TabListProps) { if (onSelect(next, { replace: true }) === false) return document.getElementById(tabDomId(next))?.focus() } + // A tap on a tab (Safari focuses no clicked button) over a pending series hides the + // handle that held the focus: the tab takes it, as after the keyboard's press. + const press = (button: HTMLElement, tab: EditorTabId) => { + flushSync(() => onSelect(tab)) + focusPressedIfLost(button) + } return (
onSelect(tab)} + onClick={(event) => press(event.currentTarget, tab)} className={`-mb-px ${BASE} ${tab === active ? ACTIVE : IDLE}`} > {t(`editor.tab.${tab}`)} diff --git a/apps/app/src/components/editor/annotations/EditableAnnotations.tsx b/apps/app/src/components/editor/annotations/EditableAnnotations.tsx index 689c5d7e..648b9eac 100644 --- a/apps/app/src/components/editor/annotations/EditableAnnotations.tsx +++ b/apps/app/src/components/editor/annotations/EditableAnnotations.tsx @@ -20,7 +20,10 @@ import { } from '@/editor/annotations/workStore' import { parseFrame, type Frame } from '@/editor/annotations/frame' import { newPointSize } from '@/editor/annotations/pointSize' -import { guardDrawing } from '@/editor/annotations/drawingGuard' +import { + focusPressedIfLost, + guardDrawing, +} from '@/editor/annotations/drawingGuard' import { usePointEdit } from '@/editor/annotations/usePointEdit' import { useZoneEdit, type ZoneTarget } from '@/editor/annotations/useZoneEdit' import { useRedrawFollowsTracing } from '@/editor/annotations/redraw' @@ -316,9 +319,7 @@ const guarded = (action: () => void) => () => { function guardedPress(button: HTMLElement, action: () => void) { if (!guardDrawing(action)) return flushSync(action) - const active = document.activeElement - if (active === null || active === document.body) - button.focus({ preventScroll: true }) + focusPressedIfLost(button) } function CanvasColumn({ diff --git a/apps/app/src/editor/annotations/drawingGuard.ts b/apps/app/src/editor/annotations/drawingGuard.ts index be13b3db..fdebba49 100644 --- a/apps/app/src/editor/annotations/drawingGuard.ts +++ b/apps/app/src/editor/annotations/drawingGuard.ts @@ -22,6 +22,16 @@ export function commitFocusedSeries(): void { active.dispatchEvent(new FocusEvent('focusout', { bubbles: true })) } +/** + * 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`. + */ +export function focusPressedIfLost(control: HTMLElement): void { + const active = document.activeElement + if (active === null || active === document.body) + control.focus({ preventScroll: true }) +} + /** An unstarted mode ends; a pending series that is not the mode's stays. */ function endModeOnly(): void { useAnnotationWork.setState((work) => ({ diff --git a/apps/app/tests/unit/editor-zone-edit.test.tsx b/apps/app/tests/unit/editor-zone-edit.test.tsx index 7ad96908..d209d7d3 100644 --- a/apps/app/tests/unit/editor-zone-edit.test.tsx +++ b/apps/app/tests/unit/editor-zone-edit.test.tsx @@ -1,4 +1,5 @@ /** 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, @@ -12,12 +13,15 @@ import { import userEvent from '@testing-library/user-event' import type { Annotation, Point } from '@faunosteo/core-wasm/types' 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 { 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 { stripLeadOf } from '@/editor/annotations/stripLead' import { initialUi } from '@/editor/annotations/uiState' import { drawnText, insert, point } from '../helpers/editorFixtures' @@ -101,9 +105,9 @@ const tab = (readOnly = false) => (
) -async function mount(text: string) { +async function mount(text: string, ui = tab()) { await openReady({ text }) - const view = render(tab()) + const view = render(ui) await waitFor(() => expect( screen.getByTestId('annotation-canvas-drawing').querySelector('svg'), @@ -1454,6 +1458,42 @@ 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. */ + function TabsOverPanel() { + const [active, setActive] = useState('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 ( + <> + + {tab()} + + ) + } + + it('a press on an editor tab commits with the focus kept, then focuses the tab', async () => { + await mount(squareText(), ) + await select() + clickHandle('Sommet 1 de N', [20, 20]) + act(() => vertex(1).focus()) + fireEvent.keyDown(vertex(1), { key: 'ArrowRight' }) + const held = vertex(1) + const seen = watchCommitFocus() + const target = screen.getByRole('tab', { name: 'Général' }) + fireEvent.click(target) + expect(pointsOf('z')![0]).toEqual([20.5, 20]) + expect(seen[0]).toBe(held) + expect(target).toHaveAttribute('aria-selected', 'true') + expect(document.activeElement).toBe(target) + }) + it('« Déplacer le sommet » focuses the first arrow', async () => { await keyedVertex() fireEvent.click(screen.getByRole('button', { name: 'Déplacer le sommet' }))