@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/esm/index.d.ts
CHANGED
|
@@ -18,6 +18,11 @@ export interface Range {
|
|
|
18
18
|
endIndex: number;
|
|
19
19
|
overscan: number;
|
|
20
20
|
count: number;
|
|
21
|
+
/**
|
|
22
|
+
* Lane count of the virtualizer. When greater than 1, `overscan` counts
|
|
23
|
+
* rows of `lanes` items rather than single items.
|
|
24
|
+
*/
|
|
25
|
+
lanes?: number;
|
|
21
26
|
}
|
|
22
27
|
type Key = number | string | bigint;
|
|
23
28
|
export interface VirtualItem {
|
|
@@ -117,6 +122,7 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
|
|
|
117
122
|
private _clampedAdjustment;
|
|
118
123
|
shouldAdjustScrollPositionOnItemSizeChange: undefined | ((item: VirtualItem, delta: number, instance: Virtualizer<TScrollElement, TItemElement>) => boolean);
|
|
119
124
|
elementsCache: Map<Key, TItemElement>;
|
|
125
|
+
private _resizeBatch;
|
|
120
126
|
private now;
|
|
121
127
|
private observer;
|
|
122
128
|
range: {
|
|
@@ -135,6 +141,7 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
|
|
|
135
141
|
private _flushIosDeferredIfReady;
|
|
136
142
|
private rafId;
|
|
137
143
|
private scheduleScrollReconcile;
|
|
144
|
+
private getScrollStateTarget;
|
|
138
145
|
private reconcileScroll;
|
|
139
146
|
private getSize;
|
|
140
147
|
private getScrollOffset;
|
|
@@ -150,7 +157,7 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
|
|
|
150
157
|
};
|
|
151
158
|
getVirtualIndexes: {
|
|
152
159
|
(): number[];
|
|
153
|
-
updateDeps(newDeps: [(range: Range) => Array<number>, number, number, number | null, number | null]): void;
|
|
160
|
+
updateDeps(newDeps: [(range: Range) => Array<number>, number, number, number | null, number | null, number]): void;
|
|
154
161
|
};
|
|
155
162
|
indexFromElement: (node: TItemElement) => number;
|
|
156
163
|
/**
|
|
@@ -176,6 +183,7 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
|
|
|
176
183
|
scrollToIndex: (index: number, { align: initialAlign, behavior, }?: ScrollToIndexOptions) => void;
|
|
177
184
|
scrollBy: (delta: number, { behavior }?: ScrollToOffsetOptions) => void;
|
|
178
185
|
scrollToEnd: ({ behavior }?: ScrollToEndOptions) => void;
|
|
186
|
+
cancelScroll: () => void;
|
|
179
187
|
getTotalSize: () => number;
|
|
180
188
|
/**
|
|
181
189
|
* Returns a snapshot of currently-measured items suitable for round-
|
package/dist/esm/index.js
CHANGED
|
@@ -17,8 +17,9 @@ const getRect = (element) => {
|
|
|
17
17
|
};
|
|
18
18
|
const defaultKeyExtractor = (index) => index;
|
|
19
19
|
const defaultRangeExtractor = (range) => {
|
|
20
|
-
const
|
|
21
|
-
const
|
|
20
|
+
const overscan = range.overscan * Math.max(range.lanes ?? 1, 1);
|
|
21
|
+
const start = Math.max(range.startIndex - overscan, 0);
|
|
22
|
+
const end = Math.min(range.endIndex + overscan, range.count - 1);
|
|
22
23
|
const len = end - start + 1;
|
|
23
24
|
const arr = new Array(len);
|
|
24
25
|
for (let i = 0; i < len; i++) {
|
|
@@ -211,6 +212,7 @@ class Virtualizer {
|
|
|
211
212
|
this._intendedScrollOffset = null;
|
|
212
213
|
this._clampedAdjustment = null;
|
|
213
214
|
this.elementsCache = /* @__PURE__ */ new Map();
|
|
215
|
+
this._resizeBatch = null;
|
|
214
216
|
this.now = () => {
|
|
215
217
|
var _a, _b, _c;
|
|
216
218
|
return ((_c = (_b = (_a = this.targetWindow) == null ? void 0 : _a.performance) == null ? void 0 : _b.now) == null ? void 0 : _c.call(_b)) ?? Date.now();
|
|
@@ -225,32 +227,40 @@ class Virtualizer {
|
|
|
225
227
|
return null;
|
|
226
228
|
}
|
|
227
229
|
return _ro = new this.targetWindow.ResizeObserver((entries) => {
|
|
228
|
-
|
|
229
|
-
const
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
return;
|
|
230
|
+
const run = () => {
|
|
231
|
+
const batch = { sync: false };
|
|
232
|
+
this._resizeBatch = batch;
|
|
233
|
+
try {
|
|
234
|
+
entries.forEach(measureEntry);
|
|
235
|
+
} finally {
|
|
236
|
+
this._resizeBatch = null;
|
|
237
|
+
if (batch.notify) {
|
|
238
|
+
this.notify(batch.sync);
|
|
239
|
+
this._retryClampedAdjustment();
|
|
241
240
|
}
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
index,
|
|
246
|
-
this.options.measureElement(node, entry, this)
|
|
247
|
-
);
|
|
248
|
-
}
|
|
249
|
-
};
|
|
250
|
-
this.options.useAnimationFrameWithResizeObserver ? requestAnimationFrame(run) : run();
|
|
251
|
-
});
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
this.options.useAnimationFrameWithResizeObserver ? requestAnimationFrame(run) : run();
|
|
252
244
|
});
|
|
253
245
|
};
|
|
246
|
+
const measureEntry = (entry) => {
|
|
247
|
+
const node = entry.target;
|
|
248
|
+
const index = this.indexFromElement(node);
|
|
249
|
+
if (!node.isConnected) {
|
|
250
|
+
this.observer.unobserve(node);
|
|
251
|
+
for (const [cacheKey, cachedNode] of this.elementsCache) {
|
|
252
|
+
if (cachedNode === node) {
|
|
253
|
+
this.elementsCache.delete(cacheKey);
|
|
254
|
+
break;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
if (!this.isIndexInRange(index)) return;
|
|
260
|
+
if (this.shouldMeasureDuringScroll(index)) {
|
|
261
|
+
this.resizeItem(index, this.options.measureElement(node, entry, this));
|
|
262
|
+
}
|
|
263
|
+
};
|
|
254
264
|
return {
|
|
255
265
|
disconnect: () => {
|
|
256
266
|
var _a;
|
|
@@ -449,11 +459,14 @@ class Virtualizer {
|
|
|
449
459
|
);
|
|
450
460
|
this.unsubs.push(
|
|
451
461
|
this.options.observeElementOffset(this, (offset, isScrolling) => {
|
|
462
|
+
var _a2;
|
|
452
463
|
if (isScrolling && this._intendedScrollOffset === null && offset === this.scrollOffset) {
|
|
453
464
|
return;
|
|
454
465
|
}
|
|
455
466
|
if (this._intendedScrollOffset !== null && Math.abs(offset - this._intendedScrollOffset) < 1.5) {
|
|
456
467
|
offset = this._intendedScrollOffset;
|
|
468
|
+
} else if (((_a2 = this.scrollState) == null ? void 0 : _a2.index) != null && this.scrollState.hasReachedTarget && !approxEqual(offset, this.scrollState.lastTargetOffset) && !approxEqual(offset, this.getScrollStateTarget(this.scrollState)) && (this._clampedAdjustment === null || Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5)) {
|
|
469
|
+
this.scrollState = null;
|
|
457
470
|
}
|
|
458
471
|
this._intendedScrollOffset = null;
|
|
459
472
|
if (this._clampedAdjustment !== null && Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5) {
|
|
@@ -820,15 +833,17 @@ class Virtualizer {
|
|
|
820
833
|
this.options.overscan,
|
|
821
834
|
this.options.count,
|
|
822
835
|
startIndex,
|
|
823
|
-
endIndex
|
|
836
|
+
endIndex,
|
|
837
|
+
this.options.lanes
|
|
824
838
|
];
|
|
825
839
|
},
|
|
826
|
-
(rangeExtractor, overscan, count, startIndex, endIndex) => {
|
|
840
|
+
(rangeExtractor, overscan, count, startIndex, endIndex, lanes) => {
|
|
827
841
|
return startIndex === null || endIndex === null ? [] : rangeExtractor({
|
|
828
842
|
startIndex,
|
|
829
843
|
endIndex,
|
|
830
844
|
overscan,
|
|
831
|
-
count
|
|
845
|
+
count,
|
|
846
|
+
lanes
|
|
832
847
|
});
|
|
833
848
|
},
|
|
834
849
|
{
|
|
@@ -888,17 +903,18 @@ class Virtualizer {
|
|
|
888
903
|
this.observer.observe(node);
|
|
889
904
|
this.elementsCache.set(key, node);
|
|
890
905
|
}
|
|
891
|
-
if ((!this.isScrolling || this.scrollState) && this.shouldMeasureDuringScroll(index)) {
|
|
906
|
+
if (node.isConnected && (!this.isScrolling || this.scrollState) && this.shouldMeasureDuringScroll(index)) {
|
|
892
907
|
this.resizeItem(index, this.options.measureElement(node, void 0, this));
|
|
893
908
|
}
|
|
894
909
|
};
|
|
895
910
|
this.resizeItem = (index, size) => {
|
|
896
|
-
var _a, _b, _c;
|
|
911
|
+
var _a, _b, _c, _d, _e;
|
|
897
912
|
if (!this.isIndexInRange(index)) return;
|
|
913
|
+
if ((_a = this._resizeBatch) == null ? void 0 : _a.sync) this.getMeasurements();
|
|
898
914
|
let cachedSize;
|
|
899
915
|
let itemStart;
|
|
900
916
|
let key;
|
|
901
|
-
const flat = (
|
|
917
|
+
const flat = (_b = this._singleLaneMeasurements) == null ? void 0 : _b.flat;
|
|
902
918
|
if (this.options.lanes === 1 && flat != null) {
|
|
903
919
|
key = this.options.getItemKey(index);
|
|
904
920
|
itemStart = flat[index * 2];
|
|
@@ -913,7 +929,7 @@ class Virtualizer {
|
|
|
913
929
|
const itemSize = this.itemSizeCache.get(key) ?? cachedSize;
|
|
914
930
|
const delta = size - itemSize;
|
|
915
931
|
if (delta !== 0) {
|
|
916
|
-
const wasAtEnd = this.options.anchorTo === "end" && ((
|
|
932
|
+
const wasAtEnd = this.options.anchorTo === "end" && ((_c = this.scrollState) == null ? void 0 : _c.behavior) !== "smooth" && this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold;
|
|
917
933
|
const prevTotalSize = wasAtEnd ? this.getTotalSize() : 0;
|
|
918
934
|
const scrollOffsetWithAdj = this.getScrollOffset() + this.scrollAdjustments;
|
|
919
935
|
const isFirstMeasure = !this.itemSizeCache.has(key);
|
|
@@ -932,7 +948,7 @@ class Virtualizer {
|
|
|
932
948
|
// scrolling up" cascade.
|
|
933
949
|
itemStart + itemSize <= scrollOffsetWithAdj && this.scrollDirection !== "backward"
|
|
934
950
|
);
|
|
935
|
-
const shouldAdjustScroll = ((
|
|
951
|
+
const shouldAdjustScroll = ((_d = this.scrollState) == null ? void 0 : _d.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
|
|
936
952
|
// The callback expects a VirtualItem; build one lazily only
|
|
937
953
|
// when the consumer actually supplied a custom predicate.
|
|
938
954
|
this.measurementsCache[index] ?? {
|
|
@@ -959,6 +975,11 @@ class Virtualizer {
|
|
|
959
975
|
} else if (shouldAdjustScroll) {
|
|
960
976
|
adjustedSync = this.applyScrollAdjustment(delta);
|
|
961
977
|
}
|
|
978
|
+
if (this._resizeBatch) {
|
|
979
|
+
this._resizeBatch.notify = true;
|
|
980
|
+
(_e = this._resizeBatch).sync || (_e.sync = adjustedSync);
|
|
981
|
+
return;
|
|
982
|
+
}
|
|
962
983
|
this.notify(adjustedSync);
|
|
963
984
|
this._retryClampedAdjustment();
|
|
964
985
|
}
|
|
@@ -1047,7 +1068,9 @@ class Virtualizer {
|
|
|
1047
1068
|
}
|
|
1048
1069
|
}
|
|
1049
1070
|
if (align === "end" && index === this.options.count - 1) {
|
|
1050
|
-
|
|
1071
|
+
const maxOffset = this.getMaxScrollOffset();
|
|
1072
|
+
const toOffset2 = maxOffset - this.options.paddingEnd + this.options.scrollPaddingEnd;
|
|
1073
|
+
return [Math.max(Math.min(toOffset2, maxOffset), 0), align];
|
|
1051
1074
|
}
|
|
1052
1075
|
const toOffset = align === "end" ? item.end + this.options.scrollPaddingEnd : item.start - this.options.scrollPaddingStart;
|
|
1053
1076
|
return [
|
|
@@ -1065,7 +1088,8 @@ class Virtualizer {
|
|
|
1065
1088
|
behavior,
|
|
1066
1089
|
startedAt: now,
|
|
1067
1090
|
lastTargetOffset: offset,
|
|
1068
|
-
stableFrames: 0
|
|
1091
|
+
stableFrames: 0,
|
|
1092
|
+
hasReachedTarget: false
|
|
1069
1093
|
};
|
|
1070
1094
|
this._scrollToOffset(offset, { adjustments: void 0, behavior });
|
|
1071
1095
|
this.scheduleScrollReconcile();
|
|
@@ -1088,7 +1112,8 @@ class Virtualizer {
|
|
|
1088
1112
|
behavior,
|
|
1089
1113
|
startedAt: now,
|
|
1090
1114
|
lastTargetOffset: offset,
|
|
1091
|
-
stableFrames: 0
|
|
1115
|
+
stableFrames: 0,
|
|
1116
|
+
hasReachedTarget: approxEqual(offset, this.getScrollOffset())
|
|
1092
1117
|
};
|
|
1093
1118
|
this._scrollToOffset(offset, { adjustments: void 0, behavior });
|
|
1094
1119
|
this.scheduleScrollReconcile();
|
|
@@ -1102,23 +1127,41 @@ class Virtualizer {
|
|
|
1102
1127
|
behavior,
|
|
1103
1128
|
startedAt: now,
|
|
1104
1129
|
lastTargetOffset: offset,
|
|
1105
|
-
stableFrames: 0
|
|
1130
|
+
stableFrames: 0,
|
|
1131
|
+
hasReachedTarget: false
|
|
1106
1132
|
};
|
|
1107
1133
|
this._scrollToOffset(offset, { adjustments: void 0, behavior });
|
|
1108
1134
|
this.scheduleScrollReconcile();
|
|
1109
1135
|
};
|
|
1110
1136
|
this.scrollToEnd = ({ behavior = "auto" } = {}) => {
|
|
1111
1137
|
if (this.options.count > 0) {
|
|
1112
|
-
this.
|
|
1138
|
+
this._iosDeferredAdjustment = 0;
|
|
1139
|
+
const offset = Math.max(this.getMaxScrollOffset(), 0);
|
|
1140
|
+
this.scrollState = {
|
|
1141
|
+
index: this.options.count - 1,
|
|
1113
1142
|
align: "end",
|
|
1114
|
-
behavior
|
|
1115
|
-
|
|
1143
|
+
behavior,
|
|
1144
|
+
toEnd: true,
|
|
1145
|
+
startedAt: this.now(),
|
|
1146
|
+
lastTargetOffset: offset,
|
|
1147
|
+
stableFrames: 0,
|
|
1148
|
+
hasReachedTarget: approxEqual(offset, this.getScrollOffset())
|
|
1149
|
+
};
|
|
1150
|
+
this._scrollToOffset(offset, { adjustments: void 0, behavior });
|
|
1151
|
+
this.scheduleScrollReconcile();
|
|
1116
1152
|
return;
|
|
1117
1153
|
}
|
|
1118
1154
|
this.scrollToOffset(Math.max(this.getTotalSize() - this.getSize(), 0), {
|
|
1119
1155
|
behavior
|
|
1120
1156
|
});
|
|
1121
1157
|
};
|
|
1158
|
+
this.cancelScroll = () => {
|
|
1159
|
+
if (this.rafId != null && this.targetWindow) {
|
|
1160
|
+
this.targetWindow.cancelAnimationFrame(this.rafId);
|
|
1161
|
+
this.rafId = null;
|
|
1162
|
+
}
|
|
1163
|
+
this.scrollState = null;
|
|
1164
|
+
};
|
|
1122
1165
|
this.getTotalSize = () => {
|
|
1123
1166
|
var _a, _b;
|
|
1124
1167
|
const measurements = this.getMeasurements();
|
|
@@ -1189,12 +1232,15 @@ class Virtualizer {
|
|
|
1189
1232
|
// `resizeItem` uses that to decide whether the follow-up `notify` must be
|
|
1190
1233
|
// synchronous so the grown transforms commit in the same paint (#1227).
|
|
1191
1234
|
applyScrollAdjustment(delta, behavior) {
|
|
1235
|
+
var _a;
|
|
1192
1236
|
if (delta === 0) return false;
|
|
1193
1237
|
if (process.env.NODE_ENV !== "production" && this.options.debug) {
|
|
1194
1238
|
console.info("correction", delta);
|
|
1195
1239
|
}
|
|
1196
1240
|
if (isIOSWebKit() && (this.isScrolling || this._iosTouching || this._iosJustTouchEnded)) {
|
|
1197
|
-
this.
|
|
1241
|
+
if (((_a = this.scrollState) == null ? void 0 : _a.index) == null) {
|
|
1242
|
+
this._iosDeferredAdjustment += delta;
|
|
1243
|
+
}
|
|
1198
1244
|
return false;
|
|
1199
1245
|
} else {
|
|
1200
1246
|
const target = this.getScrollOffset() + this.scrollAdjustments + delta;
|
|
@@ -1224,6 +1270,10 @@ class Virtualizer {
|
|
|
1224
1270
|
this.reconcileScroll();
|
|
1225
1271
|
});
|
|
1226
1272
|
}
|
|
1273
|
+
getScrollStateTarget(state) {
|
|
1274
|
+
const offsetInfo = state.toEnd ? [Math.max(this.getMaxScrollOffset(), 0)] : state.index != null ? this.getOffsetForIndex(state.index, state.align) : void 0;
|
|
1275
|
+
return offsetInfo ? offsetInfo[0] : state.lastTargetOffset;
|
|
1276
|
+
}
|
|
1227
1277
|
reconcileScroll() {
|
|
1228
1278
|
if (!this.scrollState) return;
|
|
1229
1279
|
const el = this.scrollElement;
|
|
@@ -1233,11 +1283,12 @@ class Virtualizer {
|
|
|
1233
1283
|
this.scrollState = null;
|
|
1234
1284
|
return;
|
|
1235
1285
|
}
|
|
1236
|
-
|
|
1237
|
-
const targetOffset =
|
|
1238
|
-
const STABLE_FRAMES = 1;
|
|
1286
|
+
this.getMeasurements();
|
|
1287
|
+
const targetOffset = this.getScrollStateTarget(this.scrollState);
|
|
1288
|
+
const STABLE_FRAMES = this.scrollState.index == null ? 1 : this.options.useAnimationFrameWithResizeObserver ? 3 : 2;
|
|
1239
1289
|
const targetChanged = targetOffset !== this.scrollState.lastTargetOffset;
|
|
1240
1290
|
if (!targetChanged && approxEqual(targetOffset, this.getScrollOffset())) {
|
|
1291
|
+
this.scrollState.hasReachedTarget = true;
|
|
1241
1292
|
this.scrollState.stableFrames++;
|
|
1242
1293
|
if (this.scrollState.stableFrames >= STABLE_FRAMES) {
|
|
1243
1294
|
if (this.getScrollOffset() !== targetOffset) {
|
|
@@ -1256,7 +1307,9 @@ class Virtualizer {
|
|
|
1256
1307
|
const distance = Math.abs(targetOffset - this.getScrollOffset());
|
|
1257
1308
|
const keepSmooth = this.scrollState.behavior === "smooth" && distance > viewport;
|
|
1258
1309
|
this.scrollState.lastTargetOffset = targetOffset;
|
|
1259
|
-
if (
|
|
1310
|
+
if (keepSmooth) {
|
|
1311
|
+
this.scrollState.hasReachedTarget = false;
|
|
1312
|
+
} else {
|
|
1260
1313
|
this.scrollState.behavior = "auto";
|
|
1261
1314
|
}
|
|
1262
1315
|
this._scrollToOffset(targetOffset, {
|