@tanstack/virtual-core 3.19.12 → 3.19.14

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.
@@ -203,16 +203,22 @@ class Virtualizer {
203
203
  this._singleLaneMeasurements = null;
204
204
  this.itemSizeCache = /* @__PURE__ */ new Map();
205
205
  this.itemSizeCacheVersion = 0;
206
+ this.builtMeasurementOptions = null;
207
+ this.builtItemSizeCacheVersion = -1;
208
+ this.pendingResizes = [];
206
209
  this.laneAssignments = /* @__PURE__ */ new Map();
207
210
  this.pendingMin = null;
208
211
  this.prevLanes = void 0;
209
212
  this.lanesChangedFlag = false;
210
213
  this.lanesSettling = false;
214
+ this.initialMeasurementsCacheApplied = false;
211
215
  this.pendingScrollAnchor = null;
212
216
  this.scrollRect = null;
213
217
  this.scrollOffset = null;
214
218
  this.scrollDirection = null;
215
219
  this._holdOffset = 0;
220
+ this._wheelTimer = null;
221
+ this._edgeFlushFrame = null;
216
222
  this._reportedOverscroll = false;
217
223
  this._trailing = 0;
218
224
  this._drawnListEnd = Infinity;
@@ -504,6 +510,16 @@ class Virtualizer {
504
510
  this.isScrolling = false;
505
511
  this.scrollDirection = null;
506
512
  this._holdOffset = 0;
513
+ if (this.targetWindow) {
514
+ if (this._wheelTimer !== null) {
515
+ this.targetWindow.clearTimeout(this._wheelTimer);
516
+ }
517
+ if (this._edgeFlushFrame !== null) {
518
+ this.targetWindow.cancelAnimationFrame(this._edgeFlushFrame);
519
+ }
520
+ }
521
+ this._wheelTimer = null;
522
+ this._edgeFlushFrame = null;
507
523
  this._reportedOverscroll = false;
508
524
  this._trailing = 0;
509
525
  this._clampedAdjustment = null;
@@ -614,6 +630,7 @@ class Virtualizer {
614
630
  this.scrollState = null;
615
631
  }
616
632
  this._flushHoldIfReady(true);
633
+ this._flushHoldAtTop();
617
634
  if (this.scrollState) {
618
635
  this.scheduleScrollReconcile();
619
636
  }
@@ -621,9 +638,23 @@ class Virtualizer {
621
638
  })
622
639
  );
623
640
  if ("addEventListener" in this.scrollElement) {
624
- this.unsubs.push(
625
- this._iosGesture.listen(this.scrollElement)
626
- );
641
+ const target = this.scrollElement;
642
+ this.unsubs.push(this._iosGesture.listen(target));
643
+ if (iosGesture.isWebKit()) {
644
+ const onWheel = () => {
645
+ const win = this.targetWindow;
646
+ if (win === null) return;
647
+ if (this._wheelTimer !== null) win.clearTimeout(this._wheelTimer);
648
+ this._wheelTimer = win.setTimeout(() => {
649
+ this._wheelTimer = null;
650
+ this._flushHoldIfReady(true);
651
+ this._retryClampedAdjustment();
652
+ }, 150);
653
+ this._flushHoldAtTop();
654
+ };
655
+ target.addEventListener("wheel", onWheel, addEventListenerOptions);
656
+ this.unsubs.push(() => target.removeEventListener("wheel", onWheel));
657
+ }
627
658
  }
628
659
  this._scrollToOffset(this.getScrollOffset());
629
660
  this._readTrailing(this._elementMaxScrollOffset());
@@ -701,7 +732,28 @@ class Virtualizer {
701
732
  }
702
733
  return null;
703
734
  };
