@tanstack/virtual-core 3.14.0 → 3.15.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 +267 -106
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +26 -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 +26 -5
- package/dist/esm/index.js +268 -107
- 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 +452 -138
- 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,9 +158,11 @@ 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;
|
|
@@ -195,6 +170,11 @@ class Virtualizer {
|
|
|
195
170
|
this.scrollOffset = null;
|
|
196
171
|
this.scrollDirection = null;
|
|
197
172
|
this.scrollAdjustments = 0;
|
|
173
|
+
this._iosDeferredAdjustment = 0;
|
|
174
|
+
this._iosTouching = false;
|
|
175
|
+
this._iosJustTouchEnded = false;
|
|
176
|
+
this._iosTouchEndTimerId = null;
|
|
177
|
+
this._intendedScrollOffset = null;
|
|
198
178
|
this.elementsCache = /* @__PURE__ */ new Map();
|
|
199
179
|
this.now = () => {
|
|
200
180
|
var _a, _b, _c;
|
|
@@ -216,6 +196,12 @@ class Virtualizer {
|
|
|
216
196
|
const index = this.indexFromElement(node);
|
|
217
197
|
if (!node.isConnected) {
|
|
218
198
|
this.observer.unobserve(node);
|
|
199
|
+
for (const [cacheKey, cachedNode] of this.elementsCache) {
|
|
200
|
+
if (cachedNode === node) {
|
|
201
|
+
this.elementsCache.delete(cacheKey);
|
|
202
|
+
break;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
219
205
|
return;
|
|
220
206
|
}
|
|
221
207
|
if (this.shouldMeasureDuringScroll(index)) {
|
|
@@ -247,10 +233,7 @@ class Virtualizer {
|
|
|
247
233
|
})();
|
|
248
234
|
this.range = null;
|
|
249
235
|
this.setOptions = (opts2) => {
|
|
250
|
-
|
|
251
|
-
if (typeof value === "undefined") delete opts2[key];
|
|
252
|
-
});
|
|
253
|
-
this.options = {
|
|
236
|
+
const merged = {
|
|
254
237
|
debug: false,
|
|
255
238
|
initialOffset: 0,
|
|
256
239
|
overscan: 1,
|
|
@@ -275,9 +258,13 @@ class Virtualizer {
|
|
|
275
258
|
isRtl: false,
|
|
276
259
|
useScrollendEvent: false,
|
|
277
260
|
useAnimationFrameWithResizeObserver: false,
|
|
278
|
-
laneAssignmentMode: "estimate"
|
|
279
|
-
...opts2
|
|
261
|
+
laneAssignmentMode: "estimate"
|
|
280
262
|
};
|
|
263
|
+
for (const key in opts2) {
|
|
264
|
+
const v = opts2[key];
|
|
265
|
+
if (v !== void 0) merged[key] = v;
|
|
266
|
+
}
|
|
267
|
+
this.options = merged;
|
|
281
268
|
};
|
|
282
269
|
this.notify = (sync) => {
|
|
283
270
|
var _a, _b;
|
|
@@ -348,22 +335,83 @@ class Virtualizer {
|
|
|
348
335
|
);
|
|
349
336
|
this.unsubs.push(
|
|
350
337
|
this.options.observeElementOffset(this, (offset, isScrolling) => {
|
|
338
|
+
if (this._intendedScrollOffset !== null && Math.abs(offset - this._intendedScrollOffset) < 1.5) {
|
|
339
|
+
offset = this._intendedScrollOffset;
|
|
340
|
+
}
|
|
341
|
+
this._intendedScrollOffset = null;
|
|
351
342
|
this.scrollAdjustments = 0;
|
|
352
343
|
this.scrollDirection = isScrolling ? this.getScrollOffset() < offset ? "forward" : "backward" : null;
|
|
353
344
|
this.scrollOffset = offset;
|
|
354
345
|
this.isScrolling = isScrolling;
|
|
346
|
+
this._flushIosDeferredIfReady();
|
|
355
347
|
if (this.scrollState) {
|
|
356
348
|
this.scheduleScrollReconcile();
|
|
357
349
|
}
|
|
358
350
|
this.maybeNotify();
|
|
359
351
|
})
|
|
360
352
|
);
|
|
353
|
+
if ("addEventListener" in this.scrollElement) {
|
|
354
|
+
const scrollEl = this.scrollElement;
|
|
355
|
+
const onTouchStart = () => {
|
|
356
|
+
this._iosTouching = true;
|
|
357
|
+
this._iosJustTouchEnded = false;
|
|
358
|
+
if (this._iosTouchEndTimerId !== null && this.targetWindow != null) {
|
|
359
|
+
this.targetWindow.clearTimeout(this._iosTouchEndTimerId);
|
|
360
|
+
this._iosTouchEndTimerId = null;
|
|
361
|
+
}
|
|
362
|
+
};
|
|
363
|
+
const onTouchEnd = () => {
|
|
364
|
+
this._iosTouching = false;
|
|
365
|
+
if (!isIOSWebKit() || this.targetWindow == null) {
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
this._iosJustTouchEnded = true;
|
|
369
|
+
this._iosTouchEndTimerId = this.targetWindow.setTimeout(() => {
|
|
370
|
+
this._iosJustTouchEnded = false;
|
|
371
|
+
this._iosTouchEndTimerId = null;
|
|
372
|
+
this._flushIosDeferredIfReady();
|
|
373
|
+
}, 150);
|
|
374
|
+
};
|
|
375
|
+
scrollEl.addEventListener(
|
|
376
|
+
"touchstart",
|
|
377
|
+
onTouchStart,
|
|
378
|
+
addEventListenerOptions
|
|
379
|
+
);
|
|
380
|
+
scrollEl.addEventListener(
|
|
381
|
+
"touchend",
|
|
382
|
+
onTouchEnd,
|
|
383
|
+
addEventListenerOptions
|
|
384
|
+
);
|
|
385
|
+
this.unsubs.push(() => {
|
|
386
|
+
scrollEl.removeEventListener("touchstart", onTouchStart);
|
|
387
|
+
scrollEl.removeEventListener("touchend", onTouchEnd);
|
|
388
|
+
if (this._iosTouchEndTimerId !== null && this.targetWindow != null) {
|
|
389
|
+
this.targetWindow.clearTimeout(this._iosTouchEndTimerId);
|
|
390
|
+
this._iosTouchEndTimerId = null;
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
}
|
|
361
394
|
this._scrollToOffset(this.getScrollOffset(), {
|
|
362
395
|
adjustments: void 0,
|
|
363
396
|
behavior: void 0
|
|
364
397
|
});
|
|
365
398
|
}
|
|
366
399
|
};
|
|
400
|
+
this._flushIosDeferredIfReady = () => {
|
|
401
|
+
if (this._iosDeferredAdjustment === 0) return;
|
|
402
|
+
if (this.isScrolling) return;
|
|
403
|
+
if (this._iosTouching) return;
|
|
404
|
+
if (this._iosJustTouchEnded) return;
|
|
405
|
+
const cur = this.getScrollOffset();
|
|
406
|
+
const max = this.getMaxScrollOffset();
|
|
407
|
+
if (cur < 0 || cur > max) return;
|
|
408
|
+
const delta = this._iosDeferredAdjustment;
|
|
409
|
+
this._iosDeferredAdjustment = 0;
|
|
410
|
+
this._scrollToOffset(cur, {
|
|
411
|
+
adjustments: this.scrollAdjustments += delta,
|
|
412
|
+
behavior: void 0
|
|
413
|
+
});
|
|
414
|
+
};
|
|
367
415
|
this.rafId = null;
|
|
368
416
|
this.getSize = () => {
|
|
369
417
|
if (!this.options.enabled) {
|
|
@@ -424,7 +472,7 @@ class Virtualizer {
|
|
|
424
472
|
this.lanesChangedFlag = true;
|
|
425
473
|
}
|
|
426
474
|
this.prevLanes = lanes;
|
|
427
|
-
this.
|
|
475
|
+
this.pendingMin = null;
|
|
428
476
|
return {
|
|
429
477
|
count,
|
|
430
478
|
paddingStart,
|
|
@@ -440,7 +488,7 @@ class Virtualizer {
|
|
|
440
488
|
}
|
|
441
489
|
);
|
|
442
490
|
this.getMeasurements = utils.memo(
|
|
443
|
-
() => [this.getMeasurementOptions(), this.
|
|
491
|
+
() => [this.getMeasurementOptions(), this.itemSizeCacheVersion],
|
|
444
492
|
({
|
|
445
493
|
count,
|
|
446
494
|
paddingStart,
|
|
@@ -449,7 +497,8 @@ class Virtualizer {
|
|
|
449
497
|
enabled,
|
|
450
498
|
lanes,
|
|
451
499
|
laneAssignmentMode
|
|
452
|
-
},
|
|
500
|
+
}, _itemSizeCacheVersion) => {
|
|
501
|
+
const itemSizeCache = this.itemSizeCache;
|
|
453
502
|
if (!enabled) {
|
|
454
503
|
this.measurementsCache = [];
|
|
455
504
|
this.itemSizeCache.clear();
|
|
@@ -469,7 +518,7 @@ class Virtualizer {
|
|
|
469
518
|
this.measurementsCache = [];
|
|
470
519
|
this.itemSizeCache.clear();
|
|
471
520
|
this.laneAssignments.clear();
|
|
472
|
-
this.
|
|
521
|
+
this.pendingMin = null;
|
|
473
522
|
}
|
|
474
523
|
if (this.measurementsCache.length === 0 && !this.lanesSettling) {
|
|
475
524
|
this.measurementsCache = this.options.initialMeasurementsCache;
|
|
@@ -477,11 +526,40 @@ class Virtualizer {
|
|
|
477
526
|
this.itemSizeCache.set(item.key, item.size);
|
|
478
527
|
});
|
|
479
528
|
}
|
|
480
|
-
const min = this.lanesSettling ? 0 : this.
|
|
481
|
-
this.
|
|
529
|
+
const min = this.lanesSettling ? 0 : this.pendingMin ?? 0;
|
|
530
|
+
this.pendingMin = null;
|
|
482
531
|
if (this.lanesSettling && this.measurementsCache.length === count) {
|
|
483
532
|
this.lanesSettling = false;
|
|
484
533
|
}
|
|
534
|
+
if (lanes === 1) {
|
|
535
|
+
const gap = this.options.gap;
|
|
536
|
+
const need = count * 2;
|
|
537
|
+
let flat = this._flatMeasurements;
|
|
538
|
+
if (!flat || flat.length < need) {
|
|
539
|
+
const next = new Float64Array(need);
|
|
540
|
+
if (flat && min > 0) next.set(flat.subarray(0, min * 2));
|
|
541
|
+
flat = next;
|
|
542
|
+
this._flatMeasurements = flat;
|
|
543
|
+
}
|
|
544
|
+
let runningStart;
|
|
545
|
+
if (min === 0) {
|
|
546
|
+
runningStart = paddingStart + scrollMargin;
|
|
547
|
+
} else {
|
|
548
|
+
const prevIdx = min - 1;
|
|
549
|
+
runningStart = flat[prevIdx * 2] + flat[prevIdx * 2 + 1] + gap;
|
|
550
|
+
}
|
|
551
|
+
for (let i = min; i < count; i++) {
|
|
552
|
+
const key = getItemKey(i);
|
|
553
|
+
const measuredSize = itemSizeCache.get(key);
|
|
554
|
+
const size = typeof measuredSize === "number" ? measuredSize : this.options.estimateSize(i);
|
|
555
|
+
flat[i * 2] = runningStart;
|
|
556
|
+
flat[i * 2 + 1] = size;
|
|
557
|
+
runningStart += size + gap;
|
|
558
|
+
}
|
|
559
|
+
const view = lazyMeasurements.createLazyMeasurementsView(count, flat, getItemKey);
|
|
560
|
+
this.measurementsCache = view;
|
|
561
|
+
return view;
|
|
562
|
+
}
|
|
485
563
|
const measurements = this.measurementsCache.slice(0, min);
|
|
486
564
|
const laneLastIndex = new Array(lanes).fill(
|
|
487
565
|
void 0
|
|
@@ -544,7 +622,11 @@ class Virtualizer {
|
|
|
544
622
|
measurements,
|
|
545
623
|
outerSize,
|
|
546
624
|
scrollOffset,
|
|
547
|
-
lanes
|
|
625
|
+
lanes,
|
|
626
|
+
// Pass the typed array so binary search + forward-walk can
|
|
627
|
+
// read start/end directly from Float64Array, skipping the
|
|
628
|
+
// Proxy traps that materialize a full VirtualItem per probe.
|
|
629
|
+
flat: lanes === 1 && this._flatMeasurements != null ? this._flatMeasurements : null
|
|
548
630
|
}) : null;
|
|
549
631
|
},
|
|
550
632
|
{
|
|
@@ -640,22 +722,64 @@ class Virtualizer {
|
|
|
640
722
|
};
|
|
641
723
|
this.resizeItem = (index, size) => {
|
|
642
724
|
var _a;
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
725
|
+
if (index < 0 || index >= this.options.count) return;
|
|
726
|
+
let cachedSize;
|
|
727
|
+
let itemStart;
|
|
728
|
+
let key;
|
|
729
|
+
const flat = this._flatMeasurements;
|
|
730
|
+
if (this.options.lanes === 1 && flat !== null) {
|
|
731
|
+
key = this.options.getItemKey(index);
|
|
732
|
+
itemStart = flat[index * 2];
|
|
733
|
+
cachedSize = flat[index * 2 + 1];
|
|
734
|
+
} else {
|
|
735
|
+
const item = this.measurementsCache[index];
|
|
736
|
+
if (!item) return;
|
|
737
|
+
key = item.key;
|
|
738
|
+
itemStart = item.start;
|
|
739
|
+
cachedSize = item.size;
|
|
740
|
+
}
|
|
741
|
+
const itemSize = this.itemSizeCache.get(key) ?? cachedSize;
|
|
646
742
|
const delta = size - itemSize;
|
|
647
743
|
if (delta !== 0) {
|
|
648
|
-
if (((_a = this.scrollState) == null ? void 0 : _a.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
|
|
744
|
+
if (((_a = this.scrollState) == null ? void 0 : _a.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
|
|
745
|
+
// The callback expects a VirtualItem; build one lazily only
|
|
746
|
+
// when the consumer actually supplied a custom predicate.
|
|
747
|
+
this.measurementsCache[index] ?? {
|
|
748
|
+
index,
|
|
749
|
+
key,
|
|
750
|
+
start: itemStart,
|
|
751
|
+
size: cachedSize,
|
|
752
|
+
end: itemStart + cachedSize,
|
|
753
|
+
lane: 0
|
|
754
|
+
},
|
|
755
|
+
delta,
|
|
756
|
+
this
|
|
757
|
+
) : (
|
|
758
|
+
// Default: adjust scrollTop only when the resize is an above-
|
|
759
|
+
// viewport item AND we're not actively scrolling backward.
|
|
760
|
+
// Adjusting during backward scroll fights the user's scroll
|
|
761
|
+
// direction and produces the "items jump while scrolling up"
|
|
762
|
+
// jank reported across many issues. Users who want the old
|
|
763
|
+
// behavior can pass shouldAdjustScrollPositionOnItemSizeChange.
|
|
764
|
+
itemStart < this.getScrollOffset() + this.scrollAdjustments && this.scrollDirection !== "backward"
|
|
765
|
+
))) {
|
|
649
766
|
if (process.env.NODE_ENV !== "production" && this.options.debug) {
|
|
650
767
|
console.info("correction", delta);
|
|
651
768
|
}
|
|
652
|
-
this.
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
769
|
+
if (isIOSWebKit() && (this.isScrolling || this._iosTouching || this._iosJustTouchEnded)) {
|
|
770
|
+
this._iosDeferredAdjustment += delta;
|
|
771
|
+
} else {
|
|
772
|
+
this._scrollToOffset(this.getScrollOffset(), {
|
|
773
|
+
adjustments: this.scrollAdjustments += delta,
|
|
774
|
+
behavior: void 0
|
|
775
|
+
});
|
|
776
|
+
}
|
|
656
777
|
}
|
|
657
|
-
this.
|
|
658
|
-
|
|
778
|
+
if (this.pendingMin === null || index < this.pendingMin) {
|
|
779
|
+
this.pendingMin = index;
|
|
780
|
+
}
|
|
781
|
+
this.itemSizeCache.set(key, size);
|
|
782
|
+
this.itemSizeCacheVersion++;
|
|
659
783
|
this.notify(false);
|
|
660
784
|
}
|
|
661
785
|
};
|
|
@@ -680,14 +804,15 @@ class Virtualizer {
|
|
|
680
804
|
if (measurements.length === 0) {
|
|
681
805
|
return void 0;
|
|
682
806
|
}
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
807
|
+
const flat = this._flatMeasurements;
|
|
808
|
+
const useFlat = this.options.lanes === 1 && flat != null;
|
|
809
|
+
const idx = findNearestBinarySearch(
|
|
810
|
+
0,
|
|
811
|
+
measurements.length - 1,
|
|
812
|
+
useFlat ? (i) => flat[i * 2] : (i) => utils.notUndefined(measurements[i]).start,
|
|
813
|
+
offset
|
|
690
814
|
);
|
|
815
|
+
return utils.notUndefined(measurements[idx]);
|
|
691
816
|
};
|
|
692
817
|
this.getMaxScrollOffset = () => {
|
|
693
818
|
if (!this.scrollElement) return 0;
|
|
@@ -794,7 +919,13 @@ class Virtualizer {
|
|
|
794
919
|
if (measurements.length === 0) {
|
|
795
920
|
end = this.options.paddingStart;
|
|
796
921
|
} else if (this.options.lanes === 1) {
|
|
797
|
-
|
|
922
|
+
const lastIdx = measurements.length - 1;
|
|
923
|
+
const flat = this._flatMeasurements;
|
|
924
|
+
if (flat != null) {
|
|
925
|
+
end = flat[lastIdx * 2] + flat[lastIdx * 2 + 1];
|
|
926
|
+
} else {
|
|
927
|
+
end = ((_a = measurements[lastIdx]) == null ? void 0 : _a.end) ?? 0;
|
|
928
|
+
}
|
|
798
929
|
} else {
|
|
799
930
|
const endByLane = Array(this.options.lanes).fill(null);
|
|
800
931
|
let endIndex = measurements.length - 1;
|
|
@@ -812,15 +943,36 @@ class Virtualizer {
|
|
|
812
943
|
0
|
|
813
944
|
);
|
|
814
945
|
};
|
|
946
|
+
this.takeSnapshot = () => {
|
|
947
|
+
const snapshot = [];
|
|
948
|
+
if (this.itemSizeCache.size === 0) return snapshot;
|
|
949
|
+
const m = this.getMeasurements();
|
|
950
|
+
for (const item of m) {
|
|
951
|
+
if (item && this.itemSizeCache.has(item.key)) {
|
|
952
|
+
snapshot.push({
|
|
953
|
+
index: item.index,
|
|
954
|
+
key: item.key,
|
|
955
|
+
start: item.start,
|
|
956
|
+
size: item.size,
|
|
957
|
+
end: item.end,
|
|
958
|
+
lane: item.lane
|
|
959
|
+
});
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
return snapshot;
|
|
963
|
+
};
|
|
815
964
|
this._scrollToOffset = (offset, {
|
|
816
965
|
adjustments,
|
|
817
966
|
behavior
|
|
818
967
|
}) => {
|
|
968
|
+
this._intendedScrollOffset = offset + (adjustments ?? 0);
|
|
819
969
|
this.options.scrollToFn(offset, { behavior, adjustments }, this);
|
|
820
970
|
};
|
|
821
971
|
this.measure = () => {
|
|
822
|
-
this.
|
|
823
|
-
this.
|
|
972
|
+
this.pendingMin = null;
|
|
973
|
+
this.itemSizeCache.clear();
|
|
974
|
+
this.laneAssignments.clear();
|
|
975
|
+
this.itemSizeCacheVersion++;
|
|
824
976
|
this.notify(false);
|
|
825
977
|
};
|
|
826
978
|
this.setOptions(opts);
|
|
@@ -852,17 +1004,28 @@ class Virtualizer {
|
|
|
852
1004
|
if (!targetChanged && utils.approxEqual(targetOffset, this.getScrollOffset())) {
|
|
853
1005
|
this.scrollState.stableFrames++;
|
|
854
1006
|
if (this.scrollState.stableFrames >= STABLE_FRAMES) {
|
|
1007
|
+
if (this.getScrollOffset() !== targetOffset) {
|
|
1008
|
+
this._scrollToOffset(targetOffset, {
|
|
1009
|
+
adjustments: void 0,
|
|
1010
|
+
behavior: "auto"
|
|
1011
|
+
});
|
|
1012
|
+
}
|
|
855
1013
|
this.scrollState = null;
|
|
856
1014
|
return;
|
|
857
1015
|
}
|
|
858
1016
|
} else {
|
|
859
1017
|
this.scrollState.stableFrames = 0;
|
|
860
1018
|
if (targetChanged) {
|
|
1019
|
+
const viewport = this.getSize() || 600;
|
|
1020
|
+
const distance = Math.abs(targetOffset - this.getScrollOffset());
|
|
1021
|
+
const keepSmooth = this.scrollState.behavior === "smooth" && distance > viewport;
|
|
861
1022
|
this.scrollState.lastTargetOffset = targetOffset;
|
|
862
|
-
|
|
1023
|
+
if (!keepSmooth) {
|
|
1024
|
+
this.scrollState.behavior = "auto";
|
|
1025
|
+
}
|
|
863
1026
|
this._scrollToOffset(targetOffset, {
|
|
864
1027
|
adjustments: void 0,
|
|
865
|
-
behavior: "auto"
|
|
1028
|
+
behavior: keepSmooth ? "smooth" : "auto"
|
|
866
1029
|
});
|
|
867
1030
|
}
|
|
868
1031
|
}
|
|
@@ -891,25 +1054,22 @@ function calculateRange({
|
|
|
891
1054
|
measurements,
|
|
892
1055
|
outerSize,
|
|
893
1056
|
scrollOffset,
|
|
894
|
-
lanes
|
|
1057
|
+
lanes,
|
|
1058
|
+
flat
|
|
895
1059
|
}) {
|
|
896
1060
|
const lastIndex = measurements.length - 1;
|
|
897
|
-
const
|
|
1061
|
+
const getStart = flat ? (index) => flat[index * 2] : (index) => measurements[index].start;
|
|
1062
|
+
const getEnd = flat ? (index) => flat[index * 2] + flat[index * 2 + 1] : (index) => measurements[index].end;
|
|
898
1063
|
if (measurements.length <= lanes) {
|
|
899
1064
|
return {
|
|
900
1065
|
startIndex: 0,
|
|
901
1066
|
endIndex: lastIndex
|
|
902
1067
|
};
|
|
903
1068
|
}
|
|
904
|
-
let startIndex = findNearestBinarySearch(
|
|
905
|
-
0,
|
|
906
|
-
lastIndex,
|
|
907
|
-
getOffset,
|
|
908
|
-
scrollOffset
|
|
909
|
-
);
|
|
1069
|
+
let startIndex = findNearestBinarySearch(0, lastIndex, getStart, scrollOffset);
|
|
910
1070
|
let endIndex = startIndex;
|
|
911
1071
|
if (lanes === 1) {
|
|
912
|
-
while (endIndex < lastIndex &&
|
|
1072
|
+
while (endIndex < lastIndex && getEnd(endIndex) < scrollOffset + outerSize) {
|
|
913
1073
|
endIndex++;
|
|
914
1074
|
}
|
|
915
1075
|
} else if (lanes > 1) {
|
|
@@ -935,6 +1095,7 @@ exports.debounce = utils.debounce;
|
|
|
935
1095
|
exports.memo = utils.memo;
|
|
936
1096
|
exports.notUndefined = utils.notUndefined;
|
|
937
1097
|
exports.Virtualizer = Virtualizer;
|
|
1098
|
+
exports._resetIOSDetectionForTests = _resetIOSDetectionForTests;
|
|
938
1099
|
exports.defaultKeyExtractor = defaultKeyExtractor;
|
|
939
1100
|
exports.defaultRangeExtractor = defaultRangeExtractor;
|
|
940
1101
|
exports.elementScroll = elementScroll;
|