UNPKG

@primer/react-brand

Version:

Primer Brand is a GitHub's design system for creating React-based marketing websites and digital experiences.

28 lines (27 loc) 822 B
import { useProvidedRefOrCreate as e } from "./useRef.js"; import t from "react"; import { focusTrap as n } from "@primer/behaviors"; //#region src/hooks/useFocusTrap.ts function r(r, i = []) { let a = e(r?.containerRef), o = e(r?.initialFocusRef), s = r?.disabled, c = t.useRef(null), l = t.useRef(null); function u() { c.current?.abort(), r?.restoreFocusOnCleanUp && l.current instanceof HTMLElement && l.current.focus(), l.current = null; } return t.useEffect(() => { if (a.current instanceof HTMLElement) if (s) u(); else return l.current ||= document.activeElement, c.current = n(a.current, o.current ?? void 0) ?? null, () => { u(); }; }, [ a, o, s, ...i ]), { containerRef: a, initialFocusRef: o }; } //#endregion export { r as useFocusTrap }; //# sourceMappingURL=useFocusTrap.js.map