@tanstack/virtual-core 3.17.11 → 3.19.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 +98 -45
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +9 -1
- package/dist/esm/index.d.ts +9 -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 +178 -56
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;
|
|
@@ -451,11 +461,14 @@ class Virtualizer {
|
|
|
451
461
|
);
|
|
452
462
|
this.unsubs.push(
|
|
453
463
|
this.options.observeElementOffset(this, (offset, isScrolling) => {
|
|
464
|
+
var _a2;
|
|
454
465
|
if (isScrolling && this._intendedScrollOffset === null && offset === this.scrollOffset) {
|
|
455
466
|
return;
|
|
456
467
|
}
|
|
457
468
|
if (this._intendedScrollOffset !== null && Math.abs(offset - this._intendedScrollOffset) < 1.5) {
|
|
458
469
|
offset = this._intendedScrollOffset;
|
|
470
|
+
} else if (((_a2 = this.scrollState) == null ? void 0 : _a2.index) != null && this.scrollState.hasReachedTarget && !utils.approxEqual(offset, this.scrollState.lastTargetOffset) && !utils.approxEqual(offset, this.getScrollStateTarget(this.scrollState)) && (this._clampedAdjustment === null || Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5)) {
|
|
471
|
+
this.scrollState = null;
|
|
459
472
|
}
|
|
460
473
|
this._intendedScrollOffset = null;
|
|
461
474
|
if (this._clampedAdjustment !== null && Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5) {
|
|
@@ -822,15 +835,17 @@ class Virtualizer {
|
|
|
822
835
|
this.options.overscan,
|
|
823
836
|
this.options.count,
|
|
824
837
|
startIndex,
|
|
825
|
-
endIndex
|
|
838
|
+
endIndex,
|
|
839
|
+
this.options.lanes
|
|
826
840
|
];
|
|
827
841
|
},
|
|
828
|
-
(rangeExtractor, overscan, count, startIndex, endIndex) => {
|
|
842
|
+
(rangeExtractor, overscan, count, startIndex, endIndex, lanes) => {
|
|
829
843
|
return startIndex === null || endIndex === null ? [] : rangeExtractor({
|
|
830
844
|
startIndex,
|
|
831
845
|
endIndex,
|
|
832
846
|
overscan,
|
|
833
|
-
count
|
|
847
|
+
count,
|
|
848
|
+
lanes
|
|
834
849
|
});
|
|
835
850
|
},
|
|
836
851
|
{
|
|
@@ -890,17 +905,18 @@ class Virtualizer {
|
|
|
890
905
|
this.observer.observe(node);
|
|
891
906
|
this.elementsCache.set(key, node);
|
|
892
907
|
}
|
|
893
|
-
if ((!this.isScrolling || this.scrollState) && this.shouldMeasureDuringScroll(index)) {
|
|
908
|
+
if (node.isConnected && (!this.isScrolling || this.scrollState) && this.shouldMeasureDuringScroll(index)) {
|
|
894
909
|
this.resizeItem(index, this.options.measureElement(node, void 0, this));
|
|
895
910
|
}
|
|
896
911
|
};
|
|
897
912
|
this.resizeItem = (index, size) => {
|
|
898
|
-
var _a, _b, _c;
|
|
913
|
+
var _a, _b, _c, _d, _e;
|
|
899
914
|
if (!this.isIndexInRange(index)) return;
|
|
915
|
+
if ((_a = this._resizeBatch) == null ? void 0 : _a.sync) this.getMeasurements();
|
|
900
916
|
let cachedSize;
|
|
901
917
|
let itemStart;
|
|
902
918
|
let key;
|
|
903
|
-
const flat = (
|
|
919
|
+
const flat = (_b = this._singleLaneMeasurements) == null ? void 0 : _b.flat;
|
|
904
920
|
if (this.options.lanes === 1 && flat != null) {
|
|
905
921
|
key = this.options.getItemKey(index);
|
|
906
922
|
itemStart = flat[index * 2];
|
|
@@ -915,7 +931,7 @@ class Virtualizer {
|
|
|
915
931
|
const itemSize = this.itemSizeCache.get(key) ?? cachedSize;
|
|
916
932
|
const delta = size - itemSize;
|
|
917
933
|
if (delta !== 0) {
|
|
918
|
-
const wasAtEnd = this.options.anchorTo === "end" && ((
|
|
934
|
+
const wasAtEnd = this.options.anchorTo === "end" && ((_c = this.scrollState) == null ? void 0 : _c.behavior) !== "smooth" && this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold;
|
|
919
935
|
const prevTotalSize = wasAtEnd ? this.getTotalSize() : 0;
|
|
920
936
|
const scrollOffsetWithAdj = this.getScrollOffset() + this.scrollAdjustments;
|
|
921
937
|
const isFirstMeasure = !this.itemSizeCache.has(key);
|
|
@@ -934,7 +950,7 @@ class Virtualizer {
|
|
|
934
950
|
// scrolling up" cascade.
|
|
935
951
|
itemStart + itemSize <= scrollOffsetWithAdj && this.scrollDirection !== "backward"
|
|
936
952
|
);
|
|
937
|
-
const shouldAdjustScroll = ((
|
|
953
|
+
const shouldAdjustScroll = ((_d = this.scrollState) == null ? void 0 : _d.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
|
|
938
954
|
// The callback expects a VirtualItem; build one lazily only
|
|
939
955
|
// when the consumer actually supplied a custom predicate.
|
|
940
956
|
this.measurementsCache[index] ?? {
|
|
@@ -961,6 +977,11 @@ class Virtualizer {
|
|
|
961
977
|
} else if (shouldAdjustScroll) {
|
|
962
978
|
adjustedSync = this.applyScrollAdjustment(delta);
|
|
963
979
|
}
|
|
980
|
+
if (this._resizeBatch) {
|
|
981
|
+
this._resizeBatch.notify = true;
|
|
982
|
+
(_e = this._resizeBatch).sync || (_e.sync = adjustedSync);
|
|
983
|
+
return;
|
|
984
|
+
}
|
|
964
985
|
this.notify(adjustedSync);
|
|
965
986
|
this._retryClampedAdjustment();
|
|
966
987
|
}
|
|
@@ -1049,7 +1070,9 @@ class Virtualizer {
|
|
|
1049
1070
|
}
|
|
1050
1071
|
}
|
|
1051
1072
|
if (align === "end" && index === this.options.count - 1) {
|
|
1052
|
-
|
|
1073
|
+
const maxOffset = this.getMaxScrollOffset();
|
|
1074
|
+
const toOffset2 = maxOffset - this.options.paddingEnd + this.options.scrollPaddingEnd;
|
|
1075
|
+
return [Math.max(Math.min(toOffset2, maxOffset), 0), align];
|
|
1053
1076
|
}
|
|
1054
1077
|
const toOffset = align === "end" ? item.end + this.options.scrollPaddingEnd : item.start - this.options.scrollPaddingStart;
|
|
1055
1078
|
return [
|
|
@@ -1067,7 +1090,8 @@ class Virtualizer {
|
|
|
1067
1090
|
behavior,
|
|
1068
1091
|
startedAt: now,
|
|
1069
1092
|
lastTargetOffset: offset,
|
|
1070
|
-
stableFrames: 0
|
|
1093
|
+
stableFrames: 0,
|
|
1094
|
+
hasReachedTarget: false
|
|
1071
1095
|
};
|
|
1072
1096
|
this._scrollToOffset(offset, { adjustments: void 0, behavior });
|
|
1073
1097
|
this.scheduleScrollReconcile();
|
|
@@ -1090,7 +1114,8 @@ class Virtualizer {
|
|
|
1090
1114
|
behavior,
|
|
1091
1115
|
startedAt: now,
|
|
1092
1116
|
lastTargetOffset: offset,
|
|
1093
|
-
stableFrames: 0
|
|
1117
|
+
stableFrames: 0,
|
|
1118
|
+
hasReachedTarget: utils.approxEqual(offset, this.getScrollOffset())
|
|
1094
1119
|
};
|
|
1095
1120
|
this._scrollToOffset(offset, { adjustments: void 0, behavior });
|
|
1096
1121
|
this.scheduleScrollReconcile();
|
|
@@ -1104,23 +1129,41 @@ class Virtualizer {
|
|
|
1104
1129
|
behavior,
|
|
1105
1130
|
startedAt: now,
|
|
1106
1131
|
lastTargetOffset: offset,
|
|
1107
|
-
stableFrames: 0
|
|
1132
|
+
stableFrames: 0,
|
|
1133
|
+
hasReachedTarget: false
|
|
1108
1134
|
};
|
|
1109
1135
|
this._scrollToOffset(offset, { adjustments: void 0, behavior });
|
|
1110
1136
|
this.scheduleScrollReconcile();
|
|
1111
1137
|
};
|
|
1112
1138
|
this.scrollToEnd = ({ behavior = "auto" } = {}) => {
|
|
1113
1139
|
if (this.options.count > 0) {
|
|
1114
|
-
this.
|
|
1140
|
+
this._iosDeferredAdjustment = 0;
|
|
1141
|
+
const offset = Math.max(this.getMaxScrollOffset(), 0);
|
|
1142
|
+
this.scrollState = {
|
|
1143
|
+
index: this.options.count - 1,
|
|
1115
1144
|
align: "end",
|
|
1116
|
-
behavior
|
|
1117
|
-
|
|
1145
|
+
behavior,
|
|
1146
|
+
toEnd: true,
|
|
1147
|
+
startedAt: this.now(),
|
|
1148
|
+
lastTargetOffset: offset,
|
|
1149
|
+
stableFrames: 0,
|
|
1150
|
+
hasReachedTarget: utils.approxEqual(offset, this.getScrollOffset())
|
|
1151
|
+
};
|
|
1152
|
+
this._scrollToOffset(offset, { adjustments: void 0, behavior });
|
|
1153
|
+
this.scheduleScrollReconcile();
|
|
1118
1154
|
return;
|
|
1119
1155
|
}
|
|
1120
1156
|
this.scrollToOffset(Math.max(this.getTotalSize() - this.getSize(), 0), {
|
|
1121
1157
|
behavior
|
|
1122
1158
|
});
|
|
1123
1159
|
};
|
|
1160
|
+
this.cancelScroll = () => {
|
|
1161
|
+
if (this.rafId != null && this.targetWindow) {
|
|
1162
|
+
this.targetWindow.cancelAnimationFrame(this.rafId);
|
|
1163
|
+
this.rafId = null;
|
|
1164
|
+
}
|
|
1165
|
+
this.scrollState = null;
|
|
1166
|
+
};
|
|
1124
1167
|
this.getTotalSize = () => {
|
|
1125
1168
|
var _a, _b;
|
|
1126
1169
|
const measurements = this.getMeasurements();
|
|
@@ -1191,12 +1234,15 @@ class Virtualizer {
|
|
|
1191
1234
|
// `resizeItem` uses that to decide whether the follow-up `notify` must be
|
|
1192
1235
|
// synchronous so the grown transforms commit in the same paint (#1227).
|
|
1193
1236
|
applyScrollAdjustment(delta, behavior) {
|
|
1237
|
+
var _a;
|
|
1194
1238
|
if (delta === 0) return false;
|
|
1195
1239
|
if (process.env.NODE_ENV !== "production" && this.options.debug) {
|
|
1196
1240
|
console.info("correction", delta);
|
|
1197
1241
|
}
|
|
1198
1242
|
if (isIOSWebKit() && (this.isScrolling || this._iosTouching || this._iosJustTouchEnded)) {
|
|
1199
|
-
this.
|
|
1243
|
+
if (((_a = this.scrollState) == null ? void 0 : _a.index) == null) {
|
|
1244
|
+
this._iosDeferredAdjustment += delta;
|
|
1245
|
+
}
|
|
1200
1246
|
return false;
|
|
1201
1247
|
} else {
|
|
1202
1248
|
const target = this.getScrollOffset() + this.scrollAdjustments + delta;
|
|
@@ -1226,6 +1272,10 @@ class Virtualizer {
|
|
|
1226
1272
|
this.reconcileScroll();
|
|
1227
1273
|
});
|
|
1228
1274
|
}
|
|
1275
|
+
getScrollStateTarget(state) {
|
|
1276
|
+
const offsetInfo = state.toEnd ? [Math.max(this.getMaxScrollOffset(), 0)] : state.index != null ? this.getOffsetForIndex(state.index, state.align) : void 0;
|
|
1277
|
+
return offsetInfo ? offsetInfo[0] : state.lastTargetOffset;
|
|
1278
|
+
}
|
|
1229
1279
|
reconcileScroll() {
|
|
1230
1280
|
if (!this.scrollState) return;
|
|
1231
1281
|
const el = this.scrollElement;
|
|
@@ -1235,11 +1285,12 @@ class Virtualizer {
|
|
|
1235
1285
|
this.scrollState = null;
|
|
1236
1286
|
return;
|
|
1237
1287
|
}
|
|
1238
|
-
|
|
1239
|
-
const targetOffset =
|
|
1240
|
-
const STABLE_FRAMES = 1;
|
|
1288
|
+
this.getMeasurements();
|
|
1289
|
+
const targetOffset = this.getScrollStateTarget(this.scrollState);
|
|
1290
|
+
const STABLE_FRAMES = this.scrollState.index == null ? 1 : this.options.useAnimationFrameWithResizeObserver ? 3 : 2;
|
|
1241
1291
|
const targetChanged = targetOffset !== this.scrollState.lastTargetOffset;
|
|
1242
1292
|
if (!targetChanged && utils.approxEqual(targetOffset, this.getScrollOffset())) {
|
|
1293
|
+
this.scrollState.hasReachedTarget = true;
|
|
1243
1294
|
this.scrollState.stableFrames++;
|
|
1244
1295
|
if (this.scrollState.stableFrames >= STABLE_FRAMES) {
|
|
1245
1296
|
if (this.getScrollOffset() !== targetOffset) {
|
|
@@ -1258,7 +1309,9 @@ class Virtualizer {
|
|
|
1258
1309
|
const distance = Math.abs(targetOffset - this.getScrollOffset());
|
|
1259
1310
|
const keepSmooth = this.scrollState.behavior === "smooth" && distance > viewport;
|
|
1260
1311
|
this.scrollState.lastTargetOffset = targetOffset;
|
|
1261
|
-
if (
|
|
1312
|
+
if (keepSmooth) {
|
|
1313
|
+
this.scrollState.hasReachedTarget = false;
|
|
1314
|
+
} else {
|
|
1262
1315
|
this.scrollState.behavior = "auto";
|
|
1263
1316
|
}
|
|
1264
1317
|
this._scrollToOffset(targetOffset, {
|