(margin = "200px")
| 3 | import { useRef, useState, useEffect } from "react"; |
| 4 | |
| 5 | export function useInView(margin = "200px"): [React.RefObject<HTMLDivElement>, boolean] { |
| 6 | const ref = useRef<HTMLDivElement>(null); |
| 7 | const [inView, setInView] = useState(false); |
| 8 | |
| 9 | useEffect(() => { |
| 10 | const el = ref.current; |
| 11 | if (!el) return; |
| 12 | |
| 13 | const observer = new IntersectionObserver( |
| 14 | ([entry]) => setInView(entry.isIntersecting), |
| 15 | { rootMargin: margin } |
| 16 | ); |
| 17 | observer.observe(el); |
| 18 | return () => observer.disconnect(); |
| 19 | }, [margin]); |
| 20 | |
| 21 | return [ref, inView]; |
| 22 | } |
no outgoing calls
no test coverage detected