@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
|
@@ -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,425 @@
|
|
|
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
|
-
return function () {
|
|
200
|
-
observer.unobserve();
|
|
201
|
-
};
|
|
202
|
-
}, [element]);
|
|
203
|
-
return rect;
|
|
204
|
-
};
|
|
158
|
+
//
|
|
159
|
+
const defaultKeyExtractor = index => index;
|
|
160
|
+
const defaultRangeExtractor = range => {
|
|
161
|
+
const start = Math.max(range.startIndex - range.overscan, 0);
|
|
162
|
+
const end = Math.min(range.endIndex + range.overscan, range.count - 1);
|
|
163
|
+
const arr = [];
|
|
205
164
|
|
|
206
|
-
|
|
165
|
+
for (let i = start; i <= end; i++) {
|
|
166
|
+
arr.push(i);
|
|
167
|
+
}
|
|
207
168
|
|
|
208
|
-
|
|
209
|
-
return 50;
|
|
169
|
+
return arr;
|
|
210
170
|
};
|
|
171
|
+
const observeElementRect = (instance, cb) => {
|
|
172
|
+
const observer = observeRect(instance.scrollElement, rect => {
|
|
173
|
+
cb(rect);
|
|
174
|
+
});
|
|
211
175
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
176
|
+
if (!instance.scrollElement) {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
215
179
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
return
|
|
180
|
+
cb(instance.scrollElement.getBoundingClientRect());
|
|
181
|
+
observer.observe();
|
|
182
|
+
return () => {
|
|
183
|
+
observer.unobserve();
|
|
184
|
+
};
|
|
219
185
|
};
|
|
186
|
+
const observeWindowRect = (instance, cb) => {
|
|
187
|
+
const onResize = () => {
|
|
188
|
+
cb({
|
|
189
|
+
width: instance.scrollElement.innerWidth,
|
|
190
|
+
height: instance.scrollElement.innerHeight
|
|
191
|
+
});
|
|
192
|
+
};
|
|
220
193
|
|
|
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);
|
|
194
|
+
if (!instance.scrollElement) {
|
|
195
|
+
return;
|
|
228
196
|
}
|
|
229
197
|
|
|
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
|
|
198
|
+
onResize();
|
|
199
|
+
instance.scrollElement.addEventListener('resize', onResize, {
|
|
200
|
+
capture: false,
|
|
201
|
+
passive: true
|
|
266
202
|
});
|
|
203
|
+
return () => {
|
|
204
|
+
instance.scrollElement.removeEventListener('resize', onResize);
|
|
205
|
+
};
|
|
206
|
+
};
|
|
207
|
+
const observeElementOffset = (instance, cb) => {
|
|
208
|
+
const onScroll = () => cb(instance.scrollElement[instance.options.horizontal ? 'scrollLeft' : 'scrollTop']);
|
|
267
209
|
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
latestRef.current.scrollOffset = scrollOffset;
|
|
273
|
-
var useMeasureParent = useObserver || useRect;
|
|
210
|
+
if (!instance.scrollElement) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
274
213
|
|
|
275
|
-
|
|
276
|
-
|
|
214
|
+
onScroll();
|
|
215
|
+
instance.scrollElement.addEventListener('scroll', onScroll, {
|
|
216
|
+
capture: false,
|
|
217
|
+
passive: true
|
|
218
|
+
});
|
|
219
|
+
return () => {
|
|
220
|
+
instance.scrollElement.removeEventListener('scroll', onScroll);
|
|
221
|
+
};
|
|
222
|
+
};
|
|
223
|
+
const observeWindowOffset = (instance, cb) => {
|
|
224
|
+
const onScroll = () => cb(instance.scrollElement[instance.options.horizontal ? 'scrollX' : 'scrollY']);
|
|
277
225
|
|
|
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);
|
|
226
|
+
if (!instance.scrollElement) {
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
301
229
|
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
230
|
+
onScroll();
|
|
231
|
+
instance.scrollElement.addEventListener('scroll', onScroll, {
|
|
232
|
+
capture: false,
|
|
233
|
+
passive: true
|
|
234
|
+
});
|
|
235
|
+
return () => {
|
|
236
|
+
instance.scrollElement.removeEventListener('scroll', onScroll);
|
|
237
|
+
};
|
|
238
|
+
};
|
|
239
|
+
const measureElement = (element, instance) => {
|
|
240
|
+
return element.getBoundingClientRect()[instance.options.horizontal ? 'width' : 'height'];
|
|
241
|
+
};
|
|
242
|
+
const windowScroll = (offset, canSmooth, instance) => {
|
|
243
|
+
var _instance$scrollEleme;
|
|
244
|
+
(_instance$scrollEleme = instance.scrollElement) == null ? void 0 : _instance$scrollEleme.scrollTo({
|
|
245
|
+
[instance.options.horizontal ? 'left' : 'top']: offset,
|
|
246
|
+
behavior: canSmooth ? 'smooth' : undefined
|
|
247
|
+
});
|
|
248
|
+
};
|
|
249
|
+
const elementScroll = (offset, canSmooth, instance) => {
|
|
250
|
+
var _instance$scrollEleme2;
|
|
251
|
+
(_instance$scrollEleme2 = instance.scrollElement) == null ? void 0 : _instance$scrollEleme2.scrollTo({
|
|
252
|
+
[instance.options.horizontal ? 'left' : 'top']: offset,
|
|
253
|
+
behavior: canSmooth ? 'smooth' : undefined
|
|
254
|
+
});
|
|
255
|
+
};
|
|
256
|
+
class Virtualizer {
|
|
257
|
+
constructor(_opts) {
|
|
258
|
+
var _this = this;
|
|
259
|
+
|
|
260
|
+
this.unsubs = [];
|
|
261
|
+
this.scrollElement = null;
|
|
262
|
+
this.measurementsCache = [];
|
|
263
|
+
this.itemMeasurementsCache = {};
|
|
264
|
+
this.pendingMeasuredCacheIndexes = [];
|
|
265
|
+
|
|
266
|
+
this.setOptions = opts => {
|
|
267
|
+
Object.entries(opts).forEach(_ref => {
|
|
268
|
+
let [key, value] = _ref;
|
|
269
|
+
if (typeof value === 'undefined') delete opts[key];
|
|
270
|
+
});
|
|
271
|
+
this.options = {
|
|
272
|
+
debug: false,
|
|
273
|
+
initialOffset: 0,
|
|
274
|
+
overscan: 1,
|
|
275
|
+
paddingStart: 0,
|
|
276
|
+
paddingEnd: 0,
|
|
277
|
+
horizontal: false,
|
|
278
|
+
getItemKey: defaultKeyExtractor,
|
|
279
|
+
rangeExtractor: defaultRangeExtractor,
|
|
280
|
+
enableSmoothScroll: false,
|
|
281
|
+
onChange: () => {},
|
|
282
|
+
measureElement,
|
|
283
|
+
initialRect: {
|
|
284
|
+
width: 0,
|
|
285
|
+
height: 0
|
|
286
|
+
},
|
|
287
|
+
...opts
|
|
288
|
+
};
|
|
289
|
+
};
|
|
305
290
|
|
|
306
|
-
|
|
291
|
+
this.notify = () => {
|
|
292
|
+
var _this$options$onChang, _this$options;
|
|
307
293
|
|
|
308
|
-
|
|
294
|
+
(_this$options$onChang = (_this$options = this.options).onChange) == null ? void 0 : _this$options$onChang.call(_this$options, this);
|
|
295
|
+
};
|
|
309
296
|
|
|
310
|
-
|
|
297
|
+
this.cleanup = () => {
|
|
298
|
+
this.unsubs.filter(Boolean).forEach(d => d());
|
|
299
|
+
this.unsubs = [];
|
|
300
|
+
};
|
|
311
301
|
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
size: _size,
|
|
316
|
-
end: _end,
|
|
317
|
-
key: key
|
|
302
|
+
this._didMount = () => {
|
|
303
|
+
return () => {
|
|
304
|
+
this.cleanup();
|
|
318
305
|
};
|
|
319
|
-
}
|
|
306
|
+
};
|
|
320
307
|
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
308
|
+
this._willUpdate = () => {
|
|
309
|
+
const scrollElement = this.options.getScrollElement();
|
|
310
|
+
|
|
311
|
+
if (this.scrollElement !== scrollElement) {
|
|
312
|
+
this.cleanup();
|
|
313
|
+
this.scrollElement = scrollElement;
|
|
314
|
+
this.unsubs.push(this.options.observeElementRect(this, rect => {
|
|
315
|
+
this.scrollRect = rect;
|
|
316
|
+
this.notify();
|
|
317
|
+
}));
|
|
318
|
+
this.unsubs.push(this.options.observeElementOffset(this, offset => {
|
|
319
|
+
this.scrollOffset = offset;
|
|
320
|
+
this.notify();
|
|
321
|
+
}));
|
|
322
|
+
}
|
|
323
|
+
};
|
|
334
324
|
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
setScrollOffset(offset);
|
|
325
|
+
this.getSize = () => {
|
|
326
|
+
return this.scrollRect[this.options.horizontal ? 'width' : 'height'];
|
|
338
327
|
};
|
|
339
328
|
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
329
|
+
this.getMeasurements = memo(() => [this.options.count, this.options.paddingStart, this.options.getItemKey, this.itemMeasurementsCache], (count, paddingStart, getItemKey, measurementsCache) => {
|
|
330
|
+
const min = this.pendingMeasuredCacheIndexes.length > 0 ? Math.min(...this.pendingMeasuredCacheIndexes) : 0;
|
|
331
|
+
this.pendingMeasuredCacheIndexes = [];
|
|
332
|
+
const measurements = this.measurementsCache.slice(0, min);
|
|
333
|
+
|
|
334
|
+
for (let i = min; i < count; i++) {
|
|
335
|
+
const key = getItemKey(i);
|
|
336
|
+
const measuredSize = measurementsCache[key];
|
|
337
|
+
const start = measurements[i - 1] ? measurements[i - 1].end : paddingStart;
|
|
338
|
+
const size = typeof measuredSize === 'number' ? measuredSize : this.options.estimateSize(i);
|
|
339
|
+
const end = start + size;
|
|
340
|
+
measurements[i] = {
|
|
341
|
+
index: i,
|
|
342
|
+
start,
|
|
343
|
+
size,
|
|
344
|
+
end,
|
|
345
|
+
key
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
this.measurementsCache = measurements;
|
|
350
|
+
return measurements;
|
|
351
|
+
}, {
|
|
352
|
+
key: 'getMeasurements',
|
|
353
|
+
debug: () => this.options.debug
|
|
344
354
|
});
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
355
|
+
this.calculateRange = memo(() => [this.getMeasurements(), this.getSize(), this.scrollOffset], (measurements, outerSize, scrollOffset) => {
|
|
356
|
+
return calculateRange({
|
|
357
|
+
measurements,
|
|
358
|
+
outerSize,
|
|
359
|
+
scrollOffset
|
|
360
|
+
});
|
|
361
|
+
}, {
|
|
362
|
+
key: 'calculateRange',
|
|
363
|
+
debug: () => this.options.debug
|
|
364
|
+
});
|
|
365
|
+
this.getIndexes = memo(() => [this.options.rangeExtractor, this.calculateRange(), this.options.overscan, this.options.count], (rangeExtractor, range, overscan, count) => {
|
|
366
|
+
return rangeExtractor({ ...range,
|
|
367
|
+
overscan,
|
|
368
|
+
count: count
|
|
369
|
+
});
|
|
370
|
+
}, {
|
|
371
|
+
key: 'getIndexes'
|
|
360
372
|
});
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
373
|
+
this.getVirtualItems = memo(() => [this.getIndexes(), this.getMeasurements(), this.options.measureElement], (indexes, measurements, measureElement) => {
|
|
374
|
+
const virtualItems = [];
|
|
375
|
+
|
|
376
|
+
for (let k = 0, len = indexes.length; k < len; k++) {
|
|
377
|
+
const i = indexes[k];
|
|
378
|
+
const measurement = measurements[i];
|
|
379
|
+
const item = { ...measurement,
|
|
380
|
+
measureElement: measurableItem => {
|
|
381
|
+
if (measurableItem) {
|
|
382
|
+
const measuredItemSize = measureElement(measurableItem, this);
|
|
383
|
+
|
|
384
|
+
if (measuredItemSize !== item.size) {
|
|
385
|
+
if (item.start < this.scrollOffset) {
|
|
386
|
+
if (this.options.debug) console.info('correction', measuredItemSize - item.size);
|
|
387
|
+
|
|
388
|
+
if (!this.destinationOffset) {
|
|
389
|
+
this._scrollToOffset(this.scrollOffset + (measuredItemSize - item.size), false);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
this.pendingMeasuredCacheIndexes.push(i);
|
|
394
|
+
this.itemMeasurementsCache = { ...this.itemMeasurementsCache,
|
|
395
|
+
[item.key]: measuredItemSize
|
|
396
|
+
};
|
|
397
|
+
this.notify();
|
|
381
398
|
}
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
};
|
|
402
|
+
virtualItems.push(item);
|
|
403
|
+
}
|
|
382
404
|
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
405
|
+
return virtualItems;
|
|
406
|
+
}, {
|
|
407
|
+
key: 'getIndexes'
|
|
408
|
+
});
|
|
386
409
|
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
410
|
+
this.scrollToOffset = function (toOffset, _temp) {
|
|
411
|
+
let {
|
|
412
|
+
align
|
|
413
|
+
} = _temp === void 0 ? {
|
|
414
|
+
align: 'start'
|
|
415
|
+
} : _temp;
|
|
416
|
+
|
|
417
|
+
const attempt = () => {
|
|
418
|
+
const offset = _this.scrollOffset;
|
|
419
|
+
|
|
420
|
+
const size = _this.getSize();
|
|
421
|
+
|
|
422
|
+
if (align === 'auto') {
|
|
423
|
+
if (toOffset <= offset) {
|
|
424
|
+
align = 'start';
|
|
425
|
+
} else if (toOffset >= offset + size) {
|
|
426
|
+
align = 'end';
|
|
427
|
+
} else {
|
|
428
|
+
align = 'start';
|
|
390
429
|
}
|
|
391
430
|
}
|
|
392
|
-
});
|
|
393
431
|
|
|
394
|
-
|
|
432
|
+
if (align === 'start') {
|
|
433
|
+
_this._scrollToOffset(toOffset, true);
|
|
434
|
+
} else if (align === 'end') {
|
|
435
|
+
_this._scrollToOffset(toOffset - size, true);
|
|
436
|
+
} else if (align === 'center') {
|
|
437
|
+
_this._scrollToOffset(toOffset - size / 2, true);
|
|
438
|
+
}
|
|
439
|
+
};
|
|
440
|
+
|
|
441
|
+
attempt();
|
|
442
|
+
requestAnimationFrame(() => {
|
|
443
|
+
attempt();
|
|
444
|
+
});
|
|
395
445
|
};
|
|
396
446
|
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
447
|
+
this.scrollToIndex = function (index, _temp2) {
|
|
448
|
+
let {
|
|
449
|
+
align,
|
|
450
|
+
...rest
|
|
451
|
+
} = _temp2 === void 0 ? {
|
|
452
|
+
align: 'auto'
|
|
453
|
+
} : _temp2;
|
|
400
454
|
|
|
401
|
-
|
|
402
|
-
}, [indexes, defaultScrollToFn, horizontal, measurements]);
|
|
403
|
-
var mountedRef = React__namespace.useRef(false);
|
|
404
|
-
useIsomorphicLayoutEffect(function () {
|
|
405
|
-
if (mountedRef.current) {
|
|
406
|
-
setMeasuredCache({});
|
|
407
|
-
}
|
|
455
|
+
const measurements = _this.getMeasurements();
|
|
408
456
|
|
|
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
|
-
}
|
|
457
|
+
const offset = _this.scrollOffset;
|
|
430
458
|
|
|
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
|
-
}
|
|
459
|
+
const size = _this.getSize();
|
|
455
460
|
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
461
|
+
const {
|
|
462
|
+
count
|
|
463
|
+
} = _this.options;
|
|
464
|
+
const measurement = measurements[Math.max(0, Math.min(index, count - 1))];
|
|
465
|
+
|
|
466
|
+
if (!measurement) {
|
|
462
467
|
return;
|
|
463
468
|
}
|
|
464
|
-
}
|
|
465
469
|
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
// we can't know about those dynamic sizes until
|
|
476
|
-
// we try and render them. So double down!
|
|
477
|
-
tryScrollToIndex(index, options);
|
|
478
|
-
requestAnimationFrame(function () {
|
|
479
|
-
tryScrollToIndex(index, options);
|
|
480
|
-
});
|
|
481
|
-
}, [tryScrollToIndex]);
|
|
482
|
-
return {
|
|
483
|
-
virtualItems: virtualItems,
|
|
484
|
-
totalSize: totalSize,
|
|
485
|
-
scrollToOffset: scrollToOffset,
|
|
486
|
-
scrollToIndex: scrollToIndex,
|
|
487
|
-
measure: measure
|
|
488
|
-
};
|
|
489
|
-
};
|
|
470
|
+
if (align === 'auto') {
|
|
471
|
+
if (measurement.end >= offset + size) {
|
|
472
|
+
align = 'end';
|
|
473
|
+
} else if (measurement.start <= offset) {
|
|
474
|
+
align = 'start';
|
|
475
|
+
} else {
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
490
479
|
|
|
491
|
-
|
|
480
|
+
const toOffset = align === 'center' ? measurement.start + measurement.size / 2 : align === 'end' ? measurement.end : measurement.start;
|
|
481
|
+
|
|
482
|
+
_this.scrollToOffset(toOffset, {
|
|
483
|
+
align,
|
|
484
|
+
...rest
|
|
485
|
+
});
|
|
486
|
+
};
|
|
487
|
+
|
|
488
|
+
this.getTotalSize = () => {
|
|
489
|
+
var _this$getMeasurements;
|
|
490
|
+
|
|
491
|
+
return (((_this$getMeasurements = this.getMeasurements()[this.options.count - 1]) == null ? void 0 : _this$getMeasurements.end) || this.options.paddingStart) + this.options.paddingEnd;
|
|
492
|
+
};
|
|
493
|
+
|
|
494
|
+
this._scrollToOffset = (offset, canSmooth) => {
|
|
495
|
+
clearTimeout(this.scrollCheckFrame);
|
|
496
|
+
this.destinationOffset = offset;
|
|
497
|
+
this.options.scrollToFn(offset, canSmooth, this);
|
|
498
|
+
let scrollCheckFrame;
|
|
499
|
+
|
|
500
|
+
const check = () => {
|
|
501
|
+
let lastOffset = this.scrollOffset;
|
|
502
|
+
this.scrollCheckFrame = scrollCheckFrame = setTimeout(() => {
|
|
503
|
+
if (this.scrollCheckFrame !== scrollCheckFrame) {
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
if (this.scrollOffset === lastOffset) {
|
|
508
|
+
this.destinationOffset = undefined;
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
lastOffset = this.scrollOffset;
|
|
513
|
+
check();
|
|
514
|
+
}, 100);
|
|
515
|
+
};
|
|
516
|
+
|
|
517
|
+
check();
|
|
518
|
+
};
|
|
519
|
+
|
|
520
|
+
this.measure = () => {
|
|
521
|
+
this.itemMeasurementsCache = {};
|
|
522
|
+
this.notify();
|
|
523
|
+
};
|
|
524
|
+
|
|
525
|
+
this.setOptions(_opts);
|
|
526
|
+
this.scrollRect = this.options.initialRect;
|
|
527
|
+
this.scrollOffset = this.options.initialOffset;
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
const findNearestBinarySearch = (low, high, getCurrentValue, value) => {
|
|
492
533
|
while (low <= high) {
|
|
493
|
-
|
|
494
|
-
|
|
534
|
+
const middle = (low + high) / 2 | 0;
|
|
535
|
+
const currentValue = getCurrentValue(middle);
|
|
495
536
|
|
|
496
537
|
if (currentValue < value) {
|
|
497
538
|
low = middle + 1;
|
|
@@ -509,31 +550,81 @@
|
|
|
509
550
|
}
|
|
510
551
|
};
|
|
511
552
|
|
|
512
|
-
function calculateRange(
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
553
|
+
function calculateRange(_ref2) {
|
|
554
|
+
let {
|
|
555
|
+
measurements,
|
|
556
|
+
outerSize,
|
|
557
|
+
scrollOffset
|
|
558
|
+
} = _ref2;
|
|
559
|
+
const count = measurements.length - 1;
|
|
517
560
|
|
|
518
|
-
|
|
519
|
-
return measurements[index].start;
|
|
520
|
-
};
|
|
561
|
+
const getOffset = index => measurements[index].start;
|
|
521
562
|
|
|
522
|
-
|
|
523
|
-
|
|
563
|
+
const startIndex = findNearestBinarySearch(0, count, getOffset, scrollOffset);
|
|
564
|
+
let endIndex = startIndex;
|
|
524
565
|
|
|
525
|
-
while (
|
|
526
|
-
|
|
566
|
+
while (endIndex < count && measurements[endIndex].end < scrollOffset + outerSize) {
|
|
567
|
+
endIndex++;
|
|
527
568
|
}
|
|
528
569
|
|
|
529
570
|
return {
|
|
530
|
-
|
|
531
|
-
|
|
571
|
+
startIndex,
|
|
572
|
+
endIndex
|
|
532
573
|
};
|
|
533
574
|
}
|
|
534
575
|
|
|
576
|
+
const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? React__namespace.useLayoutEffect : React__namespace.useEffect;
|
|
577
|
+
|
|
578
|
+
function useVirtualizerBase(options) {
|
|
579
|
+
const rerender = React__namespace.useReducer(() => ({}), {})[1];
|
|
580
|
+
const resolvedOptions = { ...options,
|
|
581
|
+
onChange: instance => {
|
|
582
|
+
rerender();
|
|
583
|
+
options.onChange == null ? void 0 : options.onChange(instance);
|
|
584
|
+
}
|
|
585
|
+
};
|
|
586
|
+
const [instance] = React__namespace.useState(() => new Virtualizer(resolvedOptions));
|
|
587
|
+
instance.setOptions(resolvedOptions);
|
|
588
|
+
React__namespace.useEffect(() => {
|
|
589
|
+
return instance._didMount();
|
|
590
|
+
}, []);
|
|
591
|
+
useIsomorphicLayoutEffect(() => {
|
|
592
|
+
return instance._willUpdate();
|
|
593
|
+
});
|
|
594
|
+
return instance;
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
function useVirtualizer(options) {
|
|
598
|
+
return useVirtualizerBase({
|
|
599
|
+
observeElementRect: observeElementRect,
|
|
600
|
+
observeElementOffset: observeElementOffset,
|
|
601
|
+
scrollToFn: elementScroll,
|
|
602
|
+
...options
|
|
603
|
+
});
|
|
604
|
+
}
|
|
605
|
+
function useWindowVirtualizer(options) {
|
|
606
|
+
return useVirtualizerBase({
|
|
607
|
+
getScrollElement: () => typeof window !== 'undefined' ? window : null,
|
|
608
|
+
observeElementRect: observeWindowRect,
|
|
609
|
+
observeElementOffset: observeWindowOffset,
|
|
610
|
+
scrollToFn: windowScroll,
|
|
611
|
+
...options
|
|
612
|
+
});
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
exports.Virtualizer = Virtualizer;
|
|
616
|
+
exports.defaultKeyExtractor = defaultKeyExtractor;
|
|
535
617
|
exports.defaultRangeExtractor = defaultRangeExtractor;
|
|
536
|
-
exports.
|
|
618
|
+
exports.elementScroll = elementScroll;
|
|
619
|
+
exports.measureElement = measureElement;
|
|
620
|
+
exports.memo = memo;
|
|
621
|
+
exports.observeElementOffset = observeElementOffset;
|
|
622
|
+
exports.observeElementRect = observeElementRect;
|
|
623
|
+
exports.observeWindowOffset = observeWindowOffset;
|
|
624
|
+
exports.observeWindowRect = observeWindowRect;
|
|
625
|
+
exports.useVirtualizer = useVirtualizer;
|
|
626
|
+
exports.useWindowVirtualizer = useWindowVirtualizer;
|
|
627
|
+
exports.windowScroll = windowScroll;
|
|
537
628
|
|
|
538
629
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
539
630
|
|