mirror of
https://github.com/ershisan99/DevToysWeb.git
synced 2025-12-17 20:49:24 +00:00
refactor: clean some codes
This commit is contained in:
23
hooks/useAutoScroll.ts
Normal file
23
hooks/useAutoScroll.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
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;
|
||||
};
|
||||
Reference in New Issue
Block a user