1 line
6.7 KiB
Plaintext
1 line
6.7 KiB
Plaintext
{"version":3,"file":"index.mjs","sources":["../../src/index.tsx"],"sourcesContent":["import 'intersection-observer'\nimport * as React from 'react'\nimport useLayoutEffect from '@react-hook/passive-layout-effect'\n\nfunction useIntersectionObserver<T extends HTMLElement = HTMLElement>(\n target: React.RefObject<T> | T | null,\n options: IntersectionObserverOptions = {}\n): MockIntersectionObserverEntry | IntersectionObserverEntry {\n const {\n root = null,\n pollInterval = null,\n useMutationObserver = false,\n rootMargin = '0px 0px 0px 0px',\n threshold = 0,\n initialIsIntersecting = false,\n } = options\n const [entry, setEntry] = React.useState<\n IntersectionObserverEntry | MockIntersectionObserverEntry\n >(() => ({\n boundingClientRect: null,\n intersectionRatio: 0,\n intersectionRect: null,\n isIntersecting: initialIsIntersecting,\n rootBounds: null,\n target: null,\n time: 0,\n }))\n const [observer, setObserver] = React.useState(() =>\n getIntersectionObserver({\n root,\n pollInterval,\n useMutationObserver,\n rootMargin,\n threshold,\n })\n )\n\n React.useEffect(() => {\n const observer = getIntersectionObserver({\n root,\n pollInterval,\n useMutationObserver,\n rootMargin,\n threshold,\n })\n setObserver(observer)\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [\n root,\n rootMargin,\n pollInterval,\n useMutationObserver,\n JSON.stringify(threshold),\n ])\n\n useLayoutEffect(() => {\n const targetEl = target && 'current' in target ? target.current : target\n if (!observer || !targetEl) return\n let didUnsubscribe = false\n observer.observer.observe(targetEl)\n\n const callback = (entries: IntersectionObserverEntry[]) => {\n if (didUnsubscribe) return\n\n for (let i = 0; i < entries.length; i++) {\n const entry = entries[i]\n if (entry.target === targetEl) {\n setEntry(entry)\n }\n }\n }\n\n observer.subscribe(callback)\n\n return () => {\n didUnsubscribe = true\n observer.observer.unobserve(targetEl)\n observer.unsubscribe(callback)\n }\n }, [target, observer])\n\n return entry\n}\n\nfunction createIntersectionObserver({\n root = null,\n pollInterval = null,\n useMutationObserver = false,\n rootMargin = '0px 0px 0px 0px',\n threshold = 0,\n}: IntersectionObserverOptions) {\n const callbacks: Set<IntersectionObserverCallback> = new Set()\n if (typeof IntersectionObserver === 'undefined') return null\n const observer = new IntersectionObserver(\n (entries) => {\n for (const callback of callbacks) callback(entries, observer)\n },\n {root, rootMargin, threshold}\n )\n // @ts-ignore\n observer.POLL_INTERVAL = pollInterval\n // @ts-ignore\n observer.USE_MUTATION_OBSERVER = useMutationObserver\n\n return {\n observer,\n getListeners() {\n return callbacks\n },\n subscribe: (callback: IntersectionObserverCallback) =>\n callbacks.add(callback),\n unsubscribe: (callback: IntersectionObserverCallback) =>\n callbacks.delete(callback),\n }\n}\n\nconst _intersectionObserver: Map<\n HTMLElement | null | undefined,\n Record<string, ReturnType<typeof createIntersectionObserver>>\n> = new Map()\n\nfunction getIntersectionObserver(options: IntersectionObserverOptions) {\n const {root, ...keys} = options\n const key = JSON.stringify(keys)\n let base = _intersectionObserver.get(root)\n if (!base) {\n base = {}\n _intersectionObserver.set(root, base)\n }\n return !base[key]\n ? (base[key] = createIntersectionObserver(options))\n : base[key]\n}\n\nexport type UseIntersectionObserverCallback = (\n entry: IntersectionObserverEntry,\n observer: IntersectionObserver\n) => any\n\nexport interface IntersectionObserverOptions {\n root?: HTMLElement | null\n pollInterval?: number | null\n useMutationObserver?: boolean\n rootMargin?: string\n threshold?: number | number[]\n initialIsIntersecting?: boolean\n}\n\nexport interface IntersectionObserverBounds {\n readonly height: number\n readonly width: number\n readonly top: number\n readonly left: number\n readonly right: number\n readonly bottom: number\n}\n\nexport interface MockIntersectionObserverEntry {\n readonly time: number | null\n readonly rootBounds: IntersectionObserverBounds | null\n readonly boundingClientRect: IntersectionObserverBounds | null\n readonly intersectionRect: IntersectionObserverBounds | null\n readonly intersectionRatio: number | null\n readonly target: HTMLElement | null\n readonly isIntersecting: boolean\n}\n\nexport default useIntersectionObserver\n"],"names":["getIntersectionObserver","options","root","keys","key","JSON","stringify","base","_intersectionObserver","get","set","pollInterval","useMutationObserver","rootMargin","threshold","callbacks","Set","IntersectionObserver","observer","entries","callback","POLL_INTERVAL","USE_MUTATION_OBSERVER","getListeners","subscribe","add","unsubscribe","delete","createIntersectionObserver","Map","target","initialIsIntersecting","entry","setEntry","React","boundingClientRect","intersectionRatio","intersectionRect","isIntersecting","rootBounds","time","setObserver","useLayoutEffect","targetEl","current","didUnsubscribe","observe","i","length","unobserve"],"mappings":"AAyHA,SAASA,EAAwBC,OACzBC,KAACA,GAAiBD,EAARE,sIAAQF,YAClBG,EAAMC,KAAKC,UAAUH,GACvBI,EAAOC,EAAsBC,IAAIP,UAChCK,IACHA,EAAO,GACPC,EAAsBE,IAAIR,EAAMK,IAE1BA,EAAKH,GAETG,EAAKH,GADJG,EAAKH,GA9CZ,gBAAoCF,KAClCA,EAAO,KAD2BS,aAElCA,EAAe,KAFmBC,oBAGlCA,EAAsB,EAHYC,WAIlCA,EAAa,kBAJqBC,UAKlCA,EAAY,KAENC,EAA+C,IAAIC,OACrB,oBAAzBC,qBAAsC,OAAO,SAClDC,EAAW,IAAID,qBAClBE,QACM,IAAMC,KAAYL,EAAWK,EAASD,EAASD,IAEtD,CAAChB,KAAAA,EAAMW,WAAAA,EAAYC,UAAAA,WAGrBI,EAASG,cAAgBV,EAEzBO,EAASI,sBAAwBV,EAE1B,CACLM,SAAAA,EACAK,aAAY,IACHR,EAETS,UAAYJ,GACVL,EAAUU,IAAIL,GAChBM,YAAcN,GACZL,EAAUY,OAAOP,IAkBJQ,CAA2B3B,kIAd9C,IAAMO,EAGF,IAAIqB,mBAnHR,SACEC,EACA7B,YAAAA,IAAAA,EAAuC,QAEjCC,KACJA,EAAO,KADHS,aAEJA,EAAe,KAFXC,oBAGJA,EAAsB,EAHlBC,WAIJA,EAAa,kBAJTC,UAKJA,EAAY,EALRiB,sBAMJA,EAAwB,GACtB9B,GACG+B,EAAOC,GAAYC,EAExB,MACAC,mBAAoB,KACpBC,kBAAmB,EACnBC,iBAAkB,KAClBC,eAAgBP,EAChBQ,WAAY,KACZT,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