File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ( ) {
You can’t perform that action at this time.
0 commit comments