feat(editor): references dialog over the finding, view mode

A modal Dialog replaces the citations side sheet; families open it too
(isCitable). The finding dialog shows only « Références citées » and
« X références », also at zero; its edit form no longer carries
reference_ids. A dirty form refusing « Aller à » or « Tout afficher »
closes the clean references dialog first.
This commit is contained in:
bchanot committed 2026-11-15 16:13:57 +01:00
1 parent ff21b0315e
commit abc781df52
29 files changed
+432 -858

No files matched your search

@@ -33,7 +33,7 @@ import EditorTabBody from './EditorTabBody'
import EditorTabs from './EditorTabs'
import ModeNotice from './ModeNotice'
import RecoveryNotice from './RecoveryNotice'
import CitationsSheet from './references/CitationsSheet'
import CitationsDialog from './references/CitationsDialog'
import ReferenceDialog from './references/ReferenceDialog'
interface Props {
@@ -85,7 +85,7 @@ interface PlacesProps {
}
/**
* The tab panels, then the citations sheet and the reference dialog: each comes after the
* The tab panels, then the references dialog and the reference dialog: each comes after the
* one under it in the tree (the tab body holds the finding dialog), so it is shown after
* it and lies over it.
*/
@@ -112,13 +112,11 @@ function Places({ nav, readOnly, reloadSheet }: PlacesProps) {
onBack: nav.backToDrawing,
}}
/>
<CitationsSheet
<CitationsDialog
citedBy={cited}
readOnly={readOnly}
onClose={nav.closeCitations}
onShowAll={() => cited && nav.showAllReferences(cited)}
navigate={nav.navigate}
onEdit={nav.references.open}
returnFocus={nav.citationsTrigger}
/>
<ReferenceDialog layer={references} readOnly={readOnly} />
@@ -206,8 +204,8 @@ function LeftDraftNotice({ shown }: { shown: boolean }) {
}
/**
* The draft page once the version is ready: header, notices, tab row, the citations
* panel and the shown place. Owns the navigation state, so the first tab is known at
* The draft page once the version is ready: header, notices, tab row, the dialogs
* and the shown place. Owns the navigation state, so the first tab is known at
* mount and later edits never move the user off their tab.
*/
function Workspace(props: Props & Who) {
@@ -7,9 +7,9 @@ import { useT } from '@/i18n/useT'
interface CitationsProps {
item: FindingItem
/** The citations sheet is open over the dialog (the trigger reads « expanded »). */
sheetOpen: boolean
/** Opens the citations sheet; a family has none (it lists its citations here). */
/** The references dialog is open over the finding dialog (the trigger reads « expanded »). */
citationsOpen: boolean
/** Opens the references dialog of the family or node. */
onShowReferences: (opener: HTMLElement) => void
}
@@ -19,21 +19,19 @@ const PILL =
const RING =
'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-accent)]'
/** « X références »: the way to the sheet of a finding node's citations. */
function SheetTrigger({
/** « X références »: the way to the references dialog, at zero too (Rev 5). */
function CitationsTrigger({
item,
count,
sheetOpen,
citationsOpen,
onShowReferences,
}: Pick<CitationsProps, 'item' | 'sheetOpen' | 'onShowReferences'> & {
count: number
}) {
}: CitationsProps & { count: number }) {
const plural = usePlural()
return (
<button
type="button"
data-citations-trigger={JSON.stringify(item.key)}
aria-expanded={sheetOpen}
aria-expanded={citationsOpen}
aria-controls="editor-citations-panel"
onClick={(event) => onShowReferences(event.currentTarget)}
className={`${PILL} ${RING}`}
@@ -44,7 +42,7 @@ function SheetTrigger({
)
}
/** A family's inline list of cited references; removable only where `remove` is given. */
/** A list of cited references; removable only where `remove` is given. */
export function CitationList({
ids,
readOnly,
@@ -80,52 +78,22 @@ export function CitationList({
)
}
/** « Références citées » and « X références »: the heading row of both modes. */
export function CitationsHeader({
item,
count,
sheetOpen,
onShowReferences,
}: Pick<CitationsProps, 'item' | 'sheetOpen' | 'onShowReferences'> & {
count: number
}) {
const t = useT()
return (
<div className="flex flex-wrap items-center justify-between gap-2">
<p className="text-sm font-medium">{t('editor.findings.references')}</p>
{count > 0 && item.element.kind === 'finding_node' && (
<SheetTrigger
item={item}
count={count}
sheetOpen={sheetOpen}
onShowReferences={onShowReferences}
/>
)}
</div>
)
}
/**
* « Références citées » of a category or a node, read: a finding node shows « X références »
* (the sheet), a family its list. Nothing here cites or removes: that is the edit form's
* (FindingEditCitations).
* « Références citées » and « X références » of a family or a node, the same in the view
* and the edit mode (Rev 5): the count is the document's, the citations are read and
* changed in the references dialog only. A zone cites nothing: no row.
*/
export default function FindingCitations(props: CitationsProps) {
export default function CitationsHeader(props: CitationsProps) {
const { item } = props
const t = useT()
if (item.element.kind === 'zone') return null
const ids = item.element.value.reference_ids
return (
<div className="space-y-2">
<CitationsHeader {...props} count={ids.length} />
{ids.length === 0 && (
<p className="text-sm text-[var(--color-ink-muted)]">
{t('editor.findings.references_none')}
</p>
)}
{item.element.kind === 'finding_category' && (
<CitationList ids={ids} readOnly />
)}
<div className="flex flex-wrap items-center justify-between gap-2">
<p className="text-sm font-medium">{t('editor.findings.references')}</p>
<CitationsTrigger
{...props}
count={item.element.value.reference_ids.length}
/>
</div>
)
}
@@ -65,7 +65,8 @@ function ItemBody(props: ItemBodyProps) {
const { layer } = props
const t = useT()
const mode = useItemMode()
const sheetOpen = !!layer.view && 'key' in layer.view && layer.view.citations
const citationsOpen =
!!layer.view && 'key' in layer.view && layer.view.citations
const showReferences = (opener: HTMLElement) =>
layer.openCitations(props.item.key, opener)
return (
@@ -77,14 +78,14 @@ function ItemBody(props: ItemBodyProps) {
guards={layer.guards}
onDone={mode.done}
onDiscardClose={() => layer.close(true)}
sheetOpen={sheetOpen}
citationsOpen={citationsOpen}
onShowReferences={showReferences}
/>
) : (
<FindingView
item={props.item}
readOnly={props.readOnly}
sheetOpen={sheetOpen}
citationsOpen={citationsOpen}
onEdit={mode.startEdit}
onDelete={() => props.onDelete(props.item)}
onShowReferences={showReferences}
@@ -1,20 +1,13 @@
import SelectField from '@/components/editor/fields/SelectField'
import {
CitationList,
CitationsHeader,
} from '@/components/editor/findings/FindingCitations'
import type { FindingItem } from '@/editor/findings/tree'
import { CitationList } from '@/components/editor/findings/FindingCitations'
import { useSheetDocument } from '@/editor/sheetDocument'
import { useT } from '@/i18n/useT'
interface Props {
item: FindingItem
/** The draft's cited ids: what the form shows, counts and hands to the sheet. */
/** The draft's cited ids: what the form shows. */
ids: readonly string[]
readOnly: boolean
sheetOpen: boolean
onChange: (ids: string[]) => void
onShowReferences: (opener: HTMLElement) => void
}
/** The references the draft does not cite yet, after the « choose » placeholder. */
@@ -28,8 +21,9 @@ function useCitableOptions(ids: readonly string[]) {
}
/**
* « Références citées » in the edit form of a category or a node: the draft's list, each
* « Retirer », and « Citer une référence ». Nothing is written before « Enregistrer ».
* The cited references of a draft: the list, each « Retirer », and « Citer une référence ».
* Nothing is written before « Enregistrer ». Mounted by no form since Rev 5 Task 11: the
* references dialog's edit mode (Task 12) takes it over.
*/
export default function FindingEditCitations(props: Props) {
const { ids, readOnly, onChange } = props
@@ -37,7 +31,6 @@ export default function FindingEditCitations(props: Props) {
const options = useCitableOptions(ids)
return (
<div className="space-y-2">
<CitationsHeader {...props} count={ids.length} />
{ids.length === 0 ? (
<p className="text-sm text-[var(--color-ink-muted)]">
{t('editor.findings.references_none')}
@@ -1,10 +1,9 @@
import { useEffect, useState } from 'react'
import { useState } from 'react'
import DraftField from '@/components/editor/fields/DraftField'
import { FormActions } from '@/components/editor/findings/DiscardConfirm'
import { useFormDraft } from '@/components/editor/findings/useDirtyGuard'
import FindingEditCitations from '@/components/editor/findings/FindingEditCitations'
import CitationsHeader from '@/components/editor/findings/FindingCitations'
import ReadOnlyNotice from '@/components/editor/findings/ReadOnlyNotice'
import { useCitationDraft } from '@/editor/findings/citationDraft'
import { patchElement, type FindingItem } from '@/editor/findings/tree'
import { isBlank, optionalText } from '@/editor/sheetDocument'
import { useEditCommand } from '@/editor/useEditCommand'
@@ -18,7 +17,6 @@ interface Draft {
descriptionFr: string
descriptionEn: string
vet: boolean
referenceIds: string[]
}
function draftOf(item: FindingItem): Draft {
@@ -30,8 +28,6 @@ function draftOf(item: FindingItem): Draft {
descriptionFr: described.fr ?? '',
descriptionEn: described.en ?? '',
vet: element.kind === 'finding_node' && element.value.requires_vet_referral,
referenceIds:
element.kind === 'zone' ? [] : [...element.value.reference_ids],
}
}
@@ -49,7 +45,11 @@ function normalized(draft: Draft): Draft {
const sameDraft = (a: Draft, b: Draft) =>
JSON.stringify(normalized(a)) === JSON.stringify(normalized(b))
/** The one `update` the form makes: names, and for a family or a node texts and citations. */
/**
* The one `update` the form makes: names, and for a family or a node its texts. The
* citations are the element's current ones (`patchElement` keeps them): the references
* dialog changes them, never this save.
*/
function patchOf(item: FindingItem, draft: Draft) {
const now = normalized(draft)
const { element } = item
@@ -65,7 +65,6 @@ function patchOf(item: FindingItem, draft: Draft) {
return patchElement(item, {
name,
description,
reference_ids: now.referenceIds,
...(element.kind === 'finding_node' ? vet : {}),
})
}
@@ -78,9 +77,9 @@ export interface FindingEditFormProps {
onDone: (saved: boolean) => void
/** « Abandonner » after a close request: the draft goes, so does the dialog. */
onDiscardClose: () => void
/** The citations sheet is open over the dialog (the trigger reads « expanded »). */
sheetOpen: boolean
/** Opens the citations sheet, which then lists the draft's citations. */
/** The references dialog is open over the finding dialog (the trigger reads « expanded »). */
citationsOpen: boolean
/** Opens the references dialog, which lists the element's citations. */
onShowReferences: (opener: HTMLElement) => void
}
@@ -177,20 +176,10 @@ function useEditSave(props: FindingEditFormProps, form: EditDraft) {
type EditDraft = ReturnType<typeof useFormDraft<Draft>>
/** The sheet opened from the form lists the draft's citations: they are published there. */
function usePublishedCitations(item: FindingItem, ids: readonly string[]) {
const publish = useCitationDraft((s) => s.publish)
useEffect(() => {
publish({ key: item.key, ids })
return () => publish(null)
}, [publish, item.key, ids])
}
/** The form's state: draft, blank names, save and cancel. */
function useEditForm(props: FindingEditFormProps) {
const form = useFormDraft(draftOf(props.item), props.guards, sameDraft)
const saving = useEditSave(props, form)
usePublishedCitations(props.item, form.draft.referenceIds)
const cancel = () =>
form.dirty && !props.readOnly
? form.guard.askToLeave()
@@ -199,9 +188,9 @@ function useEditForm(props: FindingEditFormProps) {
}
/**
* The element edited in the finding dialog (R2F-3): a local draft, one `update` on
* « Enregistrer », citations included (none when nothing changed), nothing on « Annuler »
* or a close. When the
* The element edited in the finding dialog (R2F-3): a local draft of its properties, one
* `update` on « Enregistrer » (none when nothing changed), nothing on « Annuler » or a
* close. Its citations are shown as « X références » only (Rev 5). When the
* editor turns read-only the draft stays, its fields read-only, and only « Abandonner »
* is left.
*/
@@ -238,16 +227,11 @@ export default function FindingEditForm(props: FindingEditFormProps) {
onChange={(vet) => form.set({ vet })}
/>
)}
{props.item.kind !== 'zone' && (
<FindingEditCitations
item={props.item}
ids={form.draft.referenceIds}
readOnly={props.readOnly}
sheetOpen={props.sheetOpen}
onChange={(referenceIds) => form.set({ referenceIds })}
onShowReferences={props.onShowReferences}
/>
)}
<CitationsHeader
item={props.item}
citationsOpen={props.citationsOpen}
onShowReferences={props.onShowReferences}
/>
{form.error && (
<p role="alert" className="text-sm text-[var(--color-danger-strong)]">
{form.error}
@@ -1,6 +1,6 @@
import ReadOnlyValue from '@/components/editor/fields/ReadOnlyValue'
import FindingActions from '@/components/editor/findings/FindingActions'
import FindingCitations from '@/components/editor/findings/FindingCitations'
import CitationsHeader from '@/components/editor/findings/FindingCitations'
import StatusBadge from '@/components/editor/findings/StatusBadge'
import type { FindingItem } from '@/editor/findings/tree'
import { useEditCommand } from '@/editor/useEditCommand'
@@ -9,7 +9,7 @@ import { useT } from '@/i18n/useT'
export interface FindingViewProps {
item: FindingItem
readOnly: boolean
sheetOpen: boolean
citationsOpen: boolean
onEdit: () => void
onDelete: () => void
onShowReferences: (opener: HTMLElement) => void
@@ -49,11 +49,11 @@ function Texts({ item }: { item: FindingItem }) {
}
/**
* The element read in the finding dialog (R2F-3): type and status, names, texts, its
* citations, and the actions of the edit tier. Nothing here edits a field in place.
* The element read in the finding dialog (R2F-3): type and status, names, texts, the way
* to its references dialog, and the actions of the edit tier. Nothing here edits a field in place.
*/
export default function FindingView(props: FindingViewProps) {
const { item, readOnly, sheetOpen } = props
const { item, readOnly, citationsOpen } = props
const t = useT()
const edit = useEditCommand()
return (
@@ -75,9 +75,9 @@ export default function FindingView(props: FindingViewProps) {
))}
</div>
<Texts item={item} />
<FindingCitations
<CitationsHeader
item={item}
sheetOpen={sheetOpen}
citationsOpen={citationsOpen}
onShowReferences={props.onShowReferences}
/>
<FindingActions
@@ -0,0 +1,49 @@
import type { ElementKey } from '@faunosteo/core-wasm/types'
import Dialog from '@/components/ui/Dialog'
import type { EditorTarget } from '@/editor/navigation'
import { useT } from '@/i18n/useT'
import CitationsPanel from './CitationsPanel'
import { CITATIONS_TITLE_ID } from './useCitationsPanel'
export interface CitationsDialogProps {
/** The family or finding node whose citations it shows; null while it is closed. */
citedBy: ElementKey | null
/** A user close (✕, Escape, the backdrop). */
onClose: () => boolean
/** « Tout afficher dans l'onglet Références ». */
onShowAll: () => void
/** « Aller à » a citing finding or a reference. */
navigate: (target: EditorTarget) => void
/** « X références » of the finding dialog under it, where the focus returns. */
returnFocus: () => HTMLElement | null
}
/**
* The references dialog of a family or a finding node (Rev 5): a modal layer over the
* finding dialog. The host sits after the tab body in the tree, so it is shown after the
* finding dialog and lies on top of it; Escape, ✕ and the backdrop close it only. The
* view mode reads the cited references.
*/
export default function CitationsDialog(props: CitationsDialogProps) {
const { citedBy, onClose } = props
const t = useT()
return (
<Dialog
open={citedBy !== null}
layer
size="lg"
title={t('editor.references.dialog_title')}
titleId={CITATIONS_TITLE_ID}
returnFocus={props.returnFocus}
onClose={() => (onClose() ? undefined : 'kept')}
>
{citedBy && (
<CitationsPanel
citedBy={citedBy}
onShowAll={props.onShowAll}
navigate={props.navigate}
/>
)}
</Dialog>
)
}
@@ -1,4 +1,3 @@
import type { RefObject } from 'react'
import type { LocalizedText, Reference } from '@faunosteo/core-wasm/types'
import Icon from '@/components/ui/Icon'
import { targetOfKey, type EditorTarget } from '@/editor/navigation'
@@ -7,53 +6,22 @@ import type { CitationEntry } from '@/editor/references/panel'
import { useT } from '@/i18n/useT'
import { useLocaleStore } from '@/state/localeStore'
import ReferenceTile from './ReferenceTile'
import { useCitationsPanel, type PanelHooks } from './useCitationsPanel'
import {
CITATIONS_TITLE_ID,
useCitationsPanel,
type PanelHooks,
} from './useCitationsPanel'
interface Props extends PanelHooks {
/** ✕ (Escape and the scrim close the sheet itself): a close the tile in edit may refuse. */
onClose: () => void
/** « Tout afficher dans l'onglet Références ». */
onShowAll: () => void
/** « Aller à » a citing finding or family: closes the panel, then moves. */
/** « Aller à » a citing finding or family: closes the dialog, then moves. */
navigate: (target: EditorTarget) => void
/** « Modifier »: the reference dialog over the sheet, `opener` is where the focus returns. */
onEdit: (id: string, opener: HTMLElement) => void
}
const RING =
'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-accent)]'
interface HeaderProps {
onClose: () => void
headingRef: RefObject<HTMLHeadingElement | null>
}
function PanelHeader({ onClose, headingRef }: HeaderProps) {
const t = useT()
return (
<div className="flex items-center justify-between gap-2">
<h2
ref={headingRef}
id="editor-citations-title"
tabIndex={-1}
data-place-heading
className="flex items-center gap-2 font-serif text-xl outline-none"
>
<Icon name="book" size={20} />
{t('editor.references.panel_title')}
</h2>
<button
type="button"
aria-label={t('editor.references.close')}
onClick={onClose}
className={`inline-flex h-11 w-11 items-center justify-center rounded-md ${RING}`}
>
<Icon name="close" size={18} />
</button>
</div>
)
}
/** « Zone › Famille › **Constat** »: where the finding sits, the finding itself in bold. */
function FindingPath({ path }: { path: readonly LocalizedText[] }) {
const locale = useLocaleStore((s) => s.locale)
@@ -102,7 +70,7 @@ function Summary({
)
}
/** A cited id the sheet no longer holds (P30): named; only the edit form removes it. */
/** A cited id the sheet no longer holds (P30): named, never removed in the view mode. */
function MissingRow({ entry }: { entry: CitationEntry }) {
const t = useT()
return (
@@ -115,7 +83,7 @@ function MissingRow({ entry }: { entry: CitationEntry }) {
type Panel = ReturnType<typeof useCitationsPanel>
/** One cited reference as a tile (« Modifier »). */
/** One cited reference as a tile, read only: its facts and its citers' « Aller à ». */
function EntryTile({
entry,
reference,
@@ -128,7 +96,7 @@ function EntryTile({
props: Props
}) {
const { tiles, keys, citers, entries } = panel
const { readOnly, citedBy, navigate } = props
const { citedBy, navigate } = props
const key = keys[entry.index]
return (
<ReferenceTile
@@ -139,11 +107,12 @@ function EntryTile({
count={entries.length}
citers={citers.get(reference.id) ?? []}
open={tiles.openKeys.has(key)}
readOnly={readOnly}
readOnly
movable={false}
on={{
toggle: () => tiles.toggle(key),
edit: (opener) => props.onEdit(reference.id, opener),
// Never offered: a read-only tile has no « Modifier ».
edit: () => undefined,
goTo: (target) => navigate(targetOfKey(target)),
}}
/>
@@ -176,13 +145,13 @@ function Entries({ panel, props }: { panel: Panel; props: Props }) {
}
/**
* The citations of one finding: the references it cites as tiles, « Tout afficher » for
* the whole library. No search, add, remove or order (P7): citing is the edit form's.
* The content of the side sheet
* (CitationsSheet), which is the modal; opens from a finding's trigger only.
* The citations of one family or finding node, the body of the references dialog
* (CitationsDialog): where it sits, the references it cites as tiles, the dangling ids
* named, « Tout afficher » for the whole library. The view mode: no search, add, remove,
* order or « Modifier » (P7).
*/
export default function CitationsPanel(props: Props) {
const { onClose, onShowAll } = props
const { onShowAll } = props
const t = useT()
const panel = useCitationsPanel(props)
const count = panel.entries.length
@@ -190,20 +159,18 @@ export default function CitationsPanel(props: Props) {
<section
id="editor-citations-panel"
data-testid="editor-citations-panel"
aria-labelledby="editor-citations-title"
className="flex h-full flex-col gap-3 border-l border-[var(--color-border)] bg-[var(--color-surface)] p-4 shadow-xl"
data-place="citations"
aria-labelledby={CITATIONS_TITLE_ID}
className="space-y-3"
>
<PanelHeader onClose={onClose} headingRef={panel.heading} />
<FindingPath path={panel.path} />
<Summary count={count} onShowAll={onShowAll} />
<div className="min-h-0 flex-1 space-y-3 overflow-y-auto pr-2">
{count === 0 && (
<p className="text-sm text-[var(--color-ink-muted)]">
{t('editor.references.panel_empty')}
</p>
)}
<Entries panel={panel} props={props} />
</div>
{count === 0 && (
<p className="text-sm text-[var(--color-ink-muted)]">
{t('editor.references.panel_empty')}
</p>
)}
<Entries panel={panel} props={props} />
</section>
)
}
@@ -1,90 +0,0 @@
import { AnimatePresence, motion, useReducedMotion } from 'motion/react'
import type { ElementKey } from '@faunosteo/core-wasm/types'
import ModalDialog from '@/components/ui/ModalDialog'
import type { EditorTarget } from '@/editor/navigation'
import CitationsPanel from './CitationsPanel'
/** Sheet motion (board 25): enter decelerates, the shorter exit accelerates (P9). */
const ENTER_S = 0.18
const EXIT_S = 0.14
export interface CitationsSheetProps {
/** The finding whose citations the view shows; null while the sheet is closed. */
citedBy: ElementKey | null
readOnly: boolean
/** A user close (✕, Escape, scrim). */
onClose: () => boolean
/** « Tout afficher dans l'onglet Références ». */
onShowAll: () => void
/** « Aller à » a citing finding or a reference. */
navigate: (target: EditorTarget) => void
/** « Modifier » on a tile: the reference dialog over the sheet. */
onEdit: (id: string, opener: HTMLElement) => void
/** « X références » of the dialog under the sheet, where the focus returns. */
returnFocus: () => HTMLElement | null
}
/** The slide and its timings; under reduced motion nothing moves and nothing waits. */
function useSheetMotion() {
const reduced = useReducedMotion()
return {
slide: reduced ? { x: 0 } : { x: '100%' },
enter: { duration: reduced ? 0 : ENTER_S, ease: 'easeOut' } as const,
leave: { duration: reduced ? 0 : EXIT_S, ease: 'easeIn' } as const,
}
}
/**
* The citations of a finding as a modal side sheet at the right edge, over the finding
* dialog (R2F-4): the host sits after the tab body in the tree, so it is shown after the
* dialog and lies on top of it. Escape and the scrim close the sheet only.
*/
function SheetSurface(props: CitationsSheetProps & { citedBy: ElementKey }) {
const { citedBy, onClose } = props
const sheetMotion = useSheetMotion()
const close = () => (onClose() ? undefined : 'kept')
return (
<ModalDialog
layer
onClose={close}
returnFocus={props.returnFocus}
labelledBy="editor-citations-title"
className="open:block"
>
<motion.div
aria-hidden="true"
data-testid="references-scrim"
onClick={close}
className="absolute inset-0 bg-[rgba(31,27,22,0.25)]"
initial={{ opacity: 0 }}
animate={{ opacity: 1, transition: sheetMotion.enter }}
exit={{ opacity: 0, transition: sheetMotion.leave }}
/>
<motion.div
data-place="citations"
className="absolute inset-y-0 right-0 w-[min(34rem,100vw)]"
initial={sheetMotion.slide}
animate={{ x: 0, transition: sheetMotion.enter }}
exit={{ ...sheetMotion.slide, transition: sheetMotion.leave }}
>
<CitationsPanel
readOnly={props.readOnly}
citedBy={citedBy}
onClose={onClose}
onShowAll={props.onShowAll}
navigate={props.navigate}
onEdit={props.onEdit}
/>
</motion.div>
</ModalDialog>
)
}
export default function CitationsSheet(props: CitationsSheetProps) {
const { citedBy } = props
return (
<AnimatePresence>
{citedBy && <SheetSurface key="citations" {...props} citedBy={citedBy} />}
</AnimatePresence>
)
}
@@ -1,6 +1,5 @@
import { useEffect, useMemo, useRef, useState, type FocusEvent } from 'react'
import type { ElementKey } from '@faunosteo/core-wasm/types'
import { draftIdsFor, useCitationDraft } from '@/editor/findings/citationDraft'
import type { FindingItem } from '@/editor/findings/tree'
import { citersOf } from '@/editor/references/citations'
import { citedIds, citingFinding } from '@/editor/references/filter'
@@ -9,10 +8,12 @@ import { useSheetDocument } from '@/editor/sheetDocument'
import { tileKeys, useTileState } from './useTileState'
export interface PanelHooks {
readOnly: boolean
citedBy: ElementKey
}
/** The references dialog's title: its name, and the focus of last resort (P20). */
export const CITATIONS_TITLE_ID = 'editor-citations-title'
/** The finding read from the current document; null once the document lacks it. */
function useCitedFinding(citedBy: ElementKey) {
const doc = useSheetDocument()
@@ -33,13 +34,12 @@ interface Held {
/**
* P20: when the row that holds the focus leaves the list (an undo that removes the
* reference), the focus goes to the next row's button, else the
* previous row's, else the heading. A row swapped for another at the same index (a
* previous row's, else the dialog's title. A row swapped for another at the same index (a
* tile turned into « Référence introuvable ») counts as gone, so « next » is the one
* after it.
*/
function useFocusRescue() {
const list = useRef<HTMLUListElement>(null)
const heading = useRef<HTMLHeadingElement>(null)
const held = useRef<Held | null>(null)
const rowsOf = () => [...(list.current?.children ?? [])]
const hold = (element: Element, at: number) => {
@@ -58,34 +58,30 @@ function useFocusRescue() {
const rows = rowsOf()
const next = rows.length < was.rows ? was.at : was.at + 1
const row = rows[next] ?? rows[was.at - 1]
const target = row?.querySelector('button') ?? heading.current
const target =
row?.querySelector('button') ??
document.getElementById(CITATIONS_TITLE_ID)
target?.focus()
})
return {
list,
heading,
track,
}
return { list, track }
}
/**
* What the panel shows of the finding: its cited ids and its path. Once the finding is
* gone (the sheet is leaving) the last known answer stays, so the sheet does not empty
* while it slides out and a tile in edit can still be asked to apply its draft. Opened
* from the edit form, it lists the form's staged citations, not the document's.
* What the dialog shows of the finding: the ids the document says it cites, and its
* path. Once the finding is gone (the dialog is leaving) the last known answer stays, so
* the dialog does not empty during its exit.
*/
function useShownFinding(
doc: ReturnType<typeof useSheetDocument>,
item: FindingItem | null,
citedBy: ElementKey,
) {
const staged = useCitationDraft((s) => draftIdsFor(s.draft, citedBy))
const found = useMemo(
() =>
doc && item
? { ids: staged ?? citedIds(item), path: findingPath(doc, citedBy) }
? { ids: citedIds(item), path: findingPath(doc, citedBy) }
: null,
[doc, item, citedBy, staged],
[doc, item, citedBy],
)
const [last, setLast] = useState(found)
if (found !== null && found !== last) setLast(found)
@@ -100,7 +96,7 @@ function useCitedEntries(hooks: PanelHooks) {
return { doc, path, entries, keys: tileKeys(entries) }
}
/** What the citations panel reads: the cited entries and their tiles. */
/** What the references dialog reads: the cited entries and their tiles. */
export function useCitationsPanel(hooks: PanelHooks) {
const { doc, path, entries, keys } = useCitedEntries(hooks)
const tiles = useTileState(doc ? keys : null)
@@ -113,7 +109,6 @@ export function useCitationsPanel(hooks: PanelHooks) {
citers: doc ? citersOf(doc) : new Map<string, never[]>(),
path,
list: focus.list,
heading: focus.heading,
trackFocus: focus.track,
}
}
+14 -4
View File
@@ -12,6 +12,11 @@ export interface DialogProps {
/** A view layer of the editor (ModalDialog): a Back may close it. */
layer?: boolean
title: string
/**
* A fixed id for the title, for parts that point at it; the title then also takes a
* programmatic focus (a fallback when the control holding it leaves).
*/
titleId?: string
/** The dialog's message, announced with its name (aria-describedby). */
description?: ReactNode
children: ReactNode
@@ -25,6 +30,9 @@ const SIZE: Record<NonNullable<DialogProps['size']>, string> = {
xl: 'max-w-4xl',
}
/** The title; `outline-none`: only a script focuses it (`titleId`), never a key. */
const TITLE = 'text-lg font-semibold text-[var(--color-ink)] outline-none'
export default function Dialog({ open, ...surface }: DialogProps) {
return (
<AnimatePresence>{open && <DialogSurface {...surface} />}</AnimatePresence>
@@ -36,6 +44,7 @@ function DialogSurface({
returnFocus,
layer,
title,
titleId,
description,
children,
size = 'md',
@@ -43,14 +52,14 @@ function DialogSurface({
const t = useT()
const reduced = useReducedMotion()
const scale = reduced ? 1 : 0.96
const titleId = useId()
const ownTitleId = useId()
const descriptionId = useId()
return (
<ModalDialog
onClose={onClose}
returnFocus={returnFocus}
layer={layer}
labelledBy={titleId}
labelledBy={titleId ?? ownTitleId}
describedBy={description ? descriptionId : undefined}
className="open:flex items-center justify-center p-3 sm:p-4"
>
@@ -72,8 +81,9 @@ function DialogSurface({
>
<header className="flex items-center justify-between px-4 sm:px-6 py-3 sm:py-4 border-b border-[var(--color-border)]">
<h2
id={titleId}
className="text-lg font-semibold text-[var(--color-ink)]"
id={titleId ?? ownTitleId}
tabIndex={titleId ? -1 : undefined}
className={TITLE}
>
{title}
</h2>