Refactor to use hooks (#436)
This commit is contained in:
parent
124efca9a1
commit
cb60f8b143
139 changed files with 8871 additions and 8726 deletions
29
components/custom/use-scroll-to-bottom.ts
Normal file
29
components/custom/use-scroll-to-bottom.ts
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import { useEffect, useRef, RefObject } from "react";
|
||||
|
||||
export function useScrollToBottom<T extends HTMLElement>(): [
|
||||
RefObject<T>,
|
||||
RefObject<T>,
|
||||
] {
|
||||
const containerRef = useRef<T>(null);
|
||||
const endRef = useRef<T>(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,
|
||||
});
|
||||
|
||||
return () => observer.disconnect();
|
||||
}
|
||||
}, []);
|
||||
|
||||
return [containerRef, endRef];
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue