文件
mynode/packages/pdf-viewer/hooks/useIsVisible.ts
chen 301194963b
一些检查失败了
automerge / automerge (push) Has been cancelled
react-native-android-build-apk / AssembleRelease (push) Has been cancelled
Build macOS M1 / Main (push) Has been cancelled
Joplin Continuous Integration / Main (macos-15-intel) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / Main (ubuntu-22.04-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-11-vs2026-arm) (push) Has been cancelled
Joplin Continuous Integration / Main (windows-2025) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04) (push) Has been cancelled
Joplin Continuous Integration / ServerDockerImage (ubuntu-22.04-arm) (push) Has been cancelled
Joplin UI tests / Main (ubuntu-22.04) (push) Has been cancelled
Joplin UI tests / Main (windows-2025) (push) Has been cancelled
Joplin UI tests / Mobile (push) Has been cancelled
Close stale issues / ProcessStaleIssues (push) Has been cancelled
feat: Initial clean release of CHM
2026-11-04 11:29:39 +08:00

60 行
1.5 KiB
TypeScript

import { useEffect, useState, MutableRefObject, useRef } from 'react';
const useIsVisible = (elementRef: MutableRefObject<HTMLElement>, rootRef: MutableRefObject<HTMLElement>) => {
const [isVisible, setIsVisible] = useState(false);
const lastVisible = useRef(0);
const invisibleOn = useRef(0);
useEffect(() => {
let observer: IntersectionObserver = null;
let timeout: number = null;
if (elementRef.current) {
observer = new IntersectionObserver((entries, _observer) => {
let visible = false;
// eslint-disable-next-line github/array-foreach -- Old code before rule was applied
entries.forEach((entry) => {
if (entry.isIntersecting) {
visible = true;
lastVisible.current = Date.now();
if ((invisibleOn.current - lastVisible.current) > 300) {
setIsVisible(true);
} else {
if (!timeout) {
timeout = window.setTimeout(() => {
if (invisibleOn.current < lastVisible.current) {
setIsVisible(true);
}
timeout = null;
}, 300);
}
}
}
});
if (!visible) {
invisibleOn.current = Date.now();
setIsVisible(false);
}
}, {
root: rootRef.current,
rootMargin: '0px 0px 0px 0px',
threshold: 0,
});
observer.observe(elementRef.current);
}
return () => {
if (observer) {
observer.disconnect();
}
if (timeout) {
window.clearTimeout(timeout);
timeout = null;
}
};
}, [elementRef, rootRef]);
return isVisible;
};
export default useIsVisible;