bigbuffet-rw/app/soapbox/hooks/useOnScreen.ts
marcin mikołajczak 785c6b57e3 RTL support, use Stack and HStack in more places
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
2022-11-25 18:04:11 +01:00

21 lines
477 B
TypeScript

import React from 'react';
export const useOnScreen = (ref: React.MutableRefObject<HTMLElement>) => {
const [isIntersecting, setIntersecting] = React.useState(false);
const observer = new IntersectionObserver(
([entry]) => setIntersecting(entry.isIntersecting),
);
React.useEffect(() => {
if (ref.current) {
observer.observe(ref.current);
}
return () => {
observer.disconnect();
};
}, [ref.current]);
return isIntersecting;
};