Files
opencode-manager/frontend/src/hooks/useRestoreFocusOnClose.ts
T
Chris Scott 994a8b82e8 feat(shortcuts): rework shortcut bindings, focus restore, and input guards
Resolve effective bindings from stored prefs plus defaults with leader/direct collision handling and default leader Ctrl+X; let direct actions pass through in terminals and suppress abort while an overlay is open. Add half-page scroll disengage, verified overlay focus restore, and consolidate prompt reset.
2026-10-08 12:40:04 -04:00

28 lines
945 B
TypeScript

import { useEffect, useRef, type RefObject } from 'react'
import { getFocusedElement, restoreOverlayFocus } from '@/lib/overlayFocus'
/**
* Captures the focused element when an overlay opens and restores focus to it when the overlay closes.
*
* The opener is captured during render on the closed-to-open transition, before children with
* `autoFocus` claim focus during commit, and is idempotent across StrictMode's double render.
*/
export function useRestoreFocusOnClose(
isOpen: boolean,
containerRef: RefObject<HTMLElement | null>,
): void {
const returnFocusRef = useRef<HTMLElement | null>(null)
const wasOpenRef = useRef(false)
if (isOpen && !wasOpenRef.current) {
returnFocusRef.current = getFocusedElement()
}
useEffect(() => {
if (!isOpen && wasOpenRef.current) {
restoreOverlayFocus(returnFocusRef.current, containerRef.current)
}
wasOpenRef.current = isOpen
}, [isOpen, containerRef])
}