fix(editor): a guarded press that hides the handle keeps the focus off body
This commit is contained in:
1 parent
8df52fd1be
commit
fa00e57652
4 files changed
+66
-7
No files matched your search
@@ -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 (
|
||||
<div
|
||||
role="tablist"
|
||||
@@ -108,7 +116,7 @@ function TabList({ shown, active, onSelect }: TabListProps) {
|
||||
aria-selected={tab === active}
|
||||
aria-controls={panelDomId(tab)}
|
||||
tabIndex={tab === active ? 0 : -1}
|
||||
onClick={() => onSelect(tab)}
|
||||
onClick={(event) => press(event.currentTarget, tab)}
|
||||
className={`-mb-px ${BASE} ${tab === active ? ACTIVE : IDLE}`}
|
||||
>
|
||||
{t(`editor.tab.${tab}`)}
|
||||
|
||||
@@ -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({
|
||||
|
||||
@@ -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) => ({
|
||||
|
||||
@@ -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) => (
|
||||
</div>
|
||||
)
|
||||
|
||||
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<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
|
||||
}
|
||||
return (
|
||||
<>
|
||||
<EditorTabs active={active} tier="wide" onSelect={select} />
|
||||
<KeptPlace visible={active === 'annotations'}>{tab()}</KeptPlace>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
it('a press on an editor tab commits with the focus kept, then focuses the tab', async () => {
|
||||
await mount(squareText(), <TabsOverPanel />)
|
||||
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' }))
|
||||
|
||||
Reference in new issue
Block a user