mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-20 19:46:32 +08:00
32 lines
736 B
TypeScript
32 lines
736 B
TypeScript
import type { RefObject } from 'react';
|
|
import { useEffect, useState } from 'react';
|
|
|
|
export function useHasScrollTop(ref: RefObject<HTMLElement> | null) {
|
|
const [hasScrollTop, setHasScrollTop] = useState(false);
|
|
|
|
useEffect(() => {
|
|
if (!ref?.current) {
|
|
return;
|
|
}
|
|
|
|
const container = ref.current;
|
|
|
|
function updateScrollTop() {
|
|
if (container) {
|
|
setTimeout(() => {
|
|
const hasScrollTop = container.scrollTop > 0;
|
|
setHasScrollTop(hasScrollTop);
|
|
});
|
|
}
|
|
}
|
|
|
|
container.addEventListener('scroll', updateScrollTop);
|
|
updateScrollTop();
|
|
return () => {
|
|
container.removeEventListener('scroll', updateScrollTop);
|
|
};
|
|
}, [ref]);
|
|
|
|
return hasScrollTop;
|
|
}
|