@guruhotel/aura-hooks
Version:
🪝 Hooks library designed by the Guruhotel team for Aura UI
1 lines • 1.05 kB
JavaScript
exports.__esModule=!0,exports.useFocusTrap=function(o=!0){const u=(0,e.useRef)(),c=(0,e.useRef)(null),s=(0,e.useCallback)((e=>{if(o)if(c.current&&c.current(),e){const t=t=>{c.current=(0,n.createAriaHider)(t);let o=e.querySelector("[data-autofocus]");if(!o){const t=Array.from(e.querySelectorAll(r.FOCUS_SELECTOR));o=t.find(r.tabbable)||t.find(r.focusable)||null,!o&&(0,r.focusable)(e)&&(o=e)}o?o.focus({preventScroll:!0}):"development"===process.env.NODE_ENV&&console.warn("[useFocusTrap] Failed to find focusable element within provided node",e)};setTimeout((()=>{e.ownerDocument?t(e):"development"===process.env.NODE_ENV&&console.warn("[useFocusTrap] Ref node is not part of the dom",e)})),u.current=e}else u.current=null}),[o]);return(0,e.useEffect)((()=>{if(!o)return;const e=e=>{"Tab"===e.key&&u.current&&(0,t.scopeTab)(u.current,e)};return document.addEventListener("keydown",e),()=>{document.removeEventListener("keydown",e)}}),[o]),s};var e=require("react"),r=require("./tabbable"),t=require("./scope-tab"),n=require("./create-aria-hider");