@@ -6,43 +6,32 @@ function easeInOutQuint(t) {
66 return t < 0.5 ? 16 * t * t * t * t * t : 1 + 16 * -- t * t * t * t * t ;
77}
88
9- const timeout = fn => setTimeout ( fn , 16 ) ;
10-
11- const raf = fn => requestAnimationFrame ( fn ) ;
12-
139export default function ( ) {
1410 const parentRef = React . useRef ( ) ;
1511 const scrollingRef = React . useRef ( ) ;
1612
17- const [ animationType , setAnimationType ] = React . useState ( "setTimeout" ) ;
18-
19- const animationFn = animationType === "setTimeout" ? timeout : raf ;
20-
21- const scrollToFn = React . useCallback (
22- offset => {
23- const duration = 1000 ;
24- const start = parentRef . current . scrollTop ;
25- const startTime = ( scrollingRef . current = Date . now ( ) ) ;
26-
27- const run = ( ) => {
28- if ( scrollingRef . current !== startTime ) return ;
29- const now = Date . now ( ) ;
30- const elapsed = now - startTime ;
31- const progress = easeInOutQuint ( Math . min ( elapsed / duration , 1 ) ) ;
32- const interpolated = start + ( offset - start ) * progress ;
33-
34- if ( elapsed < duration ) {
35- parentRef . current . scrollTop = interpolated ;
36- animationFn ( run ) ;
37- } else {
38- parentRef . current . scrollTop = interpolated ;
39- }
40- } ;
41-
42- animationFn ( run ) ;
43- } ,
44- [ animationFn ]
45- ) ;
13+ const scrollToFn = React . useCallback ( ( offset , defaultScrollTo ) => {
14+ const duration = 1000 ;
15+ const start = parentRef . current . scrollTop ;
16+ const startTime = ( scrollingRef . current = Date . now ( ) ) ;
17+
18+ const run = ( ) => {
19+ if ( scrollingRef . current !== startTime ) return ;
20+ const now = Date . now ( ) ;
21+ const elapsed = now - startTime ;
22+ const progress = easeInOutQuint ( Math . min ( elapsed / duration , 1 ) ) ;
23+ const interpolated = start + ( offset - start ) * progress ;
24+
25+ if ( elapsed < duration ) {
26+ defaultScrollTo ( interpolated ) ;
27+ requestAnimationFrame ( run ) ;
28+ } else {
29+ defaultScrollTo ( interpolated ) ;
30+ }
31+ } ;
32+
33+ requestAnimationFrame ( run ) ;
34+ } , [ ] ) ;
4635
4736 const rowVirtualizer = useVirtual ( {
4837 size : 10000 ,
@@ -59,19 +48,10 @@ export default function() {
5948 implement a custom scrolling function for the methods like{ " " }
6049 < code > `scrollToIndex`</ code > and < code > `scrollToOffset`</ code >
6150 </ p >
51+
6252 < br />
6353 < br />
6454
65- < label >
66- Animation Type:
67- < select
68- value = { animationType }
69- onChange = { e => setAnimationType ( e . target . value ) }
70- >
71- < option value = "setTimeout" > setTimeout</ option >
72- < option value = "raf" > requestAnimationFram</ option >
73- </ select >
74- </ label >
7555 < div >
7656 < button
7757 onClick = { ( ) =>
0 commit comments