"use client" import * as React from "react" const OPEN_DELAY_MS = 120 const CLOSE_DELAY_MS = 180 /** * Hover / focus "peek" for collapsed icon rails — the desktop counterpart of the * mobile nav flyout, minus the scrim. Spread `handlers` on the rail; `peek` says * whether the expanded sheet should be showing. * * Opens after a short intent delay (so sweeping the cursor past the rail does not * flash it) and closes after a short grace period (so the cursor can travel * between the rail and its sheet). Keyboard focus inside the rail also opens it, * and Escape dismisses it. `enabled: false` forces it closed and cancels timers. */ export function useHoverPeek(enabled: boolean) { const [peek, setPeek] = React.useState(false) const timer = React.useRef(undefined) const clear = React.useCallback(() => { window.clearTimeout(timer.current) }, []) const schedule = React.useCallback( (next: boolean, delay: number) => { clear() timer.current = window.setTimeout(() => setPeek(next), delay) }, [clear], ) React.useEffect(() => { if (enabled) return clear() setPeek(false) }, [enabled, clear]) React.useEffect(() => clear, [clear]) const handlers = React.useMemo( () => ({ onPointerEnter: (event: React.PointerEvent) => { // Touch has no hover; a tap must not trigger a sticky peek. if (!enabled || event.pointerType === "touch") return schedule(true, OPEN_DELAY_MS) }, onPointerLeave: (event: React.PointerEvent) => { if (event.pointerType === "touch") return schedule(false, CLOSE_DELAY_MS) }, onFocus: (event: React.FocusEvent) => { if (!enabled || !event.currentTarget.matches(":focus-within")) return // Only keyboard focus; mouse clicks already open it via hover. if (!(event.target as HTMLElement).matches(":focus-visible")) return schedule(true, 0) }, onBlur: (event: React.FocusEvent) => { if (event.currentTarget.contains(event.relatedTarget as Node | null)) return schedule(false, CLOSE_DELAY_MS) }, onKeyDown: (event: React.KeyboardEvent) => { if (event.key === "Escape") { clear() setPeek(false) } }, }), [enabled, schedule, clear], ) const dismiss = React.useCallback(() => { clear() setPeek(false) }, [clear]) return { peek: enabled && peek, handlers, dismiss } }