704
- this._canWriteScroll = () => !(iosGesture.isIOSWebKit() && this._iosGesture.inMotion()) && !this._reportedOverscroll;
735
+ this._canWriteScroll = () => !(iosGesture.isIOSWebKit() && this._iosGesture.inMotion()) && !this._reportedOverscroll && this._wheelTimer === null;
736
+ this._holdAtTop = () => {
737
+ if (this._holdOffset === 0) return false;
738
+ const offset = this._readElementOffset();
739
+ return offset !== null && Math.min(offset, offset + this._holdOffset) < this.options.scrollMargin + 1;
740
+ };
741
+ this._flushHoldAtTop = () => {
742
+ const win = this.targetWindow;
743
+ if (win === null || this._wheelTimer === null || this._edgeFlushFrame !== null || !this._holdAtTop()) {
744
+ return;
745
+ }
746
+ this._edgeFlushFrame = win.requestAnimationFrame(() => {
747
+ this._edgeFlushFrame = win.requestAnimationFrame(() => {
748
+ this._edgeFlushFrame = null;
749
+ if (this._wheelTimer === null || !this._holdAtTop()) return;
750
+ win.clearTimeout(this._wheelTimer);
751
+ this._wheelTimer = null;
752
+ this._flushHoldIfReady(true);
753
+ this._retryClampedAdjustment();
754
+ });
755
+ });
756
+ };
705
757
  this._flushHoldIfReady = (sync) => {
706
758
  var _a;
707
759
  if (this._holdOffset === 0) return;
@@ -731,7 +783,7 @@ class Virtualizer {
731
783
  this.scrollOffset = null;
732
784
  return 0;
733
785
  }
734
- this.scrollOffset = this.scrollOffset ?? (typeof this.options.initialOffset === "function" ? this.options.initialOffset() : this.options.initialOffset);
786
+ this.scrollOffset = this.scrollOffset ?? (typeof this.options.initialOffset === "function" ? this.options.initialOffset(this) : this.options.initialOffset);
735
787
  return this.scrollOffset;
736
788
  };
