@tanstack/virtual-core 3.17.8 → 3.17.10

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.
@@ -96,7 +96,7 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
96
96
  isScrolling: boolean;
97
97
  private scrollState;
98
98
  measurementsCache: Array<VirtualItem>;
99
- private _flatMeasurements;
99
+ private _singleLaneMeasurements;
100
100
  itemSizeCache: Map<Key, number>;
101
101
  private itemSizeCacheVersion;
102
102
  private laneAssignments;
@@ -114,6 +114,7 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
114
114
  private _iosJustTouchEnded;
115
115
  private _iosTouchEndTimerId;
116
116
  private _intendedScrollOffset;
117
+ private _clampedAdjustment;
117
118
  shouldAdjustScrollPositionOnItemSizeChange: undefined | ((item: VirtualItem, delta: number, instance: Virtualizer<TScrollElement, TItemElement>) => boolean);
118
119
  elementsCache: Map<Key, TItemElement>;
119
120
  private now;
@@ -130,6 +131,7 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
130
131
  private cleanup;
131
132
  _didMount: () => () => void;
132
133
  _willUpdate: () => void;
134
+ private _retryClampedAdjustment;
133
135
  private _flushIosDeferredIfReady;
134
136
  private rafId;
135
137
  private scheduleScrollReconcile;
@@ -169,7 +171,7 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
169
171
  getDistanceFromEnd: () => number;
170
172
  isAtEnd: (threshold?: number) => boolean;
171
173
  getOffsetForAlignment: (toOffset: number, align: ScrollAlignment, itemSize?: number) => number;
172
- getOffsetForIndex: (index: number, align?: ScrollAlignment) => readonly [number, "auto"] | readonly [number, "start" | "center" | "end"] | undefined;
174
+ getOffsetForIndex: (index: number, align?: ScrollAlignment) => readonly [number, "auto"] | readonly [number, "start" | "end" | "center"] | undefined;
173
175
  scrollToOffset: (toOffset: number, { align, behavior }?: ScrollToOffsetOptions) => void;
174
176
  scrollToIndex: (index: number, { align: initialAlign, behavior, }?: ScrollToIndexOptions) => void;
175
177
  scrollBy: (delta: number, { behavior }?: ScrollToOffsetOptions) => void;
package/dist/esm/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { createLazyMeasurementsView } from "./lazy-measurements.js";
1
+ import { createLazyMeasurementsView, getMeasurementKey } from "./lazy-measurements.js";
2
2
  import { memo, notUndefined, approxEqual, debounce } from "./utils.js";
3
3
  let _isIOSResult;
