diff --git a/apps/app/src/components/editor/annotations/EditableAnnotations.tsx b/apps/app/src/components/editor/annotations/EditableAnnotations.tsx index a7d45a3f..4045cef4 100644 --- a/apps/app/src/components/editor/annotations/EditableAnnotations.tsx +++ b/apps/app/src/components/editor/annotations/EditableAnnotations.tsx @@ -1,4 +1,5 @@ import { useEffect, useReducer, useRef, useState, type Dispatch } from 'react' +import { flushSync } from 'react-dom' import type { AnatomicalSheetDocument, Annotation, @@ -300,6 +301,19 @@ const guarded = (action: () => void) => () => { if (guardDrawing(action)) action() } +/** + * A tool or a view chip, guarded as above. Pressed with the focus still on a handle (Safari + * focuses no clicked button), the change unmounts that handle: the focus then lands on the + * pressed button, where the keyboard's press leaves it, never on `body` (rev 4 M-4). + */ +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 }) +} + function CanvasColumn({ scene, work, @@ -316,8 +330,8 @@ function CanvasColumn({ - guarded(() => dispatch({ type: 'view', viewId: id }))() + onSelect={(id, chip) => + guardedPress(chip, () => dispatch({ type: 'view', viewId: id })) } /> @@ -436,7 +450,8 @@ function CanvasStage({ }} tools={{ tool: ui.tool, - onTool: (tool) => guarded(() => dispatch({ type: 'tool', tool }))(), + onTool: (tool, button) => + guardedPress(button, () => dispatch({ type: 'tool', tool })), drawingDisabled: blocked, linkDisabled: model.children.length === 0, }} diff --git a/apps/app/src/components/editor/annotations/ToolColumn.tsx b/apps/app/src/components/editor/annotations/ToolColumn.tsx index 60da9cf5..681cdaa4 100644 --- a/apps/app/src/components/editor/annotations/ToolColumn.tsx +++ b/apps/app/src/components/editor/annotations/ToolColumn.tsx @@ -1,11 +1,12 @@ -import type { ReactNode } from 'react' +import type { MouseEvent, ReactNode } from 'react' import type { ToolId } from '@/editor/annotations/uiState' import { useT } from '@/i18n/useT' import EditorGlyph, { type GlyphName } from '../EditorGlyph' export interface ToolColumnProps { tool: ToolId - onTool: (tool: ToolId) => void + /** `button` is the pressed tool: the focus may land on it (Safari, see `guardedPress`). */ + onTool: (tool: ToolId, button: HTMLElement) => void /** Point, zone and link need a measured view and a category. */ drawingDisabled: boolean /** The link tool needs a detail view directly under the current one. */ @@ -35,7 +36,7 @@ function ToolButton({ tool?: ToolId pressed?: boolean disabled?: boolean - onClick: () => void + onClick: (event: MouseEvent) => void }): ReactNode { return (