2024-07-01 15:41:21 +07:00
|
|
|
// useIntersectionObserver.js
|
|
|
|
|
import { useEffect, useState, useRef } from "react";
|
|
|
|
|
|
|
|
|
|
export function useIntersectionObserver() {
|
|
|
|
|
const [isIntersecting, setIsIntersecting] = useState(false);
|
|
|
|
|
const ref = useRef(null);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const observer = new IntersectionObserver(
|
|
|
|
|
([entry]) => {
|
|
|
|
|
setIsIntersecting(entry.isIntersecting);
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
root: null,
|
|
|
|
|
rootMargin: "0px",
|
|
|
|
|
threshold: 0.1,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2025-03-18 12:08:42 +07:00
|
|
|
const currentRef = ref.current; // Store the ref value in a variable
|
|
|
|
|
|
|
|
|
|
if (currentRef) {
|
|
|
|
|
observer.observe(currentRef);
|
2024-07-01 15:41:21 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return () => {
|
2025-03-18 12:08:42 +07:00
|
|
|
if (currentRef) {
|
|
|
|
|
observer.unobserve(currentRef);
|
2024-07-01 15:41:21 +07:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
return [ref, isIntersecting];
|
|
|
|
|
}
|