import { RefObject, useEffect, useState } from 'react'; const useIntersection = ( ref: RefObject, options: IntersectionObserverInit ): IntersectionObserverEntry | null => { const [intersectionObserverEntry, setIntersectionObserverEntry] = useState(null); useEffect(() => { if (ref.current) { const handler = (entries: IntersectionObserverEntry[]) => { setIntersectionObserverEntry(entries[0]); }; const observer = new IntersectionObserver(handler, options); observer.observe(ref.current); return () => observer.disconnect(); } return () => {}; }, [ref.current, options.threshold, options.root, options.rootMargin]); return intersectionObserverEntry; }; export default useIntersection;