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