@tanstack/virtual-core 3.19.11 → 3.19.13
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 +79 -26
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +12 -1
- package/dist/cjs/scroll-classifier.cjs +14 -2
- package/dist/cjs/scroll-classifier.cjs.map +1 -1
- package/dist/cjs/scroll-classifier.d.cts +3 -2
- package/dist/esm/index.d.ts +12 -1
- package/dist/esm/index.js +79 -26
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/scroll-classifier.d.ts +3 -2
- package/dist/esm/scroll-classifier.js +14 -2
- package/dist/esm/scroll-classifier.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +135 -24
- package/src/scroll-classifier.ts +31 -3
package/dist/cjs/index.cjs
CHANGED
|
@@ -203,17 +203,23 @@ class Virtualizer {
|
|
|
203
203
|
this._singleLaneMeasurements = null;
|
|
204
204
|
this.itemSizeCache = /* @__PURE__ */ new Map();
|
|
205
205
|
this.itemSizeCacheVersion = 0;
|
|
206
|
+
this.builtMeasurementOptions = null;
|
|
207
|
+
this.builtItemSizeCacheVersion = -1;
|
|
208
|
+
this.pendingResizes = [];
|
|
206
209
|
this.laneAssignments = /* @__PURE__ */ new Map();
|
|
207
210
|
this.pendingMin = null;
|
|
208
211
|
this.prevLanes = void 0;
|
|
209
212
|
this.lanesChangedFlag = false;
|
|
210
213
|
this.lanesSettling = false;
|
|
214
|
+
this.initialMeasurementsCacheApplied = false;
|
|
211
215
|
this.pendingScrollAnchor = null;
|
|
212
216
|
this.scrollRect = null;
|
|
213
217
|
this.scrollOffset = null;
|
|
214
218
|
this.scrollDirection = null;
|
|
215
219
|
this._holdOffset = 0;
|
|
216
220
|
this._reportedOverscroll = false;
|
|
221
|
+
this._trailing = 0;
|
|
222
|
+
this._drawnListEnd = Infinity;
|
|
217
223
|
this._iosGesture = new iosGesture.IosGesture({
|
|
218
224
|
getWindow: () => this.targetWindow,
|
|
219
225
|
now: () => this.now(),
|
|
@@ -248,6 +254,7 @@ class Virtualizer {
|
|
|
248
254
|
});
|
|
249
255
|
this._lastElementOffset = null;
|
|
250
256
|
this.pendingWrite = null;
|
|
257
|
+
this._ownWriteAt = null;
|
|
251
258
|
this._clampedAdjustment = null;
|
|
252
259
|
this.elementsCache = /* @__PURE__ */ new Map();
|
|
253
260
|
this._resizeBatch = null;
|
|
@@ -456,7 +463,7 @@ class Virtualizer {
|
|
|
456
463
|
this.pendingScrollAnchor = { kind: "anchor", delta: anchorDelta };
|
|
457
464
|
} else {
|
|
458
465
|
this._holdOffset += anchorDelta;
|
|
459
|
-
this.
|
|
466
|
+
this._followAnchor(anchorDelta);
|
|
460
467
|
}
|
|
461
468
|
}
|
|
462
469
|
};
|
|
@@ -502,8 +509,10 @@ class Virtualizer {
|
|
|
502
509
|
this.scrollDirection = null;
|
|
503
510
|
this._holdOffset = 0;
|
|
504
511
|
this._reportedOverscroll = false;
|
|
512
|
+
this._trailing = 0;
|
|
505
513
|
this._clampedAdjustment = null;
|
|
506
514
|
this.pendingWrite = null;
|
|
515
|
+
this._ownWriteAt = null;
|
|
507
516
|
this.scrollElement = null;
|
|
508
517
|
this.targetWindow = null;
|
|
509
518
|
};
|
|
@@ -540,6 +549,7 @@ class Virtualizer {
|
|
|
540
549
|
this.options.observeElementOffset(this, (offset, isScrolling) => {
|
|
541
550
|
var _a2, _b, _c, _d;
|
|
542
551
|
const elementMax = isScrolling ? this._elementMaxScrollOffset() : null;
|
|
552
|
+
this._readTrailing(elementMax);
|
|
543
553
|
const wasOverscrolled = this._reportedOverscroll;
|
|
544
554
|
this._reportedOverscroll = offset < 0 || elementMax !== null && offset > elementMax + 1;
|
|
545
555
|
const elementOffset = offset;
|
|
@@ -565,6 +575,11 @@ class Virtualizer {
|
|
|
565
575
|
this.scrollState = null;
|
|
566
576
|
}
|
|
567
577
|
this.pendingWrite = null;
|
|
578
|
+
if (kind === "readBack" || kind === "clampedReadBack" || kind === "clampedEcho") {
|
|
579
|
+
this._ownWriteAt = offset;
|
|
580
|
+
} else if (kind !== "restEcho") {
|
|
581
|
+
this._ownWriteAt = null;
|
|
582
|
+
}
|
|
568
583
|
if (this._clampedAdjustment !== null && Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5) {
|
|
569
584
|
this._clampedAdjustment = null;
|
|
570
585
|
}
|
|
@@ -615,6 +630,8 @@ class Virtualizer {
|
|
|
615
630
|
);
|
|
616
631
|
}
|
|
617
632
|
this._scrollToOffset(this.getScrollOffset());
|
|
633
|
+
this._readTrailing(this._elementMaxScrollOffset());
|
|
634
|
+
this.maybeNotify();
|
|
618
635
|
}
|
|
619
636
|
this._noteClampMax();
|
|
620
637
|
const anchor = this.pendingScrollAnchor;
|
|
@@ -623,14 +640,11 @@ class Virtualizer {
|
|
|
623
640
|
if (anchor.kind === "anchor") {
|
|
624
641
|
if (!this._canWriteScroll()) {
|
|
625
642
|
this._holdOffset += anchor.delta;
|
|
626
|
-
this.
|
|
643
|
+
this._followAnchor(anchor.delta);
|
|
627
644
|
this.notify(false);
|
|
628
|
-
} else if (isSmoothScroll(this.scrollState) && !
|
|
629
|
-
this.getScrollOffset() - anchor.delta,
|
|
630
|
-
this.scrollState.lastTargetOffset
|
|
631
|
-
)) ;
|
|
645
|
+
} else if (isSmoothScroll(this.scrollState) && !this._landedBeforeAnchor(this.scrollState, anchor.delta)) ;
|
|
632
646
|
else {
|
|
633
|
-
this.
|
|
647
|
+
this._followAnchor(anchor.delta);
|
|
634
648
|
this._scrollToOffset(this.getScrollOffset());
|
|
635
649
|
}
|
|
636
650
|
} else {
|
|
@@ -649,7 +663,8 @@ class Virtualizer {
|
|
|
649
663
|
holdOffset: this._holdOffset,
|
|
650
664
|
scrollOffset: this.scrollOffset,
|
|
651
665
|
pendingWrite: this.pendingWrite,
|
|
652
|
-
clampedMaxAtWrite: ((_a = this._clampedAdjustment) == null ? void 0 : _a.maxAtWrite) ?? null
|
|
666
|
+
clampedMaxAtWrite: ((_a = this._clampedAdjustment) == null ? void 0 : _a.maxAtWrite) ?? null,
|
|
667
|
+
ownWriteAt: this._ownWriteAt
|
|
653
668
|
});
|
|
654
669
|
};
|
|
655
670
|
this._retryClampedAdjustment = () => {
|
|
@@ -759,23 +774,28 @@ class Virtualizer {
|
|
|
759
774
|
this.isIndexInRange = (index) => index >= 0 && index < this.options.count;
|
|
760
775
|
this.getMeasurements = utils.memo(
|
|
761
776
|
() => [this.getMeasurementOptions(), this.itemSizeCacheVersion],
|
|
762
|
-
({
|
|
763
|
-
count,
|
|
764
|
-
paddingStart,
|
|
765
|
-
scrollMargin,
|
|
766
|
-
getItemKey,
|
|
767
|
-
enabled,
|
|
768
|
-
lanes,
|
|
769
|
-
laneAssignmentMode,
|
|
770
|
-
gap
|
|
771
|
-
}, _itemSizeCacheVersion) => {
|
|
777
|
+
(measurementOptions, itemSizeCacheVersion) => {
|
|
772
778
|
var _a;
|
|
779
|
+
const {
|
|
780
|
+
count,
|
|
781
|
+
paddingStart,
|
|
782
|
+
scrollMargin,
|
|
783
|
+
getItemKey,
|
|
784
|
+
enabled,
|
|
785
|
+
lanes,
|
|
786
|
+
laneAssignmentMode,
|
|
787
|
+
gap
|
|
788
|
+
} = measurementOptions;
|
|
789
|
+
this.builtMeasurementOptions = measurementOptions;
|
|
790
|
+
this.builtItemSizeCacheVersion = itemSizeCacheVersion;
|
|
791
|
+
this.pendingResizes = [];
|
|
773
792
|
const itemSizeCache = this.itemSizeCache;
|
|
774
793
|
if (!enabled) {
|
|
775
794
|
this.measurementsCache = [];
|
|
776
795
|
this._singleLaneMeasurements = null;
|
|
777
796
|
this.itemSizeCache.clear();
|
|
778
797
|
this.laneAssignments.clear();
|
|
798
|
+
this.initialMeasurementsCacheApplied = false;
|
|
779
799
|
return [];
|
|
780
800
|
}
|
|
781
801
|
if (this.laneAssignments.size > count) {
|
|
@@ -794,7 +814,8 @@ class Virtualizer {
|
|
|
794
814
|
this.laneAssignments.clear();
|
|
795
815
|
this.pendingMin = null;
|
|
796
816
|
}
|
|
797
|
-
if (this.measurementsCache.length === 0 && !this.lanesSettling) {
|
|
817
|
+
if (!this.initialMeasurementsCacheApplied && this.measurementsCache.length === 0 && !this.lanesSettling) {
|
|
818
|
+
this.initialMeasurementsCacheApplied = true;
|
|
798
819
|
this.measurementsCache = this.options.initialMeasurementsCache;
|
|
799
820
|
this.measurementsCache.forEach((item) => {
|
|
800
821
|
this.itemSizeCache.set(item.key, item.size);
|
|
@@ -915,14 +936,15 @@ class Virtualizer {
|
|
|
915
936
|
this.options.lanes,
|
|
916
937
|
// scrollOffset is measured in the scroll element and the layout total
|
|
917
938
|
// from the list start, so the list ends scrollMargin further down.
|
|
918
|
-
this._getLayoutTotalSize() + this.options.scrollMargin
|
|
939
|
+
this._getLayoutTotalSize() + this.options.scrollMargin,
|
|
940
|
+
this._trailing
|
|
919
941
|
],
|
|
920
|
-
(measurements, outerSize, scrollOffset, lanes, listEnd) => {
|
|
942
|
+
(measurements, outerSize, scrollOffset, lanes, listEnd, trailing) => {
|
|
921
943
|
if (measurements.length === 0 || outerSize === 0) {
|
|
922
944
|
this.range = null;
|
|
923
945
|
return null;
|
|
924
946
|
}
|
|
925
|
-
const maxScrollOffset = Math.max(listEnd - outerSize, 0);
|
|
947
|
+
const maxScrollOffset = Math.max(listEnd + trailing - outerSize, 0);
|
|
926
948
|
this.range = calculateRangeImpl(
|
|
927
949
|
measurements,
|
|
928
950
|
outerSize,
|
|
@@ -1028,17 +1050,27 @@ class Virtualizer {
|
|
|
1028
1050
|
this.resizeItem(index, this.options.measureElement(node, void 0, this));
|
|
1029
1051
|
}
|
|
1030
1052
|
};
|
|
1053
|
+
this.canShiftFromPendingResizes = () => this.options.lanes === 1 && this._singleLaneMeasurements !== null && this.shouldAdjustScrollPositionOnItemSizeChange === void 0 && !this.lanesChangedFlag && !this.lanesSettling && this.getMeasurementOptions() === this.builtMeasurementOptions && this.itemSizeCacheVersion === this.builtItemSizeCacheVersion + this.pendingResizes.length;
|
|
1031
1054
|
this.resizeItem = (index, size) => {
|
|
1032
1055
|
var _a, _b, _c;
|
|
1033
1056
|
if (!this.isIndexInRange(index)) return;
|
|
1034
|
-
|
|
1057
|
+
let startShift = 0;
|
|
1058
|
+
if ((_a = this._resizeBatch) == null ? void 0 : _a.sync) {
|
|
1059
|
+
if (this.canShiftFromPendingResizes()) {
|
|
1060
|
+
for (const resize of this.pendingResizes) {
|
|
1061
|
+
if (resize.index < index) startShift += resize.delta;
|
|
1062
|
+
}
|
|
1063
|
+
} else {
|
|
1064
|
+
this.getMeasurements();
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1035
1067
|
let cachedSize;
|
|
1036
1068
|
let itemStart;
|
|
1037
1069
|
let key;
|
|
1038
1070
|
const flat = (_b = this._singleLaneMeasurements) == null ? void 0 : _b.flat;
|
|
1039
1071
|
if (this.options.lanes === 1 && flat != null) {
|
|
1040
1072
|
key = this.options.getItemKey(index);
|
|
1041
|
-
itemStart = flat[index * 2];
|
|
1073
|
+
itemStart = flat[index * 2] + startShift;
|
|
1042
1074
|
cachedSize = flat[index * 2 + 1];
|
|
1043
1075
|
} else {
|
|
1044
1076
|
const item = this.measurementsCache[index];
|
|
@@ -1088,6 +1120,7 @@ class Virtualizer {
|
|
|
1088
1120
|
}
|
|
1089
1121
|
this.itemSizeCache.set(key, size);
|
|
1090
1122
|
this.itemSizeCacheVersion++;
|
|
1123
|
+
this.pendingResizes.push({ index, delta });
|
|
1091
1124
|
let adjustedSync = false;
|
|
1092
1125
|
if (!this._awaitingCommandReadBack()) {
|
|
1093
1126
|
if (wasAtEnd) {
|
|
@@ -1107,6 +1140,9 @@ class Virtualizer {
|
|
|
1107
1140
|
this._noteClampMax();
|
|
1108
1141
|
this._flushHoldIfReady(true);
|
|
1109
1142
|
this._retryClampedAdjustment();
|
|
1143
|
+
} else if (this.options.lanes > 1 && !this.options.useCachedMeasurements && !this.laneAssignments.has(index)) {
|
|
1144
|
+
const item = this.getMeasurements()[index];
|
|
1145
|
+
if (item) this.laneAssignments.set(index, item.lane);
|
|
1110
1146
|
}
|
|
1111
1147
|
};
|
|
1112
1148
|
this.getVirtualItems = utils.memo(
|
|
@@ -1166,6 +1202,11 @@ class Virtualizer {
|
|
|
1166
1202
|
}
|
|
1167
1203
|
return offset;
|
|
1168
1204
|
};
|
|
1205
|
+
this._readTrailing = (elementMax) => {
|
|
1206
|
+
if (elementMax === null) return;
|
|
1207
|
+
const trailing = elementMax + this.getSize() - this._drawnListEnd;
|
|
1208
|
+
this._trailing = trailing > 0 ? trailing : 0;
|
|
1209
|
+
};
|
|
1169
1210
|
this._elementMaxScrollOffset = () => {
|
|
1170
1211
|
const el = this.scrollElement;
|
|
1171
1212
|
if (el === null) return null;
|
|
@@ -1331,13 +1372,17 @@ class Virtualizer {
|
|
|
1331
1372
|
}
|
|
1332
1373
|
this.scrollState = null;
|
|
1333
1374
|
};
|
|
1334
|
-
this.getTotalSize = () =>
|
|
1375
|
+
this.getTotalSize = () => {
|
|
1376
|
+
const total = Math.max(this._getLayoutTotalSize() - this._holdOffset, 0);
|
|
1377
|
+
this._drawnListEnd = total + this.options.scrollMargin;
|
|
1378
|
+
return total;
|
|
1379
|
+
};
|
|
1335
1380
|
this._getLayoutTotalSize = () => {
|
|
1336
1381
|
var _a, _b;
|
|
1337
1382
|
const measurements = this.getMeasurements();
|
|
1338
1383
|
let end;
|
|
1339
1384
|
if (measurements.length === 0) {
|
|
1340
|
-
end = this.options.paddingStart;
|
|
1385
|
+
end = this.options.scrollMargin + this.options.paddingStart;
|
|
1341
1386
|
} else if (this.options.lanes === 1) {
|
|
1342
1387
|
const lastIdx = measurements.length - 1;
|
|
1343
1388
|
const flat = (_a = this._singleLaneMeasurements) == null ? void 0 : _a.flat;
|
|
@@ -1407,6 +1452,14 @@ class Virtualizer {
|
|
|
1407
1452
|
this.scrollState.lastTargetOffset += delta;
|
|
1408
1453
|
}
|
|
1409
1454
|
};
|
|
1455
|
+
this._landedBeforeAnchor = (state, delta) => state.phase !== "queued" && (state.stableFrames > 0 || utils.approxEqual(this.getScrollOffset() - delta, state.lastTargetOffset));
|
|
1456
|
+
this._followAnchor = (delta) => {
|
|
1457
|
+
this._followOffsetTarget(delta);
|
|
1458
|
+
const state = this.scrollState;
|
|
1459
|
+
if ((state == null ? void 0 : state.index) != null && !state.toEnd && this._landedBeforeAnchor(state, delta)) {
|
|
1460
|
+
this.scrollState = null;
|
|
1461
|
+
}
|
|
1462
|
+
};
|
|
1410
1463
|
this.measure = () => {
|
|
1411
1464
|
this.pendingMin = null;
|
|
1412
1465
|
this.itemSizeCache.clear();
|