Skip to content

Commit bff7518

Browse files
committed
fix(desktop): auto-scroll
1 parent 8eab677 commit bff7518

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

packages/ui/src/hooks/create-auto-scroll.tsx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,28 @@ export function createAutoScroll(options: AutoScrollOptions) {
2222
function scrollToBottom() {
2323
if (!scrollRef || store.userScrolled || !options.working()) return
2424
setStore("autoScrolled", true)
25-
requestAnimationFrame(() => {
26-
scrollRef?.scrollTo({ top: scrollRef.scrollHeight, behavior: "smooth" })
27-
requestAnimationFrame(() => {
25+
const targetHeight = scrollRef.scrollHeight
26+
scrollRef.scrollTo({ top: targetHeight, behavior: "smooth" })
27+
28+
// Wait for scroll to complete before clearing autoScrolled
29+
const checkScrollComplete = () => {
30+
if (!scrollRef) {
31+
setStore("autoScrolled", false)
32+
return
33+
}
34+
const atBottom = scrollRef.scrollTop + scrollRef.clientHeight >= scrollRef.scrollHeight - 10
35+
const reachedTarget = scrollRef.scrollTop >= targetHeight - scrollRef.clientHeight - 10
36+
if (atBottom || reachedTarget) {
2837
batch(() => {
2938
setStore("lastScrollTop", scrollRef?.scrollTop ?? 0)
3039
setStore("lastScrollHeight", scrollRef?.scrollHeight ?? 0)
3140
setStore("autoScrolled", false)
3241
})
33-
})
34-
})
42+
} else {
43+
requestAnimationFrame(checkScrollComplete)
44+
}
45+
}
46+
requestAnimationFrame(checkScrollComplete)
3547
}
3648

3749
function handleScroll() {

0 commit comments

Comments
 (0)