@tanstack/react-virtual 3.0.0-alpha.0 → 3.0.0-beta.0
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/build/cjs/_virtual/_rollupPluginBabelHelpers.js +1 -18
- package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +1 -1
- package/build/cjs/react-virtual/src/index.js +90 -0
- package/build/cjs/react-virtual/src/index.js.map +1 -0
- package/build/cjs/virtual-core/build/esm/index.js +565 -0
- package/build/cjs/virtual-core/build/esm/index.js.map +1 -0
- package/build/esm/index.js +432 -350
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +24 -119
- package/build/types/react-virtual/src/index.d.ts +4 -0
- package/build/types/virtual-core/src/utils.d.ts +7 -0
- package/build/umd/index.development.js +443 -352
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +11 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +16 -17
- package/src/index.tsx +66 -386
- package/build/cjs/packages/react-virtual/src/index.js +0 -371
- package/build/cjs/packages/react-virtual/src/index.js.map +0 -1
- package/build/cjs/packages/react-virtual/src/useIsomorphicLayoutEffect.js +0 -40
- package/build/cjs/packages/react-virtual/src/useIsomorphicLayoutEffect.js.map +0 -1
- package/build/cjs/packages/react-virtual/src/useRect.js +0 -85
- package/build/cjs/packages/react-virtual/src/useRect.js.map +0 -1
- package/build/types/index.d.ts +0 -53
- package/build/types/useIsomorphicLayoutEffect.d.ts +0 -2
- package/build/types/useRect.d.ts +0 -6
- package/src/useIsomorphicLayoutEffect.ts +0 -4
- package/src/useRect.ts +0 -58
package/build/esm/index.js
CHANGED
|
@@ -9,41 +9,17 @@
|
|
|
9
9
|
* @license MIT
|
|
10
10
|
*/
|
|
11
11
|
import * as React from 'react';
|
|
12
|
-
import React__default from 'react';
|
|
13
|
-
|
|
14
|
-
function _extends() {
|
|
15
|
-
_extends = Object.assign || function (target) {
|
|
16
|
-
for (var i = 1; i < arguments.length; i++) {
|
|
17
|
-
var source = arguments[i];
|
|
18
|
-
|
|
19
|
-
for (var key in source) {
|
|
20
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
21
|
-
target[key] = source[key];
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
return target;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
return _extends.apply(this, arguments);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function _objectWithoutPropertiesLoose(source, excluded) {
|
|
33
|
-
if (source == null) return {};
|
|
34
|
-
var target = {};
|
|
35
|
-
var sourceKeys = Object.keys(source);
|
|
36
|
-
var key, i;
|
|
37
|
-
|
|
38
|
-
for (i = 0; i < sourceKeys.length; i++) {
|
|
39
|
-
key = sourceKeys[i];
|
|
40
|
-
if (excluded.indexOf(key) >= 0) continue;
|
|
41
|
-
target[key] = source[key];
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return target;
|
|
45
|
-
}
|
|
46
12
|
|
|
13
|
+
/**
|
|
14
|
+
* virtual-core
|
|
15
|
+
*
|
|
16
|
+
* Copyright (c) TanStack
|
|
17
|
+
*
|
|
18
|
+
* This source code is licensed under the MIT license found in the
|
|
19
|
+
* LICENSE.md file in the root directory of this source tree.
|
|
20
|
+
*
|
|
21
|
+
* @license MIT
|
|
22
|
+
*/
|
|
47
23
|
var props = ["bottom", "height", "left", "right", "top", "width"];
|
|
48
24
|
|
|
49
25
|
var rectChanged = function rectChanged(a, b) {
|
|
@@ -114,358 +90,425 @@ function observeRect(node, cb) {
|
|
|
114
90
|
};
|
|
115
91
|
}
|
|
116
92
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
93
|
+
function memo(getDeps, fn, opts) {
|
|
94
|
+
let deps = [];
|
|
95
|
+
let result;
|
|
96
|
+
return () => {
|
|
97
|
+
let depTime;
|
|
98
|
+
if (opts.key && opts.debug != null && opts.debug()) depTime = Date.now();
|
|
99
|
+
const newDeps = getDeps();
|
|
100
|
+
const depsChanged = newDeps.length !== deps.length || newDeps.some((dep, index) => deps[index] !== dep);
|
|
101
|
+
|
|
102
|
+
if (!depsChanged) {
|
|
103
|
+
return result;
|
|
104
|
+
}
|
|
121
105
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
106
|
+
deps = newDeps;
|
|
107
|
+
let resultTime;
|
|
108
|
+
if (opts.key && opts.debug != null && opts.debug()) resultTime = Date.now();
|
|
109
|
+
result = fn(...newDeps);
|
|
110
|
+
opts == null ? void 0 : opts.onChange == null ? void 0 : opts.onChange(result);
|
|
125
111
|
|
|
126
|
-
|
|
127
|
-
|
|
112
|
+
if (opts.key && opts.debug != null && opts.debug()) {
|
|
113
|
+
const depEndTime = Math.round((Date.now() - depTime) * 100) / 100;
|
|
114
|
+
const resultEndTime = Math.round((Date.now() - resultTime) * 100) / 100;
|
|
115
|
+
const resultFpsPercentage = resultEndTime / 16;
|
|
128
116
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
initialRect = {
|
|
132
|
-
width: 0,
|
|
133
|
-
height: 0
|
|
134
|
-
};
|
|
135
|
-
}
|
|
117
|
+
const pad = (str, num) => {
|
|
118
|
+
str = String(str);
|
|
136
119
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
120
|
+
while (str.length < num) {
|
|
121
|
+
str = ' ' + str;
|
|
122
|
+
}
|
|
140
123
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
dispatch = _React$useReducer[1];
|
|
124
|
+
return str;
|
|
125
|
+
};
|
|
144
126
|
|
|
145
|
-
|
|
146
|
-
useIsomorphicLayoutEffect(function () {
|
|
147
|
-
if (nodeRef.current !== element) {
|
|
148
|
-
setElement(nodeRef.current);
|
|
127
|
+
console.info("%c\u23F1 " + pad(resultEndTime, 5) + " /" + pad(depEndTime, 5) + " ms", "\n font-size: .6rem;\n font-weight: bold;\n color: hsl(" + Math.max(0, Math.min(120 - 120 * resultFpsPercentage, 120)) + "deg 100% 31%);", opts == null ? void 0 : opts.key);
|
|
149
128
|
}
|
|
150
|
-
});
|
|
151
|
-
useIsomorphicLayoutEffect(function () {
|
|
152
|
-
if (element && !initialRectSet.current) {
|
|
153
|
-
initialRectSet.current = true;
|
|
154
|
-
|
|
155
|
-
var _rect = element.getBoundingClientRect();
|
|
156
129
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
}, [element]);
|
|
162
|
-
React__default.useEffect(function () {
|
|
163
|
-
if (!element) {
|
|
164
|
-
return;
|
|
165
|
-
}
|
|
130
|
+
return result;
|
|
131
|
+
};
|
|
132
|
+
}
|
|
166
133
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
return function () {
|
|
174
|
-
observer.unobserve();
|
|
175
|
-
};
|
|
176
|
-
}, [element]);
|
|
177
|
-
return rect;
|
|
178
|
-
};
|
|
134
|
+
//
|
|
135
|
+
const defaultKeyExtractor = index => index;
|
|
136
|
+
const defaultRangeExtractor = range => {
|
|
137
|
+
const start = Math.max(range.startIndex - range.overscan, 0);
|
|
138
|
+
const end = Math.min(range.endIndex + range.overscan, range.count - 1);
|
|
139
|
+
const arr = [];
|
|
179
140
|
|
|
180
|
-
|
|
141
|
+
for (let i = start; i <= end; i++) {
|
|
142
|
+
arr.push(i);
|
|
143
|
+
}
|
|
181
144
|
|
|
182
|
-
|
|
183
|
-
return 50;
|
|
145
|
+
return arr;
|
|
184
146
|
};
|
|
147
|
+
const observeElementRect = (instance, cb) => {
|
|
148
|
+
const observer = observeRect(instance.scrollElement, rect => {
|
|
149
|
+
cb(rect);
|
|
150
|
+
});
|
|
185
151
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}
|
|
152
|
+
if (!instance.scrollElement) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
189
155
|
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
return
|
|
156
|
+
cb(instance.scrollElement.getBoundingClientRect());
|
|
157
|
+
observer.observe();
|
|
158
|
+
return () => {
|
|
159
|
+
observer.unobserve();
|
|
160
|
+
};
|
|
193
161
|
};
|
|
162
|
+
const observeWindowRect = (instance, cb) => {
|
|
163
|
+
const onResize = () => {
|
|
164
|
+
cb({
|
|
165
|
+
width: instance.scrollElement.innerWidth,
|
|
166
|
+
height: instance.scrollElement.innerHeight
|
|
167
|
+
});
|
|
168
|
+
};
|
|
194
169
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
var end = Math.min(range.end + range.overscan, range.size - 1);
|
|
198
|
-
var arr = [];
|
|
199
|
-
|
|
200
|
-
for (var _i = start; _i <= end; _i++) {
|
|
201
|
-
arr.push(_i);
|
|
170
|
+
if (!instance.scrollElement) {
|
|
171
|
+
return;
|
|
202
172
|
}
|
|
203
173
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
var _ref$size = _ref.size,
|
|
210
|
-
size = _ref$size === void 0 ? 0 : _ref$size,
|
|
211
|
-
_ref$estimateSize = _ref.estimateSize,
|
|
212
|
-
estimateSize = _ref$estimateSize === void 0 ? defaultEstimateSize : _ref$estimateSize,
|
|
213
|
-
_ref$overscan = _ref.overscan,
|
|
214
|
-
overscan = _ref$overscan === void 0 ? 1 : _ref$overscan,
|
|
215
|
-
_ref$paddingStart = _ref.paddingStart,
|
|
216
|
-
paddingStart = _ref$paddingStart === void 0 ? 0 : _ref$paddingStart,
|
|
217
|
-
_ref$paddingEnd = _ref.paddingEnd,
|
|
218
|
-
paddingEnd = _ref$paddingEnd === void 0 ? 0 : _ref$paddingEnd,
|
|
219
|
-
parentRef = _ref.parentRef,
|
|
220
|
-
_ref$horizontal = _ref.horizontal,
|
|
221
|
-
horizontal = _ref$horizontal === void 0 ? false : _ref$horizontal,
|
|
222
|
-
scrollToFn = _ref.scrollToFn,
|
|
223
|
-
useObserver = _ref.useObserver,
|
|
224
|
-
initialRect = _ref.initialRect,
|
|
225
|
-
onScrollElement = _ref.onScrollElement,
|
|
226
|
-
scrollOffsetFn = _ref.scrollOffsetFn,
|
|
227
|
-
_ref$keyExtractor = _ref.keyExtractor,
|
|
228
|
-
keyExtractor = _ref$keyExtractor === void 0 ? defaultKeyExtractor : _ref$keyExtractor,
|
|
229
|
-
_ref$measureSize = _ref.measureSize,
|
|
230
|
-
measureSize = _ref$measureSize === void 0 ? defaultMeasureSize : _ref$measureSize,
|
|
231
|
-
_ref$rangeExtractor = _ref.rangeExtractor,
|
|
232
|
-
rangeExtractor = _ref$rangeExtractor === void 0 ? defaultRangeExtractor : _ref$rangeExtractor;
|
|
233
|
-
var sizeKey = horizontal ? 'width' : 'height';
|
|
234
|
-
var scrollKey = horizontal ? 'scrollLeft' : 'scrollTop';
|
|
235
|
-
var latestRef = React.useRef({
|
|
236
|
-
outerSize: 0,
|
|
237
|
-
scrollOffset: 0,
|
|
238
|
-
measurements: [],
|
|
239
|
-
totalSize: 0
|
|
174
|
+
onResize();
|
|
175
|
+
instance.scrollElement.addEventListener('resize', onResize, {
|
|
176
|
+
capture: false,
|
|
177
|
+
passive: true
|
|
240
178
|
});
|
|
179
|
+
return () => {
|
|
180
|
+
instance.scrollElement.removeEventListener('resize', onResize);
|
|
181
|
+
};
|
|
182
|
+
};
|
|
183
|
+
const observeElementOffset = (instance, cb) => {
|
|
184
|
+
const onScroll = () => cb(instance.scrollElement[instance.options.horizontal ? 'scrollLeft' : 'scrollTop']);
|
|
241
185
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
latestRef.current.scrollOffset = scrollOffset;
|
|
247
|
-
var useMeasureParent = useObserver || useRect;
|
|
186
|
+
if (!instance.scrollElement) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
248
189
|
|
|
249
|
-
|
|
250
|
-
|
|
190
|
+
onScroll();
|
|
191
|
+
instance.scrollElement.addEventListener('scroll', onScroll, {
|
|
192
|
+
capture: false,
|
|
193
|
+
passive: true
|
|
194
|
+
});
|
|
195
|
+
return () => {
|
|
196
|
+
instance.scrollElement.removeEventListener('scroll', onScroll);
|
|
197
|
+
};
|
|
198
|
+
};
|
|
199
|
+
const observeWindowOffset = (instance, cb) => {
|
|
200
|
+
const onScroll = () => cb(instance.scrollElement[instance.options.horizontal ? 'scrollX' : 'scrollY']);
|
|
251
201
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
parentRef.current[scrollKey] = offset;
|
|
256
|
-
}
|
|
257
|
-
}, [parentRef, scrollKey]);
|
|
258
|
-
var resolvedScrollToFn = scrollToFn || defaultScrollToFn;
|
|
259
|
-
var scrollTo = React.useCallback(function (offset) {
|
|
260
|
-
resolvedScrollToFn(offset, defaultScrollToFn);
|
|
261
|
-
}, [defaultScrollToFn, resolvedScrollToFn]);
|
|
262
|
-
|
|
263
|
-
var _React$useState2 = React.useState({}),
|
|
264
|
-
measuredCache = _React$useState2[0],
|
|
265
|
-
setMeasuredCache = _React$useState2[1];
|
|
266
|
-
|
|
267
|
-
var measure = React.useCallback(function () {
|
|
268
|
-
return setMeasuredCache({});
|
|
269
|
-
}, []);
|
|
270
|
-
var pendingMeasuredCacheIndexesRef = React.useRef([]);
|
|
271
|
-
var measurements = React.useMemo(function () {
|
|
272
|
-
var min = pendingMeasuredCacheIndexesRef.current.length > 0 ? Math.min.apply(Math, pendingMeasuredCacheIndexesRef.current) : 0;
|
|
273
|
-
pendingMeasuredCacheIndexesRef.current = [];
|
|
274
|
-
var measurements = latestRef.current.measurements.slice(0, min);
|
|
202
|
+
if (!instance.scrollElement) {
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
275
205
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
206
|
+
onScroll();
|
|
207
|
+
instance.scrollElement.addEventListener('scroll', onScroll, {
|
|
208
|
+
capture: false,
|
|
209
|
+
passive: true
|
|
210
|
+
});
|
|
211
|
+
return () => {
|
|
212
|
+
instance.scrollElement.removeEventListener('scroll', onScroll);
|
|
213
|
+
};
|
|
214
|
+
};
|
|
215
|
+
const measureElement = (element, instance) => {
|
|
216
|
+
return element.getBoundingClientRect()[instance.options.horizontal ? 'width' : 'height'];
|
|
217
|
+
};
|
|
218
|
+
const windowScroll = (offset, canSmooth, instance) => {
|
|
219
|
+
var _instance$scrollEleme;
|
|
220
|
+
(_instance$scrollEleme = instance.scrollElement) == null ? void 0 : _instance$scrollEleme.scrollTo({
|
|
221
|
+
[instance.options.horizontal ? 'left' : 'top']: offset,
|
|
222
|
+
behavior: canSmooth ? 'smooth' : undefined
|
|
223
|
+
});
|
|
224
|
+
};
|
|
225
|
+
const elementScroll = (offset, canSmooth, instance) => {
|
|
226
|
+
var _instance$scrollEleme2;
|
|
227
|
+
(_instance$scrollEleme2 = instance.scrollElement) == null ? void 0 : _instance$scrollEleme2.scrollTo({
|
|
228
|
+
[instance.options.horizontal ? 'left' : 'top']: offset,
|
|
229
|
+
behavior: canSmooth ? 'smooth' : undefined
|
|
230
|
+
});
|
|
231
|
+
};
|
|
232
|
+
class Virtualizer {
|
|
233
|
+
constructor(_opts) {
|
|
234
|
+
var _this = this;
|
|
235
|
+
|
|
236
|
+
this.unsubs = [];
|
|
237
|
+
this.scrollElement = null;
|
|
238
|
+
this.measurementsCache = [];
|
|
239
|
+
this.itemMeasurementsCache = {};
|
|
240
|
+
this.pendingMeasuredCacheIndexes = [];
|
|
241
|
+
|
|
242
|
+
this.setOptions = opts => {
|
|
243
|
+
Object.entries(opts).forEach(_ref => {
|
|
244
|
+
let [key, value] = _ref;
|
|
245
|
+
if (typeof value === 'undefined') delete opts[key];
|
|
246
|
+
});
|
|
247
|
+
this.options = {
|
|
248
|
+
debug: false,
|
|
249
|
+
initialOffset: 0,
|
|
250
|
+
overscan: 1,
|
|
251
|
+
paddingStart: 0,
|
|
252
|
+
paddingEnd: 0,
|
|
253
|
+
horizontal: false,
|
|
254
|
+
getItemKey: defaultKeyExtractor,
|
|
255
|
+
rangeExtractor: defaultRangeExtractor,
|
|
256
|
+
enableSmoothScroll: false,
|
|
257
|
+
onChange: () => {},
|
|
258
|
+
measureElement,
|
|
259
|
+
initialRect: {
|
|
260
|
+
width: 0,
|
|
261
|
+
height: 0
|
|
262
|
+
},
|
|
263
|
+
...opts
|
|
264
|
+
};
|
|
265
|
+
};
|
|
279
266
|
|
|
280
|
-
|
|
267
|
+
this.notify = () => {
|
|
268
|
+
var _this$options$onChang, _this$options;
|
|
281
269
|
|
|
282
|
-
|
|
270
|
+
(_this$options$onChang = (_this$options = this.options).onChange) == null ? void 0 : _this$options$onChang.call(_this$options, this);
|
|
271
|
+
};
|
|
283
272
|
|
|
284
|
-
|
|
273
|
+
this.cleanup = () => {
|
|
274
|
+
this.unsubs.filter(Boolean).forEach(d => d());
|
|
275
|
+
this.unsubs = [];
|
|
276
|
+
};
|
|
285
277
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
size: _size,
|
|
290
|
-
end: _end,
|
|
291
|
-
key: key
|
|
278
|
+
this._didMount = () => {
|
|
279
|
+
return () => {
|
|
280
|
+
this.cleanup();
|
|
292
281
|
};
|
|
293
|
-
}
|
|
282
|
+
};
|
|
294
283
|
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
284
|
+
this._willUpdate = () => {
|
|
285
|
+
const scrollElement = this.options.getScrollElement();
|
|
286
|
+
|
|
287
|
+
if (this.scrollElement !== scrollElement) {
|
|
288
|
+
this.cleanup();
|
|
289
|
+
this.scrollElement = scrollElement;
|
|
290
|
+
this.unsubs.push(this.options.observeElementRect(this, rect => {
|
|
291
|
+
this.scrollRect = rect;
|
|
292
|
+
this.notify();
|
|
293
|
+
}));
|
|
294
|
+
this.unsubs.push(this.options.observeElementOffset(this, offset => {
|
|
295
|
+
this.scrollOffset = offset;
|
|
296
|
+
this.notify();
|
|
297
|
+
}));
|
|
298
|
+
}
|
|
299
|
+
};
|
|
308
300
|
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
setScrollOffset(offset);
|
|
301
|
+
this.getSize = () => {
|
|
302
|
+
return this.scrollRect[this.options.horizontal ? 'width' : 'height'];
|
|
312
303
|
};
|
|
313
304
|
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
305
|
+
this.getMeasurements = memo(() => [this.options.count, this.options.paddingStart, this.options.getItemKey, this.itemMeasurementsCache], (count, paddingStart, getItemKey, measurementsCache) => {
|
|
306
|
+
const min = this.pendingMeasuredCacheIndexes.length > 0 ? Math.min(...this.pendingMeasuredCacheIndexes) : 0;
|
|
307
|
+
this.pendingMeasuredCacheIndexes = [];
|
|
308
|
+
const measurements = this.measurementsCache.slice(0, min);
|
|
309
|
+
|
|
310
|
+
for (let i = min; i < count; i++) {
|
|
311
|
+
const key = getItemKey(i);
|
|
312
|
+
const measuredSize = measurementsCache[key];
|
|
313
|
+
const start = measurements[i - 1] ? measurements[i - 1].end : paddingStart;
|
|
314
|
+
const size = typeof measuredSize === 'number' ? measuredSize : this.options.estimateSize(i);
|
|
315
|
+
const end = start + size;
|
|
316
|
+
measurements[i] = {
|
|
317
|
+
index: i,
|
|
318
|
+
start,
|
|
319
|
+
size,
|
|
320
|
+
end,
|
|
321
|
+
key
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
this.measurementsCache = measurements;
|
|
326
|
+
return measurements;
|
|
327
|
+
}, {
|
|
328
|
+
key: process.env.NODE_ENV === 'development' && 'getMeasurements',
|
|
329
|
+
debug: () => this.options.debug
|
|
318
330
|
});
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
331
|
+
this.calculateRange = memo(() => [this.getMeasurements(), this.getSize(), this.scrollOffset], (measurements, outerSize, scrollOffset) => {
|
|
332
|
+
return calculateRange({
|
|
333
|
+
measurements,
|
|
334
|
+
outerSize,
|
|
335
|
+
scrollOffset
|
|
336
|
+
});
|
|
337
|
+
}, {
|
|
338
|
+
key: process.env.NODE_ENV === 'development' && 'calculateRange',
|
|
339
|
+
debug: () => this.options.debug
|
|
340
|
+
});
|
|
341
|
+
this.getIndexes = memo(() => [this.options.rangeExtractor, this.calculateRange(), this.options.overscan, this.options.count], (rangeExtractor, range, overscan, count) => {
|
|
342
|
+
return rangeExtractor({ ...range,
|
|
343
|
+
overscan,
|
|
344
|
+
count: count
|
|
345
|
+
});
|
|
346
|
+
}, {
|
|
347
|
+
key: process.env.NODE_ENV === 'development' && 'getIndexes'
|
|
334
348
|
});
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
349
|
+
this.getVirtualItems = memo(() => [this.getIndexes(), this.getMeasurements(), this.options.measureElement], (indexes, measurements, measureElement) => {
|
|
350
|
+
const virtualItems = [];
|
|
351
|
+
|
|
352
|
+
for (let k = 0, len = indexes.length; k < len; k++) {
|
|
353
|
+
const i = indexes[k];
|
|
354
|
+
const measurement = measurements[i];
|
|
355
|
+
const item = { ...measurement,
|
|
356
|
+
measureElement: measurableItem => {
|
|
357
|
+
if (measurableItem) {
|
|
358
|
+
const measuredItemSize = measureElement(measurableItem, this);
|
|
359
|
+
|
|
360
|
+
if (measuredItemSize !== item.size) {
|
|
361
|
+
if (item.start < this.scrollOffset) {
|
|
362
|
+
if (process.env.NODE_ENV === 'development' && this.options.debug) console.info('correction', measuredItemSize - item.size);
|
|
363
|
+
|
|
364
|
+
if (!this.destinationOffset) {
|
|
365
|
+
this._scrollToOffset(this.scrollOffset + (measuredItemSize - item.size), false);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
this.pendingMeasuredCacheIndexes.push(i);
|
|
370
|
+
this.itemMeasurementsCache = { ...this.itemMeasurementsCache,
|
|
371
|
+
[item.key]: measuredItemSize
|
|
372
|
+
};
|
|
373
|
+
this.notify();
|
|
355
374
|
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
virtualItems.push(item);
|
|
379
|
+
}
|
|
356
380
|
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
381
|
+
return virtualItems;
|
|
382
|
+
}, {
|
|
383
|
+
key: process.env.NODE_ENV === 'development' && 'getIndexes'
|
|
384
|
+
});
|
|
360
385
|
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
386
|
+
this.scrollToOffset = function (toOffset, _temp) {
|
|
387
|
+
let {
|
|
388
|
+
align
|
|
389
|
+
} = _temp === void 0 ? {
|
|
390
|
+
align: 'start'
|
|
391
|
+
} : _temp;
|
|
392
|
+
|
|
393
|
+
const attempt = () => {
|
|
394
|
+
const offset = _this.scrollOffset;
|
|
395
|
+
|
|
396
|
+
const size = _this.getSize();
|
|
397
|
+
|
|
398
|
+
if (align === 'auto') {
|
|
399
|
+
if (toOffset <= offset) {
|
|
400
|
+
align = 'start';
|
|
401
|
+
} else if (toOffset >= offset + size) {
|
|
402
|
+
align = 'end';
|
|
403
|
+
} else {
|
|
404
|
+
align = 'start';
|
|
364
405
|
}
|
|
365
406
|
}
|
|
366
|
-
});
|
|
367
407
|
|
|
368
|
-
|
|
408
|
+
if (align === 'start') {
|
|
409
|
+
_this._scrollToOffset(toOffset, true);
|
|
410
|
+
} else if (align === 'end') {
|
|
411
|
+
_this._scrollToOffset(toOffset - size, true);
|
|
412
|
+
} else if (align === 'center') {
|
|
413
|
+
_this._scrollToOffset(toOffset - size / 2, true);
|
|
414
|
+
}
|
|
415
|
+
};
|
|
416
|
+
|
|
417
|
+
attempt();
|
|
418
|
+
requestAnimationFrame(() => {
|
|
419
|
+
attempt();
|
|
420
|
+
});
|
|
369
421
|
};
|
|
370
422
|
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
423
|
+
this.scrollToIndex = function (index, _temp2) {
|
|
424
|
+
let {
|
|
425
|
+
align,
|
|
426
|
+
...rest
|
|
427
|
+
} = _temp2 === void 0 ? {
|
|
428
|
+
align: 'auto'
|
|
429
|
+
} : _temp2;
|
|
374
430
|
|
|
375
|
-
|
|
376
|
-
}, [indexes, defaultScrollToFn, horizontal, measurements]);
|
|
377
|
-
var mountedRef = React.useRef(false);
|
|
378
|
-
useIsomorphicLayoutEffect(function () {
|
|
379
|
-
if (mountedRef.current) {
|
|
380
|
-
setMeasuredCache({});
|
|
381
|
-
}
|
|
431
|
+
const measurements = _this.getMeasurements();
|
|
382
432
|
|
|
383
|
-
|
|
384
|
-
}, [estimateSize]);
|
|
385
|
-
var scrollToOffset = React.useCallback(function (toOffset, _temp) {
|
|
386
|
-
var _ref2 = _temp === void 0 ? {
|
|
387
|
-
align: 'start'
|
|
388
|
-
} : _temp,
|
|
389
|
-
align = _ref2.align;
|
|
390
|
-
|
|
391
|
-
var _latestRef$current = latestRef.current,
|
|
392
|
-
scrollOffset = _latestRef$current.scrollOffset,
|
|
393
|
-
outerSize = _latestRef$current.outerSize;
|
|
394
|
-
|
|
395
|
-
if (align === 'auto') {
|
|
396
|
-
if (toOffset <= scrollOffset) {
|
|
397
|
-
align = 'start';
|
|
398
|
-
} else if (toOffset >= scrollOffset + outerSize) {
|
|
399
|
-
align = 'end';
|
|
400
|
-
} else {
|
|
401
|
-
align = 'start';
|
|
402
|
-
}
|
|
403
|
-
}
|
|
433
|
+
const offset = _this.scrollOffset;
|
|
404
434
|
|
|
405
|
-
|
|
406
|
-
scrollTo(toOffset);
|
|
407
|
-
} else if (align === 'end') {
|
|
408
|
-
scrollTo(toOffset - outerSize);
|
|
409
|
-
} else if (align === 'center') {
|
|
410
|
-
scrollTo(toOffset - outerSize / 2);
|
|
411
|
-
}
|
|
412
|
-
}, [scrollTo]);
|
|
413
|
-
var tryScrollToIndex = React.useCallback(function (index, _temp2) {
|
|
414
|
-
var _ref3 = _temp2 === void 0 ? {
|
|
415
|
-
align: 'auto'
|
|
416
|
-
} : _temp2,
|
|
417
|
-
align = _ref3.align,
|
|
418
|
-
rest = _objectWithoutPropertiesLoose(_ref3, _excluded);
|
|
419
|
-
|
|
420
|
-
var _latestRef$current2 = latestRef.current,
|
|
421
|
-
measurements = _latestRef$current2.measurements,
|
|
422
|
-
scrollOffset = _latestRef$current2.scrollOffset,
|
|
423
|
-
outerSize = _latestRef$current2.outerSize;
|
|
424
|
-
var measurement = measurements[Math.max(0, Math.min(index, size - 1))];
|
|
425
|
-
|
|
426
|
-
if (!measurement) {
|
|
427
|
-
return;
|
|
428
|
-
}
|
|
435
|
+
const size = _this.getSize();
|
|
429
436
|
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
437
|
+
const {
|
|
438
|
+
count
|
|
439
|
+
} = _this.options;
|
|
440
|
+
const measurement = measurements[Math.max(0, Math.min(index, count - 1))];
|
|
441
|
+
|
|
442
|
+
if (!measurement) {
|
|
436
443
|
return;
|
|
437
444
|
}
|
|
438
|
-
}
|
|
439
445
|
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
446
|
+
if (align === 'auto') {
|
|
447
|
+
if (measurement.end >= offset + size) {
|
|
448
|
+
align = 'end';
|
|
449
|
+
} else if (measurement.start <= offset) {
|
|
450
|
+
align = 'start';
|
|
451
|
+
} else {
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
const toOffset = align === 'center' ? measurement.start + measurement.size / 2 : align === 'end' ? measurement.end : measurement.start;
|
|
457
|
+
|
|
458
|
+
_this.scrollToOffset(toOffset, {
|
|
459
|
+
align,
|
|
460
|
+
...rest
|
|
461
|
+
});
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
this.getTotalSize = () => {
|
|
465
|
+
var _this$getMeasurements;
|
|
466
|
+
|
|
467
|
+
return (((_this$getMeasurements = this.getMeasurements()[this.options.count - 1]) == null ? void 0 : _this$getMeasurements.end) || this.options.paddingStart) + this.options.paddingEnd;
|
|
468
|
+
};
|
|
469
|
+
|
|
470
|
+
this._scrollToOffset = (offset, canSmooth) => {
|
|
471
|
+
clearTimeout(this.scrollCheckFrame);
|
|
472
|
+
this.destinationOffset = offset;
|
|
473
|
+
this.options.scrollToFn(offset, canSmooth, this);
|
|
474
|
+
let scrollCheckFrame;
|
|
475
|
+
|
|
476
|
+
const check = () => {
|
|
477
|
+
let lastOffset = this.scrollOffset;
|
|
478
|
+
this.scrollCheckFrame = scrollCheckFrame = setTimeout(() => {
|
|
479
|
+
if (this.scrollCheckFrame !== scrollCheckFrame) {
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
if (this.scrollOffset === lastOffset) {
|
|
484
|
+
this.destinationOffset = undefined;
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
lastOffset = this.scrollOffset;
|
|
489
|
+
check();
|
|
490
|
+
}, 100);
|
|
491
|
+
};
|
|
492
|
+
|
|
493
|
+
check();
|
|
494
|
+
};
|
|
495
|
+
|
|
496
|
+
this.measure = () => {
|
|
497
|
+
this.itemMeasurementsCache = {};
|
|
498
|
+
this.notify();
|
|
499
|
+
};
|
|
500
|
+
|
|
501
|
+
this.setOptions(_opts);
|
|
502
|
+
this.scrollRect = this.options.initialRect;
|
|
503
|
+
this.scrollOffset = this.options.initialOffset;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
}
|
|
464
507
|
|
|
465
|
-
|
|
508
|
+
const findNearestBinarySearch = (low, high, getCurrentValue, value) => {
|
|
466
509
|
while (low <= high) {
|
|
467
|
-
|
|
468
|
-
|
|
510
|
+
const middle = (low + high) / 2 | 0;
|
|
511
|
+
const currentValue = getCurrentValue(middle);
|
|
469
512
|
|
|
470
513
|
if (currentValue < value) {
|
|
471
514
|
low = middle + 1;
|
|
@@ -483,28 +526,67 @@ var findNearestBinarySearch = function findNearestBinarySearch(low, high, getCur
|
|
|
483
526
|
}
|
|
484
527
|
};
|
|
485
528
|
|
|
486
|
-
function calculateRange(
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
529
|
+
function calculateRange(_ref2) {
|
|
530
|
+
let {
|
|
531
|
+
measurements,
|
|
532
|
+
outerSize,
|
|
533
|
+
scrollOffset
|
|
534
|
+
} = _ref2;
|
|
535
|
+
const count = measurements.length - 1;
|
|
491
536
|
|
|
492
|
-
|
|
493
|
-
return measurements[index].start;
|
|
494
|
-
};
|
|
537
|
+
const getOffset = index => measurements[index].start;
|
|
495
538
|
|
|
496
|
-
|
|
497
|
-
|
|
539
|
+
const startIndex = findNearestBinarySearch(0, count, getOffset, scrollOffset);
|
|
540
|
+
let endIndex = startIndex;
|
|
498
541
|
|
|
499
|
-
while (
|
|
500
|
-
|
|
542
|
+
while (endIndex < count && measurements[endIndex].end < scrollOffset + outerSize) {
|
|
543
|
+
endIndex++;
|
|
501
544
|
}
|
|
502
545
|
|
|
503
546
|
return {
|
|
504
|
-
|
|
505
|
-
|
|
547
|
+
startIndex,
|
|
548
|
+
endIndex
|
|
549
|
+
};
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect;
|
|
553
|
+
|
|
554
|
+
function useVirtualizerBase(options) {
|
|
555
|
+
const rerender = React.useReducer(() => ({}), {})[1];
|
|
556
|
+
const resolvedOptions = { ...options,
|
|
557
|
+
onChange: instance => {
|
|
558
|
+
rerender();
|
|
559
|
+
options.onChange == null ? void 0 : options.onChange(instance);
|
|
560
|
+
}
|
|
506
561
|
};
|
|
562
|
+
const [instance] = React.useState(() => new Virtualizer(resolvedOptions));
|
|
563
|
+
instance.setOptions(resolvedOptions);
|
|
564
|
+
React.useEffect(() => {
|
|
565
|
+
return instance._didMount();
|
|
566
|
+
}, []);
|
|
567
|
+
useIsomorphicLayoutEffect(() => {
|
|
568
|
+
return instance._willUpdate();
|
|
569
|
+
});
|
|
570
|
+
return instance;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
function useVirtualizer(options) {
|
|
574
|
+
return useVirtualizerBase({
|
|
575
|
+
observeElementRect: observeElementRect,
|
|
576
|
+
observeElementOffset: observeElementOffset,
|
|
577
|
+
scrollToFn: elementScroll,
|
|
578
|
+
...options
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
function useWindowVirtualizer(options) {
|
|
582
|
+
return useVirtualizerBase({
|
|
583
|
+
getScrollElement: () => typeof window !== 'undefined' ? window : null,
|
|
584
|
+
observeElementRect: observeWindowRect,
|
|
585
|
+
observeElementOffset: observeWindowOffset,
|
|
586
|
+
scrollToFn: windowScroll,
|
|
587
|
+
...options
|
|
588
|
+
});
|
|
507
589
|
}
|
|
508
590
|
|
|
509
|
-
export { defaultRangeExtractor,
|
|
591
|
+
export { Virtualizer, defaultKeyExtractor, defaultRangeExtractor, elementScroll, measureElement, memo, observeElementOffset, observeElementRect, observeWindowOffset, observeWindowRect, useVirtualizer, useWindowVirtualizer, windowScroll };
|
|
510
592
|
//# sourceMappingURL=index.js.map
|