25 lines
544 B
TypeScript
25 lines
544 B
TypeScript
import { useEffect, useState } from "react";
|
|
|
|
/**
|
|
* Checks if the element is visible
|
|
*
|
|
* @param ref the ref of the element
|
|
*/
|
|
export function useIsVisible(ref: any) {
|
|
const [isIntersecting, setIntersecting] = useState(false);
|
|
|
|
useEffect(() => {
|
|
if (ref.current == null) {
|
|
return () => {};
|
|
}
|
|
const observer = new IntersectionObserver(([entry]) => setIntersecting(entry.isIntersecting));
|
|
|
|
observer.observe(ref.current);
|
|
return () => {
|
|
observer.disconnect();
|
|
};
|
|
}, [ref]);
|
|
|
|
return isIntersecting;
|
|
}
|