This repository has been archived on 2024-10-29. You can view files and clone it, but cannot push or open issues or pull requests.
Files
scoresaber-reloadedv3/src/hooks/use-is-visible.ts

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;
}