Download client/src/hooks/useScrollToRef.ts from Zhitomir/chat: direct link, hf CLI and curl.
- Browser
- Download file 1.18 kB
-
https://huggingface.co/spaces/Zhitomir/chat/resolve/main/client/src/hooks/useScrollToRef.ts
- Command line
-
hf download hf://spaces/Zhitomir/chat/client/src/hooks/useScrollToRef.ts
-
curl -L -o useScrollToRef.ts https://huggingface.co/spaces/Zhitomir/chat/resolve/main/client/src/hooks/useScrollToRef.ts
1.18 kB
| import { RefObject, useCallback } from 'react'; | |
| import throttle from 'lodash/throttle'; | |
| type TUseScrollToRef = { | |
| targetRef: RefObject<HTMLDivElement>; | |
| callback: () => void; | |
| smoothCallback: () => void; | |
| }; | |
| export default function useScrollToRef({ targetRef, callback, smoothCallback }: TUseScrollToRef) { | |
| const logAndScroll = (behavior: 'instant' | 'smooth', callbackFn: () => void) => { | |
| // Debugging: | |
| // console.log(`Scrolling with behavior: ${behavior}, Time: ${new Date().toISOString()}`); | |
| targetRef.current?.scrollIntoView({ behavior }); | |
| callbackFn(); | |
| }; | |
| // eslint-disable-next-line react-hooks/exhaustive-deps | |
| const scrollToRef = useCallback( | |
| throttle(() => logAndScroll('instant', callback), 250, { leading: true }), | |
| [targetRef], | |
| ); | |
| // eslint-disable-next-line react-hooks/exhaustive-deps | |
| const scrollToRefSmooth = useCallback( | |
| throttle(() => logAndScroll('smooth', smoothCallback), 750, { leading: true }), | |
| [targetRef], | |
| ); | |
| const handleSmoothToRef: React.MouseEventHandler<HTMLButtonElement> = (e) => { | |
| e.preventDefault(); | |
| scrollToRefSmooth(); | |
| }; | |
| return { | |
| scrollToRef, | |
| handleSmoothToRef, | |
| }; | |
| } | |