fix(editor): edit mode focuses the draft, tile edit call

This commit is contained in:
bchanot committed 2026-11-15 17:43:43 +01:00
1 parent 6c0f913104
commit 5059641c04
4 files changed
+43 -3

No files matched your search

@@ -13,6 +13,24 @@ function useFocusEditButton(editing: boolean, buttonId: string) {
}
}
const FIRST_DRAFT_CONTROL =
'form[data-draft-form] :is(input, select, textarea, button):not([disabled])'
/** « Modifier » unmounts on the edit mode: the draft's first control takes the focus. */
function useFocusDraft(editing: boolean, buttonId: string) {
const dialog = useRef<Element | null>(null)
useEffect(() => {
if (!editing || !dialog.current) return
const first = dialog.current.querySelector<HTMLElement>(FIRST_DRAFT_CONTROL)
dialog.current = null
first?.focus()
}, [editing])
return () => {
dialog.current =
document.getElementById(buttonId)?.closest('dialog') ?? null
}
}
/**
* The local mode of a dialog (the finding dialog, the references dialog): view or edit,
* and whether a save was just made. No history entry. `buttonId` is its « Modifier ».
@@ -20,9 +38,13 @@ function useFocusEditButton(editing: boolean, buttonId: string) {
export function useItemMode(buttonId: string) {
const [mode, setMode] = useState({ editing: false, saved: false })
const returnToEdit = useFocusEditButton(mode.editing, buttonId)
const focusDraft = useFocusDraft(mode.editing, buttonId)
return {
...mode,
startEdit: () => setMode({ editing: true, saved: false }),
startEdit: () => {
focusDraft()
setMode({ editing: true, saved: false })
},
done: (saved: boolean) => {
returnToEdit()
setMode({ editing: false, saved })
@@ -105,7 +105,8 @@ function TileHeader({ base, props }: { base: string; props: TileProps }) {
function TileActions({ props }: { props: TileProps }) {
const t = useT()
const { position, count, citers, readOnly, movable, on } = props
if (readOnly || !on.edit) return null
const { edit } = on
if (readOnly || !edit) return null
return (
<>
<div className="flex flex-wrap items-center gap-2">
@@ -113,7 +114,7 @@ function TileActions({ props }: { props: TileProps }) {
readOnly={false}
variant="secondary"
size="sm"
onClick={(event) => on.edit?.(event.currentTarget)}
onClick={(event) => edit(event.currentTarget)}
>
{t('editor.references.edit')}
</ActionButton>
@@ -202,6 +202,14 @@ describe('the « Modifier » of the references dialog', () => {
expect(inCitations().queryByRole('button', { name: 'Aller à' })).toBeNull()
})
it('puts the focus on the first control of the draft', async () => {
const user = await mount()
await editCitations(user)
const active = document.activeElement
expect(active).not.toBe(document.body)
expect(active?.closest('form[data-draft-form]')).not.toBeNull()
})
it('is not offered read only (below 1 024 px)', async () => {
const user = await mount(900)
await openCitations(user)
@@ -415,6 +415,15 @@ describe('read-only flip and the gone rule', () => {
expect(field).toHaveFocus()
})
it('« Modifier » puts the focus on the first control of the draft', async () => {
const user = await mount()
await openFinding(user, /Cat A/)
await user.click(inDialog().getByRole('button', { name: 'Modifier' }))
const active = document.activeElement
expect(active).not.toBe(document.body)
expect(active?.closest('form[data-draft-form]')).not.toBeNull()
})
it('a flip to read-only while « Modifier » holds the focus keeps it in the dialog (the tab row is inert)', async () => {
const user = await mount()
await openFinding(user, /Cat A/)