@tanstack/virtual-core 3.18.0 → 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.
@@ -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;
@@ -823,15 +833,17 @@ class Virtualizer {
823
833
  this.options.overscan,
824
834
  this.options.count,
825
835
  startIndex,
826
- endIndex
836
+ endIndex,
837
+ this.options.lanes
827
838
  ];
828
839
  },
829
- (rangeExtractor, overscan, count, startIndex, endIndex) => {
840
+ (rangeExtractor, overscan, count, startIndex, endIndex, lanes) => {
830
841
  return startIndex === null || endIndex === null ? [] : rangeExtractor({
831
842
  startIndex,
832
843
  endIndex,
833
844
  overscan,
834
- count
845
+ count,
846
+ lanes
835
847
  });
836
848
  },
837
849
  {
@@ -896,12 +908,13 @@ class Virtualizer {
896
908
  }
897
909
  };
898
910
  this.resizeItem = (index, size) => {
899
- var _a, _b, _c;
911
+ var _a, _b, _c, _d, _e;
900
912
  if (!this.isIndexInRange(index)) return;
913
+ if ((_a = this._resizeBatch) == null ? void 0 : _a.sync) this.getMeasurements();
901
914
  let cachedSize;
902
915
  let itemStart;
903
916
  let key;
904
- const flat = (_a = this._singleLaneMeasurements) == null ? void 0 : _a.flat;
917
+ const flat = (_b = this._singleLaneMeasurements) == null ? void 0 : _b.flat;
905
918
  if (this.options.lanes === 1 && flat != null) {
906
919
  key = this.options.getItemKey(index);
907
920
  itemStart = flat[index * 2];
@@ -916,7 +929,7 @@ class Virtualizer {
916
929
  const itemSize = this.itemSizeCache.get(key) ?? cachedSize;
917
930
  const delta = size - itemSize;
918
931
  if (delta !== 0) {
919
- const wasAtEnd = this.options.anchorTo === "end" && ((_b = this.scrollState) == null ? void 0 : _b.behavior) !== "smooth" && this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold;
932
+ const wasAtEnd = this.options.anchorTo === "end" && ((_c = this.scrollState) == null ? void 0 : _c.behavior) !== "smooth" && this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold;
920
933
  const prevTotalSize = wasAtEnd ? this.getTotalSize() : 0;
921
934
  const scrollOffsetWithAdj = this.getScrollOffset() + this.scrollAdjustments;
922
935
  const isFirstMeasure = !this.itemSizeCache.has(key);
@@ -935,7 +948,7 @@ class Virtualizer {
935
948
  // scrolling up" cascade.
936
949
  itemStart + itemSize <= scrollOffsetWithAdj && this.scrollDirection !== "backward"
937
950
  );
938
- const shouldAdjustScroll = ((_c = this.scrollState) == null ? void 0 : _c.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
951
+ const shouldAdjustScroll = ((_d = this.scrollState) == null ? void 0 : _d.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
939
952
  // The callback expects a VirtualItem; build one lazily only
940
953
  // when the consumer actually supplied a custom predicate.
941
954
  this.measurementsCache[index] ?? {
@@ -962,6 +975,11 @@ class Virtualizer {
962
975
  } else if (shouldAdjustScroll) {
963
976
  adjustedSync = this.applyScrollAdjustment(delta);
964
977
  }
978
+ if (this._resizeBatch) {
979
+ this._resizeBatch.notify = true;
980
+ (_e = this._resizeBatch).sync || (_e.sync = adjustedSync);
981
+ return;
982
+ }
965
983
  this.notify(adjustedSync);
966
984
  this._retryClampedAdjustment();
967
985
  }
@@ -1214,12 +1232,15 @@ class Virtualizer {
1214
1232
  // `resizeItem` uses that to decide whether the follow-up `notify` must be
1215
1233
  // synchronous so the grown transforms commit in the same paint (#1227).
1216
1234
  applyScrollAdjustment(delta, behavior) {
1235
+ var _a;
1217
1236
  if (delta === 0) return false;
1218
1237
  if (process.env.NODE_ENV !== "production" && this.options.debug) {
1219
1238
  console.info("correction", delta);
1220
1239
  }
1221
1240
  if (isIOSWebKit() && (this.isScrolling || this._iosTouching || this._iosJustTouchEnded)) {
1222
- this._iosDeferredAdjustment += delta;
1241
+ if (((_a = this.scrollState) == null ? void 0 : _a.index) == null) {
1242
+ this._iosDeferredAdjustment += delta;
1243
+ }
1223
1244
  return false;
1224
1245
  } else {
1225
1246
  const target = this.getScrollOffset() + this.scrollAdjustments + delta;
@@ -1262,6 +1283,7 @@ class Virtualizer {
1262
1283
  this.scrollState = null;
1263
1284
  return;
1264
1285
  }
1286
+ this.getMeasurements();
1265
1287
  const targetOffset = this.getScrollStateTarget(this.scrollState);
1266
1288
  const STABLE_FRAMES = this.scrollState.index == null ? 1 : this.options.useAnimationFrameWithResizeObserver ? 3 : 2;
1267
1289
  const targetChanged = targetOffset !== this.scrollState.lastTargetOffset;