Spaces:
Configuration error
Configuration error
Download frontend/src/hooks/use-scroll-to-bottom.ts from flyingdarkdev/README: direct link, hf CLI and curl.
- Browser
- Download file 984 Bytes
-
https://huggingface.co/spaces/flyingdarkdev/README/resolve/main/frontend/src/hooks/use-scroll-to-bottom.ts
- Command line
-
hf download hf://spaces/flyingdarkdev/README/frontend/src/hooks/use-scroll-to-bottom.ts
-
curl -L -o use-scroll-to-bottom.ts https://huggingface.co/spaces/flyingdarkdev/README/resolve/main/frontend/src/hooks/use-scroll-to-bottom.ts
984 Bytes
| import { RefObject, useEffect, useState } from "react"; | |
| export function useScrollToBottom(scrollRef: RefObject<HTMLDivElement | null>) { | |
| // for auto-scroll | |
| const [autoScroll, setAutoScroll] = useState(true); | |
| const [hitBottom, setHitBottom] = useState(true); | |
| const onChatBodyScroll = (e: HTMLElement) => { | |
| const bottomHeight = e.scrollTop + e.clientHeight; | |
| const isHitBottom = bottomHeight >= e.scrollHeight - 10; | |
| setHitBottom(isHitBottom); | |
| setAutoScroll(isHitBottom); | |
| }; | |
| function scrollDomToBottom() { | |
| const dom = scrollRef.current; | |
| if (dom) { | |
| requestAnimationFrame(() => { | |
| setAutoScroll(true); | |
| dom.scrollTo({ top: dom.scrollHeight, behavior: "auto" }); | |
| }); | |
| } | |
| } | |
| // auto scroll | |
| useEffect(() => { | |
| if (autoScroll) { | |
| scrollDomToBottom(); | |
| } | |
| }); | |
| return { | |
| scrollRef, | |
| autoScroll, | |
| setAutoScroll, | |
| scrollDomToBottom, | |
| hitBottom, | |
| setHitBottom, | |
| onChatBodyScroll, | |
| }; | |
| } | |