fix(app): a focus request is consumed once, not replayed on tab return
This commit is contained in:
1 parent
4f223e04cd
commit
3d589e1556
14 files changed
+99
-30
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useRef } from 'react'
|
||||
import { createContext, useContext, 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. */
|
||||
@@ -19,17 +19,24 @@ export function focusAttrs(key: ElementKey) {
|
||||
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. */
|
||||
/** The route's answer to a consumed request: it clears it, so a remounted tab never replays. */
|
||||
export const FocusConsumedContext = createContext<(serial: number) => void>(
|
||||
() => undefined,
|
||||
)
|
||||
|
||||
/** Runs `apply` once per request, after the tab has rendered, then reports it consumed. */
|
||||
export function useFocusRequest(
|
||||
focus: FocusRequest | undefined,
|
||||
apply: (key: ElementKey) => void,
|
||||
): void {
|
||||
const handled = useRef<number | null>(null)
|
||||
const consumed = useContext(FocusConsumedContext)
|
||||
useEffect(() => {
|
||||
if (!focus || handled.current === focus.serial) return
|
||||
handled.current = focus.serial
|
||||
apply(focus.key)
|
||||
})
|
||||
consumed(focus.serial)
|
||||
}, [focus, apply, consumed])
|
||||
}
|
||||
|
||||
/** Scrolls to and focuses the row tagged with `key`. */
|
||||
|
||||
Reference in new issue
Block a user