@fluentui/react-virtualizer 9.0.0-alpha.11 → 9.0.0-alpha.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.swcrc +30 -0
- package/CHANGELOG.json +49 -1
- package/CHANGELOG.md +23 -2
- package/lib/Hooks.js.map +1 -1
- package/lib/Virtualizer.js.map +1 -1
- package/lib/components/Virtualizer/Virtualizer.js.map +1 -1
- package/lib/components/Virtualizer/Virtualizer.types.js +1 -1
- package/lib/components/Virtualizer/Virtualizer.types.js.map +1 -1
- package/lib/components/Virtualizer/index.js.map +1 -1
- package/lib/components/Virtualizer/renderVirtualizer.js +1 -9
- package/lib/components/Virtualizer/renderVirtualizer.js.map +1 -1
- package/lib/components/Virtualizer/useVirtualizer.js +3 -6
- package/lib/components/Virtualizer/useVirtualizer.js.map +1 -1
- package/lib/components/Virtualizer/useVirtualizerStyles.js.map +1 -1
- package/lib/hooks/index.js.map +1 -1
- package/lib/hooks/useIntersectionObserver.js +2 -2
- package/lib/hooks/useIntersectionObserver.js.map +1 -1
- package/lib/index.js.map +1 -1
- package/lib-commonjs/Hooks.js +5 -4
- package/lib-commonjs/Hooks.js.map +1 -1
- package/lib-commonjs/Virtualizer.js +5 -4
- package/lib-commonjs/Virtualizer.js.map +1 -1
- package/lib-commonjs/components/Virtualizer/Virtualizer.js +14 -17
- package/lib-commonjs/components/Virtualizer/Virtualizer.js.map +1 -1
- package/lib-commonjs/components/Virtualizer/Virtualizer.types.js +5 -2
- package/lib-commonjs/components/Virtualizer/Virtualizer.types.js.map +1 -1
- package/lib-commonjs/components/Virtualizer/index.js +9 -8
- package/lib-commonjs/components/Virtualizer/index.js.map +1 -1
- package/lib-commonjs/components/Virtualizer/renderVirtualizer.js +13 -21
- package/lib-commonjs/components/Virtualizer/renderVirtualizer.js.map +1 -1
- package/lib-commonjs/components/Virtualizer/useVirtualizer.js +348 -361
- package/lib-commonjs/components/Virtualizer/useVirtualizer.js.map +1 -1
- package/lib-commonjs/components/Virtualizer/useVirtualizerStyles.js +112 -107
- package/lib-commonjs/components/Virtualizer/useVirtualizerStyles.js.map +1 -1
- package/lib-commonjs/hooks/index.js +5 -4
- package/lib-commonjs/hooks/index.js.map +1 -1
- package/lib-commonjs/hooks/useIntersectionObserver.js +49 -54
- package/lib-commonjs/hooks/useIntersectionObserver.js.map +1 -1
- package/lib-commonjs/index.js +18 -40
- package/lib-commonjs/index.js.map +1 -1
- package/package.json +6 -5
|
@@ -1,370 +1,357 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
2
|
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "useVirtualizer_unstable", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: ()=>useVirtualizer_unstable
|
|
5
8
|
});
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const react_dom_1 = /*#__PURE__*/require("react-dom");
|
|
9
|
+
const _useIntersectionObserver = require("../../hooks/useIntersectionObserver");
|
|
10
|
+
const _react = require("react");
|
|
11
|
+
const _reactUtilities = require("@fluentui/react-utilities");
|
|
12
|
+
const _reactDom = require("react-dom");
|
|
11
13
|
function useVirtualizer_unstable(props) {
|
|
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
|
-
|
|
14
|
+
const { itemSize , numItems , virtualizerLength , children: renderChild , getItemSize , bufferItems =Math.round(virtualizerLength / 4.0) , bufferSize =Math.floor(bufferItems / 2.0) * itemSize , intersectionObserverRoot , axis ='vertical' , reversed =false , onUpdateIndex , onCalculateIndex } = props;
|
|
15
|
+
// Tracks the initial item to start virtualizer at, -1 implies first render cycle
|
|
16
|
+
const [virtualizerStartIndex, setVirtualizerStartIndex] = (0, _react.useState)(-1);
|
|
17
|
+
// Store ref to before padding element
|
|
18
|
+
const beforeElementRef = (0, _react.useRef)(null);
|
|
19
|
+
// Store ref to before padding element
|
|
20
|
+
const afterElementRef = (0, _react.useRef)(null);
|
|
21
|
+
// We need to store an array to track dynamic sizes, we can use this to incrementally update changes
|
|
22
|
+
const childSizes = (0, _react.useRef)(new Array(getItemSize ? numItems : 0));
|
|
23
|
+
/* We keep track of the progressive sizing/placement down the list,
|
|
24
|
+
this helps us skip re-calculations unless children/size changes */ const childProgressiveSizes = (0, _react.useRef)(new Array(getItemSize ? numItems : 0));
|
|
25
|
+
// The internal tracking REF for child array (updates often).
|
|
26
|
+
const childArray = (0, _react.useRef)(new Array(virtualizerLength));
|
|
27
|
+
// We want to be methodical about updating the render with child reference array
|
|
28
|
+
const forceUpdate = (0, _react.useReducer)(()=>({}), {})[1];
|
|
29
|
+
const horizontal = axis === 'horizontal';
|
|
30
|
+
const populateSizeArrays = ()=>{
|
|
31
|
+
if (!getItemSize) {
|
|
32
|
+
// Static sizes, never mind!
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (numItems !== childSizes.current.length) {
|
|
36
|
+
childSizes.current = new Array(numItems);
|
|
37
|
+
}
|
|
38
|
+
if (numItems !== childProgressiveSizes.current.length) {
|
|
39
|
+
childProgressiveSizes.current = new Array(numItems);
|
|
40
|
+
}
|
|
41
|
+
for(let index = 0; index < numItems; index++){
|
|
42
|
+
childSizes.current[index] = getItemSize(index);
|
|
43
|
+
if (index === 0) {
|
|
44
|
+
childProgressiveSizes.current[index] = childSizes.current[index];
|
|
45
|
+
} else {
|
|
46
|
+
childProgressiveSizes.current[index] = childProgressiveSizes.current[index - 1] + childSizes.current[index];
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
const batchUpdateNewIndex = (index)=>{
|
|
51
|
+
// Local updates
|
|
52
|
+
onUpdateIndex === null || onUpdateIndex === void 0 ? void 0 : onUpdateIndex(index, virtualizerStartIndex);
|
|
53
|
+
updateChildRows(index);
|
|
54
|
+
updateCurrentItemSizes(index);
|
|
55
|
+
// State setters
|
|
56
|
+
setVirtualizerStartIndex(index);
|
|
57
|
+
};
|
|
58
|
+
// Observe intersections of virtualized components
|
|
59
|
+
const { setObserverList } = (0, _useIntersectionObserver.useIntersectionObserver)((entries, observer)=>{
|
|
60
|
+
/* Sanity check - do we even need virtualization? */ if (virtualizerLength > numItems) {
|
|
61
|
+
if (virtualizerStartIndex !== 0) {
|
|
62
|
+
batchUpdateNewIndex(0);
|
|
63
|
+
}
|
|
64
|
+
// No-op
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
/* IO initiates this function when needed (bookend entering view) */ let measurementPos = 0;
|
|
68
|
+
let bufferCount = bufferItems;
|
|
69
|
+
// Grab latest entry that is intersecting
|
|
70
|
+
const latestEntry = entries.length === 1 ? entries[0] : entries.sort((entry1, entry2)=>entry2.time - entry1.time).find((entry)=>{
|
|
71
|
+
return entry.intersectionRatio > 0;
|
|
72
|
+
});
|
|
73
|
+
if (!latestEntry) {
|
|
74
|
+
// If we don't find an intersecting area, ignore for now.
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (latestEntry.target === afterElementRef.current) {
|
|
78
|
+
// We need to inverse the buffer count
|
|
79
|
+
bufferCount = virtualizerLength - bufferItems;
|
|
80
|
+
measurementPos = reversed ? calculateAfter() : calculateTotalSize() - calculateAfter();
|
|
81
|
+
if (!horizontal) {
|
|
82
|
+
if (reversed) {
|
|
83
|
+
// Scrolling 'up' and hit the after element below
|
|
84
|
+
measurementPos -= Math.abs(latestEntry.boundingClientRect.bottom);
|
|
85
|
+
} else if (latestEntry.boundingClientRect.top < 0) {
|
|
86
|
+
// Scrolling 'down' and hit the after element above top: 0
|
|
87
|
+
measurementPos -= latestEntry.boundingClientRect.top;
|
|
88
|
+
}
|
|
89
|
+
} else {
|
|
90
|
+
if (reversed) {
|
|
91
|
+
// Scrolling 'left' and hit the after element
|
|
92
|
+
measurementPos -= Math.abs(latestEntry.boundingClientRect.right);
|
|
93
|
+
} else if (latestEntry.boundingClientRect.left < 0) {
|
|
94
|
+
// Scrolling 'right' and hit the after element
|
|
95
|
+
measurementPos -= latestEntry.boundingClientRect.left;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
} else if (latestEntry.target === beforeElementRef.current) {
|
|
99
|
+
measurementPos = reversed ? calculateTotalSize() - calculateBefore() : calculateBefore();
|
|
100
|
+
if (!horizontal) {
|
|
101
|
+
if (!reversed) {
|
|
102
|
+
measurementPos -= Math.abs(latestEntry.boundingClientRect.bottom);
|
|
103
|
+
} else if (latestEntry.boundingClientRect.top < 0) {
|
|
104
|
+
// Scrolling 'down' in reverse order and hit the before element above top: 0
|
|
105
|
+
measurementPos -= latestEntry.boundingClientRect.top;
|
|
106
|
+
}
|
|
107
|
+
} else {
|
|
108
|
+
if (!reversed) {
|
|
109
|
+
measurementPos -= Math.abs(latestEntry.boundingClientRect.right);
|
|
110
|
+
} else if (latestEntry.boundingClientRect.left < 0) {
|
|
111
|
+
// Scrolling 'left' and hit before element
|
|
112
|
+
measurementPos -= latestEntry.boundingClientRect.left;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
104
115
|
}
|
|
105
|
-
} else {
|
|
106
116
|
if (reversed) {
|
|
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
|
-
const
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
didUpdate = true;
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
if (didUpdate) {
|
|
288
|
-
// Update our progressive size array
|
|
289
|
-
for (let i = startIndex; i < numItems; i++) {
|
|
290
|
-
const prevSize = i > 0 ? childProgressiveSizes.current[i - 1] : 0;
|
|
291
|
-
childProgressiveSizes.current[i] = prevSize + childSizes.current[i];
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
};
|
|
295
|
-
// Initialize the size array before first render.
|
|
296
|
-
const hasInitialized = react_1.useRef(false);
|
|
297
|
-
const initializeSizeArray = () => {
|
|
298
|
-
if (hasInitialized.current === false) {
|
|
299
|
-
hasInitialized.current = true;
|
|
300
|
-
populateSizeArrays();
|
|
301
|
-
}
|
|
302
|
-
};
|
|
303
|
-
// Initialization on mount - update array index to 0 (ready state).
|
|
304
|
-
// Only fire on mount (no deps).
|
|
305
|
-
react_1.useEffect(() => {
|
|
306
|
-
if (virtualizerStartIndex < 0) {
|
|
307
|
-
batchUpdateNewIndex(0);
|
|
308
|
-
}
|
|
117
|
+
// We're reversed, up is down, left is right, invert the scroll measure.
|
|
118
|
+
measurementPos = Math.max(calculateTotalSize() - Math.abs(measurementPos), 0);
|
|
119
|
+
}
|
|
120
|
+
// For now lets use hardcoded size to assess current element to paginate on
|
|
121
|
+
const startIndex = getIndexFromScrollPosition(measurementPos);
|
|
122
|
+
let bufferedIndex = Math.max(startIndex - bufferCount, 0);
|
|
123
|
+
if (onCalculateIndex) {
|
|
124
|
+
// User has chance to intervene/customize prior to render
|
|
125
|
+
// They may want to normalize this value.
|
|
126
|
+
bufferedIndex = onCalculateIndex(bufferedIndex);
|
|
127
|
+
}
|
|
128
|
+
// Safety limits
|
|
129
|
+
const maxIndex = Math.max(numItems - virtualizerLength, 0);
|
|
130
|
+
const newStartIndex = Math.min(Math.max(bufferedIndex, 0), maxIndex);
|
|
131
|
+
if (virtualizerStartIndex !== newStartIndex) {
|
|
132
|
+
// We flush sync this and perform an immediate state update
|
|
133
|
+
// due to virtualizerStartIndex invalidation.
|
|
134
|
+
(0, _reactDom.flushSync)(()=>{
|
|
135
|
+
batchUpdateNewIndex(newStartIndex);
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
}, {
|
|
139
|
+
root: intersectionObserverRoot ? intersectionObserverRoot === null || intersectionObserverRoot === void 0 ? void 0 : intersectionObserverRoot.current : null,
|
|
140
|
+
rootMargin: '0px',
|
|
141
|
+
threshold: 0
|
|
142
|
+
});
|
|
143
|
+
const findIndexRecursive = (scrollPos, lowIndex, highIndex)=>{
|
|
144
|
+
if (lowIndex > highIndex) {
|
|
145
|
+
// We shouldn't get here - but no-op the index if we do.
|
|
146
|
+
return virtualizerStartIndex;
|
|
147
|
+
}
|
|
148
|
+
const midpoint = Math.floor((lowIndex + highIndex) / 2);
|
|
149
|
+
const iBefore = Math.max(midpoint - 1, 0);
|
|
150
|
+
const iAfter = Math.min(midpoint + 1, childProgressiveSizes.current.length - 1);
|
|
151
|
+
const indexValue = childProgressiveSizes.current[midpoint];
|
|
152
|
+
const afterIndexValue = childProgressiveSizes.current[iAfter];
|
|
153
|
+
const beforeIndexValue = childProgressiveSizes.current[iBefore];
|
|
154
|
+
if (scrollPos <= afterIndexValue && scrollPos >= beforeIndexValue) {
|
|
155
|
+
/* We've found our index - if we are exactly matching before/after index that's ok,
|
|
156
|
+
better to reduce checks if it's right on the boundary. */ return midpoint;
|
|
157
|
+
}
|
|
158
|
+
if (indexValue > scrollPos) {
|
|
159
|
+
return findIndexRecursive(scrollPos, lowIndex, midpoint - 1);
|
|
160
|
+
} else {
|
|
161
|
+
return findIndexRecursive(scrollPos, midpoint + 1, highIndex);
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
const getIndexFromSizeArray = (scrollPos)=>{
|
|
165
|
+
/* Quick searches our progressive height array */ if (scrollPos === 0 || childProgressiveSizes.current.length === 0 || scrollPos <= childProgressiveSizes.current[0]) {
|
|
166
|
+
// Check start
|
|
167
|
+
return 0;
|
|
168
|
+
}
|
|
169
|
+
if (scrollPos >= childProgressiveSizes.current[childProgressiveSizes.current.length - 1]) {
|
|
170
|
+
// Check end
|
|
171
|
+
return childProgressiveSizes.current.length - 1;
|
|
172
|
+
}
|
|
173
|
+
return findIndexRecursive(scrollPos, 0, childProgressiveSizes.current.length - 1);
|
|
174
|
+
};
|
|
175
|
+
const getIndexFromScrollPosition = (scrollPos)=>{
|
|
176
|
+
if (!getItemSize) {
|
|
177
|
+
return Math.round(scrollPos / itemSize);
|
|
178
|
+
}
|
|
179
|
+
return getIndexFromSizeArray(scrollPos);
|
|
180
|
+
};
|
|
181
|
+
const calculateTotalSize = ()=>{
|
|
182
|
+
if (!getItemSize) {
|
|
183
|
+
return itemSize * numItems;
|
|
184
|
+
}
|
|
185
|
+
// Time for custom size calcs
|
|
186
|
+
return childProgressiveSizes.current[numItems - 1];
|
|
187
|
+
};
|
|
188
|
+
const calculateBefore = ()=>{
|
|
189
|
+
if (!getItemSize) {
|
|
190
|
+
// The missing items from before virtualization starts height
|
|
191
|
+
return virtualizerStartIndex * itemSize;
|
|
192
|
+
}
|
|
193
|
+
if (virtualizerStartIndex <= 0) {
|
|
194
|
+
return 0;
|
|
195
|
+
}
|
|
196
|
+
// Time for custom size calcs
|
|
197
|
+
return childProgressiveSizes.current[virtualizerStartIndex - 1];
|
|
198
|
+
};
|
|
199
|
+
const calculateAfter = ()=>{
|
|
200
|
+
if (numItems === 0) {
|
|
201
|
+
return 0;
|
|
202
|
+
}
|
|
203
|
+
const lastItemIndex = Math.min(virtualizerStartIndex + virtualizerLength, numItems - 1);
|
|
204
|
+
if (!getItemSize) {
|
|
205
|
+
// The missing items from after virtualization ends height
|
|
206
|
+
const remainingItems = numItems - lastItemIndex - 1;
|
|
207
|
+
return remainingItems * itemSize;
|
|
208
|
+
}
|
|
209
|
+
// Time for custom size calcs
|
|
210
|
+
return childProgressiveSizes.current[numItems - 1] - childProgressiveSizes.current[lastItemIndex];
|
|
211
|
+
};
|
|
212
|
+
const updateChildRows = (newIndex)=>{
|
|
213
|
+
if (numItems === 0) {
|
|
214
|
+
/* Nothing to virtualize */ return [];
|
|
215
|
+
}
|
|
216
|
+
if (childArray.current.length !== numItems) {
|
|
217
|
+
childArray.current = new Array(virtualizerLength);
|
|
218
|
+
}
|
|
219
|
+
const actualIndex = Math.max(newIndex, 0);
|
|
220
|
+
const end = Math.min(actualIndex + virtualizerLength, numItems);
|
|
221
|
+
for(let i = actualIndex; i < end; i++){
|
|
222
|
+
childArray.current[i - actualIndex] = renderChild(i);
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
const setBeforeRef = (0, _react.useCallback)((element)=>{
|
|
226
|
+
if (!element || beforeElementRef.current === element) {
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
beforeElementRef.current = element;
|
|
230
|
+
const newList = [];
|
|
231
|
+
newList.push(beforeElementRef.current);
|
|
232
|
+
if (afterElementRef.current) {
|
|
233
|
+
newList.push(afterElementRef.current);
|
|
234
|
+
}
|
|
235
|
+
// Ensure we update array if before element changed
|
|
236
|
+
setObserverList(newList);
|
|
237
|
+
}, [
|
|
238
|
+
setObserverList
|
|
239
|
+
]);
|
|
240
|
+
const setAfterRef = (0, _react.useCallback)((element)=>{
|
|
241
|
+
if (!element || afterElementRef.current === element) {
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
afterElementRef.current = element;
|
|
245
|
+
const newList = [];
|
|
246
|
+
if (beforeElementRef.current) {
|
|
247
|
+
newList.push(beforeElementRef.current);
|
|
248
|
+
}
|
|
249
|
+
newList.push(afterElementRef.current);
|
|
250
|
+
// Ensure we update array if after element changed
|
|
251
|
+
setObserverList(newList);
|
|
252
|
+
}, [
|
|
253
|
+
setObserverList
|
|
254
|
+
]);
|
|
255
|
+
const updateCurrentItemSizes = (newIndex)=>{
|
|
256
|
+
if (!getItemSize) {
|
|
257
|
+
// Static sizes, not required.
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
// We should always call our size function on index change (only for the items that will be rendered)
|
|
261
|
+
// This ensures we request the latest data for incoming items in case sizing has changed.
|
|
262
|
+
const endIndex = Math.min(newIndex + virtualizerLength, numItems);
|
|
263
|
+
const startIndex = Math.max(newIndex, 0);
|
|
264
|
+
let didUpdate = false;
|
|
265
|
+
for(let i = startIndex; i < endIndex; i++){
|
|
266
|
+
const newSize = getItemSize(i);
|
|
267
|
+
if (newSize !== childSizes.current[i]) {
|
|
268
|
+
childSizes.current[i] = newSize;
|
|
269
|
+
didUpdate = true;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
if (didUpdate) {
|
|
273
|
+
// Update our progressive size array
|
|
274
|
+
for(let i = startIndex; i < numItems; i++){
|
|
275
|
+
const prevSize = i > 0 ? childProgressiveSizes.current[i - 1] : 0;
|
|
276
|
+
childProgressiveSizes.current[i] = prevSize + childSizes.current[i];
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
// Initialize the size array before first render.
|
|
281
|
+
const hasInitialized = (0, _react.useRef)(false);
|
|
282
|
+
const initializeSizeArray = ()=>{
|
|
283
|
+
if (hasInitialized.current === false) {
|
|
284
|
+
hasInitialized.current = true;
|
|
285
|
+
populateSizeArrays();
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
// Initialization on mount - update array index to 0 (ready state).
|
|
289
|
+
// Only fire on mount (no deps).
|
|
290
|
+
(0, _react.useEffect)(()=>{
|
|
291
|
+
if (virtualizerStartIndex < 0) {
|
|
292
|
+
batchUpdateNewIndex(0);
|
|
293
|
+
}
|
|
309
294
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
295
|
+
}, []);
|
|
296
|
+
// If the user passes in an updated renderChild function - update current children
|
|
297
|
+
(0, _react.useEffect)(()=>{
|
|
298
|
+
if (virtualizerStartIndex >= 0) {
|
|
299
|
+
updateChildRows(virtualizerStartIndex);
|
|
300
|
+
forceUpdate();
|
|
301
|
+
}
|
|
317
302
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
303
|
+
}, [
|
|
304
|
+
renderChild
|
|
305
|
+
]);
|
|
306
|
+
// Ensure we have run through and updated the whole size list array at least once.
|
|
307
|
+
initializeSizeArray();
|
|
308
|
+
if (getItemSize && (numItems !== childSizes.current.length || numItems !== childProgressiveSizes.current.length)) {
|
|
309
|
+
// Child length mismatch, repopulate size arrays.
|
|
310
|
+
populateSizeArrays();
|
|
311
|
+
}
|
|
312
|
+
const isFullyInitialized = hasInitialized.current && virtualizerStartIndex >= 0;
|
|
313
|
+
return {
|
|
314
|
+
components: {
|
|
315
|
+
before: 'div',
|
|
316
|
+
after: 'div',
|
|
317
|
+
beforeContainer: 'div',
|
|
318
|
+
afterContainer: 'div'
|
|
319
|
+
},
|
|
320
|
+
virtualizedChildren: childArray.current,
|
|
321
|
+
before: (0, _reactUtilities.resolveShorthand)(props.before, {
|
|
322
|
+
required: true,
|
|
323
|
+
defaultProps: {
|
|
324
|
+
ref: setBeforeRef,
|
|
325
|
+
role: 'none'
|
|
326
|
+
}
|
|
327
|
+
}),
|
|
328
|
+
after: (0, _reactUtilities.resolveShorthand)(props.after, {
|
|
329
|
+
required: true,
|
|
330
|
+
defaultProps: {
|
|
331
|
+
ref: setAfterRef,
|
|
332
|
+
role: 'none'
|
|
333
|
+
}
|
|
334
|
+
}),
|
|
335
|
+
beforeContainer: (0, _reactUtilities.resolveShorthand)(props.beforeContainer, {
|
|
336
|
+
required: true,
|
|
337
|
+
defaultProps: {
|
|
338
|
+
role: 'none'
|
|
339
|
+
}
|
|
340
|
+
}),
|
|
341
|
+
afterContainer: (0, _reactUtilities.resolveShorthand)(props.afterContainer, {
|
|
342
|
+
required: true,
|
|
343
|
+
defaultProps: {
|
|
344
|
+
role: 'none'
|
|
345
|
+
}
|
|
346
|
+
}),
|
|
347
|
+
beforeBufferHeight: isFullyInitialized ? calculateBefore() : 0,
|
|
348
|
+
afterBufferHeight: isFullyInitialized ? calculateAfter() : 0,
|
|
349
|
+
totalVirtualizerHeight: isFullyInitialized ? calculateTotalSize() : virtualizerLength * itemSize,
|
|
350
|
+
virtualizerStartIndex,
|
|
351
|
+
axis,
|
|
352
|
+
bufferSize,
|
|
353
|
+
reversed
|
|
354
|
+
};
|
|
355
|
+
} //# sourceMappingURL=useVirtualizer.js.map
|
|
356
|
+
|
|
370
357
|
//# sourceMappingURL=useVirtualizer.js.map
|