@tanstack/virtual-core 3.14.0 → 3.16.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/dist/cjs/index.cjs +360 -110
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +38 -5
- package/dist/cjs/lazy-measurements.cjs +34 -0
- package/dist/cjs/lazy-measurements.cjs.map +1 -0
- package/dist/cjs/lazy-measurements.d.cts +4 -0
- package/dist/cjs/utils.cjs +7 -6
- package/dist/cjs/utils.cjs.map +1 -1
- package/dist/esm/index.d.ts +38 -5
- package/dist/esm/index.js +361 -111
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/lazy-measurements.d.ts +4 -0
- package/dist/esm/lazy-measurements.js +34 -0
- package/dist/esm/lazy-measurements.js.map +1 -0
- package/dist/esm/utils.js +7 -6
- package/dist/esm/utils.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +612 -143
- package/src/lazy-measurements.ts +44 -0
- package/src/utils.ts +12 -7
package/dist/cjs/index.cjs
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const lazyMeasurements = require("./lazy-measurements.cjs");
|
|
3
4
|
const utils = require("./utils.cjs");
|
|
5
|
+
let _isIOSResult;
|
|
6
|
+
const isIOSWebKit = () => {
|
|
7
|
+
if (_isIOSResult !== void 0) return _isIOSResult;
|
|
8
|
+
if (typeof navigator === "undefined") return _isIOSResult = false;
|
|
9
|
+
if (/iP(hone|od|ad)/.test(navigator.userAgent)) return _isIOSResult = true;
|
|
10
|
+
const mtp = navigator.maxTouchPoints;
|
|
11
|
+
return _isIOSResult = navigator.platform === "MacIntel" && mtp !== void 0 && mtp > 0;
|
|
12
|
+
};
|
|
13
|
+
const _resetIOSDetectionForTests = () => {
|
|
14
|
+
_isIOSResult = void 0;
|
|
15
|
+
};
|
|
4
16
|
const getRect = (element) => {
|
|
5
17
|
const { offsetWidth, offsetHeight } = element;
|
|
6
18
|
return { width: offsetWidth, height: offsetHeight };
|
|
@@ -9,9 +21,10 @@ const defaultKeyExtractor = (index) => index;
|
|
|
9
21
|
const defaultRangeExtractor = (range) => {
|
|
10
22
|
const start = Math.max(range.startIndex - range.overscan, 0);
|
|
11
23
|
const end = Math.min(range.endIndex + range.overscan, range.count - 1);
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
24
|
+
const len = end - start + 1;
|
|
25
|
+
const arr = new Array(len);
|
|
26
|
+
for (let i = 0; i < len; i++) {
|
|
27
|
+
arr[i] = start + i;
|
|
15
28
|
}
|
|
16
29
|
return arr;
|
|
17
30
|
};
|
|
@@ -70,7 +83,7 @@ const observeWindowRect = (instance, cb) => {
|
|
|
70
83
|
};
|
|
71
84
|
};
|
|
72
85
|
const supportsScrollend = typeof window == "undefined" ? true : "onscrollend" in window;
|
|
73
|
-
const
|
|
86
|
+
const observeOffset = (instance, cb, readOffset) => {
|
|
74
87
|
const element = instance.scrollElement;
|
|
75
88
|
if (!element) {
|
|
76
89
|
return;
|
|
@@ -79,60 +92,21 @@ const observeElementOffset = (instance, cb) => {
|
|
|
79
92
|
if (!targetWindow) {
|
|
80
93
|
return;
|
|
81
94
|
}
|
|
82
|
-
let offset = 0;
|
|
83
|
-
const fallback = instance.options.useScrollendEvent && supportsScrollend ? () => void 0 : utils.debounce(
|
|
84
|
-
targetWindow,
|
|
85
|
-
() => {
|
|
86
|
-
cb(offset, false);
|
|
87
|
-
},
|
|
88
|
-
instance.options.isScrollingResetDelay
|
|
89
|
-
);
|
|
90
|
-
const createHandler = (isScrolling) => () => {
|
|
91
|
-
const { horizontal, isRtl } = instance.options;
|
|
92
|
-
offset = horizontal ? element["scrollLeft"] * (isRtl && -1 || 1) : element["scrollTop"];
|
|
93
|
-
fallback();
|
|
94
|
-
cb(offset, isScrolling);
|
|
95
|
-
};
|
|
96
|
-
const handler = createHandler(true);
|
|
97
|
-
const endHandler = createHandler(false);
|
|
98
|
-
element.addEventListener("scroll", handler, addEventListenerOptions);
|
|
99
95
|
const registerScrollendEvent = instance.options.useScrollendEvent && supportsScrollend;
|
|
100
|
-
if (registerScrollendEvent) {
|
|
101
|
-
element.addEventListener("scrollend", endHandler, addEventListenerOptions);
|
|
102
|
-
}
|
|
103
|
-
return () => {
|
|
104
|
-
element.removeEventListener("scroll", handler);
|
|
105
|
-
if (registerScrollendEvent) {
|
|
106
|
-
element.removeEventListener("scrollend", endHandler);
|
|
107
|
-
}
|
|
108
|
-
};
|
|
109
|
-
};
|
|
110
|
-
const observeWindowOffset = (instance, cb) => {
|
|
111
|
-
const element = instance.scrollElement;
|
|
112
|
-
if (!element) {
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
const targetWindow = instance.targetWindow;
|
|
116
|
-
if (!targetWindow) {
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
96
|
let offset = 0;
|
|
120
|
-
const fallback =
|
|
97
|
+
const fallback = registerScrollendEvent ? null : utils.debounce(
|
|
121
98
|
targetWindow,
|
|
122
|
-
() =>
|
|
123
|
-
cb(offset, false);
|
|
124
|
-
},
|
|
99
|
+
() => cb(offset, false),
|
|
125
100
|
instance.options.isScrollingResetDelay
|
|
126
101
|
);
|
|
127
102
|
const createHandler = (isScrolling) => () => {
|
|
128
|
-
offset = element
|
|
129
|
-
fallback();
|
|
103
|
+
offset = readOffset(element);
|
|
104
|
+
fallback == null ? void 0 : fallback();
|
|
130
105
|
cb(offset, isScrolling);
|
|
131
106
|
};
|
|
132
107
|
const handler = createHandler(true);
|
|
133
108
|
const endHandler = createHandler(false);
|
|
134
109
|
element.addEventListener("scroll", handler, addEventListenerOptions);
|
|
135
|
-
const registerScrollendEvent = instance.options.useScrollendEvent && supportsScrollend;
|
|
136
110
|
if (registerScrollendEvent) {
|
|
137
111
|
element.addEventListener("scrollend", endHandler, addEventListenerOptions);
|
|
138
112
|
}
|
|
@@ -143,6 +117,15 @@ const observeWindowOffset = (instance, cb) => {
|
|
|
143
117
|
}
|
|
144
118
|
};
|
|
145
119
|
};
|
|
120
|
+
const observeElementOffset = (instance, cb) => observeOffset(instance, cb, (el) => {
|
|
121
|
+
const { horizontal, isRtl } = instance.options;
|
|
122
|
+
return horizontal ? el.scrollLeft * (isRtl && -1 || 1) : el.scrollTop;
|
|
123
|
+
});
|
|
124
|
+
const observeWindowOffset = (instance, cb) => observeOffset(
|
|
125
|
+
instance,
|
|
126
|
+
cb,
|
|
127
|
+
(win) => instance.options.horizontal ? win.scrollX : win.scrollY
|
|
128
|
+
);
|
|
146
129
|
const measureElement = (element, entry, instance) => {
|
|
147
130
|
if (entry == null ? void 0 : entry.borderBoxSize) {
|
|
148
131
|
const box = entry.borderBoxSize[0];
|
|
@@ -155,28 +138,18 @@ const measureElement = (element, entry, instance) => {
|
|
|
155
138
|
}
|
|
156
139
|
return element[instance.options.horizontal ? "offsetWidth" : "offsetHeight"];
|
|
157
140
|
};
|
|
158
|
-
const
|
|
159
|
-
adjustments = 0,
|
|
160
|
-
behavior
|
|
161
|
-
}, instance) => {
|
|
162
|
-
var _a, _b;
|
|
163
|
-
const toOffset = offset + adjustments;
|
|
164
|
-
(_b = (_a = instance.scrollElement) == null ? void 0 : _a.scrollTo) == null ? void 0 : _b.call(_a, {
|
|
165
|
-
[instance.options.horizontal ? "left" : "top"]: toOffset,
|
|
166
|
-
behavior
|
|
167
|
-
});
|
|
168
|
-
};
|
|
169
|
-
const elementScroll = (offset, {
|
|
141
|
+
const scrollWithAdjustments = (offset, {
|
|
170
142
|
adjustments = 0,
|
|
171
143
|
behavior
|
|
172
144
|
}, instance) => {
|
|
173
145
|
var _a, _b;
|
|
174
|
-
const toOffset = offset + adjustments;
|
|
175
146
|
(_b = (_a = instance.scrollElement) == null ? void 0 : _a.scrollTo) == null ? void 0 : _b.call(_a, {
|
|
176
|
-
[instance.options.horizontal ? "left" : "top"]:
|
|
147
|
+
[instance.options.horizontal ? "left" : "top"]: offset + adjustments,
|
|
177
148
|
behavior
|
|
178
149
|
});
|
|
179
150
|
};
|
|
151
|
+
const windowScroll = scrollWithAdjustments;
|
|
152
|
+
const elementScroll = scrollWithAdjustments;
|
|
180
153
|
class Virtualizer {
|
|
181
154
|
constructor(opts) {
|
|
182
155
|
this.unsubs = [];
|
|
@@ -185,16 +158,24 @@ class Virtualizer {
|
|
|
185
158
|
this.isScrolling = false;
|
|
186
159
|
this.scrollState = null;
|
|
187
160
|
this.measurementsCache = [];
|
|
161
|
+
this._flatMeasurements = null;
|
|
188
162
|
this.itemSizeCache = /* @__PURE__ */ new Map();
|
|
163
|
+
this.itemSizeCacheVersion = 0;
|
|
189
164
|
this.laneAssignments = /* @__PURE__ */ new Map();
|
|
190
|
-
this.
|
|
165
|
+
this.pendingMin = null;
|
|
191
166
|
this.prevLanes = void 0;
|
|
192
167
|
this.lanesChangedFlag = false;
|
|
193
168
|
this.lanesSettling = false;
|
|
169
|
+
this.pendingScrollAnchor = null;
|
|
194
170
|
this.scrollRect = null;
|
|
195
171
|
this.scrollOffset = null;
|
|
196
172
|
this.scrollDirection = null;
|
|
197
173
|
this.scrollAdjustments = 0;
|
|
174
|
+
this._iosDeferredAdjustment = 0;
|
|
175
|
+
this._iosTouching = false;
|
|
176
|
+
this._iosJustTouchEnded = false;
|
|
177
|
+
this._iosTouchEndTimerId = null;
|
|
178
|
+
this._intendedScrollOffset = null;
|
|
198
179
|
this.elementsCache = /* @__PURE__ */ new Map();
|
|
199
180
|
this.now = () => {
|
|
200
181
|
var _a, _b, _c;
|
|
@@ -216,6 +197,12 @@ class Virtualizer {
|
|
|
216
197
|
const index = this.indexFromElement(node);
|
|
217
198
|
if (!node.isConnected) {
|
|
218
199
|
this.observer.unobserve(node);
|
|
200
|
+
for (const [cacheKey, cachedNode] of this.elementsCache) {
|
|
201
|
+
if (cachedNode === node) {
|
|
202
|
+
this.elementsCache.delete(cacheKey);
|
|
203
|
+
break;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
219
206
|
return;
|
|
220
207
|
}
|
|
221
208
|
if (this.shouldMeasureDuringScroll(index)) {
|
|
@@ -247,10 +234,8 @@ class Virtualizer {
|
|
|
247
234
|
})();
|
|
248
235
|
this.range = null;
|
|
249
236
|
this.setOptions = (opts2) => {
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
});
|
|
253
|
-
this.options = {
|
|
237
|
+
var _a, _b;
|
|
238
|
+
const merged = {
|
|
254
239
|
debug: false,
|
|
255
240
|
initialOffset: 0,
|
|
256
241
|
overscan: 1,
|
|
@@ -270,14 +255,50 @@ class Virtualizer {
|
|
|
270
255
|
indexAttribute: "data-index",
|
|
271
256
|
initialMeasurementsCache: [],
|
|
272
257
|
lanes: 1,
|
|
258
|
+
anchorTo: "start",
|
|
259
|
+
followOnAppend: false,
|
|
260
|
+
scrollEndThreshold: 1,
|
|
273
261
|
isScrollingResetDelay: 150,
|
|
274
262
|
enabled: true,
|
|
275
263
|
isRtl: false,
|
|
276
264
|
useScrollendEvent: false,
|
|
277
265
|
useAnimationFrameWithResizeObserver: false,
|
|
278
|
-
laneAssignmentMode: "estimate"
|
|
279
|
-
...opts2
|
|
266
|
+
laneAssignmentMode: "estimate"
|
|
280
267
|
};
|
|
268
|
+
for (const key in opts2) {
|
|
269
|
+
const v = opts2[key];
|
|
270
|
+
if (v !== void 0) merged[key] = v;
|
|
271
|
+
}
|
|
272
|
+
const prevOptions = this.options;
|
|
273
|
+
let anchor = null;
|
|
274
|
+
let followOnAppend = null;
|
|
275
|
+
if (prevOptions !== void 0 && prevOptions.enabled && merged.enabled && merged.anchorTo === "end" && this.scrollElement !== null) {
|
|
276
|
+
const prevCount = prevOptions.count;
|
|
277
|
+
const nextCount = merged.count;
|
|
278
|
+
const measurements = this.getMeasurements();
|
|
279
|
+
const prevFirstKey = prevCount > 0 ? ((_a = measurements[0]) == null ? void 0 : _a.key) ?? prevOptions.getItemKey(0) : null;
|
|
280
|
+
const prevLastKey = prevCount > 0 ? ((_b = measurements[prevCount - 1]) == null ? void 0 : _b.key) ?? prevOptions.getItemKey(prevCount - 1) : null;
|
|
281
|
+
const didCountChange = nextCount !== prevCount;
|
|
282
|
+
const didEdgeKeysChange = didCountChange || prevCount > 0 && nextCount > 0 && (merged.getItemKey(0) !== prevFirstKey || merged.getItemKey(nextCount - 1) !== prevLastKey);
|
|
283
|
+
if (didEdgeKeysChange) {
|
|
284
|
+
const item = prevCount > 0 ? this.getVirtualItemForOffset(this.getScrollOffset()) ?? measurements[0] : null;
|
|
285
|
+
if (item) {
|
|
286
|
+
anchor = [item.key, this.getScrollOffset() - item.start];
|
|
287
|
+
}
|
|
288
|
+
const behavior = merged.followOnAppend === true ? "auto" : merged.followOnAppend || null;
|
|
289
|
+
if (behavior && nextCount > prevCount && this.isAtEnd(prevOptions.scrollEndThreshold) && (prevCount === 0 || merged.getItemKey(nextCount - 1) !== prevLastKey)) {
|
|
290
|
+
followOnAppend = behavior;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
this.options = merged;
|
|
295
|
+
if (anchor || followOnAppend) {
|
|
296
|
+
this.pendingScrollAnchor = [
|
|
297
|
+
(anchor == null ? void 0 : anchor[0]) ?? null,
|
|
298
|
+
(anchor == null ? void 0 : anchor[1]) ?? 0,
|
|
299
|
+
followOnAppend
|
|
300
|
+
];
|
|
301
|
+
}
|
|
281
302
|
};
|
|
282
303
|
this.notify = (sync) => {
|
|
283
304
|
var _a, _b;
|
|
@@ -348,21 +369,104 @@ class Virtualizer {
|
|
|
348
369
|
);
|
|
349
370
|
this.unsubs.push(
|
|
350
371
|
this.options.observeElementOffset(this, (offset, isScrolling) => {
|
|
372
|
+
if (this._intendedScrollOffset !== null && Math.abs(offset - this._intendedScrollOffset) < 1.5) {
|
|
373
|
+
offset = this._intendedScrollOffset;
|
|
374
|
+
}
|
|
375
|
+
this._intendedScrollOffset = null;
|
|
351
376
|
this.scrollAdjustments = 0;
|
|
352
377
|
this.scrollDirection = isScrolling ? this.getScrollOffset() < offset ? "forward" : "backward" : null;
|
|
353
378
|
this.scrollOffset = offset;
|
|
354
379
|
this.isScrolling = isScrolling;
|
|
380
|
+
this._flushIosDeferredIfReady();
|
|
355
381
|
if (this.scrollState) {
|
|
356
382
|
this.scheduleScrollReconcile();
|
|
357
383
|
}
|
|
358
384
|
this.maybeNotify();
|
|
359
385
|
})
|
|
360
386
|
);
|
|
387
|
+
if ("addEventListener" in this.scrollElement) {
|
|
388
|
+
const scrollEl = this.scrollElement;
|
|
389
|
+
const onTouchStart = () => {
|
|
390
|
+
this._iosTouching = true;
|
|
391
|
+
this._iosJustTouchEnded = false;
|
|
392
|
+
if (this._iosTouchEndTimerId !== null && this.targetWindow != null) {
|
|
393
|
+
this.targetWindow.clearTimeout(this._iosTouchEndTimerId);
|
|
394
|
+
this._iosTouchEndTimerId = null;
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
const onTouchEnd = () => {
|
|
398
|
+
this._iosTouching = false;
|
|
399
|
+
if (!isIOSWebKit() || this.targetWindow == null) {
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
this._iosJustTouchEnded = true;
|
|
403
|
+
this._iosTouchEndTimerId = this.targetWindow.setTimeout(() => {
|
|
404
|
+
this._iosJustTouchEnded = false;
|
|
405
|
+
this._iosTouchEndTimerId = null;
|
|
406
|
+
this._flushIosDeferredIfReady();
|
|
407
|
+
}, 150);
|
|
408
|
+
};
|
|
409
|
+
scrollEl.addEventListener(
|
|
410
|
+
"touchstart",
|
|
411
|
+
onTouchStart,
|
|
412
|
+
addEventListenerOptions
|
|
413
|
+
);
|
|
414
|
+
scrollEl.addEventListener(
|
|
415
|
+
"touchend",
|
|
416
|
+
onTouchEnd,
|
|
417
|
+
addEventListenerOptions
|
|
418
|
+
);
|
|
419
|
+
this.unsubs.push(() => {
|
|
420
|
+
scrollEl.removeEventListener("touchstart", onTouchStart);
|
|
421
|
+
scrollEl.removeEventListener("touchend", onTouchEnd);
|
|
422
|
+
if (this._iosTouchEndTimerId !== null && this.targetWindow != null) {
|
|
423
|
+
this.targetWindow.clearTimeout(this._iosTouchEndTimerId);
|
|
424
|
+
this._iosTouchEndTimerId = null;
|
|
425
|
+
}
|
|
426
|
+
});
|
|
427
|
+
}
|
|
361
428
|
this._scrollToOffset(this.getScrollOffset(), {
|
|
362
429
|
adjustments: void 0,
|
|
363
430
|
behavior: void 0
|
|
364
431
|
});
|
|
365
432
|
}
|
|
433
|
+
const anchor = this.pendingScrollAnchor;
|
|
434
|
+
this.pendingScrollAnchor = null;
|
|
435
|
+
if (anchor && this.scrollElement && this.options.enabled) {
|
|
436
|
+
const [key, offset, followOnAppend] = anchor;
|
|
437
|
+
if (key !== null) {
|
|
438
|
+
const { count, getItemKey } = this.options;
|
|
439
|
+
let index = 0;
|
|
440
|
+
while (index < count && getItemKey(index) !== key) {
|
|
441
|
+
index++;
|
|
442
|
+
}
|
|
443
|
+
const item = index < count ? this.getMeasurements()[index] : void 0;
|
|
444
|
+
if (item) {
|
|
445
|
+
const delta = item.start + offset - this.getScrollOffset();
|
|
446
|
+
if (!utils.approxEqual(delta, 0)) {
|
|
447
|
+
this.applyScrollAdjustment(delta);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
if (followOnAppend) {
|
|
452
|
+
this.scrollToEnd({ behavior: followOnAppend });
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
};
|
|
456
|
+
this._flushIosDeferredIfReady = () => {
|
|
457
|
+
if (this._iosDeferredAdjustment === 0) return;
|
|
458
|
+
if (this.isScrolling) return;
|
|
459
|
+
if (this._iosTouching) return;
|
|
460
|
+
if (this._iosJustTouchEnded) return;
|
|
461
|
+
const cur = this.getScrollOffset();
|
|
462
|
+
const max = this.getMaxScrollOffset();
|
|
463
|
+
if (cur < 0 || cur > max) return;
|
|
464
|
+
const delta = this._iosDeferredAdjustment;
|
|
465
|
+
this._iosDeferredAdjustment = 0;
|
|
466
|
+
this._scrollToOffset(cur, {
|
|
467
|
+
adjustments: this.scrollAdjustments += delta,
|
|
468
|
+
behavior: void 0
|
|
469
|
+
});
|
|
366
470
|
};
|
|
367
471
|
this.rafId = null;
|
|
368
472
|
this.getSize = () => {
|
|
@@ -424,7 +528,7 @@ class Virtualizer {
|
|
|
424
528
|
this.lanesChangedFlag = true;
|
|
425
529
|
}
|
|
426
530
|
this.prevLanes = lanes;
|
|
427
|
-
this.
|
|
531
|
+
this.pendingMin = null;
|
|
428
532
|
return {
|
|
429
533
|
count,
|
|
430
534
|
paddingStart,
|
|
@@ -440,7 +544,7 @@ class Virtualizer {
|
|
|
440
544
|
}
|
|
441
545
|
);
|
|
442
546
|
this.getMeasurements = utils.memo(
|
|
443
|
-
() => [this.getMeasurementOptions(), this.
|
|
547
|
+
() => [this.getMeasurementOptions(), this.itemSizeCacheVersion],
|
|
444
548
|
({
|
|
445
549
|
count,
|
|
446
550
|
paddingStart,
|
|
@@ -449,7 +553,8 @@ class Virtualizer {
|
|
|
449
553
|
enabled,
|
|
450
554
|
lanes,
|
|
451
555
|
laneAssignmentMode
|
|
452
|
-
},
|
|
556
|
+
}, _itemSizeCacheVersion) => {
|
|
557
|
+
const itemSizeCache = this.itemSizeCache;
|
|
453
558
|
if (!enabled) {
|
|
454
559
|
this.measurementsCache = [];
|
|
455
560
|
this.itemSizeCache.clear();
|
|
@@ -469,7 +574,7 @@ class Virtualizer {
|
|
|
469
574
|
this.measurementsCache = [];
|
|
470
575
|
this.itemSizeCache.clear();
|
|
471
576
|
this.laneAssignments.clear();
|
|
472
|
-
this.
|
|
577
|
+
this.pendingMin = null;
|
|
473
578
|
}
|
|
474
579
|
if (this.measurementsCache.length === 0 && !this.lanesSettling) {
|
|
475
580
|
this.measurementsCache = this.options.initialMeasurementsCache;
|
|
@@ -477,11 +582,40 @@ class Virtualizer {
|
|
|
477
582
|
this.itemSizeCache.set(item.key, item.size);
|
|
478
583
|
});
|
|
479
584
|
}
|
|
480
|
-
const min = this.lanesSettling ? 0 : this.
|
|
481
|
-
this.
|
|
585
|
+
const min = this.lanesSettling ? 0 : this.pendingMin ?? 0;
|
|
586
|
+
this.pendingMin = null;
|
|
482
587
|
if (this.lanesSettling && this.measurementsCache.length === count) {
|
|
483
588
|
this.lanesSettling = false;
|
|
484
589
|
}
|
|
590
|
+
if (lanes === 1) {
|
|
591
|
+
const gap = this.options.gap;
|
|
592
|
+
const need = count * 2;
|
|
593
|
+
let flat = this._flatMeasurements;
|
|
594
|
+
if (!flat || flat.length < need) {
|
|
595
|
+
const next = new Float64Array(need);
|
|
596
|
+
if (flat && min > 0) next.set(flat.subarray(0, min * 2));
|
|
597
|
+
flat = next;
|
|
598
|
+
this._flatMeasurements = flat;
|
|
599
|
+
}
|
|
600
|
+
let runningStart;
|
|
601
|
+
if (min === 0) {
|
|
602
|
+
runningStart = paddingStart + scrollMargin;
|
|
603
|
+
} else {
|
|
604
|
+
const prevIdx = min - 1;
|
|
605
|
+
runningStart = flat[prevIdx * 2] + flat[prevIdx * 2 + 1] + gap;
|
|
606
|
+
}
|
|
607
|
+
for (let i = min; i < count; i++) {
|
|
608
|
+
const key = getItemKey(i);
|
|
609
|
+
const measuredSize = itemSizeCache.get(key);
|
|
610
|
+
const size = typeof measuredSize === "number" ? measuredSize : this.options.estimateSize(i);
|
|
611
|
+
flat[i * 2] = runningStart;
|
|
612
|
+
flat[i * 2 + 1] = size;
|
|
613
|
+
runningStart += size + gap;
|
|
614
|
+
}
|
|
615
|
+
const view = lazyMeasurements.createLazyMeasurementsView(count, flat, getItemKey);
|
|
616
|
+
this.measurementsCache = view;
|
|
617
|
+
return view;
|
|
618
|
+
}
|
|
485
619
|
const measurements = this.measurementsCache.slice(0, min);
|
|
486
620
|
const laneLastIndex = new Array(lanes).fill(
|
|
487
621
|
void 0
|
|
@@ -544,7 +678,11 @@ class Virtualizer {
|
|
|
544
678
|
measurements,
|
|
545
679
|
outerSize,
|
|
546
680
|
scrollOffset,
|
|
547
|
-
lanes
|
|
681
|
+
lanes,
|
|
682
|
+
// Pass the typed array so binary search + forward-walk can
|
|
683
|
+
// read start/end directly from Float64Array, skipping the
|
|
684
|
+
// Proxy traps that materialize a full VirtualItem per probe.
|
|
685
|
+
flat: lanes === 1 && this._flatMeasurements != null ? this._flatMeasurements : null
|
|
548
686
|
}) : null;
|
|
549
687
|
},
|
|
550
688
|
{
|
|
@@ -639,23 +777,60 @@ class Virtualizer {
|
|
|
639
777
|
}
|
|
640
778
|
};
|
|
641
779
|
this.resizeItem = (index, size) => {
|
|
642
|
-
var _a;
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
780
|
+
var _a, _b;
|
|
781
|
+
if (index < 0 || index >= this.options.count) return;
|
|
782
|
+
let cachedSize;
|
|
783
|
+
let itemStart;
|
|
784
|
+
let key;
|
|
785
|
+
const flat = this._flatMeasurements;
|
|
786
|
+
if (this.options.lanes === 1 && flat !== null) {
|
|
787
|
+
key = this.options.getItemKey(index);
|
|
788
|
+
itemStart = flat[index * 2];
|
|
789
|
+
cachedSize = flat[index * 2 + 1];
|
|
790
|
+
} else {
|
|
791
|
+
const item = this.measurementsCache[index];
|
|
792
|
+
if (!item) return;
|
|
793
|
+
key = item.key;
|
|
794
|
+
itemStart = item.start;
|
|
795
|
+
cachedSize = item.size;
|
|
796
|
+
}
|
|
797
|
+
const itemSize = this.itemSizeCache.get(key) ?? cachedSize;
|
|
646
798
|
const delta = size - itemSize;
|
|
647
799
|
if (delta !== 0) {
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
800
|
+
const wasAtEnd = this.options.anchorTo === "end" && ((_a = this.scrollState) == null ? void 0 : _a.behavior) !== "smooth" && this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold;
|
|
801
|
+
const prevTotalSize = wasAtEnd ? this.getTotalSize() : 0;
|
|
802
|
+
const shouldAdjustScroll = ((_b = this.scrollState) == null ? void 0 : _b.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
|
|
803
|
+
// The callback expects a VirtualItem; build one lazily only
|
|
804
|
+
// when the consumer actually supplied a custom predicate.
|
|
805
|
+
this.measurementsCache[index] ?? {
|
|
806
|
+
index,
|
|
807
|
+
key,
|
|
808
|
+
start: itemStart,
|
|
809
|
+
size: cachedSize,
|
|
810
|
+
end: itemStart + cachedSize,
|
|
811
|
+
lane: 0
|
|
812
|
+
},
|
|
813
|
+
delta,
|
|
814
|
+
this
|
|
815
|
+
) : (
|
|
816
|
+
// Default: adjust scrollTop only when the resize is an above-
|
|
817
|
+
// viewport item AND we're not actively scrolling backward.
|
|
818
|
+
// Adjusting during backward scroll fights the user's scroll
|
|
819
|
+
// direction and produces the "items jump while scrolling up"
|
|
820
|
+
// jank reported across many issues. Users who want the old
|
|
821
|
+
// behavior can pass shouldAdjustScrollPositionOnItemSizeChange.
|
|
822
|
+
itemStart < this.getScrollOffset() + this.scrollAdjustments && this.scrollDirection !== "backward"
|
|
823
|
+
));
|
|
824
|
+
if (this.pendingMin === null || index < this.pendingMin) {
|
|
825
|
+
this.pendingMin = index;
|
|
826
|
+
}
|
|
827
|
+
this.itemSizeCache.set(key, size);
|
|
828
|
+
this.itemSizeCacheVersion++;
|
|
829
|
+
if (wasAtEnd) {
|
|
830
|
+
this.applyScrollAdjustment(this.getTotalSize() - prevTotalSize);
|
|
831
|
+
} else if (shouldAdjustScroll) {
|
|
832
|
+
this.applyScrollAdjustment(delta);
|
|
656
833
|
}
|
|
657
|
-
this.pendingMeasuredCacheIndexes.push(item.index);
|
|
658
|
-
this.itemSizeCache = new Map(this.itemSizeCache.set(item.key, size));
|
|
659
834
|
this.notify(false);
|
|
660
835
|
}
|
|
661
836
|
};
|
|
@@ -680,14 +855,15 @@ class Virtualizer {
|
|
|
680
855
|
if (measurements.length === 0) {
|
|
681
856
|
return void 0;
|
|
682
857
|
}
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
858
|
+
const flat = this._flatMeasurements;
|
|
859
|
+
const useFlat = this.options.lanes === 1 && flat != null;
|
|
860
|
+
const idx = findNearestBinarySearch(
|
|
861
|
+
0,
|
|
862
|
+
measurements.length - 1,
|
|
863
|
+
useFlat ? (i) => flat[i * 2] : (i) => utils.notUndefined(measurements[i]).start,
|
|
864
|
+
offset
|
|
690
865
|
);
|
|
866
|
+
return utils.notUndefined(measurements[idx]);
|
|
691
867
|
};
|
|
692
868
|
this.getMaxScrollOffset = () => {
|
|
693
869
|
if (!this.scrollElement) return 0;
|
|
@@ -698,6 +874,18 @@ class Virtualizer {
|
|
|
698
874
|
return this.options.horizontal ? doc.scrollWidth - this.scrollElement.innerWidth : doc.scrollHeight - this.scrollElement.innerHeight;
|
|
699
875
|
}
|
|
700
876
|
};
|
|
877
|
+
this.getVirtualDistanceFromEnd = () => {
|
|
878
|
+
return Math.max(
|
|
879
|
+
this.getTotalSize() - this.getSize() - this.getScrollOffset(),
|
|
880
|
+
0
|
|
881
|
+
);
|
|
882
|
+
};
|
|
883
|
+
this.getDistanceFromEnd = () => {
|
|
884
|
+
return Math.max(this.getMaxScrollOffset() - this.getScrollOffset(), 0);
|
|
885
|
+
};
|
|
886
|
+
this.isAtEnd = (threshold = this.options.scrollEndThreshold) => {
|
|
887
|
+
return this.getDistanceFromEnd() <= threshold;
|
|
888
|
+
};
|
|
701
889
|
this.getOffsetForAlignment = (toOffset, align, itemSize = 0) => {
|
|
702
890
|
if (!this.scrollElement) return 0;
|
|
703
891
|
const size = this.getSize();
|
|
@@ -787,6 +975,18 @@ class Virtualizer {
|
|
|
787
975
|
this._scrollToOffset(offset, { adjustments: void 0, behavior });
|
|
788
976
|
this.scheduleScrollReconcile();
|
|
789
977
|
};
|
|
978
|
+
this.scrollToEnd = ({ behavior = "auto" } = {}) => {
|
|
979
|
+
if (this.options.count > 0) {
|
|
980
|
+
this.scrollToIndex(this.options.count - 1, {
|
|
981
|
+
align: "end",
|
|
982
|
+
behavior
|
|
983
|
+
});
|
|
984
|
+
return;
|
|
985
|
+
}
|
|
986
|
+
this.scrollToOffset(Math.max(this.getTotalSize() - this.getSize(), 0), {
|
|
987
|
+
behavior
|
|
988
|
+
});
|
|
989
|
+
};
|
|
790
990
|
this.getTotalSize = () => {
|
|
791
991
|
var _a;
|
|
792
992
|
const measurements = this.getMeasurements();
|
|
@@ -794,7 +994,13 @@ class Virtualizer {
|
|
|
794
994
|
if (measurements.length === 0) {
|
|
795
995
|
end = this.options.paddingStart;
|
|
796
996
|
} else if (this.options.lanes === 1) {
|
|
797
|
-
|
|
997
|
+
const lastIdx = measurements.length - 1;
|
|
998
|
+
const flat = this._flatMeasurements;
|
|
999
|
+
if (flat != null) {
|
|
1000
|
+
end = flat[lastIdx * 2] + flat[lastIdx * 2 + 1];
|
|
1001
|
+
} else {
|
|
1002
|
+
end = ((_a = measurements[lastIdx]) == null ? void 0 : _a.end) ?? 0;
|
|
1003
|
+
}
|
|
798
1004
|
} else {
|
|
799
1005
|
const endByLane = Array(this.options.lanes).fill(null);
|
|
800
1006
|
let endIndex = measurements.length - 1;
|
|
@@ -812,19 +1018,54 @@ class Virtualizer {
|
|
|
812
1018
|
0
|
|
813
1019
|
);
|
|
814
1020
|
};
|
|
1021
|
+
this.takeSnapshot = () => {
|
|
1022
|
+
const snapshot = [];
|
|
1023
|
+
if (this.itemSizeCache.size === 0) return snapshot;
|
|
1024
|
+
const m = this.getMeasurements();
|
|
1025
|
+
for (const item of m) {
|
|
1026
|
+
if (item && this.itemSizeCache.has(item.key)) {
|
|
1027
|
+
snapshot.push({
|
|
1028
|
+
index: item.index,
|
|
1029
|
+
key: item.key,
|
|
1030
|
+
start: item.start,
|
|
1031
|
+
size: item.size,
|
|
1032
|
+
end: item.end,
|
|
1033
|
+
lane: item.lane
|
|
1034
|
+
});
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
return snapshot;
|
|
1038
|
+
};
|
|
815
1039
|
this._scrollToOffset = (offset, {
|
|
816
1040
|
adjustments,
|
|
817
1041
|
behavior
|
|
818
1042
|
}) => {
|
|
1043
|
+
this._intendedScrollOffset = offset + (adjustments ?? 0);
|
|
819
1044
|
this.options.scrollToFn(offset, { behavior, adjustments }, this);
|
|
820
1045
|
};
|
|
821
1046
|
this.measure = () => {
|
|
822
|
-
this.
|
|
823
|
-
this.
|
|
1047
|
+
this.pendingMin = null;
|
|
1048
|
+
this.itemSizeCache.clear();
|
|
1049
|
+
this.laneAssignments.clear();
|
|
1050
|
+
this.itemSizeCacheVersion++;
|
|
824
1051
|
this.notify(false);
|
|
825
1052
|
};
|
|
826
1053
|
this.setOptions(opts);
|
|
827
1054
|
}
|
|
1055
|
+
applyScrollAdjustment(delta, behavior) {
|
|
1056
|
+
if (delta === 0) return;
|
|
1057
|
+
if (process.env.NODE_ENV !== "production" && this.options.debug) {
|
|
1058
|
+
console.info("correction", delta);
|
|
1059
|
+
}
|
|
1060
|
+
if (isIOSWebKit() && (this.isScrolling || this._iosTouching || this._iosJustTouchEnded)) {
|
|
1061
|
+
this._iosDeferredAdjustment += delta;
|
|
1062
|
+
} else {
|
|
1063
|
+
this._scrollToOffset(this.getScrollOffset(), {
|
|
1064
|
+
adjustments: this.scrollAdjustments += delta,
|
|
1065
|
+
behavior
|
|
1066
|
+
});
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
828
1069
|
scheduleScrollReconcile() {
|
|
829
1070
|
if (!this.targetWindow) {
|
|
830
1071
|
this.scrollState = null;
|
|
@@ -852,17 +1093,28 @@ class Virtualizer {
|
|
|
852
1093
|
if (!targetChanged && utils.approxEqual(targetOffset, this.getScrollOffset())) {
|
|
853
1094
|
this.scrollState.stableFrames++;
|
|
854
1095
|
if (this.scrollState.stableFrames >= STABLE_FRAMES) {
|
|
1096
|
+
if (this.getScrollOffset() !== targetOffset) {
|
|
1097
|
+
this._scrollToOffset(targetOffset, {
|
|
1098
|
+
adjustments: void 0,
|
|
1099
|
+
behavior: "auto"
|
|
1100
|
+
});
|
|
1101
|
+
}
|
|
855
1102
|
this.scrollState = null;
|
|
856
1103
|
return;
|
|
857
1104
|
}
|
|
858
1105
|
} else {
|
|
859
1106
|
this.scrollState.stableFrames = 0;
|
|
860
1107
|
if (targetChanged) {
|
|
1108
|
+
const viewport = this.getSize() || 600;
|
|
1109
|
+
const distance = Math.abs(targetOffset - this.getScrollOffset());
|
|
1110
|
+
const keepSmooth = this.scrollState.behavior === "smooth" && distance > viewport;
|
|
861
1111
|
this.scrollState.lastTargetOffset = targetOffset;
|
|
862
|
-
|
|
1112
|
+
if (!keepSmooth) {
|
|
1113
|
+
this.scrollState.behavior = "auto";
|
|
1114
|
+
}
|
|
863
1115
|
this._scrollToOffset(targetOffset, {
|
|
864
1116
|
adjustments: void 0,
|
|
865
|
-
behavior: "auto"
|
|
1117
|
+
behavior: keepSmooth ? "smooth" : "auto"
|
|
866
1118
|
});
|
|
867
1119
|
}
|
|
868
1120
|
}
|
|
@@ -891,25 +1143,22 @@ function calculateRange({
|
|
|
891
1143
|
measurements,
|
|
892
1144
|
outerSize,
|
|
893
1145
|
scrollOffset,
|
|
894
|
-
lanes
|
|
1146
|
+
lanes,
|
|
1147
|
+
flat
|
|
895
1148
|
}) {
|
|
896
1149
|
const lastIndex = measurements.length - 1;
|
|
897
|
-
const
|
|
1150
|
+
const getStart = flat ? (index) => flat[index * 2] : (index) => measurements[index].start;
|
|
1151
|
+
const getEnd = flat ? (index) => flat[index * 2] + flat[index * 2 + 1] : (index) => measurements[index].end;
|
|
898
1152
|
if (measurements.length <= lanes) {
|
|
899
1153
|
return {
|
|
900
1154
|
startIndex: 0,
|
|
901
1155
|
endIndex: lastIndex
|
|
902
1156
|
};
|
|
903
1157
|
}
|
|
904
|
-
let startIndex = findNearestBinarySearch(
|
|
905
|
-
0,
|
|
906
|
-
lastIndex,
|
|
907
|
-
getOffset,
|
|
908
|
-
scrollOffset
|
|
909
|
-
);
|
|
1158
|
+
let startIndex = findNearestBinarySearch(0, lastIndex, getStart, scrollOffset);
|
|
910
1159
|
let endIndex = startIndex;
|
|
911
1160
|
if (lanes === 1) {
|
|
912
|
-
while (endIndex < lastIndex &&
|
|
1161
|
+
while (endIndex < lastIndex && getEnd(endIndex) < scrollOffset + outerSize) {
|
|
913
1162
|
endIndex++;
|
|
914
1163
|
}
|
|
915
1164
|
} else if (lanes > 1) {
|
|
@@ -935,6 +1184,7 @@ exports.debounce = utils.debounce;
|
|
|
935
1184
|
exports.memo = utils.memo;
|
|
936
1185
|
exports.notUndefined = utils.notUndefined;
|
|
937
1186
|
exports.Virtualizer = Virtualizer;
|
|
1187
|
+
exports._resetIOSDetectionForTests = _resetIOSDetectionForTests;
|
|
938
1188
|
exports.defaultKeyExtractor = defaultKeyExtractor;
|
|
939
1189
|
exports.defaultRangeExtractor = defaultRangeExtractor;
|
|
940
1190
|
exports.elementScroll = elementScroll;
|