@tanstack/virtual-core 3.19.13 → 3.19.15

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.
@@ -217,6 +217,8 @@ class Virtualizer {
217
217
  this.scrollOffset = null;
218
218
  this.scrollDirection = null;
219
219
  this._holdOffset = 0;
220
+ this._wheelTimer = null;
221
+ this._edgeFlushFrame = null;
220
222
  this._reportedOverscroll = false;
221
223
  this._trailing = 0;
222
224
  this._drawnListEnd = Infinity;
@@ -258,6 +260,7 @@ class Virtualizer {
258
260
  this._clampedAdjustment = null;
259
261
  this.elementsCache = /* @__PURE__ */ new Map();
260
262
  this._resizeBatch = null;
263
+ this._dropQueued = false;
261
264
  this.now = () => {
262
265
  var _a, _b, _c;
263
266
  return ((_c = (_b = (_a = this.targetWindow) == null ? void 0 : _a.performance) == null ? void 0 : _b.now) == null ? void 0 : _c.call(_b)) ?? Date.now();
@@ -508,6 +511,16 @@ class Virtualizer {
508
511
  this.isScrolling = false;
509
512
  this.scrollDirection = null;
510
513
  this._holdOffset = 0;
514
+ if (this.targetWindow) {
515
+ if (this._wheelTimer !== null) {
516
+ this.targetWindow.clearTimeout(this._wheelTimer);
517
+ }
518
+ if (this._edgeFlushFrame !== null) {
519
+ this.targetWindow.cancelAnimationFrame(this._edgeFlushFrame);
520
+ }
521
+ }
522
+ this._wheelTimer = null;
523
+ this._edgeFlushFrame = null;
511
524
  this._reportedOverscroll = false;
512
525
  this._trailing = 0;
513
526
  this._clampedAdjustment = null;
@@ -618,6 +631,7 @@ class Virtualizer {
618
631
  this.scrollState = null;
619
632
  }
620
633
  this._flushHoldIfReady(true);
634
+ this._flushHoldAtTop();
621
635
  if (this.scrollState) {
622
636
  this.scheduleScrollReconcile();
623
637
  }
@@ -625,9 +639,23 @@ class Virtualizer {
625
639
  })
626
640
  );
627
641
  if ("addEventListener" in this.scrollElement) {
628
- this.unsubs.push(
629
- this._iosGesture.listen(this.scrollElement)
630
- );
642
+ const target = this.scrollElement;
643
+ this.unsubs.push(this._iosGesture.listen(target));
644
+ if (iosGesture.isWebKit()) {
645
+ const onWheel = () => {
646
+ const win = this.targetWindow;
647
+ if (win === null) return;
648
+ if (this._wheelTimer !== null) win.clearTimeout(this._wheelTimer);
649
+ this._wheelTimer = win.setTimeout(() => {
650
+ this._wheelTimer = null;
651
+ this._flushHoldIfReady(true);
652
+ this._retryClampedAdjustment();
653
+ }, 150);
654
+ this._flushHoldAtTop();
655
+ };
656
+ target.addEventListener("wheel", onWheel, addEventListenerOptions);
657
+ this.unsubs.push(() => target.removeEventListener("wheel", onWheel));
658
+ }
631
659
  }
632
660
  this._scrollToOffset(this.getScrollOffset());
633
661
  this._readTrailing(this._elementMaxScrollOffset());
@@ -705,7 +733,28 @@ class Virtualizer {
705
733
  }
706
734
  return null;
707
735
  };
708
- this._canWriteScroll = () => !(iosGesture.isIOSWebKit() && this._iosGesture.inMotion()) && !this._reportedOverscroll;
736
+ this._canWriteScroll = () => !(iosGesture.isIOSWebKit() && this._iosGesture.inMotion()) && !this._reportedOverscroll && this._wheelTimer === null;
737
+ this._holdAtTop = () => {
738
+ if (this._holdOffset === 0) return false;
739
+ const offset = this._readElementOffset();
740
+ return offset !== null && Math.min(offset, offset + this._holdOffset) < this.options.scrollMargin + 1;
741
+ };
742
+ this._flushHoldAtTop = () => {
743
+ const win = this.targetWindow;
744
+ if (win === null || this._wheelTimer === null || this._edgeFlushFrame !== null || !this._holdAtTop()) {
745
+ return;
746
+ }
747
+ this._edgeFlushFrame = win.requestAnimationFrame(() => {
748
+ this._edgeFlushFrame = win.requestAnimationFrame(() => {
749
+ this._edgeFlushFrame = null;
750
+ if (this._wheelTimer === null || !this._holdAtTop()) return;
751
+ win.clearTimeout(this._wheelTimer);
752
+ this._wheelTimer = null;
753
+ this._flushHoldIfReady(true);
754
+ this._retryClampedAdjustment();
755
+ });
756
+ });
757
+ };
709
758
  this._flushHoldIfReady = (sync) => {
710
759
  var _a;
711
760
  if (this._holdOffset === 0) return;
@@ -735,7 +784,7 @@ class Virtualizer {
735
784
  this.scrollOffset = null;
736
785
  return 0;
737
786
  }
738
- this.scrollOffset = this.scrollOffset ?? (typeof this.options.initialOffset === "function" ? this.options.initialOffset() : this.options.initialOffset);
787
+ this.scrollOffset = this.scrollOffset ?? (typeof this.options.initialOffset === "function" ? this.options.initialOffset(this) : this.options.initialOffset);
739
788
  return this.scrollOffset;
740
789
  };
741
790
  this.getMeasurementOptions = utils.memo(
@@ -1025,14 +1074,24 @@ class Virtualizer {
1025
1074
  }
1026
1075
  return true;
1027
1076
  };
1077
+ this.dropDisconnected = () => {
1078
+ this.elementsCache.forEach((cached, key) => {
1079
+ if (!cached.isConnected) {
1080
+ this.observer.unobserve(cached);
1081
+ this.elementsCache.delete(key);
1082
+ }
1083
+ });
1084
+ };
1028
1085
  this.measureElement = (node) => {
1029
1086
  if (!node) {
1030
- this.elementsCache.forEach((cached, key2) => {
1031
- if (!cached.isConnected) {
1032
- this.observer.unobserve(cached);
1033
- this.elementsCache.delete(key2);
1034
- }
1035
- });
1087
+ this.dropDisconnected();
1088
+ if (!this._dropQueued) {
1089
+ this._dropQueued = true;
1090
+ queueMicrotask(() => {
1091
+ this._dropQueued = false;
1092
+ this.dropDisconnected();
1093
+ });
1094
+ }
1036
1095
  return;
1037
1096
  }
1038
1097
  const index = this.indexFromElement(node);
@@ -1054,6 +1113,9 @@ class Virtualizer {
1054
1113
  this.resizeItem = (index, size) => {
1055
1114
  var _a, _b, _c;
1056
1115
  if (!this.isIndexInRange(index)) return;
1116
+ if (this.getMeasurementOptions() !== this.builtMeasurementOptions) {
1117
+ this.getMeasurements();
1118
+ }
1057
1119
  let startShift = 0;
1058
1120
  if ((_a = this._resizeBatch) == null ? void 0 : _a.sync) {
1059
1121
  if (this.canShiftFromPendingResizes()) {
@@ -1575,15 +1637,23 @@ class Virtualizer {
1575
1637
  // Every scroll command starts here: it becomes the in-flight command,
1576
1638
  // writes its first target and leaves reconcile to retarget and retire it.
1577
1639
  //
1578
- // Targets are layout offsets. A held correction is not dropped by a
1579
- // command: the write lands the element `_holdOffset` short of the target,
1580
- // under the rows exposed shifted by the same amount, and the flush moves
1581
- // both together once a write is possible.
1640
+ // Targets are layout offsets. A correction held for the wheel is written
1641
+ // before the command (below). One held for a touch or a bounce is not
1642
+ // dropped by a command: the write lands the element `_holdOffset` short
1643
+ // of the target, under the rows exposed shifted by the same amount, and
1644
+ // the flush moves both together once a write is possible. Unless the
1645
+ // target is less than the hold (below).
1582
1646
  //
1583
1647
  // An index or end target that is already at its offset starts a smooth
1584
1648
  // scroll as landed; an offset target never does, so its first scroll
1585
1649
  // event is read as the user's (see the scroll handler in `_willUpdate`).
1586
1650
  _startScroll(target, offset, behavior, kind) {
1651
+ if (this._wheelTimer !== null && this.targetWindow) {
1652
+ this.targetWindow.clearTimeout(this._wheelTimer);
1653
+ this._wheelTimer = null;
1654
+ this.scrollState = null;
1655
+ this._flushHoldIfReady(true);
1656
+ }
1587
1657
  this.pendingScrollAnchor = null;
1588
1658
  this._clampedAdjustment = null;
1589
1659
  const tail = this._iosGesture.tail();
@@ -1617,6 +1687,12 @@ class Virtualizer {
1617
1687
  }
1618
1688
  this._iosGesture.end();
1619
1689
  }
1690
+ if (offset < this._holdOffset && this._canWriteScroll()) {
1691
+ this.scrollState = null;
1692
+ this._flushHoldIfReady(true);
1693
+ this._clampedAdjustment = null;
1694
+ offset = Math.min(offset, Math.max(this.getMaxScrollOffset(), 0));
1695
+ }
1620
1696
  this.scrollState = {
1621
1697
  ...target,
1622
1698
  startedAt: this.now(),