@tanstack/react-virtual 3.0.0-alpha.0 → 3.0.0-alpha.1
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/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 +563 -0
- package/build/cjs/virtual-core/build/esm/index.js.map +1 -0
- package/build/esm/index.js +428 -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 +439 -351
- 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 +62 -386
- package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +0 -50
- package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +0 -1
- 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
|
@@ -14,8 +14,6 @@
|
|
|
14
14
|
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ReactVirtual = {}, global.React));
|
|
15
15
|
})(this, (function (exports, React) { 'use strict';
|
|
16
16
|
|
|
17
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
18
|
-
|
|
19
17
|
function _interopNamespace(e) {
|
|
20
18
|
if (e && e.__esModule) return e;
|
|
21
19
|
var n = Object.create(null);
|
|
@@ -35,41 +33,17 @@
|
|
|
35
33
|
}
|
|
36
34
|
|
|
37
35
|
var React__namespace = /*#__PURE__*/_interopNamespace(React);
|
|
38
|
-
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
39
|
-
|
|
40
|
-
function _extends() {
|
|
41
|
-
_extends = Object.assign || function (target) {
|
|
42
|
-
for (var i = 1; i < arguments.length; i++) {
|
|
43
|
-
var source = arguments[i];
|
|
44
|
-
|
|
45
|
-
for (var key in source) {
|
|
46
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
47
|
-
target[key] = source[key];
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return target;
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
return _extends.apply(this, arguments);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function _objectWithoutPropertiesLoose(source, excluded) {
|
|
59
|
-
if (source == null) return {};
|
|
60
|
-
var target = {};
|
|
61
|
-
var sourceKeys = Object.keys(source);
|
|
62
|
-
var key, i;
|
|
63
|
-
|
|
64
|
-
for (i = 0; i < sourceKeys.length; i++) {
|
|
65
|
-
key = sourceKeys[i];
|
|
66
|
-
if (excluded.indexOf(key) >= 0) continue;
|
|
67
|
-
target[key] = source[key];
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
return target;
|
|
71
|
-
}
|
|
72
36
|
|
|
37
|
+
/**
|
|
38
|
+
* virtual-core
|
|
39
|
+
*
|
|
40
|
+
* Copyright (c) TanStack
|
|
41
|
+
*
|
|
42
|
+
* This source code is licensed under the MIT license found in the
|
|
43
|
+
* LICENSE.md file in the root directory of this source tree.
|
|
44
|
+
*
|
|
45
|
+
* @license MIT
|
|
46
|
+
*/
|
|
73
47
|
var props = ["bottom", "height", "left", "right", "top", "width"];
|
|
74
48
|
|
|
75
49
|
var rectChanged = function rectChanged(a, b) {
|
|
@@ -140,358 +114,422 @@
|
|
|
140
114
|
};
|
|
141
115
|
}
|
|
142
116
|
|
|
143
|
-
|
|
117
|
+
function memo(getDeps, fn, opts) {
|
|
118
|
+
let deps = [];
|
|
119
|
+
let result;
|
|
120
|
+
return () => {
|
|
121
|
+
let depTime;
|
|
122
|
+
if (opts.key && opts.debug != null && opts.debug()) depTime = Date.now();
|
|
123
|
+
const newDeps = getDeps();
|
|
124
|
+
const depsChanged = newDeps.length !== deps.length || newDeps.some((dep, index) => deps[index] !== dep);
|
|
125
|
+
|
|
126
|
+
if (!depsChanged) {
|
|
127
|
+
return result;
|
|
128
|
+
}
|
|
144
129
|
|
|
145
|
-
|
|
146
|
-
|
|
130
|
+
deps = newDeps;
|
|
131
|
+
let resultTime;
|
|
132
|
+
if (opts.key && opts.debug != null && opts.debug()) resultTime = Date.now();
|
|
133
|
+
result = fn(...newDeps);
|
|
134
|
+
opts == null ? void 0 : opts.onChange == null ? void 0 : opts.onChange(result);
|
|
147
135
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
136
|
+
if (opts.key && opts.debug != null && opts.debug()) {
|
|
137
|
+
const depEndTime = Math.round((Date.now() - depTime) * 100) / 100;
|
|
138
|
+
const resultEndTime = Math.round((Date.now() - resultTime) * 100) / 100;
|
|
139
|
+
const resultFpsPercentage = resultEndTime / 16;
|
|
151
140
|
|
|
152
|
-
|
|
153
|
-
|
|
141
|
+
const pad = (str, num) => {
|
|
142
|
+
str = String(str);
|
|
154
143
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
width: 0,
|
|
159
|
-
height: 0
|
|
160
|
-
};
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
var _React$useState = React__default["default"].useState(nodeRef.current),
|
|
164
|
-
element = _React$useState[0],
|
|
165
|
-
setElement = _React$useState[1];
|
|
144
|
+
while (str.length < num) {
|
|
145
|
+
str = ' ' + str;
|
|
146
|
+
}
|
|
166
147
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
dispatch = _React$useReducer[1];
|
|
148
|
+
return str;
|
|
149
|
+
};
|
|
170
150
|
|
|
171
|
-
|
|
172
|
-
useIsomorphicLayoutEffect(function () {
|
|
173
|
-
if (nodeRef.current !== element) {
|
|
174
|
-
setElement(nodeRef.current);
|
|
151
|
+
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);
|
|
175
152
|
}
|
|
176
|
-
});
|
|
177
|
-
useIsomorphicLayoutEffect(function () {
|
|
178
|
-
if (element && !initialRectSet.current) {
|
|
179
|
-
initialRectSet.current = true;
|
|
180
|
-
|
|
181
|
-
var _rect = element.getBoundingClientRect();
|
|
182
153
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
}
|
|
187
|
-
}, [element]);
|
|
188
|
-
React__default["default"].useEffect(function () {
|
|
189
|
-
if (!element) {
|
|
190
|
-
return;
|
|
191
|
-
}
|
|
154
|
+
return result;
|
|
155
|
+
};
|
|
156
|
+
}
|
|
192
157
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
observer.unobserve();
|
|
201
|
-
};
|
|
202
|
-
}, [element]);
|
|
203
|
-
return rect;
|
|
204
|
-
};
|
|
158
|
+
//
|
|
159
|
+
const defaultEstimateSize = () => 50;
|
|
160
|
+
const defaultKeyExtractor = index => index;
|
|
161
|
+
const defaultRangeExtractor = range => {
|
|
162
|
+
const start = Math.max(range.startIndex - range.overscan, 0);
|
|
163
|
+
const end = Math.min(range.endIndex + range.overscan, range.count - 1);
|
|
164
|
+
const arr = [];
|
|
205
165
|
|
|
206
|
-
|
|
166
|
+
for (let i = start; i <= end; i++) {
|
|
167
|
+
arr.push(i);
|
|
168
|
+
}
|
|
207
169
|
|
|
208
|
-
|
|
209
|
-
return 50;
|
|
170
|
+
return arr;
|
|
210
171
|
};
|
|
172
|
+
const observeElementRect = (instance, cb) => {
|
|
173
|
+
const observer = observeRect(instance.scrollElement, rect => {
|
|
174
|
+
cb(rect);
|
|
175
|
+
});
|
|
211
176
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
177
|
+
if (!instance.scrollElement) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
215
180
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
return
|
|
181
|
+
cb(instance.scrollElement.getBoundingClientRect());
|
|
182
|
+
observer.observe();
|
|
183
|
+
return () => {
|
|
184
|
+
observer.unobserve();
|
|
185
|
+
};
|
|
219
186
|
};
|
|
187
|
+
const observeWindowRect = (instance, cb) => {
|
|
188
|
+
const onResize = () => {
|
|
189
|
+
cb({
|
|
190
|
+
width: instance.scrollElement.innerWidth,
|
|
191
|
+
height: instance.scrollElement.innerHeight
|
|
192
|
+
});
|
|
193
|
+
};
|
|
220
194
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
var end = Math.min(range.end + range.overscan, range.size - 1);
|
|
224
|
-
var arr = [];
|
|
225
|
-
|
|
226
|
-
for (var _i = start; _i <= end; _i++) {
|
|
227
|
-
arr.push(_i);
|
|
195
|
+
if (!instance.scrollElement) {
|
|
196
|
+
return;
|
|
228
197
|
}
|
|
229
198
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
var _ref$size = _ref.size,
|
|
236
|
-
size = _ref$size === void 0 ? 0 : _ref$size,
|
|
237
|
-
_ref$estimateSize = _ref.estimateSize,
|
|
238
|
-
estimateSize = _ref$estimateSize === void 0 ? defaultEstimateSize : _ref$estimateSize,
|
|
239
|
-
_ref$overscan = _ref.overscan,
|
|
240
|
-
overscan = _ref$overscan === void 0 ? 1 : _ref$overscan,
|
|
241
|
-
_ref$paddingStart = _ref.paddingStart,
|
|
242
|
-
paddingStart = _ref$paddingStart === void 0 ? 0 : _ref$paddingStart,
|
|
243
|
-
_ref$paddingEnd = _ref.paddingEnd,
|
|
244
|
-
paddingEnd = _ref$paddingEnd === void 0 ? 0 : _ref$paddingEnd,
|
|
245
|
-
parentRef = _ref.parentRef,
|
|
246
|
-
_ref$horizontal = _ref.horizontal,
|
|
247
|
-
horizontal = _ref$horizontal === void 0 ? false : _ref$horizontal,
|
|
248
|
-
scrollToFn = _ref.scrollToFn,
|
|
249
|
-
useObserver = _ref.useObserver,
|
|
250
|
-
initialRect = _ref.initialRect,
|
|
251
|
-
onScrollElement = _ref.onScrollElement,
|
|
252
|
-
scrollOffsetFn = _ref.scrollOffsetFn,
|
|
253
|
-
_ref$keyExtractor = _ref.keyExtractor,
|
|
254
|
-
keyExtractor = _ref$keyExtractor === void 0 ? defaultKeyExtractor : _ref$keyExtractor,
|
|
255
|
-
_ref$measureSize = _ref.measureSize,
|
|
256
|
-
measureSize = _ref$measureSize === void 0 ? defaultMeasureSize : _ref$measureSize,
|
|
257
|
-
_ref$rangeExtractor = _ref.rangeExtractor,
|
|
258
|
-
rangeExtractor = _ref$rangeExtractor === void 0 ? defaultRangeExtractor : _ref$rangeExtractor;
|
|
259
|
-
var sizeKey = horizontal ? 'width' : 'height';
|
|
260
|
-
var scrollKey = horizontal ? 'scrollLeft' : 'scrollTop';
|
|
261
|
-
var latestRef = React__namespace.useRef({
|
|
262
|
-
outerSize: 0,
|
|
263
|
-
scrollOffset: 0,
|
|
264
|
-
measurements: [],
|
|
265
|
-
totalSize: 0
|
|
199
|
+
onResize();
|
|
200
|
+
instance.scrollElement.addEventListener('resize', onResize, {
|
|
201
|
+
capture: false,
|
|
202
|
+
passive: true
|
|
266
203
|
});
|
|
204
|
+
return () => {
|
|
205
|
+
instance.scrollElement.removeEventListener('resize', onResize);
|
|
206
|
+
};
|
|
207
|
+
};
|
|
208
|
+
const observeElementOffset = (instance, cb) => {
|
|
209
|
+
const onScroll = () => cb(instance.scrollElement[instance.options.horizontal ? 'scrollLeft' : 'scrollTop']);
|
|
267
210
|
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
latestRef.current.scrollOffset = scrollOffset;
|
|
273
|
-
var useMeasureParent = useObserver || useRect;
|
|
211
|
+
if (!instance.scrollElement) {
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
274
214
|
|
|
275
|
-
|
|
276
|
-
|
|
215
|
+
onScroll();
|
|
216
|
+
instance.scrollElement.addEventListener('scroll', onScroll, {
|
|
217
|
+
capture: false,
|
|
218
|
+
passive: true
|
|
219
|
+
});
|
|
220
|
+
return () => {
|
|
221
|
+
instance.scrollElement.removeEventListener('scroll', onScroll);
|
|
222
|
+
};
|
|
223
|
+
};
|
|
224
|
+
const observeWindowOffset = (instance, cb) => {
|
|
225
|
+
const onScroll = () => cb(instance.scrollElement[instance.options.horizontal ? 'scrollX' : 'scrollY']);
|
|
277
226
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
parentRef.current[scrollKey] = offset;
|
|
282
|
-
}
|
|
283
|
-
}, [parentRef, scrollKey]);
|
|
284
|
-
var resolvedScrollToFn = scrollToFn || defaultScrollToFn;
|
|
285
|
-
var scrollTo = React__namespace.useCallback(function (offset) {
|
|
286
|
-
resolvedScrollToFn(offset, defaultScrollToFn);
|
|
287
|
-
}, [defaultScrollToFn, resolvedScrollToFn]);
|
|
288
|
-
|
|
289
|
-
var _React$useState2 = React__namespace.useState({}),
|
|
290
|
-
measuredCache = _React$useState2[0],
|
|
291
|
-
setMeasuredCache = _React$useState2[1];
|
|
292
|
-
|
|
293
|
-
var measure = React__namespace.useCallback(function () {
|
|
294
|
-
return setMeasuredCache({});
|
|
295
|
-
}, []);
|
|
296
|
-
var pendingMeasuredCacheIndexesRef = React__namespace.useRef([]);
|
|
297
|
-
var measurements = React__namespace.useMemo(function () {
|
|
298
|
-
var min = pendingMeasuredCacheIndexesRef.current.length > 0 ? Math.min.apply(Math, pendingMeasuredCacheIndexesRef.current) : 0;
|
|
299
|
-
pendingMeasuredCacheIndexesRef.current = [];
|
|
300
|
-
var measurements = latestRef.current.measurements.slice(0, min);
|
|
227
|
+
if (!instance.scrollElement) {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
301
230
|
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
231
|
+
onScroll();
|
|
232
|
+
instance.scrollElement.addEventListener('scroll', onScroll, {
|
|
233
|
+
capture: false,
|
|
234
|
+
passive: true
|
|
235
|
+
});
|
|
236
|
+
return () => {
|
|
237
|
+
instance.scrollElement.removeEventListener('scroll', onScroll);
|
|
238
|
+
};
|
|
239
|
+
};
|
|
240
|
+
const defaultMeasureElement = (element, instance) => {
|
|
241
|
+
return element.getBoundingClientRect()[instance.getSizeKey()];
|
|
242
|
+
};
|
|
243
|
+
const windowScroll = (offset, canSmooth, instance) => {
|
|
244
|
+
var _instance$scrollEleme;
|
|
245
|
+
(_instance$scrollEleme = instance.scrollElement) == null ? void 0 : _instance$scrollEleme.scrollTo({
|
|
246
|
+
[instance.options.horizontal ? 'left' : 'top']: offset,
|
|
247
|
+
behavior: canSmooth ? 'smooth' : undefined
|
|
248
|
+
});
|
|
249
|
+
};
|
|
250
|
+
const elementScroll = (offset, canSmooth, instance) => {
|
|
251
|
+
var _instance$scrollEleme2;
|
|
252
|
+
(_instance$scrollEleme2 = instance.scrollElement) == null ? void 0 : _instance$scrollEleme2.scrollTo({
|
|
253
|
+
[instance.options.horizontal ? 'left' : 'top']: offset,
|
|
254
|
+
behavior: canSmooth ? 'smooth' : undefined
|
|
255
|
+
});
|
|
256
|
+
};
|
|
257
|
+
class Virtualizer {
|
|
258
|
+
//
|
|
259
|
+
// virtualItems: VirtualItem<TItemElement>[]
|
|
260
|
+
// totalSize: number
|
|
261
|
+
// scrollToOffset: (offset: number, options?: ScrollToOffsetOptions) => void
|
|
262
|
+
// scrollToIndex: (index: number, options?: ScrollToIndexOptions) => void
|
|
263
|
+
// measure: (index: number) => void
|
|
264
|
+
constructor(_opts) {
|
|
265
|
+
var _this = this;
|
|
266
|
+
|
|
267
|
+
this.unsubs = [];
|
|
268
|
+
this.scrollElement = null;
|
|
269
|
+
this.measurementsCache = [];
|
|
270
|
+
this.itemMeasurementsCache = {};
|
|
271
|
+
this.pendingMeasuredCacheIndexes = [];
|
|
272
|
+
|
|
273
|
+
this.setOptions = opts => {
|
|
274
|
+
Object.entries(opts).forEach(_ref => {
|
|
275
|
+
let [key, value] = _ref;
|
|
276
|
+
if (typeof value === 'undefined') delete opts[key];
|
|
277
|
+
});
|
|
278
|
+
this.options = {
|
|
279
|
+
debug: false,
|
|
280
|
+
initialOffset: 0,
|
|
281
|
+
estimateSize: defaultEstimateSize,
|
|
282
|
+
overscan: 1,
|
|
283
|
+
paddingStart: 0,
|
|
284
|
+
paddingEnd: 0,
|
|
285
|
+
horizontal: false,
|
|
286
|
+
keyExtractor: defaultKeyExtractor,
|
|
287
|
+
rangeExtractor: defaultRangeExtractor,
|
|
288
|
+
enableSmoothScroll: false,
|
|
289
|
+
onChange: () => {},
|
|
290
|
+
measureElement: defaultMeasureElement,
|
|
291
|
+
initialRect: {
|
|
292
|
+
width: 0,
|
|
293
|
+
height: 0
|
|
294
|
+
},
|
|
295
|
+
...opts
|
|
296
|
+
};
|
|
297
|
+
};
|
|
305
298
|
|
|
306
|
-
|
|
299
|
+
this.notify = () => {
|
|
300
|
+
var _this$options$onChang, _this$options;
|
|
307
301
|
|
|
308
|
-
|
|
302
|
+
(_this$options$onChang = (_this$options = this.options).onChange) == null ? void 0 : _this$options$onChang.call(_this$options, this);
|
|
303
|
+
};
|
|
309
304
|
|
|
310
|
-
|
|
305
|
+
this.cleanup = () => {
|
|
306
|
+
this.unsubs.filter(Boolean).forEach(d => d());
|
|
307
|
+
this.unsubs = [];
|
|
308
|
+
};
|
|
311
309
|
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
size: _size,
|
|
316
|
-
end: _end,
|
|
317
|
-
key: key
|
|
310
|
+
this._didMount = () => {
|
|
311
|
+
return () => {
|
|
312
|
+
this.cleanup();
|
|
318
313
|
};
|
|
319
|
-
}
|
|
314
|
+
};
|
|
320
315
|
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
316
|
+
this._willUpdate = () => {
|
|
317
|
+
const scrollElement = this.options.getScrollElement();
|
|
318
|
+
|
|
319
|
+
if (this.scrollElement !== scrollElement) {
|
|
320
|
+
this.cleanup();
|
|
321
|
+
this.scrollElement = scrollElement;
|
|
322
|
+
this.unsubs.push(this.options.observeElementRect(this, rect => {
|
|
323
|
+
this.scrollRect = rect;
|
|
324
|
+
this.notify();
|
|
325
|
+
}));
|
|
326
|
+
this.unsubs.push(this.options.observeElementOffset(this, offset => {
|
|
327
|
+
this.scrollOffset = offset;
|
|
328
|
+
this.notify();
|
|
329
|
+
}));
|
|
330
|
+
}
|
|
331
|
+
};
|
|
334
332
|
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
setScrollOffset(offset);
|
|
333
|
+
this.getSize = () => {
|
|
334
|
+
return this.scrollRect[this.getSizeKey()];
|
|
338
335
|
};
|
|
339
336
|
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
337
|
+
this.getMeasurements = memo(() => [this.options.count, this.options.paddingStart, this.getEstimateSizeFn(), this.options.keyExtractor, this.itemMeasurementsCache], (count, paddingStart, estimateSize, keyExtractor, measurementsCache) => {
|
|
338
|
+
const min = this.pendingMeasuredCacheIndexes.length > 0 ? Math.min(...this.pendingMeasuredCacheIndexes) : 0;
|
|
339
|
+
this.pendingMeasuredCacheIndexes = [];
|
|
340
|
+
const measurements = this.measurementsCache.slice(0, min);
|
|
341
|
+
|
|
342
|
+
for (let i = min; i < count; i++) {
|
|
343
|
+
const key = keyExtractor(i);
|
|
344
|
+
const measuredSize = measurementsCache[key];
|
|
345
|
+
const start = measurements[i - 1] ? measurements[i - 1].end : paddingStart;
|
|
346
|
+
const size = typeof measuredSize === 'number' ? measuredSize : estimateSize(i);
|
|
347
|
+
const end = start + size;
|
|
348
|
+
measurements[i] = {
|
|
349
|
+
index: i,
|
|
350
|
+
start,
|
|
351
|
+
size,
|
|
352
|
+
end,
|
|
353
|
+
key
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
this.measurementsCache = measurements;
|
|
358
|
+
return measurements;
|
|
359
|
+
}, {
|
|
360
|
+
key: 'getMeasurements',
|
|
361
|
+
debug: () => this.options.debug
|
|
344
362
|
});
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
var indexes = React__namespace.useMemo(function () {
|
|
355
|
-
return rangeExtractor({
|
|
356
|
-
start: start,
|
|
357
|
-
end: end,
|
|
358
|
-
overscan: overscan,
|
|
359
|
-
size: measurements.length
|
|
363
|
+
this.calculateRange = memo(() => [this.getMeasurements(), this.getSize(), this.scrollOffset], (measurements, outerSize, scrollOffset) => {
|
|
364
|
+
return calculateRange({
|
|
365
|
+
measurements,
|
|
366
|
+
outerSize,
|
|
367
|
+
scrollOffset
|
|
368
|
+
});
|
|
369
|
+
}, {
|
|
370
|
+
key: 'calculateRange',
|
|
371
|
+
debug: () => this.options.debug
|
|
360
372
|
});
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
373
|
+
this.getIndexes = memo(() => [this.options.rangeExtractor, this.calculateRange(), this.options.overscan, this.options.count], (rangeExtractor, range, overscan, count) => {
|
|
374
|
+
return rangeExtractor({ ...range,
|
|
375
|
+
overscan,
|
|
376
|
+
count: count
|
|
377
|
+
});
|
|
378
|
+
}, {
|
|
379
|
+
key: 'getIndexes'
|
|
380
|
+
});
|
|
381
|
+
this.getVirtualItems = memo(() => [this.getIndexes(), this.getMeasurements(), this.options.measureElement], (indexes, measurements, measureElement) => {
|
|
382
|
+
const virtualItems = [];
|
|
383
|
+
|
|
384
|
+
for (let k = 0, len = indexes.length; k < len; k++) {
|
|
385
|
+
const i = indexes[k];
|
|
386
|
+
const measurement = measurements[i];
|
|
387
|
+
const item = { ...measurement,
|
|
388
|
+
measureElement: measurableItem => {
|
|
389
|
+
if (measurableItem) {
|
|
390
|
+
const measuredItemSize = measureElement(measurableItem, this);
|
|
391
|
+
|
|
392
|
+
if (measuredItemSize !== item.size) {
|
|
393
|
+
if (item.start < this.scrollOffset) {
|
|
394
|
+
if (this.options.debug) console.info('correction', measuredItemSize - item.size);
|
|
395
|
+
|
|
396
|
+
this._scrollToOffset(this.scrollOffset + (measuredItemSize - item.size), false);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
this.pendingMeasuredCacheIndexes.push(i);
|
|
400
|
+
this.itemMeasurementsCache = { ...this.itemMeasurementsCache,
|
|
401
|
+
[item.key]: measuredItemSize
|
|
402
|
+
};
|
|
403
|
+
this.notify();
|
|
381
404
|
}
|
|
382
|
-
|
|
383
|
-
pendingMeasuredCacheIndexesRef.current.push(i);
|
|
384
|
-
setMeasuredCache(function (old) {
|
|
385
|
-
var _extends2;
|
|
386
|
-
|
|
387
|
-
return _extends({}, old, (_extends2 = {}, _extends2[item.key] = measuredSize, _extends2));
|
|
388
|
-
});
|
|
389
405
|
}
|
|
390
406
|
}
|
|
407
|
+
};
|
|
408
|
+
virtualItems.push(item);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
return virtualItems;
|
|
412
|
+
}, {
|
|
413
|
+
key: 'getIndexes'
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
this.scrollToOffset = function (toOffset, _temp) {
|
|
417
|
+
let {
|
|
418
|
+
align
|
|
419
|
+
} = _temp === void 0 ? {
|
|
420
|
+
align: 'start'
|
|
421
|
+
} : _temp;
|
|
422
|
+
const offset = _this.scrollOffset;
|
|
423
|
+
|
|
424
|
+
const size = _this.getSize();
|
|
425
|
+
|
|
426
|
+
if (align === 'auto') {
|
|
427
|
+
if (toOffset <= offset) {
|
|
428
|
+
align = 'start';
|
|
429
|
+
} else if (toOffset >= offset + size) {
|
|
430
|
+
align = 'end';
|
|
431
|
+
} else {
|
|
432
|
+
align = 'start';
|
|
391
433
|
}
|
|
392
|
-
}
|
|
434
|
+
}
|
|
393
435
|
|
|
394
|
-
|
|
436
|
+
if (align === 'start') {
|
|
437
|
+
_this._scrollToOffset(toOffset, true);
|
|
438
|
+
} else if (align === 'end') {
|
|
439
|
+
_this._scrollToOffset(toOffset - size, true);
|
|
440
|
+
} else if (align === 'center') {
|
|
441
|
+
_this._scrollToOffset(toOffset - size / 2, true);
|
|
442
|
+
}
|
|
395
443
|
};
|
|
396
444
|
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
445
|
+
this.tryScrollToIndex = function (index, _temp2) {
|
|
446
|
+
let {
|
|
447
|
+
align,
|
|
448
|
+
...rest
|
|
449
|
+
} = _temp2 === void 0 ? {
|
|
450
|
+
align: 'auto'
|
|
451
|
+
} : _temp2;
|
|
400
452
|
|
|
401
|
-
|
|
402
|
-
}, [indexes, defaultScrollToFn, horizontal, measurements]);
|
|
403
|
-
var mountedRef = React__namespace.useRef(false);
|
|
404
|
-
useIsomorphicLayoutEffect(function () {
|
|
405
|
-
if (mountedRef.current) {
|
|
406
|
-
setMeasuredCache({});
|
|
407
|
-
}
|
|
453
|
+
const measurements = _this.getMeasurements();
|
|
408
454
|
|
|
409
|
-
|
|
410
|
-
}, [estimateSize]);
|
|
411
|
-
var scrollToOffset = React__namespace.useCallback(function (toOffset, _temp) {
|
|
412
|
-
var _ref2 = _temp === void 0 ? {
|
|
413
|
-
align: 'start'
|
|
414
|
-
} : _temp,
|
|
415
|
-
align = _ref2.align;
|
|
416
|
-
|
|
417
|
-
var _latestRef$current = latestRef.current,
|
|
418
|
-
scrollOffset = _latestRef$current.scrollOffset,
|
|
419
|
-
outerSize = _latestRef$current.outerSize;
|
|
420
|
-
|
|
421
|
-
if (align === 'auto') {
|
|
422
|
-
if (toOffset <= scrollOffset) {
|
|
423
|
-
align = 'start';
|
|
424
|
-
} else if (toOffset >= scrollOffset + outerSize) {
|
|
425
|
-
align = 'end';
|
|
426
|
-
} else {
|
|
427
|
-
align = 'start';
|
|
428
|
-
}
|
|
429
|
-
}
|
|
455
|
+
const offset = _this.scrollOffset;
|
|
430
456
|
|
|
431
|
-
|
|
432
|
-
scrollTo(toOffset);
|
|
433
|
-
} else if (align === 'end') {
|
|
434
|
-
scrollTo(toOffset - outerSize);
|
|
435
|
-
} else if (align === 'center') {
|
|
436
|
-
scrollTo(toOffset - outerSize / 2);
|
|
437
|
-
}
|
|
438
|
-
}, [scrollTo]);
|
|
439
|
-
var tryScrollToIndex = React__namespace.useCallback(function (index, _temp2) {
|
|
440
|
-
var _ref3 = _temp2 === void 0 ? {
|
|
441
|
-
align: 'auto'
|
|
442
|
-
} : _temp2,
|
|
443
|
-
align = _ref3.align,
|
|
444
|
-
rest = _objectWithoutPropertiesLoose(_ref3, _excluded);
|
|
445
|
-
|
|
446
|
-
var _latestRef$current2 = latestRef.current,
|
|
447
|
-
measurements = _latestRef$current2.measurements,
|
|
448
|
-
scrollOffset = _latestRef$current2.scrollOffset,
|
|
449
|
-
outerSize = _latestRef$current2.outerSize;
|
|
450
|
-
var measurement = measurements[Math.max(0, Math.min(index, size - 1))];
|
|
451
|
-
|
|
452
|
-
if (!measurement) {
|
|
453
|
-
return;
|
|
454
|
-
}
|
|
457
|
+
const size = _this.getSize();
|
|
455
458
|
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
459
|
+
const {
|
|
460
|
+
count
|
|
461
|
+
} = _this.options;
|
|
462
|
+
const measurement = measurements[Math.max(0, Math.min(index, count - 1))];
|
|
463
|
+
|
|
464
|
+
if (!measurement) {
|
|
462
465
|
return;
|
|
463
466
|
}
|
|
464
|
-
}
|
|
465
467
|
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
468
|
+
if (align === 'auto') {
|
|
469
|
+
if (measurement.end >= offset + size) {
|
|
470
|
+
align = 'end';
|
|
471
|
+
} else if (measurement.start <= offset) {
|
|
472
|
+
align = 'start';
|
|
473
|
+
} else {
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
const toOffset = align === 'center' ? measurement.start + measurement.size / 2 : align === 'end' ? measurement.end : measurement.start;
|
|
479
|
+
|
|
480
|
+
_this.scrollToOffset(toOffset, {
|
|
481
|
+
align,
|
|
482
|
+
...rest
|
|
483
|
+
});
|
|
484
|
+
};
|
|
485
|
+
|
|
486
|
+
this.scrollToIndex = (index, options) => {
|
|
487
|
+
// We do a double request here because of
|
|
488
|
+
// dynamic sizes which can cause offset shift
|
|
489
|
+
// and end up in the wrong spot. Unfortunately,
|
|
490
|
+
// we can't know about those dynamic sizes until
|
|
491
|
+
// we try and render them. So double down!
|
|
492
|
+
this.tryScrollToIndex(index, options);
|
|
493
|
+
requestAnimationFrame(() => {
|
|
494
|
+
this.tryScrollToIndex(index, options);
|
|
495
|
+
});
|
|
496
|
+
};
|
|
497
|
+
|
|
498
|
+
this.getTotalSize = () => {
|
|
499
|
+
var _this$getMeasurements;
|
|
500
|
+
|
|
501
|
+
return (((_this$getMeasurements = this.getMeasurements()[this.options.count - 1]) == null ? void 0 : _this$getMeasurements.end) || this.options.paddingStart) + this.options.paddingEnd;
|
|
502
|
+
};
|
|
503
|
+
|
|
504
|
+
this.getSizeKey = () => this.options.horizontal ? 'width' : 'height';
|
|
505
|
+
|
|
506
|
+
this._scrollToOffset = (offset, canSmooth) => {
|
|
507
|
+
this.options.scrollToFn(offset, this.options.enableSmoothScroll && canSmooth, this);
|
|
508
|
+
};
|
|
509
|
+
|
|
510
|
+
this.getEstimateSizeFn = memo(() => [this.options.estimateSize], d => d, {
|
|
511
|
+
key: false,
|
|
512
|
+
onChange: () => {
|
|
513
|
+
this.itemMeasurementsCache = {};
|
|
514
|
+
}
|
|
480
515
|
});
|
|
481
|
-
}, [tryScrollToIndex]);
|
|
482
|
-
return {
|
|
483
|
-
virtualItems: virtualItems,
|
|
484
|
-
totalSize: totalSize,
|
|
485
|
-
scrollToOffset: scrollToOffset,
|
|
486
|
-
scrollToIndex: scrollToIndex,
|
|
487
|
-
measure: measure
|
|
488
|
-
};
|
|
489
|
-
};
|
|
490
516
|
|
|
491
|
-
|
|
517
|
+
this.measure = () => {
|
|
518
|
+
this.itemMeasurementsCache = {};
|
|
519
|
+
this.notify();
|
|
520
|
+
};
|
|
521
|
+
|
|
522
|
+
this.setOptions(_opts);
|
|
523
|
+
this.scrollRect = this.options.initialRect;
|
|
524
|
+
this.scrollOffset = this.options.initialOffset;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
const findNearestBinarySearch = (low, high, getCurrentValue, value) => {
|
|
492
530
|
while (low <= high) {
|
|
493
|
-
|
|
494
|
-
|
|
531
|
+
const middle = (low + high) / 2 | 0;
|
|
532
|
+
const currentValue = getCurrentValue(middle);
|
|
495
533
|
|
|
496
534
|
if (currentValue < value) {
|
|
497
535
|
low = middle + 1;
|
|
@@ -509,31 +547,81 @@
|
|
|
509
547
|
}
|
|
510
548
|
};
|
|
511
549
|
|
|
512
|
-
function calculateRange(
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
550
|
+
function calculateRange(_ref2) {
|
|
551
|
+
let {
|
|
552
|
+
measurements,
|
|
553
|
+
outerSize,
|
|
554
|
+
scrollOffset
|
|
555
|
+
} = _ref2;
|
|
556
|
+
const count = measurements.length - 1;
|
|
517
557
|
|
|
518
|
-
|
|
519
|
-
return measurements[index].start;
|
|
520
|
-
};
|
|
558
|
+
const getOffset = index => measurements[index].start;
|
|
521
559
|
|
|
522
|
-
|
|
523
|
-
|
|
560
|
+
const startIndex = findNearestBinarySearch(0, count, getOffset, scrollOffset);
|
|
561
|
+
let endIndex = startIndex;
|
|
524
562
|
|
|
525
|
-
while (
|
|
526
|
-
|
|
563
|
+
while (endIndex < count && measurements[endIndex].end < scrollOffset + outerSize) {
|
|
564
|
+
endIndex++;
|
|
527
565
|
}
|
|
528
566
|
|
|
529
567
|
return {
|
|
530
|
-
|
|
531
|
-
|
|
568
|
+
startIndex,
|
|
569
|
+
endIndex
|
|
532
570
|
};
|
|
533
571
|
}
|
|
534
572
|
|
|
573
|
+
const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? React__namespace.useLayoutEffect : React__namespace.useEffect;
|
|
574
|
+
|
|
575
|
+
function useVirtualBase(options) {
|
|
576
|
+
const rerender = React__namespace.useReducer(() => ({}), {})[1];
|
|
577
|
+
const resolvedOptions = { ...options,
|
|
578
|
+
onChange: instance => {
|
|
579
|
+
rerender();
|
|
580
|
+
options.onChange == null ? void 0 : options.onChange(instance);
|
|
581
|
+
}
|
|
582
|
+
};
|
|
583
|
+
const [instance] = React__namespace.useState(() => new Virtualizer(resolvedOptions));
|
|
584
|
+
instance.setOptions(resolvedOptions);
|
|
585
|
+
React__namespace.useEffect(() => {
|
|
586
|
+
return instance._didMount();
|
|
587
|
+
}, []);
|
|
588
|
+
useIsomorphicLayoutEffect(() => {
|
|
589
|
+
return instance._willUpdate();
|
|
590
|
+
});
|
|
591
|
+
return instance;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
function useVirtual(options) {
|
|
595
|
+
return useVirtualBase({
|
|
596
|
+
observeElementRect: observeElementRect,
|
|
597
|
+
observeElementOffset: observeElementOffset,
|
|
598
|
+
scrollToFn: elementScroll,
|
|
599
|
+
...options
|
|
600
|
+
});
|
|
601
|
+
}
|
|
602
|
+
function useVirtualWindow(options) {
|
|
603
|
+
return useVirtualBase({
|
|
604
|
+
observeElementRect: observeWindowRect,
|
|
605
|
+
observeElementOffset: observeWindowOffset,
|
|
606
|
+
scrollToFn: windowScroll,
|
|
607
|
+
...options
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
exports.Virtualizer = Virtualizer;
|
|
612
|
+
exports.defaultEstimateSize = defaultEstimateSize;
|
|
613
|
+
exports.defaultKeyExtractor = defaultKeyExtractor;
|
|
614
|
+
exports.defaultMeasureElement = defaultMeasureElement;
|
|
535
615
|
exports.defaultRangeExtractor = defaultRangeExtractor;
|
|
616
|
+
exports.elementScroll = elementScroll;
|
|
617
|
+
exports.memo = memo;
|
|
618
|
+
exports.observeElementOffset = observeElementOffset;
|
|
619
|
+
exports.observeElementRect = observeElementRect;
|
|
620
|
+
exports.observeWindowOffset = observeWindowOffset;
|
|
621
|
+
exports.observeWindowRect = observeWindowRect;
|
|
536
622
|
exports.useVirtual = useVirtual;
|
|
623
|
+
exports.useVirtualWindow = useVirtualWindow;
|
|
624
|
+
exports.windowScroll = windowScroll;
|
|
537
625
|
|
|
538
626
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
539
627
|
|