737
789
  this.getMeasurementOptions = utils.memo(
@@ -770,23 +822,28 @@ class Virtualizer {
770
822
  this.isIndexInRange = (index) => index >= 0 && index < this.options.count;
771
823
  this.getMeasurements = utils.memo(
772
824
  () => [this.getMeasurementOptions(), this.itemSizeCacheVersion],
773
- ({
774
- count,
775
- paddingStart,
776
- scrollMargin,
777
- getItemKey,
778
- enabled,
779
- lanes,
780
- laneAssignmentMode,
781
- gap
782
- }, _itemSizeCacheVersion) => {
825
+ (measurementOptions, itemSizeCacheVersion) => {
783
826
  var _a;
827
+ const {
828
+ count,
829
+ paddingStart,
830
+ scrollMargin,
831
+ getItemKey,
832
+ enabled,
833
+ lanes,
834
+ laneAssignmentMode,
835
+ gap
836
+ } = measurementOptions;
837
+ this.builtMeasurementOptions = measurementOptions;
838
+ this.builtItemSizeCacheVersion = itemSizeCacheVersion;
839
+ this.pendingResizes = [];
784
840
  const itemSizeCache = this.itemSizeCache;
785
841
  if (!enabled) {
786
842
  this.measurementsCache = [];
787
843
  this._singleLaneMeasurements = null;
788
844
  this.itemSizeCache.clear();
789
845
  this.laneAssignments.clear();
846
+ this.initialMeasurementsCacheApplied = false;
790
847
  return [];
791
848
  }
792
849
  if (this.laneAssignments.size > count) {
@@ -805,7 +862,8 @@ class Virtualizer {
805
862
  this.laneAssignments.clear();
806
863
  this.pendingMin = null;
807
864
  }
808
- if (this.measurementsCache.length === 0 && !this.lanesSettling) {
865
+ if (!this.initialMeasurementsCacheApplied && this.measurementsCache.length === 0 && !this.lanesSettling) {
866
+ this.initialMeasurementsCacheApplied = true;
809
867
  this.measurementsCache = this.options.initialMeasurementsCache;
810
868
  this.measurementsCache.forEach((item) => {
811
869
  this.itemSizeCache.set(item.key, item.size);
@@ -1040,17 +1098,30 @@ class Virtualizer {
1040
1098
  this.resizeItem(index, this.options.measureElement(node, void 0, this));
1041
1099
  }
1042
1100
  };
1101
+ this.canShiftFromPendingResizes = () => this.options.lanes === 1 && this._singleLaneMeasurements !== null && this.shouldAdjustScrollPositionOnItemSizeChange === void 0 && !this.lanesChangedFlag && !this.lanesSettling && this.getMeasurementOptions() === this.builtMeasurementOptions && this.itemSizeCacheVersion === this.builtItemSizeCacheVersion + this.pendingResizes.length;
1043
1102
  this.resizeItem = (index, size) => {
1044
1103
  var _a, _b, _c;
1045
1104
  if (!this.isIndexInRange(index)) return;
1046
- if ((_a = this._resizeBatch) == null ? void 0 : _a.sync) this.getMeasurements();
1105
+ if (this.getMeasurementOptions() !== this.builtMeasurementOptions) {
1106
+ this.getMeasurements();
1107
+ }
1108
+ let startShift = 0;
1109
+ if ((_a = this._resizeBatch) == null ? void 0 : _a.sync) {
1110
+ if (this.canShiftFromPendingResizes()) {
1111
+ for (const resize of this.pendingResizes) {
1112
+ if (resize.index < index) startShift += resize.delta;
1113
+ }
1114
+ } else {
1115
+ this.getMeasurements();
1116
+ }
1117
+ }
1047
1118
  let cachedSize;
1048
1119
  let itemStart;
1049
1120
  let key;
1050
1121
  const flat = (_b = this._singleLaneMeasurements) == null ? void 0 : _b.flat;
1051
1122
  if (this.options.lanes === 1 && flat != null) {
1052
1123
  key = this.options.getItemKey(index);
1053
- itemStart = flat[index * 2];
1124
+ itemStart = flat[index * 2] + startShift;
1054
1125
  cachedSize = flat[index * 2 + 1];
1055
1126
  } else {
1056
1127
  const item = this.measurementsCache[index];
@@ -1100,6 +1171,7 @@ class Virtualizer {
1100
1171
  }
1101
1172
  this.itemSizeCache.set(key, size);
1102
1173
  this.itemSizeCacheVersion++;
1174
+ this.pendingResizes.push({ index, delta });
1103
1175
  let adjustedSync = false;
1104
1176
  if (!this._awaitingCommandReadBack()) {
1105
1177
  if (wasAtEnd) {
@@ -1554,15 +1626,22 @@ class Virtualizer {
1554
1626
  // Every scroll command starts here: it becomes the in-flight command,
1555
1627
  // writes its first target and leaves reconcile to retarget and retire it.
1556
1628
  //
1557
- // Targets are layout offsets. A held correction is not dropped by a
1558
- // command: the write lands the element `_holdOffset` short of the target,
1559
- // under the rows exposed shifted by the same amount, and the flush moves
1560
- // both together once a write is possible.
1629
+ // Targets are layout offsets. A correction held for the wheel is written
1630
+ // before the command (below). One held for a touch or a bounce is not
1631
+ // dropped by a command: the write lands the element `_holdOffset` short
1632
+ // of the target, under the rows exposed shifted by the same amount, and
1633
+ // the flush moves both together once a write is possible.
1561
1634
  //
1562
1635
  // An index or end target that is already at its offset starts a smooth
1563
1636
  // scroll as landed; an offset target never does, so its first scroll
1564
1637
  // event is read as the user's (see the scroll handler in `_willUpdate`).
1565
1638
  _startScroll(target, offset, behavior, kind) {
1639
+ if (this._wheelTimer !== null && this.targetWindow) {
1640
+ this.targetWindow.clearTimeout(this._wheelTimer);
1641
+ this._wheelTimer = null;
1642
+ this.scrollState = null;
1643
+ this._flushHoldIfReady(true);
1644
+ }
1566
1645
  this.pendingScrollAnchor = null;
1567
1646
  this._clampedAdjustment = null;
1568
1647
  const tail = this._iosGesture.tail();