@@ -9,6 +9,7 @@ export const useAutoScroll = ( hostRef, shadowBodySizeRef, selectedTab ) => {
99 const scrollPositionRef = useRef ( 0 )
1010 const animationFrameRef = useRef ( null )
1111 const isScrollingRef = useRef ( false )
12+ const delayTimeoutRef = useRef ( null )
1213
1314 const smoothScrollToBottom = ( shadowDomBody , targetScrollTop ) => {
1415 if ( ! shadowDomBody || ! isScrollingRef . current ) {
@@ -49,7 +50,13 @@ export const useAutoScroll = ( hostRef, shadowBodySizeRef, selectedTab ) => {
4950 scrollPositionRef . current = 0
5051 isScrollingRef . current = true
5152
52- setTimeout ( ( ) => {
53+ // Clear any existing timeout
54+ if ( delayTimeoutRef . current ) {
55+ clearTimeout ( delayTimeoutRef . current )
56+ delayTimeoutRef . current = null
57+ }
58+
59+ delayTimeoutRef . current = setTimeout ( ( ) => {
5360 if ( scrollPositionRef . current === - 1 || ! isScrollingRef . current ) {
5461 return
5562 }
@@ -64,6 +71,8 @@ export const useAutoScroll = ( hostRef, shadowBodySizeRef, selectedTab ) => {
6471 if ( targetScrollTop > 0 ) {
6572 smoothScrollToBottom ( shadowDomBody , targetScrollTop )
6673 }
74+
75+ delayTimeoutRef . current = null
6776 } , 1000 )
6877 }
6978 }
@@ -78,6 +87,12 @@ export const useAutoScroll = ( hostRef, shadowBodySizeRef, selectedTab ) => {
7887 animationFrameRef . current = null
7988 }
8089
90+ // Clear any existing timeout
91+ if ( delayTimeoutRef . current ) {
92+ clearTimeout ( delayTimeoutRef . current )
93+ delayTimeoutRef . current = null
94+ }
95+
8196 shadowDomBody . scrollTo ( {
8297 top : 0 ,
8398 behavior : 'smooth' ,
@@ -93,16 +108,29 @@ export const useAutoScroll = ( hostRef, shadowBodySizeRef, selectedTab ) => {
93108 cancelAnimationFrame ( animationFrameRef . current )
94109 animationFrameRef . current = null
95110 }
111+
112+ // Clear any existing timeout
113+ if ( delayTimeoutRef . current ) {
114+ clearTimeout ( delayTimeoutRef . current )
115+ delayTimeoutRef . current = null
116+ }
96117 scrollPositionRef . current = - 1
97118 }
98119
99120 // Cleanup any pending animation on unmount.
100121 useEffect ( ( ) => {
101122 return ( ) => {
123+ isScrollingRef . current = false
102124 if ( animationFrameRef . current ) {
103125 cancelAnimationFrame ( animationFrameRef . current )
104126 animationFrameRef . current = null
105127 }
128+
129+ // Clear any existing timeout
130+ if ( delayTimeoutRef . current ) {
131+ clearTimeout ( delayTimeoutRef . current )
132+ delayTimeoutRef . current = null
133+ }
106134 scrollPositionRef . current = - 1
107135 }
108136 } , [ ] )
0 commit comments