2024-03-16 04:45:46 +08:00
|
|
|
import {
|
|
|
|
useCallback, useEffect, useMemo, useRef,
|
|
|
|
} from 'react';
|
|
|
|
|
|
|
|
interface Handlers {
|
|
|
|
startObserving(): void;
|
|
|
|
stopObserving(): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
const useStickyScroll = (el: HTMLElement | null) => {
|
|
|
|
const elHeight = useRef(0);
|
2024-07-03 04:11:16 +08:00
|
|
|
const timeout = useRef<number>();
|
2024-03-16 04:45:46 +08:00
|
|
|
const handlers = useRef<Handlers>({
|
|
|
|
startObserving: () => {},
|
|
|
|
stopObserving: () => {},
|
|
|
|
});
|
|
|
|
|
|
|
|
const observer = useMemo(
|
|
|
|
() => new ResizeObserver((entries) => {
|
|
|
|
entries.forEach((entry) => {
|
|
|
|
const { target } = entry;
|
|
|
|
if (target instanceof HTMLElement) {
|
|
|
|
if (target.offsetHeight > elHeight.current) {
|
|
|
|
elHeight.current = target.offsetHeight;
|
|
|
|
target.scrollTop = target.scrollHeight + target.clientHeight;
|
|
|
|
} else {
|
|
|
|
elHeight.current = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
|
|
|
|
handlers.current.startObserving = useCallback(() => {
|
|
|
|
if (!el) return;
|
|
|
|
clearTimeout(timeout.current);
|
|
|
|
observer.observe(el);
|
|
|
|
}, [el]);
|
|
|
|
|
|
|
|
handlers.current.stopObserving = useCallback(() => {
|
|
|
|
if (!el) return;
|
|
|
|
timeout.current = setTimeout(() => {
|
|
|
|
observer.unobserve(el);
|
|
|
|
}, 500);
|
|
|
|
}, [el]);
|
|
|
|
|
|
|
|
useEffect(
|
|
|
|
() => () => {
|
|
|
|
observer.disconnect();
|
|
|
|
},
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
|
|
|
|
return handlers.current;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useStickyScroll;
|