1
1
import { useEvent } from 'rc-util' ;
2
2
import raf from 'rc-util/lib/raf' ;
3
+ import isVisible from 'rc-util/lib/Dom/isVisible' ;
3
4
import * as React from 'react' ;
4
5
5
6
const SPEED_PTG = 1 / 3 ;
@@ -20,9 +21,12 @@ export default function useScrollTo(
20
21
scrollingRef . current = false ;
21
22
} ;
22
23
24
+ const scrollRafTimesRef = React . useRef < number > ( ) ;
25
+
23
26
const startScroll = ( ) => {
24
27
const ul = ulRef . current ;
25
28
scrollDistRef . current = null ;
29
+ scrollRafTimesRef . current = 0 ;
26
30
27
31
if ( ul ) {
28
32
const targetLi = ul . querySelector < HTMLLIElement > ( `[data-value="${ value } "]` ) ;
@@ -31,16 +35,19 @@ export default function useScrollTo(
31
35
const doScroll = ( ) => {
32
36
stopScroll ( ) ;
33
37
scrollingRef . current = true ;
38
+ scrollRafTimesRef . current += 1 ;
34
39
35
40
const { scrollTop : currentTop } = ul ;
36
41
37
42
const firstLiTop = firstLi . offsetTop ;
38
43
const targetLiTop = targetLi . offsetTop ;
39
44
const targetTop = targetLiTop - firstLiTop ;
40
45
41
- // Wait for element exist
42
- if ( targetLiTop === 0 && targetLi !== firstLi ) {
43
- scrollRafRef . current = raf ( doScroll ) ;
46
+ // Wait for element exist. 5 frames is enough
47
+ if ( ( targetLiTop === 0 && targetLi !== firstLi ) || ! isVisible ( ul ) ) {
48
+ if ( scrollRafTimesRef . current <= 5 ) {
49
+ scrollRafRef . current = raf ( doScroll ) ;
50
+ }
44
51
return ;
45
52
}
46
53
0 commit comments