@tanstack/virtual-core 3.17.9 → 3.17.11
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 +63 -23
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +2 -2
- package/dist/cjs/lazy-measurements.cjs +8 -4
- package/dist/cjs/lazy-measurements.cjs.map +1 -1
- package/dist/cjs/lazy-measurements.d.cts +2 -3
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/index.js +65 -25
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/lazy-measurements.d.ts +2 -3
- package/dist/esm/lazy-measurements.js +9 -5
- package/dist/esm/lazy-measurements.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +92 -29
- package/src/lazy-measurements.ts +13 -10
package/dist/esm/index.d.ts
CHANGED
|
@@ -96,7 +96,7 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
|
|
|
96
96
|
isScrolling: boolean;
|
|
97
97
|
private scrollState;
|
|
98
98
|
measurementsCache: Array<VirtualItem>;
|
|
99
|
-
private
|
|
99
|
+
private _singleLaneMeasurements;
|
|
100
100
|
itemSizeCache: Map<Key, number>;
|
|
101
101
|
private itemSizeCacheVersion;
|
|
102
102
|
private laneAssignments;
|
|
@@ -171,7 +171,7 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
|
|
|
171
171
|
getDistanceFromEnd: () => number;
|
|
172
172
|
isAtEnd: (threshold?: number) => boolean;
|
|
173
173
|
getOffsetForAlignment: (toOffset: number, align: ScrollAlignment, itemSize?: number) => number;
|
|
174
|
-
getOffsetForIndex: (index: number, align?: ScrollAlignment) => readonly [number, "auto"] | readonly [number, "start" | "
|
|
174
|
+
getOffsetForIndex: (index: number, align?: ScrollAlignment) => readonly [number, "auto"] | readonly [number, "start" | "end" | "center"] | undefined;
|
|
175
175
|
scrollToOffset: (toOffset: number, { align, behavior }?: ScrollToOffsetOptions) => void;
|
|
176
176
|
scrollToIndex: (index: number, { align: initialAlign, behavior, }?: ScrollToIndexOptions) => void;
|
|
177
177
|
scrollBy: (delta: number, { behavior }?: ScrollToOffsetOptions) => void;
|
package/dist/esm/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { createLazyMeasurementsView } from "./lazy-measurements.js";
|
|
2
|
-
import { memo,
|
|
1
|
+
import { createLazyMeasurementsView, getMeasurementKey } from "./lazy-measurements.js";
|
|
2
|
+
import { memo, approxEqual, notUndefined, debounce } from "./utils.js";
|
|
3
3
|
let _isIOSResult;
|
|
4
4
|
const isIOSWebKit = () => {
|
|
5
5
|
if (_isIOSResult !== void 0) return _isIOSResult;
|
|
@@ -94,7 +94,7 @@ const observeOffset = (instance, cb, readOffset) => {
|
|
|
94
94
|
let offset = 0;
|
|
95
95
|
const fallback = registerScrollendEvent ? null : debounce(
|
|
96
96
|
targetWindow,
|
|
97
|
-
() => cb(
|
|
97
|
+
() => cb(readOffset(element), false),
|
|
98
98
|
instance.options.isScrollingResetDelay
|
|
99
99
|
);
|
|
100
100
|
const createHandler = (isScrolling) => () => {
|
|
@@ -162,6 +162,27 @@ const scrollWithAdjustments = (offset, {
|
|
|
162
162
|
};
|
|
163
163
|
const windowScroll = scrollWithAdjustments;
|
|
164
164
|
const elementScroll = scrollWithAdjustments;
|
|
165
|
+
function isAppendWithTrim(prevCount, nextCount, getPreviousKey, getNextKey) {
|
|
166
|
+
if (nextCount === 0) return false;
|
|
167
|
+
const firstKey = getNextKey(0);
|
|
168
|
+
const removedKeys = /* @__PURE__ */ new Set();
|
|
169
|
+
let removedCount = 0;
|
|
170
|
+
while (removedCount < prevCount) {
|
|
171
|
+
const key = getPreviousKey(removedCount);
|
|
172
|
+
if (key === firstKey) break;
|
|
173
|
+
removedKeys.add(key);
|
|
174
|
+
removedCount++;
|
|
175
|
+
}
|
|
176
|
+
const retainedCount = prevCount - removedCount;
|
|
177
|
+
if (retainedCount === 0 || retainedCount >= nextCount) return false;
|
|
178
|
+
for (let i = 0; i < retainedCount; i++) {
|
|
179
|
+
if (getNextKey(i) !== getPreviousKey(removedCount + i)) return false;
|
|
180
|
+
}
|
|
181
|
+
for (let i = retainedCount; i < nextCount; i++) {
|
|
182
|
+
if (removedKeys.has(getNextKey(i))) return false;
|
|
183
|
+
}
|
|
184
|
+
return true;
|
|
185
|
+
}
|
|
165
186
|
class Virtualizer {
|
|
166
187
|
constructor(opts) {
|
|
167
188
|
this.unsubs = [];
|
|
@@ -170,7 +191,7 @@ class Virtualizer {
|
|
|
170
191
|
this.isScrolling = false;
|
|
171
192
|
this.scrollState = null;
|
|
172
193
|
this.measurementsCache = [];
|
|
173
|
-
this.
|
|
194
|
+
this._singleLaneMeasurements = null;
|
|
174
195
|
this.itemSizeCache = /* @__PURE__ */ new Map();
|
|
175
196
|
this.itemSizeCacheVersion = 0;
|
|
176
197
|
this.laneAssignments = /* @__PURE__ */ new Map();
|
|
@@ -248,7 +269,7 @@ class Virtualizer {
|
|
|
248
269
|
})();
|
|
249
270
|
this.range = null;
|
|
250
271
|
this.setOptions = (opts2) => {
|
|
251
|
-
var _a
|
|
272
|
+
var _a;
|
|
252
273
|
const merged = {
|
|
253
274
|
debug: false,
|
|
254
275
|
initialOffset: 0,
|
|
@@ -292,8 +313,10 @@ class Virtualizer {
|
|
|
292
313
|
const prevCount = prevOptions.count;
|
|
293
314
|
const nextCount = merged.count;
|
|
294
315
|
const measurements = this.getMeasurements();
|
|
295
|
-
const
|
|
296
|
-
const
|
|
316
|
+
const previousItems = ((_a = this._singleLaneMeasurements) == null ? void 0 : _a.items) ?? measurements;
|
|
317
|
+
const getPreviousKey = (index) => getMeasurementKey(previousItems[index]);
|
|
318
|
+
const prevFirstKey = prevCount > 0 ? getPreviousKey(0) : null;
|
|
319
|
+
const prevLastKey = prevCount > 0 ? getPreviousKey(prevCount - 1) : null;
|
|
297
320
|
const didCountChange = nextCount !== prevCount;
|
|
298
321
|
const didEdgeKeysChange = didCountChange || prevCount > 0 && nextCount > 0 && (merged.getItemKey(0) !== prevFirstKey || merged.getItemKey(nextCount - 1) !== prevLastKey);
|
|
299
322
|
if (didEdgeKeysChange) {
|
|
@@ -303,8 +326,15 @@ class Virtualizer {
|
|
|
303
326
|
anchor = [item.key, this.getScrollOffset() - item.start];
|
|
304
327
|
}
|
|
305
328
|
const behavior = merged.followOnAppend === true ? "auto" : merged.followOnAppend || null;
|
|
306
|
-
if (behavior && nextCount >
|
|
307
|
-
|
|
329
|
+
if (behavior && nextCount > 0 && this.isAtEnd(prevOptions.scrollEndThreshold) && (prevCount === 0 || merged.getItemKey(nextCount - 1) !== prevLastKey)) {
|
|
330
|
+
if (nextCount > prevCount || isAppendWithTrim(
|
|
331
|
+
prevCount,
|
|
332
|
+
nextCount,
|
|
333
|
+
getPreviousKey,
|
|
334
|
+
merged.getItemKey
|
|
335
|
+
)) {
|
|
336
|
+
followOnAppend = behavior;
|
|
337
|
+
}
|
|
308
338
|
}
|
|
309
339
|
}
|
|
310
340
|
}
|
|
@@ -327,7 +357,7 @@ class Virtualizer {
|
|
|
327
357
|
const anchorItem = newMeasurements[idx];
|
|
328
358
|
if (anchorItem) {
|
|
329
359
|
const newOffset = Math.max(0, anchorItem.start + anchorOffset);
|
|
330
|
-
if (newOffset !== this.scrollOffset) {
|
|
360
|
+
if (!followOnAppend && newOffset !== this.scrollOffset) {
|
|
331
361
|
anchorDelta = newOffset - this.scrollOffset;
|
|
332
362
|
this.scrollOffset = newOffset;
|
|
333
363
|
anchorResolved = true;
|
|
@@ -394,7 +424,7 @@ class Virtualizer {
|
|
|
394
424
|
};
|
|
395
425
|
};
|
|
396
426
|
this._willUpdate = () => {
|
|
397
|
-
var _a;
|
|
427
|
+
var _a, _b;
|
|
398
428
|
const scrollElement = this.options.enabled ? this.options.getScrollElement() : null;
|
|
399
429
|
if (this.scrollElement !== scrollElement) {
|
|
400
430
|
this.cleanup();
|
|
@@ -496,7 +526,11 @@ class Virtualizer {
|
|
|
496
526
|
if (anchorDelta !== 0) {
|
|
497
527
|
this._iosDeferredAdjustment += anchorDelta;
|
|
498
528
|
}
|
|
499
|
-
} else
|
|
529
|
+
} else if (((_b = this.scrollState) == null ? void 0 : _b.behavior) === "smooth" && !approxEqual(
|
|
530
|
+
this.getScrollOffset() - anchorDelta,
|
|
531
|
+
this.scrollState.lastTargetOffset
|
|
532
|
+
)) ;
|
|
533
|
+
else {
|
|
500
534
|
this._scrollToOffset(this.getScrollOffset(), {
|
|
501
535
|
adjustments: void 0,
|
|
502
536
|
behavior: void 0
|
|
@@ -605,9 +639,11 @@ class Virtualizer {
|
|
|
605
639
|
laneAssignmentMode,
|
|
606
640
|
gap
|
|
607
641
|
}, _itemSizeCacheVersion) => {
|
|
642
|
+
var _a;
|
|
608
643
|
const itemSizeCache = this.itemSizeCache;
|
|
609
644
|
if (!enabled) {
|
|
610
645
|
this.measurementsCache = [];
|
|
646
|
+
this._singleLaneMeasurements = null;
|
|
611
647
|
this.itemSizeCache.clear();
|
|
612
648
|
this.laneAssignments.clear();
|
|
613
649
|
return [];
|
|
@@ -623,6 +659,7 @@ class Virtualizer {
|
|
|
623
659
|
this.lanesChangedFlag = false;
|
|
624
660
|
this.lanesSettling = true;
|
|
625
661
|
this.measurementsCache = [];
|
|
662
|
+
this._singleLaneMeasurements = null;
|
|
626
663
|
this.itemSizeCache.clear();
|
|
627
664
|
this.laneAssignments.clear();
|
|
628
665
|
this.pendingMin = null;
|
|
@@ -640,13 +677,13 @@ class Virtualizer {
|
|
|
640
677
|
}
|
|
641
678
|
if (lanes === 1) {
|
|
642
679
|
const need = count * 2;
|
|
643
|
-
let flat = this.
|
|
680
|
+
let flat = (_a = this._singleLaneMeasurements) == null ? void 0 : _a.flat;
|
|
644
681
|
if (!flat || flat.length < need) {
|
|
645
682
|
const next = new Float64Array(need);
|
|
646
683
|
if (flat && min > 0) next.set(flat.subarray(0, min * 2));
|
|
647
684
|
flat = next;
|
|
648
|
-
this._flatMeasurements = flat;
|
|
649
685
|
}
|
|
686
|
+
const items = min === 0 ? new Array(count) : this._singleLaneMeasurements.items.slice();
|
|
650
687
|
let runningStart;
|
|
651
688
|
if (min === 0) {
|
|
652
689
|
runningStart = paddingStart + scrollMargin;
|
|
@@ -656,13 +693,15 @@ class Virtualizer {
|
|
|
656
693
|
}
|
|
657
694
|
for (let i = min; i < count; i++) {
|
|
658
695
|
const key = getItemKey(i);
|
|
696
|
+
items[i] = key;
|
|
659
697
|
const measuredSize = itemSizeCache.get(key);
|
|
660
698
|
const size = typeof measuredSize === "number" ? measuredSize : this.options.estimateSize(i);
|
|
661
699
|
flat[i * 2] = runningStart;
|
|
662
700
|
flat[i * 2 + 1] = size;
|
|
663
701
|
runningStart += size + gap;
|
|
664
702
|
}
|
|
665
|
-
|
|
703
|
+
this._singleLaneMeasurements = { flat, items };
|
|
704
|
+
const view = createLazyMeasurementsView(items, flat);
|
|
666
705
|
this.measurementsCache = view;
|
|
667
706
|
return view;
|
|
668
707
|
}
|
|
@@ -757,7 +796,7 @@ class Virtualizer {
|
|
|
757
796
|
lanes,
|
|
758
797
|
// Pass the typed array so binary search + forward-walk can read
|
|
759
798
|
// start/end directly from Float64Array, skipping the Proxy traps.
|
|
760
|
-
lanes === 1 && this.
|
|
799
|
+
lanes === 1 && this._singleLaneMeasurements !== null ? this._singleLaneMeasurements.flat : null
|
|
761
800
|
);
|
|
762
801
|
return this.range;
|
|
763
802
|
},
|
|
@@ -854,13 +893,13 @@ class Virtualizer {
|
|
|
854
893
|
}
|
|
855
894
|
};
|
|
856
895
|
this.resizeItem = (index, size) => {
|
|
857
|
-
var _a, _b;
|
|
896
|
+
var _a, _b, _c;
|
|
858
897
|
if (!this.isIndexInRange(index)) return;
|
|
859
898
|
let cachedSize;
|
|
860
899
|
let itemStart;
|
|
861
900
|
let key;
|
|
862
|
-
const flat = this.
|
|
863
|
-
if (this.options.lanes === 1 && flat
|
|
901
|
+
const flat = (_a = this._singleLaneMeasurements) == null ? void 0 : _a.flat;
|
|
902
|
+
if (this.options.lanes === 1 && flat != null) {
|
|
864
903
|
key = this.options.getItemKey(index);
|
|
865
904
|
itemStart = flat[index * 2];
|
|
866
905
|
cachedSize = flat[index * 2 + 1];
|
|
@@ -874,7 +913,7 @@ class Virtualizer {
|
|
|
874
913
|
const itemSize = this.itemSizeCache.get(key) ?? cachedSize;
|
|
875
914
|
const delta = size - itemSize;
|
|
876
915
|
if (delta !== 0) {
|
|
877
|
-
const wasAtEnd = this.options.anchorTo === "end" && ((
|
|
916
|
+
const wasAtEnd = this.options.anchorTo === "end" && ((_b = this.scrollState) == null ? void 0 : _b.behavior) !== "smooth" && this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold;
|
|
878
917
|
const prevTotalSize = wasAtEnd ? this.getTotalSize() : 0;
|
|
879
918
|
const scrollOffsetWithAdj = this.getScrollOffset() + this.scrollAdjustments;
|
|
880
919
|
const isFirstMeasure = !this.itemSizeCache.has(key);
|
|
@@ -893,7 +932,7 @@ class Virtualizer {
|
|
|
893
932
|
// scrolling up" cascade.
|
|
894
933
|
itemStart + itemSize <= scrollOffsetWithAdj && this.scrollDirection !== "backward"
|
|
895
934
|
);
|
|
896
|
-
const shouldAdjustScroll = ((
|
|
935
|
+
const shouldAdjustScroll = ((_c = this.scrollState) == null ? void 0 : _c.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
|
|
897
936
|
// The callback expects a VirtualItem; build one lazily only
|
|
898
937
|
// when the consumer actually supplied a custom predicate.
|
|
899
938
|
this.measurementsCache[index] ?? {
|
|
@@ -941,11 +980,12 @@ class Virtualizer {
|
|
|
941
980
|
}
|
|
942
981
|
);
|
|
943
982
|
this.getVirtualItemForOffset = (offset) => {
|
|
983
|
+
var _a;
|
|
944
984
|
const measurements = this.getMeasurements();
|
|
945
985
|
if (measurements.length === 0) {
|
|
946
986
|
return void 0;
|
|
947
987
|
}
|
|
948
|
-
const flat = this.
|
|
988
|
+
const flat = (_a = this._singleLaneMeasurements) == null ? void 0 : _a.flat;
|
|
949
989
|
const useFlat = this.options.lanes === 1 && flat != null;
|
|
950
990
|
const idx = findNearestBinarySearch(
|
|
951
991
|
0,
|
|
@@ -1080,18 +1120,18 @@ class Virtualizer {
|
|
|
1080
1120
|
});
|
|
1081
1121
|
};
|
|
1082
1122
|
this.getTotalSize = () => {
|
|
1083
|
-
var _a;
|
|
1123
|
+
var _a, _b;
|
|
1084
1124
|
const measurements = this.getMeasurements();
|
|
1085
1125
|
let end;
|
|
1086
1126
|
if (measurements.length === 0) {
|
|
1087
1127
|
end = this.options.paddingStart;
|
|
1088
1128
|
} else if (this.options.lanes === 1) {
|
|
1089
1129
|
const lastIdx = measurements.length - 1;
|
|
1090
|
-
const flat = this.
|
|
1130
|
+
const flat = (_a = this._singleLaneMeasurements) == null ? void 0 : _a.flat;
|
|
1091
1131
|
if (flat != null) {
|
|
1092
1132
|
end = flat[lastIdx * 2] + flat[lastIdx * 2 + 1];
|
|
1093
1133
|
} else {
|
|
1094
|
-
end = ((
|
|
1134
|
+
end = ((_b = measurements[lastIdx]) == null ? void 0 : _b.end) ?? 0;
|
|
1095
1135
|
}
|
|
1096
1136
|
} else {
|
|
1097
1137
|
const endByLane = Array(this.options.lanes).fill(null);
|