import { useEffect, useRef, useState } from 'react'; /** * Hook for observing element intersection with viewport for lazy loading, infinite scroll, etc. */ export function useIntersectionObserver( options: IntersectionObserverInit = {} ): [React.RefObject, boolean] { const [isIntersecting, setIsIntersecting] = useState(false); const ref = useRef(null); useEffect(() => { const element = ref.current; if (!element) return; const observer = new IntersectionObserver( ([entry]) => { setIsIntersecting(entry.isIntersecting); }, { threshold: 0.1, rootMargin: '50px', ...options, } ); observer.observe(element); return () => { observer.unobserve(element); }; }, [options]); return [ref, isIntersecting]; }