{"version":3,"file":"utils.cjs","names":[],"sources":["../../src/utils.ts"],"sourcesContent":["import * as Solid from 'solid-js'\n\n/**\n * React hook to wrap `IntersectionObserver`.\n *\n * This hook will create an `IntersectionObserver` and observe the ref passed to it.\n *\n * When the intersection changes, the callback will be called with the `IntersectionObserverEntry`.\n *\n * @param ref - The ref to observe\n * @param callback - The callback to call when the intersection changes\n * @param disabled - Whether observation is disabled\n * @param cleanupWhenDisabled - Whether cleanup is needed without an observer (defaults to true)\n * @returns The IntersectionObserver instance\n * @example\n * ```tsx\n * const MyComponent = () => {\n * const ref = React.useRef<HTMLDivElement>(null)\n * useIntersectionObserver(\n *  ref,\n *  (entry) => { doSomething(entry) },\n *  false\n * )\n * return <div ref={ref} />\n * ```\n */\nexport function useIntersectionObserver<T extends Element>(\n  ref: Solid.Accessor<T | null>,\n  callback: (entry?: IntersectionObserverEntry) => void,\n  disabled: Solid.Accessor<boolean>,\n  cleanupWhenDisabled?: Solid.Accessor<boolean>,\n): Solid.Accessor<IntersectionObserver | null> {\n  const isIntersectionObserverAvailable =\n    typeof IntersectionObserver === 'function'\n  let observerRef: IntersectionObserver | null = null\n\n  Solid.createEffect(() => {\n    const r = ref()\n    if (disabled() || !r || !isIntersectionObserverAvailable) {\n      if (cleanupWhenDisabled?.() ?? true) {\n        Solid.onCleanup(() => callback())\n      }\n      return\n    }\n\n    let active = true\n    const observer = new IntersectionObserver(\n      (entries) => {\n        // Queued notifications can arrive after this effect has cleaned up.\n        if (active) {\n          callback(entries.pop())\n        }\n      },\n      { rootMargin: '100px' },\n    )\n\n    observerRef = observer\n    observer.observe(r)\n\n    Solid.onCleanup(() => {\n      active = false\n      observer.disconnect()\n      callback()\n    })\n  })\n\n  return () => observerRef\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0BA,SAAgB,wBACd,KACA,UACA,UACA,qBAC6C;CAC7C,MAAM,kCACJ,OAAO,yBAAyB;CAClC,IAAI,cAA2C;CAE/C,SAAM,mBAAmB;EACvB,MAAM,IAAI,IAAI;EACd,IAAI,SAAS,KAAK,CAAC,KAAK,CAAC,iCAAiC;GACxD,IAAI,sBAAsB,KAAK,MAC7B,SAAM,gBAAgB,SAAS,CAAC;GAElC;EACF;EAEA,IAAI,SAAS;EACb,MAAM,WAAW,IAAI,sBAClB,YAAY;GAEX,IAAI,QACF,SAAS,QAAQ,IAAI,CAAC;EAE1B,GACA,EAAE,YAAY,QAAQ,CACxB;EAEA,cAAc;EACd,SAAS,QAAQ,CAAC;EAElB,SAAM,gBAAgB;GACpB,SAAS;GACT,SAAS,WAAW;GACpB,SAAS;EACX,CAAC;CACH,CAAC;CAED,aAAa;AACf"}