{"version":3,"sources":["../../src/use-target-element/index.ts"],"sourcesContent":["import { useRef } from 'react'\nimport { useIsomorphicLayoutEffect } from '../use-isomorphic-layout-effect'\nimport { normalizeElement } from './normalize-element'\n\nimport type { MutableRefObject, RefObject } from 'react'\nimport type { Gettable } from '../utils/basic'\nimport type { AtomBaseTarget, ElementTarget, GlobalTarget } from './normalize-element'\n\nexport * from './normalize-element'\n\nexport type UseTargetElementOptions = {\n  /**\n   * Callback when the target element is initialized\n   *\n   * @since 1.14.0\n   */\n  onInitialized?: (element: Element | GlobalTarget | null) => void\n  /**\n   * Callback when the target element changes\n   *\n   * @since 1.14.0\n   */\n  onChange?: (element: Element | GlobalTarget | null) => void\n}\n\n/**\n * A React Hook that helps to get the target element via selector, ref, element and more.\n */\nexport function useTargetElement<T extends GlobalTarget>(\n  target: Gettable<T>,\n  options?: UseTargetElementOptions,\n): RefObject<T>\nexport function useTargetElement<T extends Element | null>(\n  target: Gettable<T>,\n  options?: UseTargetElementOptions,\n): RefObject<T>\nexport function useTargetElement<T extends keyof HTMLElementTagNameMap>(\n  target: Gettable<T>,\n  options?: UseTargetElementOptions,\n): RefObject<HTMLElementTagNameMap[T]>\nexport function useTargetElement<T extends keyof SVGElementTagNameMap>(\n  target: Gettable<T>,\n  options?: UseTargetElementOptions,\n): RefObject<SVGElementTagNameMap[T]>\nexport function useTargetElement<A extends AtomBaseTarget, T extends ElementTarget<A> = ElementTarget<A>>(\n  target: Gettable<T>,\n  options?: UseTargetElementOptions,\n): RefObject<A>\nexport function useTargetElement<\n  R extends GlobalTarget | Element,\n  T extends string | null | undefined | false | MutableRefObject<AtomBaseTarget | undefined> | R,\n>(target: Gettable<T>, options?: UseTargetElementOptions): RefObject<R>\nexport function useTargetElement(target: any, options: UseTargetElementOptions = {}) {\n  const elementRef = useRef(null)\n  const initializedRef = useRef(false)\n\n  useIsomorphicLayoutEffect(() => {\n    const nextElement = normalizeElement(target)\n\n    if (!initializedRef.current) {\n      if (nextElement) {\n        initializedRef.current = true\n        elementRef.current = nextElement\n        options.onInitialized?.(elementRef.current)\n      }\n\n      return\n    }\n\n    if (nextElement !== elementRef.current) {\n      elementRef.current = nextElement\n      options.onChange?.(nextElement)\n    }\n  })\n\n  return elementRef\n}\n"],"names":["useTargetElement","target","options","elementRef","useRef","initializedRef","useIsomorphicLayoutEffect","nextElement","normalizeElement","current","onInitialized","onChange"],"mappings":";;;;+BAoDgBA;;;eAAAA;;;qBApDO;yCACmB;6CACT;;;;;;;;;;;;;;AAkD1B,SAASA,iBAAiBC,MAAW;QAAEC,UAAAA,iEAAmC,CAAC;IAChF,IAAMC,aAAaC,IAAAA,aAAM,EAAC;IAC1B,IAAMC,iBAAiBD,IAAAA,aAAM,EAAC;IAE9BE,IAAAA,oDAAyB,EAAC;QACxB,IAAMC,cAAcC,IAAAA,kCAAgB,EAACP;QAErC,IAAI,CAACI,eAAeI,OAAO,EAAE;YAC3B,IAAIF,aAAa;oBAGfL;gBAFAG,eAAeI,OAAO,GAAG;gBACzBN,WAAWM,OAAO,GAAGF;iBACrBL,yBAAAA,QAAQQ,aAAa,cAArBR,6CAAAA,4BAAAA,SAAwBC,WAAWM,OAAO;YAC5C;YAEA;QACF;QAEA,IAAIF,gBAAgBJ,WAAWM,OAAO,EAAE;gBAEtCP;YADAC,WAAWM,OAAO,GAAGF;aACrBL,oBAAAA,QAAQS,QAAQ,cAAhBT,wCAAAA,uBAAAA,SAAmBK;QACrB;IACF;IAEA,OAAOJ;AACT"}