UNPKG

@guruhotel/aura-hooks

Version:

🪝 Hooks library designed by the Guruhotel team for Aura UI

1 lines 557 B
import{useRef as e}from"react";import{useDidUpdate as t}from"../use-did-update/use-did-update";export function useFocusReturn({opened:n,shouldReturnFocus:o=!0}){const u=e(),r=()=>{var e;u.current&&"focus"in u.current&&"function"==typeof u.current.focus&&(null==(e=u.current)||e.focus())};return t((()=>{let e=-1;const t=t=>{"Tab"===t.key&&window.clearTimeout(e)};return document.addEventListener("keydown",t),n?u.current=document.activeElement:o&&(e=window.setTimeout(r,10)),()=>{window.clearTimeout(e),document.removeEventListener("keydown",t)}}),[n,o]),r}