forked from stackblitz-labs/bolt.diy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: chat autoscroll (stackblitz-labs#6)
- Loading branch information
1 parent
f4987a4
commit df25c67
Showing
5 changed files
with
71 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './useMessageParser'; | ||
export * from './usePromptEnhancer'; | ||
export * from './useSnapScroll'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { useRef, useCallback } from 'react'; | ||
|
||
export function useSnapScroll() { | ||
const autoScrollRef = useRef(true); | ||
const scrollNodeRef = useRef<HTMLDivElement>(); | ||
const onScrollRef = useRef<() => void>(); | ||
const observerRef = useRef<ResizeObserver>(); | ||
|
||
const messageRef = useCallback((node: HTMLDivElement | null) => { | ||
if (node) { | ||
const observer = new ResizeObserver(() => { | ||
if (autoScrollRef.current) { | ||
if (scrollNodeRef.current) { | ||
const { scrollHeight, clientHeight } = scrollNodeRef.current; | ||
const scrollTarget = scrollHeight - clientHeight; | ||
|
||
scrollNodeRef.current.scrollTo({ | ||
top: scrollTarget, | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
observer.observe(node); | ||
} else { | ||
observerRef.current?.disconnect(); | ||
observerRef.current = undefined; | ||
} | ||
}, []); | ||
|
||
const scrollRef = useCallback((node: HTMLDivElement | null) => { | ||
if (node) { | ||
onScrollRef.current = () => { | ||
const { scrollTop, scrollHeight, clientHeight } = node; | ||
const scrollTarget = scrollHeight - clientHeight; | ||
|
||
autoScrollRef.current = Math.abs(scrollTop - scrollTarget) <= 10; | ||
}; | ||
|
||
node.addEventListener('scroll', onScrollRef.current); | ||
|
||
scrollNodeRef.current = node; | ||
} else { | ||
if (onScrollRef.current) { | ||
scrollNodeRef.current?.removeEventListener('scroll', onScrollRef.current); | ||
} | ||
|
||
scrollNodeRef.current = undefined; | ||
onScrollRef.current = undefined; | ||
} | ||
}, []); | ||
|
||
return [messageRef, scrollRef]; | ||
} |