1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-27 10:32:58 +02:00
joplin/packages/pdf-viewer/hooks/useIsVisible.ts
2022-08-27 13:32:20 +01:00

38 lines
884 B
TypeScript

import { useEffect, useState, MutableRefObject } from 'react';
const useIsVisible = (elementRef: MutableRefObject<HTMLElement>, rootRef: MutableRefObject<HTMLElement>) => {
const [isVisible, setIsVisible] = useState(false);
useEffect(() => {
let observer: IntersectionObserver = null;
if (elementRef.current) {
observer = new IntersectionObserver((entries, _observer) => {
let visible = false;
entries.forEach((entry) => {
if (entry.isIntersecting) {
visible = true;
setIsVisible(true);
}
});
if (!visible) {
setIsVisible(false);
}
}, {
root: rootRef.current,
rootMargin: '0px 0px 0px 0px',
threshold: 0,
});
observer.observe(elementRef.current);
}
return () => {
if (observer) {
observer.disconnect();
}
};
}, [elementRef, rootRef]);
return isVisible;
};
export default useIsVisible;