-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathuseScrollTo.tsx
More file actions
183 lines (155 loc) · 4.8 KB
/
useScrollTo.tsx
File metadata and controls
183 lines (155 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* eslint-disable no-param-reassign */
import * as React from 'react';
import raf from 'rc-util/lib/raf';
import type { GetKey } from '../interface';
import type CacheMap from '../utils/CacheMap';
import useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect';
import { warning } from 'rc-util';
const MAX_TIMES = 10;
export type ScrollAlign = 'top' | 'bottom' | 'auto';
export type ScrollPos = {
left?: number;
top?: number;
};
export type ScrollTarget =
| {
index: number;
align?: ScrollAlign;
offset?: number;
}
| {
key: React.Key;
align?: ScrollAlign;
offset?: number;
};
export default function useScrollTo<T>(
containerRef: React.RefObject<HTMLDivElement>,
data: T[],
heights: CacheMap,
itemHeight: number,
getKey: GetKey<T>,
collectHeight: () => void,
syncScrollTop: (newTop: number) => void,
triggerFlash: () => void,
): (arg: number | ScrollTarget) => void {
const scrollRef = React.useRef<number>();
const [syncState, setSyncState] = React.useState<{
times: number;
index: number;
offset: number;
originAlign: ScrollAlign;
targetAlign?: 'top' | 'bottom';
lastTop?: number;
}>(null);
// ========================== Sync Scroll ==========================
useLayoutEffect(() => {
if (syncState && syncState.times < MAX_TIMES) {
// Never reach
if (!containerRef.current) {
setSyncState((ori) => ({ ...ori }));
return;
}
collectHeight();
const { targetAlign, originAlign, index, offset } = syncState;
const height = containerRef.current.clientHeight;
let needCollectHeight = false;
let newTargetAlign: 'top' | 'bottom' | null = targetAlign;
let targetTop: number | null = null;
// Go to next frame if height not exist
if (height) {
const mergedAlign = targetAlign || originAlign;
// Get top & bottom
let itemTop = 0;
let itemBottom = 0;
const maxLen = Math.min(data.length - 1, index);
for (let i = 0; i <= maxLen; i += 1) {
const key = getKey(data[i]);
itemTop = itemBottom;
const cacheHeight = heights.get(key);
itemBottom = itemTop + (cacheHeight === undefined ? itemHeight : cacheHeight);
}
// Check if need sync height (visible range has item not record height)
let leftHeight = mergedAlign === 'top' ? offset : height - offset;
for (let i = maxLen; i >= 0; i -= 1) {
const key = getKey(data[i]);
const cacheHeight = heights.get(key);
if (cacheHeight === undefined) {
needCollectHeight = true;
break;
}
leftHeight -= cacheHeight;
if (leftHeight <= 0) {
break;
}
}
// Scroll to
switch (mergedAlign) {
case 'top':
targetTop = itemTop - offset;
break;
case 'bottom':
targetTop = itemBottom - height + offset;
break;
default: {
const { scrollTop } = containerRef.current;
const scrollBottom = scrollTop + height;
if (itemTop < scrollTop) {
newTargetAlign = 'top';
} else if (itemBottom > scrollBottom) {
newTargetAlign = 'bottom';
}
}
}
if (targetTop !== null) {
syncScrollTop(targetTop);
}
// One more time for sync
if (targetTop !== syncState.lastTop) {
needCollectHeight = true;
}
}
// Trigger next effect
if (needCollectHeight) {
setSyncState({
...syncState,
times: syncState.times + 1,
targetAlign: newTargetAlign,
lastTop: targetTop,
});
}
} else if (process.env.NODE_ENV !== 'production' && syncState?.times === MAX_TIMES) {
warning(
false,
'Seems `scrollTo` with `rc-virtual-list` reach the max limitation. Please fire issue for us. Thanks.',
);
}
}, [syncState, containerRef.current]);
// =========================== Scroll To ===========================
return (arg) => {
// When not argument provided, we think dev may want to show the scrollbar
if (arg === null || arg === undefined) {
triggerFlash();
return;
}
// Normal scroll logic
raf.cancel(scrollRef.current);
if (typeof arg === 'number') {
syncScrollTop(arg);
} else if (arg && typeof arg === 'object') {
let index: number;
const { align } = arg;
if ('index' in arg) {
({ index } = arg);
} else {
index = data.findIndex((item) => getKey(item) === arg.key);
}
const { offset = 0 } = arg;
setSyncState({
times: 0,
index,
offset,
originAlign: align,
});
}
};
}