@tanstack/virtual-core 3.18.0 → 3.19.1
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 +98 -45
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +7 -1
- package/dist/esm/index.d.ts +7 -1
- package/dist/esm/index.js +98 -45
- package/dist/esm/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +163 -50
package/dist/cjs/index.cjs
CHANGED
|
@@ -19,8 +19,9 @@ const getRect = (element) => {
|
|
|
19
19
|
};
|
|
20
20
|
const defaultKeyExtractor = (index) => index;
|
|
21
21
|
const defaultRangeExtractor = (range) => {
|
|
22
|
-
const
|
|
23
|
-
const
|
|
22
|
+
const overscan = range.overscan * Math.max(range.lanes ?? 1, 1);
|
|
23
|
+
const start = Math.max(range.startIndex - overscan, 0);
|
|
24
|
+
const end = Math.min(range.endIndex + overscan, range.count - 1);
|
|
24
25
|
const len = end - start + 1;
|
|
25
26
|
const arr = new Array(len);
|
|
26
27
|
for (let i = 0; i < len; i++) {
|
|
@@ -213,6 +214,7 @@ class Virtualizer {
|
|
|
213
214
|
this._intendedScrollOffset = null;
|
|
214
215
|
this._clampedAdjustment = null;
|
|
215
216
|
this.elementsCache = /* @__PURE__ */ new Map();
|
|
217
|
+
this._resizeBatch = null;
|
|
216
218
|
this.now = () => {
|
|
217
219
|
var _a, _b, _c;
|
|
218
220
|
return ((_c = (_b = (_a = this.targetWindow) == null ? void 0 : _a.performance) == null ? void 0 : _b.now) == null ? void 0 : _c.call(_b)) ?? Date.now();
|
|
@@ -227,32 +229,40 @@ class Virtualizer {
|
|
|
227
229
|
return null;
|
|
228
230
|
}
|
|
229
231
|
return _ro = new this.targetWindow.ResizeObserver((entries) => {
|
|
230
|
-
|
|
231
|
-
const
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
return;
|
|
232
|
+
const run = () => {
|
|
233
|
+
const batch = { sync: false };
|
|
234
|
+
this._resizeBatch = batch;
|
|
235
|
+
try {
|
|
236
|
+
entries.forEach(measureEntry);
|
|
237
|
+
} finally {
|
|
238
|
+
this._resizeBatch = null;
|
|
239
|
+
if (batch.notify) {
|
|
240
|
+
this.notify(batch.sync);
|
|
241
|
+
this._retryClampedAdjustment();
|
|
243
242
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
index,
|
|
248
|
-
this.options.measureElement(node, entry, this)
|
|
249
|
-
);
|
|
250
|
-
}
|
|
251
|
-
};
|
|
252
|
-
this.options.useAnimationFrameWithResizeObserver ? requestAnimationFrame(run) : run();
|
|
253
|
-
});
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
this.options.useAnimationFrameWithResizeObserver ? requestAnimationFrame(run) : run();
|
|
254
246
|
});
|
|
255
247
|
};
|
|
248
|
+
const measureEntry = (entry) => {
|
|
249
|
+
const node = entry.target;
|
|
250
|
+
const index = this.indexFromElement(node);
|
|
251
|
+
if (!node.isConnected) {
|
|
252
|
+
this.observer.unobserve(node);
|
|
253
|
+
for (const [cacheKey, cachedNode] of this.elementsCache) {
|
|
254
|
+
if (cachedNode === node) {
|
|
255
|
+
this.elementsCache.delete(cacheKey);
|
|
256
|
+
break;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
if (!this.isIndexInRange(index)) return;
|
|
262
|
+
if (this.shouldMeasureDuringScroll(index)) {
|
|
263
|
+
this.resizeItem(index, this.options.measureElement(node, entry, this));
|
|
264
|
+
}
|
|
265
|
+
};
|
|
256
266
|
return {
|
|
257
267
|
disconnect: () => {
|
|
258
268
|
var _a;
|
|
@@ -457,10 +467,24 @@ class Virtualizer {
|
|
|
457
467
|
}
|
|
458
468
|
if (this._intendedScrollOffset !== null && Math.abs(offset - this._intendedScrollOffset) < 1.5) {
|
|
459
469
|
offset = this._intendedScrollOffset;
|
|
460
|
-
} else if (((_a2 = this.scrollState) == null ? void 0 : _a2.index) != null &&
|
|
470
|
+
} else if (((_a2 = this.scrollState) == null ? void 0 : _a2.index) != null && // A non-smooth write lands at once, so any other scroll event is
|
|
471
|
+
// the user's, even before reconcile saw the target reached (the
|
|
472
|
+
// target may keep moving, or iOS ignored the write under a
|
|
473
|
+
// finger). A write the browser clamped because the sizer hasn't
|
|
474
|
+
// grown yet reads back at the max, which is also where the fresh
|
|
475
|
+
// target clamps, so the check below keeps it reconciling (#1294).
|
|
476
|
+
// Not when rows above the target shrink before the read-back and
|
|
477
|
+
// pull the target below the new max: the read-back then looks
|
|
478
|
+
// like the user and the scroll stops at the max. That needs the
|
|
479
|
+
// sizer re-rendered before the scroll event, which browsers have
|
|
480
|
+
// not been seen to do (pinned by a `.fails` test).
|
|
481
|
+
// A smooth scroll's own animation events look like a user's, so
|
|
482
|
+
// it only yields once it has reached the target.
|
|
483
|
+
(this.scrollState.hasReachedTarget || !this.scrollState.startedSmooth) && !utils.approxEqual(offset, this.scrollState.lastTargetOffset) && !utils.approxEqual(offset, this.getScrollStateTarget(this.scrollState)) && (this._clampedAdjustment === null || Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5)) {
|
|
461
484
|
this.scrollState = null;
|
|
462
485
|
}
|
|
463
486
|
this._intendedScrollOffset = null;
|
|
487
|
+
if (this.scrollState) this.scrollState.writtenOffset = null;
|
|
464
488
|
if (this._clampedAdjustment !== null && Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5) {
|
|
465
489
|
this._clampedAdjustment = null;
|
|
466
490
|
}
|
|
@@ -825,15 +849,17 @@ class Virtualizer {
|
|
|
825
849
|
this.options.overscan,
|
|
826
850
|
this.options.count,
|
|
827
851
|
startIndex,
|
|
828
|
-
endIndex
|
|
852
|
+
endIndex,
|
|
853
|
+
this.options.lanes
|
|
829
854
|
];
|
|
830
855
|
},
|
|
831
|
-
(rangeExtractor, overscan, count, startIndex, endIndex) => {
|
|
856
|
+
(rangeExtractor, overscan, count, startIndex, endIndex, lanes) => {
|
|
832
857
|
return startIndex === null || endIndex === null ? [] : rangeExtractor({
|
|
833
858
|
startIndex,
|
|
834
859
|
endIndex,
|
|
835
860
|
overscan,
|
|
836
|
-
count
|
|
861
|
+
count,
|
|
862
|
+
lanes
|
|
837
863
|
});
|
|
838
864
|
},
|
|
839
865
|
{
|
|
@@ -898,12 +924,13 @@ class Virtualizer {
|
|
|
898
924
|
}
|
|
899
925
|
};
|
|
900
926
|
this.resizeItem = (index, size) => {
|
|
901
|
-
var _a, _b, _c;
|
|
927
|
+
var _a, _b, _c, _d, _e, _f;
|
|
902
928
|
if (!this.isIndexInRange(index)) return;
|
|
929
|
+
if ((_a = this._resizeBatch) == null ? void 0 : _a.sync) this.getMeasurements();
|
|
903
930
|
let cachedSize;
|
|
904
931
|
let itemStart;
|
|
905
932
|
let key;
|
|
906
|
-
const flat = (
|
|
933
|
+
const flat = (_b = this._singleLaneMeasurements) == null ? void 0 : _b.flat;
|
|
907
934
|
if (this.options.lanes === 1 && flat != null) {
|
|
908
935
|
key = this.options.getItemKey(index);
|
|
909
936
|
itemStart = flat[index * 2];
|
|
@@ -918,7 +945,7 @@ class Virtualizer {
|
|
|
918
945
|
const itemSize = this.itemSizeCache.get(key) ?? cachedSize;
|
|
919
946
|
const delta = size - itemSize;
|
|
920
947
|
if (delta !== 0) {
|
|
921
|
-
const wasAtEnd = this.options.anchorTo === "end" && ((
|
|
948
|
+
const wasAtEnd = this.options.anchorTo === "end" && ((_c = this.scrollState) == null ? void 0 : _c.behavior) !== "smooth" && this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold;
|
|
922
949
|
const prevTotalSize = wasAtEnd ? this.getTotalSize() : 0;
|
|
923
950
|
const scrollOffsetWithAdj = this.getScrollOffset() + this.scrollAdjustments;
|
|
924
951
|
const isFirstMeasure = !this.itemSizeCache.has(key);
|
|
@@ -937,7 +964,7 @@ class Virtualizer {
|
|
|
937
964
|
// scrolling up" cascade.
|
|
938
965
|
itemStart + itemSize <= scrollOffsetWithAdj && this.scrollDirection !== "backward"
|
|
939
966
|
);
|
|
940
|
-
const shouldAdjustScroll = ((
|
|
967
|
+
const shouldAdjustScroll = ((_d = this.scrollState) == null ? void 0 : _d.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
|
|
941
968
|
// The callback expects a VirtualItem; build one lazily only
|
|
942
969
|
// when the consumer actually supplied a custom predicate.
|
|
943
970
|
this.measurementsCache[index] ?? {
|
|
@@ -956,13 +983,24 @@ class Virtualizer {
|
|
|
956
983
|
}
|
|
957
984
|
this.itemSizeCache.set(key, size);
|
|
958
985
|
this.itemSizeCacheVersion++;
|
|
986
|
+
const awaitingCommandReadBack = ((_e = this.scrollState) == null ? void 0 : _e.writtenOffset) != null && !utils.approxEqual(
|
|
987
|
+
this.scrollState.writtenOffset,
|
|
988
|
+
this.getScrollOffset() + this.scrollAdjustments
|
|
989
|
+
);
|
|
959
990
|
let adjustedSync = false;
|
|
960
|
-
if (
|
|
961
|
-
|
|
962
|
-
this.
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
991
|
+
if (!awaitingCommandReadBack) {
|
|
992
|
+
if (wasAtEnd) {
|
|
993
|
+
adjustedSync = this.applyScrollAdjustment(
|
|
994
|
+
this.getTotalSize() - prevTotalSize
|
|
995
|
+
);
|
|
996
|
+
} else if (shouldAdjustScroll) {
|
|
997
|
+
adjustedSync = this.applyScrollAdjustment(delta);
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
if (this._resizeBatch) {
|
|
1001
|
+
this._resizeBatch.notify = true;
|
|
1002
|
+
(_f = this._resizeBatch).sync || (_f.sync = adjustedSync);
|
|
1003
|
+
return;
|
|
966
1004
|
}
|
|
967
1005
|
this.notify(adjustedSync);
|
|
968
1006
|
this._retryClampedAdjustment();
|
|
@@ -1073,7 +1111,8 @@ class Virtualizer {
|
|
|
1073
1111
|
startedAt: now,
|
|
1074
1112
|
lastTargetOffset: offset,
|
|
1075
1113
|
stableFrames: 0,
|
|
1076
|
-
hasReachedTarget: false
|
|
1114
|
+
hasReachedTarget: false,
|
|
1115
|
+
startedSmooth: behavior === "smooth"
|
|
1077
1116
|
};
|
|
1078
1117
|
this._scrollToOffset(offset, { adjustments: void 0, behavior });
|
|
1079
1118
|
this.scheduleScrollReconcile();
|
|
@@ -1097,13 +1136,18 @@ class Virtualizer {
|
|
|
1097
1136
|
startedAt: now,
|
|
1098
1137
|
lastTargetOffset: offset,
|
|
1099
1138
|
stableFrames: 0,
|
|
1100
|
-
hasReachedTarget: utils.approxEqual(offset, this.getScrollOffset())
|
|
1139
|
+
hasReachedTarget: utils.approxEqual(offset, this.getScrollOffset()),
|
|
1140
|
+
startedSmooth: behavior === "smooth"
|
|
1101
1141
|
};
|
|
1102
1142
|
this._scrollToOffset(offset, { adjustments: void 0, behavior });
|
|
1103
1143
|
this.scheduleScrollReconcile();
|
|
1104
1144
|
};
|
|
1105
1145
|
this.scrollBy = (delta, { behavior = "auto" } = {}) => {
|
|
1106
|
-
|
|
1146
|
+
var _a;
|
|
1147
|
+
const offset = this.getOffsetForAlignment(
|
|
1148
|
+
(((_a = this.scrollState) == null ? void 0 : _a.writtenOffset) ?? this.getScrollOffset()) + delta,
|
|
1149
|
+
"start"
|
|
1150
|
+
);
|
|
1107
1151
|
const now = this.now();
|
|
1108
1152
|
this.scrollState = {
|
|
1109
1153
|
index: null,
|
|
@@ -1112,7 +1156,8 @@ class Virtualizer {
|
|
|
1112
1156
|
startedAt: now,
|
|
1113
1157
|
lastTargetOffset: offset,
|
|
1114
1158
|
stableFrames: 0,
|
|
1115
|
-
hasReachedTarget: false
|
|
1159
|
+
hasReachedTarget: false,
|
|
1160
|
+
startedSmooth: behavior === "smooth"
|
|
1116
1161
|
};
|
|
1117
1162
|
this._scrollToOffset(offset, { adjustments: void 0, behavior });
|
|
1118
1163
|
this.scheduleScrollReconcile();
|
|
@@ -1129,7 +1174,8 @@ class Virtualizer {
|
|
|
1129
1174
|
startedAt: this.now(),
|
|
1130
1175
|
lastTargetOffset: offset,
|
|
1131
1176
|
stableFrames: 0,
|
|
1132
|
-
hasReachedTarget: utils.approxEqual(offset, this.getScrollOffset())
|
|
1177
|
+
hasReachedTarget: utils.approxEqual(offset, this.getScrollOffset()),
|
|
1178
|
+
startedSmooth: behavior === "smooth"
|
|
1133
1179
|
};
|
|
1134
1180
|
this._scrollToOffset(offset, { adjustments: void 0, behavior });
|
|
1135
1181
|
this.scheduleScrollReconcile();
|
|
@@ -1200,6 +1246,9 @@ class Virtualizer {
|
|
|
1200
1246
|
behavior
|
|
1201
1247
|
}) => {
|
|
1202
1248
|
this._intendedScrollOffset = offset + (adjustments ?? 0);
|
|
1249
|
+
if (this.scrollState) {
|
|
1250
|
+
this.scrollState.writtenOffset = behavior === "smooth" ? null : this._intendedScrollOffset;
|
|
1251
|
+
}
|
|
1203
1252
|
this.options.scrollToFn(offset, { behavior, adjustments }, this);
|
|
1204
1253
|
};
|
|
1205
1254
|
this.measure = () => {
|
|
@@ -1216,12 +1265,15 @@ class Virtualizer {
|
|
|
1216
1265
|
// `resizeItem` uses that to decide whether the follow-up `notify` must be
|
|
1217
1266
|
// synchronous so the grown transforms commit in the same paint (#1227).
|
|
1218
1267
|
applyScrollAdjustment(delta, behavior) {
|
|
1268
|
+
var _a;
|
|
1219
1269
|
if (delta === 0) return false;
|
|
1220
1270
|
if (process.env.NODE_ENV !== "production" && this.options.debug) {
|
|
1221
1271
|
console.info("correction", delta);
|
|
1222
1272
|
}
|
|
1223
1273
|
if (isIOSWebKit() && (this.isScrolling || this._iosTouching || this._iosJustTouchEnded)) {
|
|
1224
|
-
this.
|
|
1274
|
+
if (((_a = this.scrollState) == null ? void 0 : _a.index) == null) {
|
|
1275
|
+
this._iosDeferredAdjustment += delta;
|
|
1276
|
+
}
|
|
1225
1277
|
return false;
|
|
1226
1278
|
} else {
|
|
1227
1279
|
const target = this.getScrollOffset() + this.scrollAdjustments + delta;
|
|
@@ -1264,6 +1316,7 @@ class Virtualizer {
|
|
|
1264
1316
|
this.scrollState = null;
|
|
1265
1317
|
return;
|
|
1266
1318
|
}
|
|
1319
|
+
this.getMeasurements();
|
|
1267
1320
|
const targetOffset = this.getScrollStateTarget(this.scrollState);
|
|
1268
1321
|
const STABLE_FRAMES = this.scrollState.index == null ? 1 : this.options.useAnimationFrameWithResizeObserver ? 3 : 2;
|
|
1269
1322
|
const targetChanged = targetOffset !== this.scrollState.lastTargetOffset;
|