@tanstack/virtual-core 3.17.9 → 3.17.10
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 +56 -20
- 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 +57 -21
- 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 +76 -28
- 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,4 +1,4 @@
|
|
|
1
|
-
import { createLazyMeasurementsView } from "./lazy-measurements.js";
|
|
1
|
+
import { createLazyMeasurementsView, getMeasurementKey } from "./lazy-measurements.js";
|
|
2
2
|
import { memo, notUndefined, approxEqual, debounce } from "./utils.js";
|
|
3
3
|
let _isIOSResult;
|
|
4
4
|
const isIOSWebKit = () => {
|
|
@@ -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;
|
|
@@ -605,9 +635,11 @@ class Virtualizer {
|
|
|
605
635
|
laneAssignmentMode,
|
|
606
636
|
gap
|
|
607
637
|
}, _itemSizeCacheVersion) => {
|
|
638
|
+
var _a;
|
|
608
639
|
const itemSizeCache = this.itemSizeCache;
|
|
609
640
|
if (!enabled) {
|
|
610
641
|
this.measurementsCache = [];
|
|
642
|
+
this._singleLaneMeasurements = null;
|
|
611
643
|
this.itemSizeCache.clear();
|
|
612
644
|
this.laneAssignments.clear();
|
|
613
645
|
return [];
|
|
@@ -623,6 +655,7 @@ class Virtualizer {
|
|
|
623
655
|
this.lanesChangedFlag = false;
|
|
624
656
|
this.lanesSettling = true;
|
|
625
657
|
this.measurementsCache = [];
|
|
658
|
+
this._singleLaneMeasurements = null;
|
|
626
659
|
this.itemSizeCache.clear();
|
|
627
660
|
this.laneAssignments.clear();
|
|
628
661
|
this.pendingMin = null;
|
|
@@ -640,13 +673,13 @@ class Virtualizer {
|
|
|
640
673
|
}
|
|
641
674
|
if (lanes === 1) {
|
|
642
675
|
const need = count * 2;
|
|
643
|
-
let flat = this.
|
|
676
|
+
let flat = (_a = this._singleLaneMeasurements) == null ? void 0 : _a.flat;
|
|
644
677
|
if (!flat || flat.length < need) {
|
|
645
678
|
const next = new Float64Array(need);
|
|
646
679
|
if (flat && min > 0) next.set(flat.subarray(0, min * 2));
|
|
647
680
|
flat = next;
|
|
648
|
-
this._flatMeasurements = flat;
|
|
649
681
|
}
|
|
682
|
+
const items = min === 0 ? new Array(count) : this._singleLaneMeasurements.items.slice();
|
|
650
683
|
let runningStart;
|
|
651
684
|
if (min === 0) {
|
|
652
685
|
runningStart = paddingStart + scrollMargin;
|
|
@@ -656,13 +689,15 @@ class Virtualizer {
|
|
|
656
689
|
}
|
|
657
690
|
for (let i = min; i < count; i++) {
|
|
658
691
|
const key = getItemKey(i);
|
|
692
|
+
items[i] = key;
|
|
659
693
|
const measuredSize = itemSizeCache.get(key);
|
|
660
694
|
const size = typeof measuredSize === "number" ? measuredSize : this.options.estimateSize(i);
|
|
661
695
|
flat[i * 2] = runningStart;
|
|
662
696
|
flat[i * 2 + 1] = size;
|
|
663
697
|
runningStart += size + gap;
|
|
664
698
|
}
|
|
665
|
-
|
|
699
|
+
this._singleLaneMeasurements = { flat, items };
|
|
700
|
+
const view = createLazyMeasurementsView(items, flat);
|
|
666
701
|
this.measurementsCache = view;
|
|
667
702
|
return view;
|
|
668
703
|
}
|
|
@@ -757,7 +792,7 @@ class Virtualizer {
|
|
|
757
792
|
lanes,
|
|
758
793
|
// Pass the typed array so binary search + forward-walk can read
|
|
759
794
|
// start/end directly from Float64Array, skipping the Proxy traps.
|
|
760
|
-
lanes === 1 && this.
|
|
795
|
+
lanes === 1 && this._singleLaneMeasurements !== null ? this._singleLaneMeasurements.flat : null
|
|
761
796
|
);
|
|
762
797
|
return this.range;
|
|
763
798
|
},
|
|
@@ -854,13 +889,13 @@ class Virtualizer {
|
|
|
854
889
|
}
|
|
855
890
|
};
|
|
856
891
|
this.resizeItem = (index, size) => {
|
|
857
|
-
var _a, _b;
|
|
892
|
+
var _a, _b, _c;
|
|
858
893
|
if (!this.isIndexInRange(index)) return;
|
|
859
894
|
let cachedSize;
|
|
860
895
|
let itemStart;
|
|
861
896
|
let key;
|
|
862
|
-
const flat = this.
|
|
863
|
-
if (this.options.lanes === 1 && flat
|
|
897
|
+
const flat = (_a = this._singleLaneMeasurements) == null ? void 0 : _a.flat;
|
|
898
|
+
if (this.options.lanes === 1 && flat != null) {
|
|
864
899
|
key = this.options.getItemKey(index);
|
|
865
900
|
itemStart = flat[index * 2];
|
|
866
901
|
cachedSize = flat[index * 2 + 1];
|
|
@@ -874,7 +909,7 @@ class Virtualizer {
|
|
|
874
909
|
const itemSize = this.itemSizeCache.get(key) ?? cachedSize;
|
|
875
910
|
const delta = size - itemSize;
|
|
876
911
|
if (delta !== 0) {
|
|
877
|
-
const wasAtEnd = this.options.anchorTo === "end" && ((
|
|
912
|
+
const wasAtEnd = this.options.anchorTo === "end" && ((_b = this.scrollState) == null ? void 0 : _b.behavior) !== "smooth" && this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold;
|
|
878
913
|
const prevTotalSize = wasAtEnd ? this.getTotalSize() : 0;
|
|
879
914
|
const scrollOffsetWithAdj = this.getScrollOffset() + this.scrollAdjustments;
|
|
880
915
|
const isFirstMeasure = !this.itemSizeCache.has(key);
|
|
@@ -893,7 +928,7 @@ class Virtualizer {
|
|
|
893
928
|
// scrolling up" cascade.
|
|
894
929
|
itemStart + itemSize <= scrollOffsetWithAdj && this.scrollDirection !== "backward"
|
|
895
930
|
);
|
|
896
|
-
const shouldAdjustScroll = ((
|
|
931
|
+
const shouldAdjustScroll = ((_c = this.scrollState) == null ? void 0 : _c.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
|
|
897
932
|
// The callback expects a VirtualItem; build one lazily only
|
|
898
933
|
// when the consumer actually supplied a custom predicate.
|
|
899
934
|
this.measurementsCache[index] ?? {
|
|
@@ -941,11 +976,12 @@ class Virtualizer {
|
|
|
941
976
|
}
|
|
942
977
|
);
|
|
943
978
|
this.getVirtualItemForOffset = (offset) => {
|
|
979
|
+
var _a;
|
|
944
980
|
const measurements = this.getMeasurements();
|
|
945
981
|
if (measurements.length === 0) {
|
|
946
982
|
return void 0;
|
|
947
983
|
}
|
|
948
|
-
const flat = this.
|
|
984
|
+
const flat = (_a = this._singleLaneMeasurements) == null ? void 0 : _a.flat;
|
|
949
985
|
const useFlat = this.options.lanes === 1 && flat != null;
|
|
950
986
|
const idx = findNearestBinarySearch(
|
|
951
987
|
0,
|
|
@@ -1080,18 +1116,18 @@ class Virtualizer {
|
|
|
1080
1116
|
});
|
|
1081
1117
|
};
|
|
1082
1118
|
this.getTotalSize = () => {
|
|
1083
|
-
var _a;
|
|
1119
|
+
var _a, _b;
|
|
1084
1120
|
const measurements = this.getMeasurements();
|
|
1085
1121
|
let end;
|
|
1086
1122
|
if (measurements.length === 0) {
|
|
1087
1123
|
end = this.options.paddingStart;
|
|
1088
1124
|
} else if (this.options.lanes === 1) {
|
|
1089
1125
|
const lastIdx = measurements.length - 1;
|
|
1090
|
-
const flat = this.
|
|
1126
|
+
const flat = (_a = this._singleLaneMeasurements) == null ? void 0 : _a.flat;
|
|
1091
1127
|
if (flat != null) {
|
|
1092
1128
|
end = flat[lastIdx * 2] + flat[lastIdx * 2 + 1];
|
|
1093
1129
|
} else {
|
|
1094
|
-
end = ((
|
|
1130
|
+
end = ((_b = measurements[lastIdx]) == null ? void 0 : _b.end) ?? 0;
|
|
1095
1131
|
}
|
|
1096
1132
|
} else {
|
|
1097
1133
|
const endByLane = Array(this.options.lanes).fill(null);
|