{"version":3,"sources":["../../src/hooks/useIntersectionObserver.ts"],"sourcesContent":["// Minified version of https://github.com/juliencrn/usehooks-ts/blob/master/packages/usehooks-ts/src/useIntersectionObserver/useIntersectionObserver.ts\n\nimport { useEffect, useRef, useState } from 'react'\n\ntype State = {\n    isIntersecting: boolean\n    entry?: IntersectionObserverEntry\n}\n\ntype UseIntersectionObserverOptions = {\n    threshold?: number\n    onChange?: (isIntersecting: boolean, entry: IntersectionObserverEntry) => void\n    root?: Element | Document | null\n    rootMargin?: string\n}\n\ntype IntersectionReturnType = {\n    ref: (node?: Element | null) => void\n    isIntersecting: boolean\n    entry?: IntersectionObserverEntry\n}\n\nexport function useIntersectionObserver ({\n    threshold = 0,\n    root = null,\n    rootMargin = '0%',\n    onChange,\n}: UseIntersectionObserverOptions = {}): IntersectionReturnType {\n    const [ref, setRef] = useState<Element | null | undefined>(null)\n    const [state, setState] = useState<State>(() => ({\n        isIntersecting: false,\n        entry: undefined,\n    }))\n\n    const callbackRef = useRef<UseIntersectionObserverOptions['onChange']>()\n    callbackRef.current = onChange\n\n    useEffect(() => {\n        if (!ref) return\n        if (!('IntersectionObserver' in window)) return\n\n        const observer = new IntersectionObserver((entries) => {\n            const thresholds = observer.thresholds\n\n            entries.forEach((entry) => {\n                const isIntersecting =\n                    entry.isIntersecting &&\n                    thresholds.some(threshold => entry.intersectionRatio >= threshold)\n\n                setState({ isIntersecting, entry })\n\n                if (callbackRef.current) {\n                    callbackRef.current(isIntersecting, entry)\n                }\n            })\n        }, { threshold, root, rootMargin })\n\n        observer.observe(ref)\n\n        return () => {\n            observer.disconnect()\n        }\n    }, [ref, root, rootMargin, threshold])\n\n    // ensures that if the observed element changes, the intersection observer is reinitialized\n    const prevRef = useRef<Element | null>(null)\n\n    useEffect(() => {\n        if (\n            !ref &&\n            state.entry?.target &&\n            prevRef.current !== state.entry.target\n        ) {\n            prevRef.current = state.entry.target\n            setState({ isIntersecting: false, entry: undefined })\n        }\n    }, [ref, state.entry])\n\n    return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