fix(editor): no focus in the drawing place lands under the strip

The scroll margin now covers every native focusable of the place (tools,
view chips, add buttons, the aside's controls), the strip's own commands
excepted, as a real Tab walk found them hidden (I-3). A vertex chosen
from the keyboard takes the focus with a scroll (M-1).
This commit is contained in:
bchanot committed 2026-11-19 14:58:19 +01:00
1 parent 59e189c375
commit 966ad4fe45
4 files changed
+159 -18

No files matched your search

@@ -606,6 +606,26 @@ function stripGeometry(page: Page) {
})
}
/**
* The focused element's top against the strip's bottom, while the focus is in the drawing
* place and outside the strip (the strip's own commands never scroll the page).
*/
function focusedUnderStrip(page: Page) {
return page.evaluate(() => {
const active = document.activeElement as HTMLElement
const strip = document.querySelector('[data-testid="edit-strip"]')!
const inPlace =
!!active.closest('[data-place="annotations.drawing"]') &&
!strip.contains(active)
return {
inPlace,
name: `${active.tagName} « ${(active.getAttribute('aria-label') ?? active.textContent ?? '').trim().slice(0, 30)} »`,
top: active.getBoundingClientRect().top,
stripBottom: strip.getBoundingClientRect().bottom,
}
})
}
for (const viewport of VIEWPORTS) {
test.describe(`the strip under the top bar at ${viewport.width} px (U2 Task 14)`, () => {
test.use({
@@ -771,6 +791,80 @@ for (const viewport of VIEWPORTS) {
).toBeGreaterThanOrEqual((await stripGeometry(page)).bottom)
})
test("Tab and Shift+Tab onto the drawing place's native controls land below the strip", async ({
page,
}) => {
await open(page, ...manyPoints())
await pickAnnotation(page, dict, 'Pt 0')
// A commit puts « Défaire » in the message line under the drawing.
await page.getByTestId('point-handle').focus()
await page.keyboard.press('ArrowRight')
await page.keyboard.press('Enter')
await expect(page.getByTestId('gesture-message')).toBeVisible()
// Each walk starts on the canvas box, the page scrolled so the canvas top and the
// aside's top sit under the strip; the focus starts there without a scroll.
for (const key of ['Shift+Tab', 'Tab']) {
await page.evaluate(() => {
const canvas = document.querySelector<HTMLElement>(
'[data-testid="annotation-canvas"]',
)!
const top = canvas.getBoundingClientRect().top + window.scrollY
window.scrollTo(0, top - 100)
canvas.focus({ preventScroll: true })
})
for (let step = 0; step < 14; step++) {
await page.keyboard.press(key)
const at = await focusedUnderStrip(page)
if (!at.inPlace) break
expect(
at.top,
`${key} ${step + 1}: ${at.name} below the strip`,
).toBeGreaterThanOrEqual(at.stripBottom - 0.5)
}
}
})
test('Delete from the keyboard focuses the next vertex below the strip', async ({
page,
}) => {
await open(page, ...manyPoints(), {
...pointOf('zone-k', 'main', { cx: 0, cy: 0, r: 1 }, 'Zone K'),
kind: 'zone',
geometry: {
type: 'polygon',
points: [
[300, 600],
[300, 150],
[600, 150],
[600, 600],
],
},
})
await pickAnnotation(page, dict, 'Zone K')
// The next vertex (drawn higher) starts under the strip, the focused one below it.
const top = await page.evaluate(() => {
const next = document.querySelector('[data-vertex="1"]')!
const y = next.getBoundingClientRect().top + window.scrollY
window.scrollTo(0, y - 90)
document
.querySelector<SVGElement>('[data-vertex="0"]')!
.focus({ preventScroll: true })
return next.getBoundingClientRect().top
})
const strip = await stripGeometry(page)
expect(
top,
'precondition: the next vertex is under the strip',
).toBeLessThan(strip.bottom)
await page.keyboard.press('Delete')
await expect(page.locator('[data-vertex="3"]')).toHaveCount(0)
const at = await focusedUnderStrip(page)
expect(at.name).toContain('Sommet')
expect(at.top, `${at.name} below the strip`).toBeGreaterThanOrEqual(
(await stripGeometry(page)).bottom - 0.5,
)
})
test('with the strip hidden the scroll offset is not applied', async ({
page,
}) => {
@@ -1,4 +1,9 @@
import { useLayoutEffect, useRef, type KeyboardEvent } from 'react'
import {
useLayoutEffect,
useRef,
type KeyboardEvent,
type RefObject,
} from 'react'
import type { Point } from '@faunosteo/core-wasm/types'
import type { HandleSizes } from '@/editor/annotations/handleSize'
import { MIN_VERTICES, showsInsert } from '@/editor/annotations/polygonEdit'
@@ -258,13 +263,34 @@ function focusIsOurs(group: SVGGElement, held: boolean): boolean {
return held && fell
}
/**
* Whether the group's last input was a key, read by the focus move: a key's focus may
* scroll (the scroll margin then keeps the vertex clear of the strip, rev 4 M-1), a
* pointer's or a finger's never moves the page under it.
*/
function useLastInputKey() {
const fromKey = useRef(false)
const handlers = {
onKeyDownCapture: () => {
fromKey.current = true
},
onPointerDownCapture: () => {
fromKey.current = false
},
}
return { fromKey, handlers }
}
/**
* A keyboard choice (« + », Insert, Delete, Enter on « − ») moves the focus to the vertex
* now chosen, so the keys keep acting where the user looks, even when the focused handle
* was removed; a pointer choice leaves the focus alone. It follows every new choice (the
* store's object), a re-choice of the same index on a new text included.
* was removed. It follows every new choice (the store's object), a re-choice of the same
* index on a new text included.
*/
function useFocusFollowsChoice(chosen: number | null) {
function useFocusFollowsChoice(
chosen: number | null,
fromKey: RefObject<boolean>,
) {
const group = useRef<SVGGElement | null>(null)
const held = useRef(false)
const choice = useAnnotationWork((s) => s.chosen)
@@ -272,11 +298,13 @@ function useFocusFollowsChoice(chosen: number | null) {
const element = group.current
if (chosen === null || !element || !focusIsOurs(element, held.current))
return
const active = document.activeElement
if (active?.getAttribute('data-vertex') === String(chosen)) return
const target = element.querySelector(`[data-vertex="${chosen}"]`)
if (target instanceof SVGElement) target.focus({ preventScroll: true })
}, [chosen, choice])
if (!(target instanceof SVGElement)) return
// A removed vertex leaves its focused handle in place, now drawn at the next one.
if (target !== document.activeElement)
target.focus({ preventScroll: !fromKey.current })
else if (fromKey.current) target.scrollIntoView({ block: 'nearest' })
}, [chosen, choice, fromKey])
// Read after every commit: where the focus was before the next change unmounts it.
useLayoutEffect(() => {
held.current = group.current?.contains(document.activeElement) ?? false
@@ -306,7 +334,8 @@ export default function ZoneHandles({
const t = useT()
const { points } = zone
const chosen = useChosen(zone.annotationId, points.length)
const group = useFocusFollowsChoice(chosen)
const input = useLastInputKey()
const group = useFocusFollowsChoice(chosen, input.fromKey)
const inserts = shownInserts(points, scale)
const named = (key: string, n: number) =>
t(`editor.annotations.handle.${key}`)
@@ -322,6 +351,7 @@ export default function ZoneHandles({
role="group"
aria-label={groupName}
data-testid="zone-handles"
{...input.handlers}
>
{points.map((p, i) =>
inserts[i] ? (
+15 -3
View File
@@ -16,11 +16,23 @@ body,
sticky strip (its height plus the gap below it, space-y-3). It is a margin on the
drawing place's focus targets, not a padding on the page: scroll-padding also pushed the
page when the focus went to the sticky bar or strip themselves (probed, U2 Task 14), and a
page-wide rule outlived a hidden strip. The place is `hidden` while another section
shows, and the strip is sticky from 1 024 px only. */
page-wide rule outlived a hidden strip. Every focusable of the place gets it (rows,
handles, the canvas box, tools, view chips, the aside's controls: a Tab walk probed them
under the strip, rev 4 I-3), except the strip's own commands: their focus must not
scroll. The place is `hidden` while another section shows, and the strip is sticky from
1 024 px only. */
@media (min-width: 1024px) {
[data-place='annotations.drawing']:not([hidden])
:is([data-focus-key], [data-focus-target], [role='group'][tabindex='0']) {
:is(
a,
button,
input,
select,
textarea,
[tabindex],
[data-focus-key],
[data-focus-target]
):not([data-testid='edit-strip'] *) {
scroll-margin-top: calc(
var(--shell-top-bar-height) + var(--edit-strip-height) + 1.5rem
);
+11 -6
View File
@@ -230,12 +230,17 @@ The edit strip (U2 Task 14) stays under the shell's top bar from 1 024 px: it is
(above the drawing, under the loupe, the menus, the bar and the dialogs), and no
ancestor sets an `overflow` other than visible. The bar's height (3.5 rem plus
the safe-area inset) and the strip's (`--edit-strip-height`) are CSS variables
of `styles/index.css`, which also feed a `scroll-margin-top` on the drawing
place's focus targets (rows, every focusable handle through `data-focus-target`,
the canvas box), from 1 024 px and only while the place is not `hidden`, so a
focus or a `scrollIntoView` never lands under either. It is a margin, not a page
`scroll-padding`: the padding made the page jump when the focus went to the
sticky bar or strip themselves. Below 1 024 px the strip is not sticky.
of `styles/index.css`, which also feed a `scroll-margin-top` on every focusable
of the drawing place (links, buttons, fields, `tabindex` elements, rows through
`data-focus-key`, handles through `data-focus-target`, the canvas box) except
the strip's own commands, from 1 024 px and only while the place is not
`hidden`, so a focus or a `scrollIntoView` never lands under either. It is a
margin, not a page `scroll-padding`: the padding made the page jump when the
focus went to the sticky bar or strip themselves, which is also why the strip's
commands carry none. A vertex chosen from the keyboard (Delete, Backspace,
Insert, « + ») takes the focus with a scroll, so the margin applies; one chosen
by a pointer or a finger takes it without a scroll. Below 1 024 px the strip is
not sticky.
A creation (U2 Task 7, A2, A4, A12) is drawn by the overlay too, never by the
core, since the new shape is not in the document until « Créer »: