protofolio / frontend /src /hooks /useScrollAnimation.ts
moustafa7
Initial commit
81d1e4c
Raw History Blame Contribute Delete
594 Bytes
import { useEffect, useRef, useState } from "react";
export function useScrollAnimation(threshold = 0.1) {
const ref = useRef<HTMLDivElement>(null);
const [isVisible, setIsVisible] = useState(false);
useEffect(() => {
const observer = new IntersectionObserver(
([entry]) => {
if (entry.isIntersecting) {
setIsVisible(true);
observer.unobserve(entry.target);
}
},
{ threshold }
);
if (ref.current) observer.observe(ref.current);
return () => observer.disconnect();
}, [threshold]);
return { ref, isVisible };
}