@mantine/hooks
Version:
A collection of 50+ hooks for state and UI management
1 lines • 1.64 kB
Source Map (JSON)
{"version":3,"file":"use-intersection.mjs","names":[],"sources":["../../src/use-intersection/use-intersection.ts"],"sourcesContent":["import { useCallback, useRef, useState } from 'react';\n\nexport interface UseIntersectionReturnValue<T> {\n ref: React.RefCallback<T | null>;\n entry: IntersectionObserverEntry | null;\n}\n\nexport function useIntersection<T extends HTMLElement = any>(\n options?: IntersectionObserverInit\n): UseIntersectionReturnValue<T> {\n const [entry, setEntry] = useState<IntersectionObserverEntry | null>(null);\n\n const observer = useRef<IntersectionObserver | null>(null);\n\n const ref: React.RefCallback<T | null> = useCallback(\n (element) => {\n if (observer.current) {\n observer.current.disconnect();\n observer.current = null;\n }\n\n if (element === null) {\n setEntry(null);\n return;\n }\n\n observer.current = new IntersectionObserver(([_entry]) => {\n setEntry(_entry);\n }, options);\n\n observer.current.observe(element);\n },\n [options?.rootMargin, options?.root, options?.threshold]\n );\n\n return { ref, entry };\n}\n\nexport namespace useIntersection {\n export type ReturnValue<T> = UseIntersectionReturnValue<T>;\n}\n"],"mappings":";;;AAOA,SAAgB,gBACd,SAC+B;CAC/B,MAAM,CAAC,OAAO,YAAY,SAA2C,KAAK;CAE1E,MAAM,WAAW,OAAoC,KAAK;AAuB1D,QAAO;EAAE,KArBgC,aACtC,YAAY;AACX,OAAI,SAAS,SAAS;AACpB,aAAS,QAAQ,YAAY;AAC7B,aAAS,UAAU;;AAGrB,OAAI,YAAY,MAAM;AACpB,aAAS,KAAK;AACd;;AAGF,YAAS,UAAU,IAAI,sBAAsB,CAAC,YAAY;AACxD,aAAS,OAAO;MACf,QAAQ;AAEX,YAAS,QAAQ,QAAQ,QAAQ;KAEnC;GAAC,SAAS;GAAY,SAAS;GAAM,SAAS;GAAU,CACzD;EAEa;EAAO"}