ai-learning / lib /hooks /useInView.ts
samuellimabraz's picture
feat: initial app
7198b5e unverified
Raw
History Blame Contribute Delete
520 Bytes
"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 };
}