Spaces:
Sleeping
Sleeping
| "use client"; | |
| import { useEffect, useRef, useState } from "react"; | |
| export function useInView<T extends Element>(threshold = 0.4) { | |
| const ref = useRef<T | null>(null); | |
| const [inView, setInView] = useState(false); | |
| useEffect(() => { | |
| const node = ref.current; | |
| if (!node) return; | |
| const obs = new IntersectionObserver( | |
| ([entry]) => setInView(entry.isIntersecting), | |
| { threshold }, | |
| ); | |
| obs.observe(node); | |
| return () => obs.disconnect(); | |
| }, [threshold]); | |
| return { ref, inView }; | |
| } | |