mirror of
https://github.com/ershisan99/DevToysWeb.git
synced 2025-12-16 12:32:48 +00:00
24 lines
517 B
TypeScript
24 lines
517 B
TypeScript
import { DependencyList, useEffect, useRef } from "react";
|
|
|
|
export const useAutoScroll = <T extends HTMLElement = HTMLElement>(
|
|
deps: DependencyList,
|
|
behavior: ScrollBehavior = "smooth"
|
|
) => {
|
|
const ref = useRef<T>(null);
|
|
|
|
useEffect(() => {
|
|
const { current } = ref;
|
|
|
|
if (current) {
|
|
current.scrollTo({
|
|
left: current.scrollWidth,
|
|
top: current.scrollHeight,
|
|
behavior,
|
|
});
|
|
}
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, deps);
|
|
|
|
return ref;
|
|
};
|