{"version":3,"sources":["../../src/use-active-element/index.ts"],"sourcesContent":["import { useEventListener } from '../use-event-listener'\nimport { useMount } from '../use-mount'\nimport { useSafeState } from '../use-safe-state'\n\nexport interface UseActiveElementOptions {\n  /**\n   * Whether to traverse the shadow DOM to find the active element\n   *\n   * @defaultValue false\n   */\n  deep?: boolean\n}\n\n/**\n * A React Hook that returns the currently **active element** (via `document.activeElement`), optionally traversing the shadow DOM.\n *\n * @param {UseActiveElementOptions} options - `UseActiveElementOptions`, options to configure the hook, see {@link UseActiveElementOptions}\n * @returns {Element | null} `Element | null`, the currently active element, `null` if there is none, see {@link Element}\n */\nexport function useActiveElement(options: UseActiveElementOptions = {}): Element | null {\n  const [element, setElement] = useSafeState<Element | null>(null)\n\n  function updateElement() {\n    setElement(getActiveElement(options.deep))\n  }\n\n  function blurHandler(event: FocusEvent) {\n    if (event.relatedTarget === null) {\n      updateElement()\n    }\n  }\n\n  useMount(updateElement)\n\n  useEventListener(() => window, 'focus', updateElement, true)\n  useEventListener(() => window, 'blur', blurHandler, true)\n\n  return element\n}\n\n/**\n * Get the currently active element, optionally traversing the shadow DOM.\n *\n * @param {boolean} [deep=false] - `boolean`, Whether to traverse the shadow DOM to find the active element\n */\nfunction getActiveElement(deep: boolean = false): Element | null {\n  let element = document.activeElement\n\n  if (deep) {\n    while (element?.shadowRoot) {\n      element = element.shadowRoot.activeElement\n    }\n  }\n\n  return element as Element\n}\n"],"names":["useActiveElement","options","useSafeState","element","setElement","updateElement","getActiveElement","deep","blurHandler","event","relatedTarget","useMount","useEventListener","window","document","activeElement","shadowRoot"],"mappings":";;;;+BAmBgBA;;;eAAAA;;;gCAnBiB;wBACR;4BACI;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiBtB,SAASA;QAAiBC,UAAAA,iEAAmC,CAAC;IACnE,IAA8BC,iCAAAA,IAAAA,0BAAY,EAAiB,WAApDC,UAAuBD,kBAAdE,aAAcF;IAE9B,SAASG;QACPD,WAAWE,iBAAiBL,QAAQM,IAAI;IAC1C;IAEA,SAASC,YAAYC,KAAiB;QACpC,IAAIA,MAAMC,aAAa,KAAK,MAAM;YAChCL;QACF;IACF;IAEAM,IAAAA,kBAAQ,EAACN;IAETO,IAAAA,kCAAgB,EAAC;eAAMC;OAAQ,SAASR,eAAe;IACvDO,IAAAA,kCAAgB,EAAC;eAAMC;OAAQ,QAAQL,aAAa;IAEpD,OAAOL;AACT;AAEA;;;;CAIC,GACD,SAASG;QAAiBC,OAAAA,iEAAgB;IACxC,IAAIJ,UAAUW,SAASC,aAAa;IAEpC,IAAIR,MAAM;QACR,MAAOJ,oBAAAA,8BAAAA,QAASa,UAAU,CAAE;YAC1Bb,UAAUA,QAAQa,UAAU,CAACD,aAAa;QAC5C;IACF;IAEA,OAAOZ;AACT"}