import { useEffect, useRef } from "react"; export function useOverlayFocus( visible: boolean, innerElementRef: React.MutableRefObject ) { const outSideActiveElementRef = useRef(null); useEffect(() => { if (visible) { if (innerElementRef.current) { outSideActiveElementRef.current = document.activeElement as HTMLElement; innerElementRef.current.focus({ preventScroll: true }); } } else if (outSideActiveElementRef.current) { outSideActiveElementRef.current.focus({ preventScroll: true }); outSideActiveElementRef.current = null; } }, [innerElementRef, visible]); }