4
4
  const isIOSWebKit = () => {
@@ -162,6 +162,27 @@ const scrollWithAdjustments = (offset, {
162
162
  };
163
163
  const windowScroll = scrollWithAdjustments;
164
164
  const elementScroll = scrollWithAdjustments;
165
+ function isAppendWithTrim(prevCount, nextCount, getPreviousKey, getNextKey) {
166
+ if (nextCount === 0) return false;
167
+ const firstKey = getNextKey(0);
168
+ const removedKeys = /* @__PURE__ */ new Set();
169
+ let removedCount = 0;
170
+ while (removedCount < prevCount) {
171
+ const key = getPreviousKey(removedCount);
172
+ if (key === firstKey) break;
173
+ removedKeys.add(key);
174
+ removedCount++;
175
+ }
176
+ const retainedCount = prevCount - removedCount;
177
+ if (retainedCount === 0 || retainedCount >= nextCount) return false;
178
+ for (let i = 0; i < retainedCount; i++) {
179
+ if (getNextKey(i) !== getPreviousKey(removedCount + i)) return false;
180
+ }
181
+ for (let i = retainedCount; i < nextCount; i++) {
182
+ if (removedKeys.has(getNextKey(i))) return false;
183
+ }
184
+ return true;
185
+ }
165
186
  class Virtualizer {
166
187
  constructor(opts) {
167
188
  this.unsubs = [];
@@ -170,7 +191,7 @@ class Virtualizer {
170
191
  this.isScrolling = false;
171
192
  this.scrollState = null;
172
193
  this.measurementsCache = [];
173
- this._flatMeasurements = null;
194
+ this._singleLaneMeasurements = null;
174
195
  this.itemSizeCache = /* @__PURE__ */ new Map();
175
196
  this.itemSizeCacheVersion = 0;
176
197
  this.laneAssignments = /* @__PURE__ */ new Map();
@@ -188,6 +209,7 @@ class Virtualizer {
188
209
  this._iosJustTouchEnded = false;
189
210
  this._iosTouchEndTimerId = null;
190
211
  this._intendedScrollOffset = null;
212
+ this._clampedAdjustment = null;
191
213
  this.elementsCache = /* @__PURE__ */ new Map();
192
214
  this.now = () => {
193
215
  var _a, _b, _c;
@@ -247,7 +269,7 @@ class Virtualizer {
247
269
  })();
248
270
  this.range = null;
249
271
  this.setOptions = (opts2) => {
250
- var _a, _b;
272
+ var _a;
251
273
  const merged = {
252
274
  debug: false,
253
275
  initialOffset: 0,
@@ -291,8 +313,10 @@ class Virtualizer {
291
313
  const prevCount = prevOptions.count;
292
314
  const nextCount = merged.count;
293
315
  const measurements = this.getMeasurements();
294
- const prevFirstKey = prevCount > 0 ? ((_a = measurements[0]) == null ? void 0 : _a.key) ?? prevOptions.getItemKey(0) : null;
295
- const prevLastKey = prevCount > 0 ? ((_b = measurements[prevCount - 1]) == null ? void 0 : _b.key) ?? prevOptions.getItemKey(prevCount - 1) : null;
316
+ const previousItems = ((_a = this._singleLaneMeasurements) == null ? void 0 : _a.items) ?? measurements;
317
+ const getPreviousKey = (index) => getMeasurementKey(previousItems[index]);
318
+ const prevFirstKey = prevCount > 0 ? getPreviousKey(0) : null;
319
+ const prevLastKey = prevCount > 0 ? getPreviousKey(prevCount - 1) : null;
296
320
  const didCountChange = nextCount !== prevCount;
297
321
  const didEdgeKeysChange = didCountChange || prevCount > 0 && nextCount > 0 && (merged.getItemKey(0) !== prevFirstKey || merged.getItemKey(nextCount - 1) !== prevLastKey);
298
322
  if (didEdgeKeysChange) {
@@ -302,8 +326,15 @@ class Virtualizer {
302
326
  anchor = [item.key, this.getScrollOffset() - item.start];
303
327
  }
304
328
  const behavior = merged.followOnAppend === true ? "auto" : merged.followOnAppend || null;
305
- if (behavior && nextCount > prevCount && this.isAtEnd(prevOptions.scrollEndThreshold) && (prevCount === 0 || merged.getItemKey(nextCount - 1) !== prevLastKey)) {
306
- followOnAppend = behavior;
329
+ if (behavior && nextCount > 0 && this.isAtEnd(prevOptions.scrollEndThreshold) && (prevCount === 0 || merged.getItemKey(nextCount - 1) !== prevLastKey)) {
330
+ if (nextCount > prevCount || isAppendWithTrim(
331
+ prevCount,
332
+ nextCount,
333
+ getPreviousKey,
334
+ merged.getItemKey
335
+ )) {
336
+ followOnAppend = behavior;
337
+ }
307
338
  }
308
339
  }
309
340
  }
@@ -326,7 +357,7 @@ class Virtualizer {
326
357
  const anchorItem = newMeasurements[idx];
327
358
  if (anchorItem) {
328
359
  const newOffset = Math.max(0, anchorItem.start + anchorOffset);
329
- if (newOffset !== this.scrollOffset) {
360
+ if (!followOnAppend && newOffset !== this.scrollOffset) {
330
361
  anchorDelta = newOffset - this.scrollOffset;
331
362
  this.scrollOffset = newOffset;
332
363
  anchorResolved = true;
@@ -383,6 +414,7 @@ class Virtualizer {
383
414
  this._iosDeferredAdjustment = 0;
384
415
  this._iosTouching = false;
385
416
  this._iosJustTouchEnded = false;
417
+ this._clampedAdjustment = null;
386
418
  this.scrollElement = null;
387
419
  this.targetWindow = null;
388
420
  };
@@ -424,6 +456,9 @@ class Virtualizer {
424
456
  offset = this._intendedScrollOffset;
425
457
  }
426
458
  this._intendedScrollOffset = null;
459
+ if (this._clampedAdjustment !== null && Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5) {
460
+ this._clampedAdjustment = null;
461
+ }
427
462
  this.scrollAdjustments = 0;
428
463
  const prevOffset = this.getScrollOffset();
429
464
  this.scrollDirection = isScrolling ? prevOffset === offset ? this.scrollDirection : prevOffset < offset ? "forward" : "backward" : null;
@@ -502,6 +537,21 @@ class Virtualizer {
502
537
  this.scrollToEnd({ behavior: followOnAppend });
503
538
  }
504
539
  }
540
+ this._retryClampedAdjustment();
541
+ };
542
+ this._retryClampedAdjustment = () => {
543
+ if (this._clampedAdjustment === null || !this.scrollElement || !this.options.enabled) {
544
+ return;
545
+ }
546
+ const { target, maxAtWrite } = this._clampedAdjustment;
547
+ const max = this.getMaxScrollOffset();
548
+ if (max > maxAtWrite + 0.5) {
549
+ this._clampedAdjustment = target > max + 0.5 ? { target, maxAtWrite: max } : null;
550
+ this._scrollToOffset(target, {
551
+ adjustments: void 0,
552
+ behavior: void 0
553
+ });
554
+ }
505
555
  };
506
556
  this._flushIosDeferredIfReady = () => {
507
557
  if (this._iosDeferredAdjustment === 0) return;
@@ -585,9 +635,11 @@ class Virtualizer {
585
635
  laneAssignmentMode,
586
636
  gap
587
637
  }, _itemSizeCacheVersion) => {
638
+ var _a;
588
639
  const itemSizeCache = this.itemSizeCache;
589
640
  if (!enabled) {
590
641
  this.measurementsCache = [];
642
+ this._singleLaneMeasurements = null;
591
643
  this.itemSizeCache.clear();
592
644
  this.laneAssignments.clear();
593
645
  return [];
@@ -603,6 +655,7 @@ class Virtualizer {
603
655
  this.lanesChangedFlag = false;
604
656
  this.lanesSettling = true;
605
657
  this.measurementsCache = [];
658
+ this._singleLaneMeasurements = null;
606
659
  this.itemSizeCache.clear();
607
660
  this.laneAssignments.clear();
608
661
  this.pendingMin = null;
@@ -620,13 +673,13 @@ class Virtualizer {
620
673
  }
621
674
  if (lanes === 1) {
622
675
  const need = count * 2;
623
- let flat = this._flatMeasurements;
676
+ let flat = (_a = this._singleLaneMeasurements) == null ? void 0 : _a.flat;
624
677
  if (!flat || flat.length < need) {
625
678
  const next = new Float64Array(need);
626
679
  if (flat && min > 0) next.set(flat.subarray(0, min * 2));
627
680
  flat = next;
628
- this._flatMeasurements = flat;
629
681
  }
682
+ const items = min === 0 ? new Array(count) : this._singleLaneMeasurements.items.slice();
630
683
  let runningStart;
631
684
  if (min === 0) {
632
685
  runningStart = paddingStart + scrollMargin;
@@ -636,13 +689,15 @@ class Virtualizer {
636
689
  }
637
690
  for (let i = min; i < count; i++) {
638
691
  const key = getItemKey(i);
692
+ items[i] = key;
639
693
  const measuredSize = itemSizeCache.get(key);
640
694
  const size = typeof measuredSize === "number" ? measuredSize : this.options.estimateSize(i);
641
695
  flat[i * 2] = runningStart;
642
696
  flat[i * 2 + 1] = size;
643
697
  runningStart += size + gap;
644
698
  }
645
- const view = createLazyMeasurementsView(count, flat, getItemKey);
699
+ this._singleLaneMeasurements = { flat, items };
700
+ const view = createLazyMeasurementsView(items, flat);
646
701
  this.measurementsCache = view;
647
702
  return view;
648
703
  }
@@ -737,7 +792,7 @@ class Virtualizer {
737
792
  lanes,
738
793
  // Pass the typed array so binary search + forward-walk can read
739
794
  // start/end directly from Float64Array, skipping the Proxy traps.
740
- lanes === 1 && this._flatMeasurements != null ? this._flatMeasurements : null
795
+ lanes === 1 && this._singleLaneMeasurements !== null ? this._singleLaneMeasurements.flat : null
741
796
  );
742
797
  return this.range;
743
798
  },
@@ -834,13 +889,13 @@ class Virtualizer {
834
889
  }
835
890
  };
836
891
  this.resizeItem = (index, size) => {
837
- var _a, _b;
892
+ var _a, _b, _c;
838
893
  if (!this.isIndexInRange(index)) return;
839
894
  let cachedSize;
840
895
  let itemStart;
841
896
  let key;
842
- const flat = this._flatMeasurements;
843
- if (this.options.lanes === 1 && flat !== null) {
897
+ const flat = (_a = this._singleLaneMeasurements) == null ? void 0 : _a.flat;
898
+ if (this.options.lanes === 1 && flat != null) {
844
899
  key = this.options.getItemKey(index);
845
900
  itemStart = flat[index * 2];
846
901
  cachedSize = flat[index * 2 + 1];
@@ -854,7 +909,7 @@ class Virtualizer {
854
909
  const itemSize = this.itemSizeCache.get(key) ?? cachedSize;
855
910
  const delta = size - itemSize;
856
911
  if (delta !== 0) {
857
- const wasAtEnd = this.options.anchorTo === "end" && ((_a = this.scrollState) == null ? void 0 : _a.behavior) !== "smooth" && this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold;
912
+ const wasAtEnd = this.options.anchorTo === "end" && ((_b = this.scrollState) == null ? void 0 : _b.behavior) !== "smooth" && this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold;
858
913
  const prevTotalSize = wasAtEnd ? this.getTotalSize() : 0;
859
914
  const scrollOffsetWithAdj = this.getScrollOffset() + this.scrollAdjustments;
860
915
  const isFirstMeasure = !this.itemSizeCache.has(key);
@@ -873,7 +928,7 @@ class Virtualizer {
873
928
  // scrolling up" cascade.
874
929
  itemStart + itemSize <= scrollOffsetWithAdj && this.scrollDirection !== "backward"
875
930
  );
876
- const shouldAdjustScroll = ((_b = this.scrollState) == null ? void 0 : _b.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
931
+ const shouldAdjustScroll = ((_c = this.scrollState) == null ? void 0 : _c.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
877
932
  // The callback expects a VirtualItem; build one lazily only
878
933
  // when the consumer actually supplied a custom predicate.
879
934
  this.measurementsCache[index] ?? {
@@ -901,6 +956,7 @@ class Virtualizer {
901
956
  adjustedSync = this.applyScrollAdjustment(delta);
902
957
  }
903
958
  this.notify(adjustedSync);
959
+ this._retryClampedAdjustment();
904
960
  }
905
961
  };
906
962
  this.getVirtualItems = memo(
@@ -920,11 +976,12 @@ class Virtualizer {
920
976
  }
921
977
  );
922
978
  this.getVirtualItemForOffset = (offset) => {
979
+ var _a;
923
980
  const measurements = this.getMeasurements();
924
981
  if (measurements.length === 0) {
925
982
  return void 0;
926
983
  }
927
- const flat = this._flatMeasurements;
984
+ const flat = (_a = this._singleLaneMeasurements) == null ? void 0 : _a.flat;
928
985
  const useFlat = this.options.lanes === 1 && flat != null;
929
986
  const idx = findNearestBinarySearch(
930
987
  0,
@@ -1059,18 +1116,18 @@ class Virtualizer {
1059
1116
  });
1060
1117
  };
1061
1118
  this.getTotalSize = () => {
1062
- var _a;
1119
+ var _a, _b;
1063
1120
  const measurements = this.getMeasurements();
1064
1121
  let end;
1065
1122
  if (measurements.length === 0) {
1066
1123
  end = this.options.paddingStart;
1067
1124
  } else if (this.options.lanes === 1) {
1068
1125
  const lastIdx = measurements.length - 1;
1069
- const flat = this._flatMeasurements;
1126
+ const flat = (_a = this._singleLaneMeasurements) == null ? void 0 : _a.flat;
1070
1127
  if (flat != null) {
1071
1128
  end = flat[lastIdx * 2] + flat[lastIdx * 2 + 1];
1072
1129
  } else {
1073
- end = ((_a = measurements[lastIdx]) == null ? void 0 : _a.end) ?? 0;
1130
+ end = ((_b = measurements[lastIdx]) == null ? void 0 : _b.end) ?? 0;
1074
1131
  }
1075
1132
  } else {
1076
1133
  const endByLane = Array(this.options.lanes).fill(null);
@@ -1136,6 +1193,10 @@ class Virtualizer {
1136
1193
  this._iosDeferredAdjustment += delta;
1137
1194
  return false;
1138
1195
  } else {
1196
+ const target = this.getScrollOffset() + this.scrollAdjustments + delta;
1197
+ const el = this.scrollElement;
1198
+ const maxAtWrite = el !== null && ("scrollHeight" in el || "document" in el) ? this.getMaxScrollOffset() : null;
1199
+ this._clampedAdjustment = maxAtWrite !== null && target > maxAtWrite + 0.5 ? { target, maxAtWrite } : null;
1139
1200
  this._scrollToOffset(this.getScrollOffset(), {
1140
1201
  adjustments: this.scrollAdjustments += delta,
1141
1202
  behavior