Skip to content

Commit 092824a

Browse files
authored
fix: Skip scroll when container not ready (#48)
1 parent 01ec55e commit 092824a

File tree

1 file changed

+41
-37
lines changed

1 file changed

+41
-37
lines changed

src/hooks/useScrollTo.tsx

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -36,52 +36,56 @@ export default function useScrollTo<T>(
3636
if (times < 0 || !containerRef.current) return;
3737

3838
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;
4539
let needCollectHeight = false;
40+
let newTargetAlign: 'top' | 'bottom' | null = targetAlign;
4641

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;
5245

53-
stackTop = itemBottom;
46+
// Get top & bottom
47+
let stackTop = 0;
48+
let itemTop = 0;
49+
let itemBottom = 0;
5450

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);
5956

60-
// Scroll to
61-
let targetTop: number | null = null;
62-
let newTargetAlign: 'top' | 'bottom' | null = targetAlign;
57+
stackTop = itemBottom;
6358

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;
7961
}
8062
}
81-
}
8263

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+
}
8589
}
8690

8791
// We will retry since element may not sync height as it described

0 commit comments

Comments
 (0)