@@ -36,52 +36,56 @@ export default function useScrollTo<T>(
36
36
if ( times < 0 || ! containerRef . current ) return ;
37
37
38
38
const height = containerRef . current . clientHeight ;
39
- const mergedAlign = targetAlign || align ;
40
-
41
- // Get top & bottom
42
- let stackTop = 0 ;
43
- let itemTop = 0 ;
44
- let itemBottom = 0 ;
45
39
let needCollectHeight = false ;
40
+ let newTargetAlign : 'top' | 'bottom' | null = targetAlign ;
46
41
47
- for ( let i = 0 ; i <= index ; i += 1 ) {
48
- const key = getKey ( data [ i ] ) ;
49
- itemTop = stackTop ;
50
- const cacheHeight = heights . get ( key ) ;
51
- itemBottom = itemTop + ( cacheHeight === undefined ? itemHeight : cacheHeight ) ;
42
+ // Go to next frame if height not exist
43
+ if ( height ) {
44
+ const mergedAlign = targetAlign || align ;
52
45
53
- stackTop = itemBottom ;
46
+ // Get top & bottom
47
+ let stackTop = 0 ;
48
+ let itemTop = 0 ;
49
+ let itemBottom = 0 ;
54
50
55
- if ( i === index && cacheHeight === undefined ) {
56
- needCollectHeight = true ;
57
- }
58
- }
51
+ for ( let i = 0 ; i <= index ; i += 1 ) {
52
+ const key = getKey ( data [ i ] ) ;
53
+ itemTop = stackTop ;
54
+ const cacheHeight = heights . get ( key ) ;
55
+ itemBottom = itemTop + ( cacheHeight === undefined ? itemHeight : cacheHeight ) ;
59
56
60
- // Scroll to
61
- let targetTop : number | null = null ;
62
- let newTargetAlign : 'top' | 'bottom' | null = targetAlign ;
57
+ stackTop = itemBottom ;
63
58
64
- switch ( mergedAlign ) {
65
- case 'top' :
66
- targetTop = itemTop ;
67
- break ;
68
- case 'bottom' :
69
- targetTop = itemBottom - height ;
70
- break ;
71
-
72
- default : {
73
- const { scrollTop } = containerRef . current ;
74
- const scrollBottom = scrollTop + height ;
75
- if ( itemTop < scrollTop ) {
76
- newTargetAlign = 'top' ;
77
- } else if ( itemBottom > scrollBottom ) {
78
- newTargetAlign = 'bottom' ;
59
+ if ( i === index && cacheHeight === undefined ) {
60
+ needCollectHeight = true ;
79
61
}
80
62
}
81
- }
82
63
83
- if ( targetTop !== null && targetTop !== containerRef . current . scrollTop ) {
84
- syncScrollTop ( targetTop ) ;
64
+ // Scroll to
65
+ let targetTop : number | null = null ;
66
+
67
+ switch ( mergedAlign ) {
68
+ case 'top' :
69
+ targetTop = itemTop ;
70
+ break ;
71
+ case 'bottom' :
72
+ targetTop = itemBottom - height ;
73
+ break ;
74
+
75
+ default : {
76
+ const { scrollTop } = containerRef . current ;
77
+ const scrollBottom = scrollTop + height ;
78
+ if ( itemTop < scrollTop ) {
79
+ newTargetAlign = 'top' ;
80
+ } else if ( itemBottom > scrollBottom ) {
81
+ newTargetAlign = 'bottom' ;
82
+ }
83
+ }
84
+ }
85
+
86
+ if ( targetTop !== null && targetTop !== containerRef . current . scrollTop ) {
87
+ syncScrollTop ( targetTop ) ;
88
+ }
85
89
}
86
90
87
91
// We will retry since element may not sync height as it described
0 commit comments