{"version":3,"file":"use-focus-trap.cjs","names":["useRef","FOCUS_SELECTOR","tabbable","focusable","useCallback"],"sources":["../../src/use-focus-trap/use-focus-trap.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\nimport { scopeTab } from './scope-tab';\nimport { FOCUS_SELECTOR, focusable, tabbable } from './tabbable';\n\nconst activeTraps = new Set<React.RefObject<HTMLElement | null>>();\n\nfunction hasNestedActiveTrap(ref: React.RefObject<HTMLElement | null>, node: HTMLElement) {\n  const activeElement = (node.getRootNode() as unknown as DocumentOrShadowRoot).activeElement;\n  if (!activeElement) {\n    return false;\n  }\n\n  for (const trap of activeTraps) {\n    const nested = trap.current;\n    if (\n      trap !== ref &&\n      nested &&\n      nested !== node &&\n      node.contains(nested) &&\n      nested.contains(activeElement)\n    ) {\n      return true;\n    }\n  }\n\n  return false;\n}\n\nexport function useFocusTrap(active = true): React.RefCallback<HTMLElement | null> {\n  const ref = useRef<HTMLElement>(null);\n\n  const focusNode = (node: HTMLElement) => {\n    let focusElement: HTMLElement | null = node.querySelector('[data-autofocus]');\n\n    if (!focusElement) {\n      const children = Array.from<HTMLElement>(node.querySelectorAll(FOCUS_SELECTOR));\n      focusElement = children.find(tabbable) || children.find(focusable) || null;\n      if (!focusElement && focusable(node)) {\n        focusElement = node;\n      }\n    }\n\n    if (focusElement) {\n      focusElement.focus({ preventScroll: true });\n    } else if (process.env.NODE_ENV === 'development') {\n      // oxlint-disable-next-line no-console\n      console.warn(\n        '[@mantine/hooks/use-focus-trap] Failed to find focusable element within provided node',\n        node\n      );\n    }\n  };\n\n  const setRef = useCallback(\n    (node: HTMLElement | null) => {\n      if (!active) {\n        return;\n      }\n\n      if (node === null) {\n        ref.current = null;\n        return;\n      }\n\n      if (ref.current === node) {\n        return;\n      }\n\n      // Delay processing the HTML node by a frame. This ensures focus is assigned correctly.\n      setTimeout(() => {\n        if (node.getRootNode()) {\n          focusNode(node);\n        } else if (process.env.NODE_ENV === 'development') {\n          // oxlint-disable-next-line no-console\n          console.warn('[@mantine/hooks/use-focus-trap] Ref node is not part of the dom', node);\n        }\n      });\n\n      ref.current = node;\n    },\n    [active]\n  );\n\n  useEffect(() => {\n    if (!active) {\n      return undefined;\n    }\n\n    if (ref.current) {\n      setTimeout(() => {\n        if (ref.current) {\n          focusNode(ref.current);\n        }\n      });\n    }\n\n    const handleKeyDown = (event: KeyboardEvent) => {\n      if (\n        event.key !== 'Tab' ||\n        event.defaultPrevented ||\n        !ref.current ||\n        hasNestedActiveTrap(ref, ref.current)\n      ) {\n        return;\n      }\n\n      scopeTab(ref.current, event);\n    };\n\n    activeTraps.add(ref);\n    document.addEventListener('keydown', handleKeyDown);\n    return () => {\n      activeTraps.delete(ref);\n      document.removeEventListener('keydown', handleKeyDown);\n    };\n  }, [active]);\n\n  return setRef;\n}\n"],"mappings":";;;;;AAIA,MAAM,8BAAc,IAAI,IAAyC;AAEjE,SAAS,oBAAoB,KAA0C,MAAmB;CACxF,MAAM,gBAAiB,KAAK,YAAY,CAAC,CAAqC;CAC9E,IAAI,CAAC,eACH,OAAO;CAGT,KAAK,MAAM,QAAQ,aAAa;EAC9B,MAAM,SAAS,KAAK;EACpB,IACE,SAAS,OACT,UACA,WAAW,QACX,KAAK,SAAS,MAAM,KACpB,OAAO,SAAS,aAAa,GAE7B,OAAO;CAEX;CAEA,OAAO;AACT;AAEA,SAAgB,aAAa,SAAS,MAA6C;CACjF,MAAM,OAAA,GAAMA,MAAAA,OAAAA,CAAoB,IAAI;CAEpC,MAAM,aAAa,SAAsB;EACvC,IAAI,eAAmC,KAAK,cAAc,kBAAkB;EAE5E,IAAI,CAAC,cAAc;GACjB,MAAM,WAAW,MAAM,KAAkB,KAAK,iBAAiBC,iBAAAA,cAAc,CAAC;GAC9E,eAAe,SAAS,KAAKC,iBAAAA,QAAQ,KAAK,SAAS,KAAKC,iBAAAA,SAAS,KAAK;GACtE,IAAI,CAAC,gBAAgBA,iBAAAA,UAAU,IAAI,GACjC,eAAe;EAEnB;EAEA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