mirror of
https://github.com/stefanpejcic/openpanel
synced 2025-06-26 18:28:26 +00:00
34 lines
836 B
TypeScript
34 lines
836 B
TypeScript
import React from "react";
|
|
|
|
export const useInView = (
|
|
ref: React.RefObject<HTMLElement>,
|
|
rootMargin = "0px",
|
|
): boolean => {
|
|
const [isIntersecting, setIntersecting] = React.useState(false);
|
|
|
|
React.useEffect(() => {
|
|
const curr = ref?.current;
|
|
|
|
const observer = new IntersectionObserver(
|
|
([entry]) => {
|
|
// Update our state when observer callback fires
|
|
setIntersecting(entry.isIntersecting);
|
|
},
|
|
{
|
|
rootMargin,
|
|
},
|
|
);
|
|
if (curr) {
|
|
observer.observe(curr);
|
|
}
|
|
return () => {
|
|
if (curr) {
|
|
observer.unobserve(curr);
|
|
}
|
|
};
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, []);
|
|
|
|
return isIntersecting;
|
|
};
|