fix(editor): a tool or view chip tap keeps the focus off body
Safari focuses no tapped button: a tool or a view chip pressed after a keyboard series commits through the guard with the focus on the handle, then the change unmounts the handle. The focus now lands on the pressed button, without a scroll, only when it fell to body (M-4).
This commit is contained in:
1 parent
966ad4fe45
commit
531bbef619
4 files changed
+66
-9
No files matched your search
@@ -1,4 +1,5 @@
|
|||||||
import { useEffect, useReducer, useRef, useState, type Dispatch } from 'react'
|
import { useEffect, useReducer, useRef, useState, type Dispatch } from 'react'
|
||||||
|
import { flushSync } from 'react-dom'
|
||||||
import type {
|
import type {
|
||||||
AnatomicalSheetDocument,
|
AnatomicalSheetDocument,
|
||||||
Annotation,
|
Annotation,
|
||||||
@@ -300,6 +301,19 @@ const guarded = (action: () => void) => () => {
|
|||||||
if (guardDrawing(action)) action()
|
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({
|
function CanvasColumn({
|
||||||
scene,
|
scene,
|
||||||
work,
|
work,
|
||||||
@@ -316,8 +330,8 @@ function CanvasColumn({
|
|||||||
<ViewSwitcher
|
<ViewSwitcher
|
||||||
views={doc.views}
|
views={doc.views}
|
||||||
current={scene.viewId}
|
current={scene.viewId}
|
||||||
onSelect={(id) =>
|
onSelect={(id, chip) =>
|
||||||
guarded(() => dispatch({ type: 'view', viewId: id }))()
|
guardedPress(chip, () => dispatch({ type: 'view', viewId: id }))
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
<CanvasStage scene={scene} work={work} onDeselect={onDeselect} />
|
<CanvasStage scene={scene} work={work} onDeselect={onDeselect} />
|
||||||
@@ -436,7 +450,8 @@ function CanvasStage({
|
|||||||
}}
|
}}
|
||||||
tools={{
|
tools={{
|
||||||
tool: ui.tool,
|
tool: ui.tool,
|
||||||
onTool: (tool) => guarded(() => dispatch({ type: 'tool', tool }))(),
|
onTool: (tool, button) =>
|
||||||
|
guardedPress(button, () => dispatch({ type: 'tool', tool })),
|
||||||
drawingDisabled: blocked,
|
drawingDisabled: blocked,
|
||||||
linkDisabled: model.children.length === 0,
|
linkDisabled: model.children.length === 0,
|
||||||
}}
|
}}
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
import type { ReactNode } from 'react'
|
import type { MouseEvent, ReactNode } from 'react'
|
||||||
import type { ToolId } from '@/editor/annotations/uiState'
|
import type { ToolId } from '@/editor/annotations/uiState'
|
||||||
import { useT } from '@/i18n/useT'
|
import { useT } from '@/i18n/useT'
|
||||||
import EditorGlyph, { type GlyphName } from '../EditorGlyph'
|
import EditorGlyph, { type GlyphName } from '../EditorGlyph'
|
||||||
|
|
||||||
export interface ToolColumnProps {
|
export interface ToolColumnProps {
|
||||||
tool: ToolId
|
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. */
|
/** Point, zone and link need a measured view and a category. */
|
||||||
drawingDisabled: boolean
|
drawingDisabled: boolean
|
||||||
/** The link tool needs a detail view directly under the current one. */
|
/** The link tool needs a detail view directly under the current one. */
|
||||||
@@ -35,7 +36,7 @@ function ToolButton({
|
|||||||
tool?: ToolId
|
tool?: ToolId
|
||||||
pressed?: boolean
|
pressed?: boolean
|
||||||
disabled?: boolean
|
disabled?: boolean
|
||||||
onClick: () => void
|
onClick: (event: MouseEvent<HTMLButtonElement>) => void
|
||||||
}): ReactNode {
|
}): ReactNode {
|
||||||
return (
|
return (
|
||||||
<button
|
<button
|
||||||
@@ -67,7 +68,7 @@ export default function ToolColumn(props: ToolColumnProps) {
|
|||||||
label={t(`editor.annotations.tool.${id}`)}
|
label={t(`editor.annotations.tool.${id}`)}
|
||||||
pressed={props.tool === id}
|
pressed={props.tool === id}
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
onClick={() => props.onTool(id)}
|
onClick={(event) => props.onTool(id, event.currentTarget)}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -15,7 +15,8 @@ export default function ViewSwitcher({
|
|||||||
}: {
|
}: {
|
||||||
views: readonly SheetView[]
|
views: readonly SheetView[]
|
||||||
current: string
|
current: string
|
||||||
onSelect: (viewId: string) => void
|
/** `chip` is the pressed chip: the focus may land on it (Safari). */
|
||||||
|
onSelect: (viewId: string, chip: HTMLElement) => void
|
||||||
}) {
|
}) {
|
||||||
const t = useT()
|
const t = useT()
|
||||||
const locale = useLocaleStore((s) => s.locale)
|
const locale = useLocaleStore((s) => s.locale)
|
||||||
@@ -30,7 +31,7 @@ export default function ViewSwitcher({
|
|||||||
key={view.id}
|
key={view.id}
|
||||||
type="button"
|
type="button"
|
||||||
aria-pressed={view.id === current}
|
aria-pressed={view.id === current}
|
||||||
onClick={() => onSelect(view.id)}
|
onClick={(event) => onSelect(view.id, event.currentTarget)}
|
||||||
className={`${CHIP} ${
|
className={`${CHIP} ${
|
||||||
view.id === current
|
view.id === current
|
||||||
? 'bg-[var(--color-ink)] text-white'
|
? 'bg-[var(--color-ink)] text-white'
|
||||||
|
|||||||
@@ -1356,6 +1356,46 @@ describe('Safari: the focus after a press that commits a pending series', () =>
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
it('a press on a tool commits with the focus kept, then focuses the tool', async () => {
|
||||||
|
await keyedVertex()
|
||||||
|
const held = vertex(1)
|
||||||
|
const seen = watchCommitFocus()
|
||||||
|
const tool = screen.getByRole('button', { name: 'Point' })
|
||||||
|
fireEvent.click(tool)
|
||||||
|
expect(pointsOf('z')![0]).toEqual([20.5, 20])
|
||||||
|
expect(seen[0]).toBe(held)
|
||||||
|
expect(tool).toHaveAttribute('aria-pressed', 'true')
|
||||||
|
expect(document.activeElement).toBe(tool)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('a press on a view chip commits with the focus kept, then focuses the chip', async () => {
|
||||||
|
const detail = insert(squareText(), {
|
||||||
|
kind: 'view',
|
||||||
|
value: {
|
||||||
|
kind: 'detail',
|
||||||
|
id: 'view-det',
|
||||||
|
name: { fr: 'Tête', en: 'Head' },
|
||||||
|
svg_asset_id: null,
|
||||||
|
view_box: { min_x: 10, min_y: 10, width: 80, height: 80 },
|
||||||
|
parent_view_id: 'view-main',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
await mount(detail)
|
||||||
|
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 views = screen.getByRole('group', { name: 'Vues' })
|
||||||
|
const chip = within(views).getByRole('button', { name: 'Tête' })
|
||||||
|
fireEvent.click(chip)
|
||||||
|
expect(pointsOf('z')![0]).toEqual([20.5, 20])
|
||||||
|
expect(seen[0]).toBe(held)
|
||||||
|
expect(chip).toHaveAttribute('aria-pressed', 'true')
|
||||||
|
expect(document.activeElement).toBe(chip)
|
||||||
|
})
|
||||||
|
|
||||||
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' }))
|
||||||
|
|||||||
Reference in new issue
Block a user