Spaces:
Running
Running
File size: 788 Bytes
5012205 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import { useEffect, useRef, type RefObject } from 'react';
export function useScrollToBottom(): [
RefObject<HTMLDivElement>,
RefObject<HTMLDivElement>,
] {
const containerRef = useRef<HTMLDivElement>(null);
const endRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const container = containerRef.current;
const end = endRef.current;
if (container && end) {
const observer = new MutationObserver(() => {
end.scrollIntoView({ behavior: 'instant', block: 'end' });
});
observer.observe(container, {
childList: true,
subtree: true,
attributes: true,
characterData: true,
});
return () => observer.disconnect();
}
}, []);
// @ts-expect-error error
return [containerRef, endRef];
} |