feat(app): Aller à selects the element in its tab, reload after submit
This commit is contained in:
1 parent
2a54fa4c78
commit
4f223e04cd
24 files changed
+473
-66
No files matched your search
@@ -0,0 +1,48 @@
|
||||
import { useEffect, useRef } from 'react'
|
||||
import type { ElementKey } from '@faunosteo/core-wasm/types'
|
||||
|
||||
/** « Aller à … »: the element to show and a serial so each request is consumed once. */
|
||||
export interface FocusRequest {
|
||||
key: ElementKey
|
||||
serial: number
|
||||
}
|
||||
|
||||
/** The attributes that make a row of a list reachable by a focus request. */
|
||||
export function focusAttrs(key: ElementKey) {
|
||||
return {
|
||||
'data-focus-key': JSON.stringify(key),
|
||||
tabIndex: -1,
|
||||
} as const
|
||||
}
|
||||
|
||||
/** The ring a programmatically focused row shows. */
|
||||
export const FOCUS_CLASS =
|
||||
'focus:outline focus:outline-2 focus:outline-[var(--color-accent)]'
|
||||
|
||||
/** Runs `apply` once per request serial, after the tab has rendered. */
|
||||
export function useFocusRequest(
|
||||
focus: FocusRequest | undefined,
|
||||
apply: (key: ElementKey) => void,
|
||||
): void {
|
||||
const handled = useRef<number | null>(null)
|
||||
useEffect(() => {
|
||||
if (!focus || handled.current === focus.serial) return
|
||||
handled.current = focus.serial
|
||||
apply(focus.key)
|
||||
})
|
||||
}
|
||||
|
||||
/** Scrolls to and focuses the row tagged with `key`. */
|
||||
export function focusRow(key: ElementKey): void {
|
||||
const wanted = JSON.stringify(key)
|
||||
const row = [
|
||||
...document.querySelectorAll<HTMLElement>('[data-focus-key]'),
|
||||
].find((candidate) => candidate.dataset.focusKey === wanted)
|
||||
row?.scrollIntoView?.({ block: 'center' })
|
||||
row?.focus()
|
||||
}
|
||||
|
||||
/** Row-based tabs (Général, Vues, Références, Vertèbres): focus the named row. */
|
||||
export function useRowFocus(focus: FocusRequest | undefined): void {
|
||||
useFocusRequest(focus, focusRow)
|
||||
}
|
||||
Reference in new issue
Block a user