mirror of
https://github.com/MetaFam/TheGame.git
synced 2026-02-11 22:45:04 -05:00
20 lines
487 B
TypeScript
20 lines
487 B
TypeScript
import { RefObject, useEffect, useState } from 'react';
|
|
|
|
export const useOnScreen = (ref: RefObject<HTMLDivElement>): boolean => {
|
|
const [isIntersecting, setIntersecting] = useState(false);
|
|
|
|
useEffect(() => {
|
|
const observer = new IntersectionObserver(([entry]) =>
|
|
setIntersecting(entry.isIntersecting),
|
|
);
|
|
if (ref.current) {
|
|
observer.observe(ref.current);
|
|
}
|
|
return () => {
|
|
observer.disconnect();
|
|
};
|
|
}, [ref]);
|
|
|
|
return isIntersecting;
|
|
};
|