@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.
@@ -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: {
@@ -151,7 +157,7 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
151
157
  };
152
158
  getVirtualIndexes: {
153
159
  (): number[];
154
- 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;
155
161
  };
156
162
  indexFromElement: (node: TItemElement) => number;
157
163
  /**
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 start = Math.max(range.startIndex - range.overscan, 0);
21
- const end = Math.min(range.endIndex + range.overscan, range.count - 1);
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
- entries.forEach((entry) => {
229
- const run = () => {
230
- const node = entry.target;
231
- const index = this.indexFromElement(node);
232
- if (!node.isConnected) {
233
- this.observer.unobserve(node);
234
- for (const [cacheKey, cachedNode] of this.elementsCache) {
235
- if (cachedNode === node) {
236
- this.elementsCache.delete(cacheKey);
237
- break;
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
- if (!this.isIndexInRange(index)) return;
243
- if (this.shouldMeasureDuringScroll(index)) {
244
- this.resizeItem(
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;
@@ -455,10 +465,24 @@ class Virtualizer {
455
465
  }
456
466
  if (this._intendedScrollOffset !== null && Math.abs(offset - this._intendedScrollOffset) < 1.5) {
457
467
  offset = this._intendedScrollOffset;
458
- } 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)) {
468
+ } else if (((_a2 = this.scrollState) == null ? void 0 : _a2.index) != null && // A non-smooth write lands at once, so any other scroll event is
469
+ // the user's, even before reconcile saw the target reached (the
470
+ // target may keep moving, or iOS ignored the write under a
471
+ // finger). A write the browser clamped because the sizer hasn't
472
+ // grown yet reads back at the max, which is also where the fresh
473
+ // target clamps, so the check below keeps it reconciling (#1294).
474
+ // Not when rows above the target shrink before the read-back and
475
+ // pull the target below the new max: the read-back then looks
476
+ // like the user and the scroll stops at the max. That needs the
477
+ // sizer re-rendered before the scroll event, which browsers have
478
+ // not been seen to do (pinned by a `.fails` test).
479
+ // A smooth scroll's own animation events look like a user's, so
480
+ // it only yields once it has reached the target.
481
+ (this.scrollState.hasReachedTarget || !this.scrollState.startedSmooth) && !approxEqual(offset, this.scrollState.lastTargetOffset) && !approxEqual(offset, this.getScrollStateTarget(this.scrollState)) && (this._clampedAdjustment === null || Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5)) {
459
482
  this.scrollState = null;
460
483
  }
461
484
  this._intendedScrollOffset = null;
485
+ if (this.scrollState) this.scrollState.writtenOffset = null;
462
486
  if (this._clampedAdjustment !== null && Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5) {
463
487
  this._clampedAdjustment = null;
464
488
  }
@@ -823,15 +847,17 @@ class Virtualizer {
823
847
  this.options.overscan,
824
848
  this.options.count,
825
849
  startIndex,
826
- endIndex
850
+ endIndex,
851
+ this.options.lanes
827
852
  ];
828
853
  },
829
- (rangeExtractor, overscan, count, startIndex, endIndex) => {
854
+ (rangeExtractor, overscan, count, startIndex, endIndex, lanes) => {
830
855
  return startIndex === null || endIndex === null ? [] : rangeExtractor({
831
856
  startIndex,
832
857
  endIndex,
833
858
  overscan,
834
- count
859
+ count,
860
+ lanes
835
861
  });
836
862
  },
837
863
  {
@@ -896,12 +922,13 @@ class Virtualizer {
896
922
  }
897
923
  };
898
924
  this.resizeItem = (index, size) => {
899
- var _a, _b, _c;
925
+ var _a, _b, _c, _d, _e, _f;
900
926
  if (!this.isIndexInRange(index)) return;
927
+ if ((_a = this._resizeBatch) == null ? void 0 : _a.sync) this.getMeasurements();
901
928
  let cachedSize;
902
929
  let itemStart;
903
930
  let key;
904
- const flat = (_a = this._singleLaneMeasurements) == null ? void 0 : _a.flat;
931
+ const flat = (_b = this._singleLaneMeasurements) == null ? void 0 : _b.flat;
905
932
  if (this.options.lanes === 1 && flat != null) {
906
933
  key = this.options.getItemKey(index);
907
934
  itemStart = flat[index * 2];
@@ -916,7 +943,7 @@ class Virtualizer {
916
943
  const itemSize = this.itemSizeCache.get(key) ?? cachedSize;
917
944
  const delta = size - itemSize;
918
945
  if (delta !== 0) {
919
- const wasAtEnd = this.options.anchorTo === "end" && ((_b = this.scrollState) == null ? void 0 : _b.behavior) !== "smooth" && this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold;
946
+ const wasAtEnd = this.options.anchorTo === "end" && ((_c = this.scrollState) == null ? void 0 : _c.behavior) !== "smooth" && this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold;
920
947
  const prevTotalSize = wasAtEnd ? this.getTotalSize() : 0;
921
948
  const scrollOffsetWithAdj = this.getScrollOffset() + this.scrollAdjustments;
922
949
  const isFirstMeasure = !this.itemSizeCache.has(key);
@@ -935,7 +962,7 @@ class Virtualizer {
935
962
  // scrolling up" cascade.
936
963
  itemStart + itemSize <= scrollOffsetWithAdj && this.scrollDirection !== "backward"
937
964
  );
938
- const shouldAdjustScroll = ((_c = this.scrollState) == null ? void 0 : _c.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
965
+ const shouldAdjustScroll = ((_d = this.scrollState) == null ? void 0 : _d.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
939
966
  // The callback expects a VirtualItem; build one lazily only
940
967
  // when the consumer actually supplied a custom predicate.
941
968
  this.measurementsCache[index] ?? {
@@ -954,13 +981,24 @@ class Virtualizer {
954
981
  }
955
982
  this.itemSizeCache.set(key, size);
956
983
  this.itemSizeCacheVersion++;
984
+ const awaitingCommandReadBack = ((_e = this.scrollState) == null ? void 0 : _e.writtenOffset) != null && !approxEqual(
985
+ this.scrollState.writtenOffset,
986
+ this.getScrollOffset() + this.scrollAdjustments
987
+ );
957
988
  let adjustedSync = false;
958
- if (wasAtEnd) {
959
- adjustedSync = this.applyScrollAdjustment(
960
- this.getTotalSize() - prevTotalSize
961
- );
962
- } else if (shouldAdjustScroll) {
963
- adjustedSync = this.applyScrollAdjustment(delta);
989
+ if (!awaitingCommandReadBack) {
990
+ if (wasAtEnd) {
991
+ adjustedSync = this.applyScrollAdjustment(
992
+ this.getTotalSize() - prevTotalSize
993
+ );
994
+ } else if (shouldAdjustScroll) {
995
+ adjustedSync = this.applyScrollAdjustment(delta);
996
+ }
997
+ }
998
+ if (this._resizeBatch) {
999
+ this._resizeBatch.notify = true;
1000
+ (_f = this._resizeBatch).sync || (_f.sync = adjustedSync);
1001
+ return;
964
1002
  }
965
1003
  this.notify(adjustedSync);
966
1004
  this._retryClampedAdjustment();
@@ -1071,7 +1109,8 @@ class Virtualizer {
1071
1109
  startedAt: now,
1072
1110
  lastTargetOffset: offset,
1073
1111
  stableFrames: 0,
1074
- hasReachedTarget: false
1112
+ hasReachedTarget: false,
1113
+ startedSmooth: behavior === "smooth"
1075
1114
  };
1076
1115
  this._scrollToOffset(offset, { adjustments: void 0, behavior });
1077
1116
  this.scheduleScrollReconcile();
@@ -1095,13 +1134,18 @@ class Virtualizer {
1095
1134
  startedAt: now,
1096
1135
  lastTargetOffset: offset,
1097
1136
  stableFrames: 0,
1098
- hasReachedTarget: approxEqual(offset, this.getScrollOffset())
1137
+ hasReachedTarget: approxEqual(offset, this.getScrollOffset()),
1138
+ startedSmooth: behavior === "smooth"
1099
1139
  };
1100
1140
  this._scrollToOffset(offset, { adjustments: void 0, behavior });
1101
1141
  this.scheduleScrollReconcile();
1102
1142
  };
1103
1143
  this.scrollBy = (delta, { behavior = "auto" } = {}) => {
1104
- const offset = this.getScrollOffset() + delta;
1144
+ var _a;
1145
+ const offset = this.getOffsetForAlignment(
1146
+ (((_a = this.scrollState) == null ? void 0 : _a.writtenOffset) ?? this.getScrollOffset()) + delta,
1147
+ "start"
1148
+ );
1105
1149
  const now = this.now();
1106
1150
  this.scrollState = {
1107
1151
  index: null,
@@ -1110,7 +1154,8 @@ class Virtualizer {
1110
1154
  startedAt: now,
1111
1155
  lastTargetOffset: offset,
1112
1156
  stableFrames: 0,
1113
- hasReachedTarget: false
1157
+ hasReachedTarget: false,
1158
+ startedSmooth: behavior === "smooth"
1114
1159
  };
1115
1160
  this._scrollToOffset(offset, { adjustments: void 0, behavior });
1116
1161
  this.scheduleScrollReconcile();
@@ -1127,7 +1172,8 @@ class Virtualizer {
1127
1172
  startedAt: this.now(),
1128
1173
  lastTargetOffset: offset,
1129
1174
  stableFrames: 0,
1130
- hasReachedTarget: approxEqual(offset, this.getScrollOffset())
1175
+ hasReachedTarget: approxEqual(offset, this.getScrollOffset()),
1176
+ startedSmooth: behavior === "smooth"
1131
1177
  };
1132
1178
  this._scrollToOffset(offset, { adjustments: void 0, behavior });
1133
1179
  this.scheduleScrollReconcile();
@@ -1198,6 +1244,9 @@ class Virtualizer {
1198
1244
  behavior
1199
1245
  }) => {
1200
1246
  this._intendedScrollOffset = offset + (adjustments ?? 0);
1247
+ if (this.scrollState) {
1248
+ this.scrollState.writtenOffset = behavior === "smooth" ? null : this._intendedScrollOffset;
1249
+ }
1201
1250
  this.options.scrollToFn(offset, { behavior, adjustments }, this);
1202
1251
  };
1203
1252
  this.measure = () => {
@@ -1214,12 +1263,15 @@ class Virtualizer {
1214
1263
  // `resizeItem` uses that to decide whether the follow-up `notify` must be
1215
1264
  // synchronous so the grown transforms commit in the same paint (#1227).
1216
1265
  applyScrollAdjustment(delta, behavior) {
1266
+ var _a;
1217
1267
  if (delta === 0) return false;
1218
1268
  if (process.env.NODE_ENV !== "production" && this.options.debug) {
1219
1269
  console.info("correction", delta);
1220
1270
  }
1221
1271
  if (isIOSWebKit() && (this.isScrolling || this._iosTouching || this._iosJustTouchEnded)) {
1222
- this._iosDeferredAdjustment += delta;
1272
+ if (((_a = this.scrollState) == null ? void 0 : _a.index) == null) {
1273
+ this._iosDeferredAdjustment += delta;
1274
+ }
1223
1275
  return false;
1224
1276
  } else {
1225
1277
  const target = this.getScrollOffset() + this.scrollAdjustments + delta;
@@ -1262,6 +1314,7 @@ class Virtualizer {
1262
1314
  this.scrollState = null;
1263
1315
  return;
1264
1316
  }
1317
+ this.getMeasurements();
1265
1318
  const targetOffset = this.getScrollStateTarget(this.scrollState);
1266
1319
  const STABLE_FRAMES = this.scrollState.index == null ? 1 : this.options.useAnimationFrameWithResizeObserver ? 3 : 2;
1267
1320
  const targetChanged = targetOffset !== this.scrollState.lastTargetOffset;