@tanstack/virtual-core 3.19.1 → 3.19.2

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.
@@ -165,6 +165,9 @@ const scrollWithAdjustments = (offset, {
165
165
  };
166
166
  const windowScroll = scrollWithAdjustments;
167
167
  const elementScroll = scrollWithAdjustments;
168
+ const initialScrollPhase = (behavior, atTarget) => behavior === "smooth" ? atTarget ? "smoothLanded" : "smooth" : "instant";
169
+ const isSmoothScroll = (state) => (state == null ? void 0 : state.phase) === "smooth" || (state == null ? void 0 : state.phase) === "smoothLanded";
170
+ const isAnimating = (state) => state.phase === "smooth" || state.phase === "smoothFinishing";
168
171
  function isAppendWithTrim(prevCount, nextCount, getPreviousKey, getNextKey) {
169
172
  if (nextCount === 0) return false;
170
173
  const firstKey = getNextKey(0);
@@ -436,7 +439,7 @@ class Virtualizer {
436
439
  };
437
440
  };
438
441
  this._willUpdate = () => {
439
- var _a, _b;
442
+ var _a;
440
443
  const scrollElement = this.options.enabled ? this.options.getScrollElement() : null;
441
444
  if (this.scrollElement !== scrollElement) {
442
445
  this.cleanup();
@@ -478,9 +481,9 @@ class Virtualizer {
478
481
  // like the user and the scroll stops at the max. That needs the
479
482
  // sizer re-rendered before the scroll event, which browsers have
480
483
  // not been seen to do (pinned by a `.fails` test).
481
- // A smooth scroll's own animation events look like a user's, so
482
- // it only yields once it has reached the target.
483
- (this.scrollState.hasReachedTarget || !this.scrollState.startedSmooth) && !utils.approxEqual(offset, this.scrollState.lastTargetOffset) && !utils.approxEqual(offset, this.getScrollStateTarget(this.scrollState)) && (this._clampedAdjustment === null || Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5)) {
484
+ // While a smooth scroll's animation may still report, its events
485
+ // look like a user's, so it yields only once it has landed.
486
+ !isAnimating(this.scrollState) && !utils.approxEqual(offset, this.scrollState.lastTargetOffset) && !utils.approxEqual(offset, this.getScrollStateTarget(this.scrollState)) && (this._clampedAdjustment === null || Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5)) {
484
487
  this.scrollState = null;
485
488
  }
486
489
  this._intendedScrollOffset = null;
@@ -532,9 +535,15 @@ class Virtualizer {
532
535
  onTouchEnd,
533
536
  addEventListenerOptions
534
537
  );
538
+ scrollEl.addEventListener(
539
+ "touchcancel",
540
+ onTouchEnd,
541
+ addEventListenerOptions
542
+ );
535
543
  this.unsubs.push(() => {
536
544
  scrollEl.removeEventListener("touchstart", onTouchStart);
537
545
  scrollEl.removeEventListener("touchend", onTouchEnd);
546
+ scrollEl.removeEventListener("touchcancel", onTouchEnd);
538
547
  if (this._iosTouchEndTimerId !== null && this.targetWindow != null) {
539
548
  this.targetWindow.clearTimeout(this._iosTouchEndTimerId);
540
549
  this._iosTouchEndTimerId = null;
@@ -555,7 +564,7 @@ class Virtualizer {
555
564
  if (anchorDelta !== 0) {
556
565
  this._iosDeferredAdjustment += anchorDelta;
557
566
  }
558
- } else if (((_b = this.scrollState) == null ? void 0 : _b.behavior) === "smooth" && !utils.approxEqual(
567
+ } else if (isSmoothScroll(this.scrollState) && !utils.approxEqual(
559
568
  this.getScrollOffset() - anchorDelta,
560
569
  this.scrollState.lastTargetOffset
561
570
  )) ;
@@ -880,7 +889,7 @@ class Virtualizer {
880
889
  };
881
890
  this.shouldMeasureDuringScroll = (index) => {
882
891
  var _a;
883
- if (!this.scrollState || this.scrollState.behavior !== "smooth") {
892
+ if (!isSmoothScroll(this.scrollState)) {
884
893
  return true;
885
894
  }
886
895
  const scrollIndex = this.scrollState.index ?? ((_a = this.getVirtualItemForOffset(this.scrollState.lastTargetOffset)) == null ? void 0 : _a.index);
@@ -924,7 +933,7 @@ class Virtualizer {
924
933
  }
925
934
  };
926
935
  this.resizeItem = (index, size) => {
927
- var _a, _b, _c, _d, _e, _f;
936
+ var _a, _b, _c, _d;
928
937
  if (!this.isIndexInRange(index)) return;
929
938
  if ((_a = this._resizeBatch) == null ? void 0 : _a.sync) this.getMeasurements();
930
939
  let cachedSize;
@@ -945,7 +954,7 @@ class Virtualizer {
945
954
  const itemSize = this.itemSizeCache.get(key) ?? cachedSize;
946
955
  const delta = size - itemSize;
947
956
  if (delta !== 0) {
948
- const wasAtEnd = this.options.anchorTo === "end" && ((_c = this.scrollState) == null ? void 0 : _c.behavior) !== "smooth" && this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold;
957
+ const wasAtEnd = this.options.anchorTo === "end" && !isSmoothScroll(this.scrollState) && this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold;
949
958
  const prevTotalSize = wasAtEnd ? this.getTotalSize() : 0;
950
959
  const scrollOffsetWithAdj = this.getScrollOffset() + this.scrollAdjustments;
951
960
  const isFirstMeasure = !this.itemSizeCache.has(key);
@@ -964,7 +973,7 @@ class Virtualizer {
964
973
  // scrolling up" cascade.
965
974
  itemStart + itemSize <= scrollOffsetWithAdj && this.scrollDirection !== "backward"
966
975
  );
967
- const shouldAdjustScroll = ((_d = this.scrollState) == null ? void 0 : _d.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
976
+ const shouldAdjustScroll = !isSmoothScroll(this.scrollState) && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
968
977
  // The callback expects a VirtualItem; build one lazily only
969
978
  // when the consumer actually supplied a custom predicate.
970
979
  this.measurementsCache[index] ?? {
@@ -983,7 +992,7 @@ class Virtualizer {
983
992
  }
984
993
  this.itemSizeCache.set(key, size);
985
994
  this.itemSizeCacheVersion++;
986
- const awaitingCommandReadBack = ((_e = this.scrollState) == null ? void 0 : _e.writtenOffset) != null && !utils.approxEqual(
995
+ const awaitingCommandReadBack = ((_c = this.scrollState) == null ? void 0 : _c.writtenOffset) != null && !utils.approxEqual(
987
996
  this.scrollState.writtenOffset,
988
997
  this.getScrollOffset() + this.scrollAdjustments
989
998
  );
@@ -999,7 +1008,7 @@ class Virtualizer {
999
1008
  }
1000
1009
  if (this._resizeBatch) {
1001
1010
  this._resizeBatch.notify = true;
1002
- (_f = this._resizeBatch).sync || (_f.sync = adjustedSync);
1011
+ (_d = this._resizeBatch).sync || (_d.sync = adjustedSync);
1003
1012
  return;
1004
1013
  }
1005
1014
  this.notify(adjustedSync);
@@ -1107,12 +1116,10 @@ class Virtualizer {
1107
1116
  this.scrollState = {
1108
1117
  index: null,
1109
1118
  align,
1110
- behavior,
1111
1119
  startedAt: now,
1112
1120
  lastTargetOffset: offset,
1113
1121
  stableFrames: 0,
1114
- hasReachedTarget: false,
1115
- startedSmooth: behavior === "smooth"
1122
+ phase: initialScrollPhase(behavior, false)
1116
1123
  };
1117
1124
  this._scrollToOffset(offset, { adjustments: void 0, behavior });
1118
1125
  this.scheduleScrollReconcile();
@@ -1132,12 +1139,13 @@ class Virtualizer {
1132
1139
  this.scrollState = {
1133
1140
  index,
1134
1141
  align,
1135
- behavior,
1136
1142
  startedAt: now,
1137
1143
  lastTargetOffset: offset,
1138
1144
  stableFrames: 0,
1139
- hasReachedTarget: utils.approxEqual(offset, this.getScrollOffset()),
1140
- startedSmooth: behavior === "smooth"
1145
+ phase: initialScrollPhase(
1146
+ behavior,
1147
+ utils.approxEqual(offset, this.getScrollOffset())
1148
+ )
1141
1149
  };
1142
1150
  this._scrollToOffset(offset, { adjustments: void 0, behavior });
1143
1151
  this.scheduleScrollReconcile();
@@ -1152,12 +1160,10 @@ class Virtualizer {
1152
1160
  this.scrollState = {
1153
1161
  index: null,
1154
1162
  align: "start",
1155
- behavior,
1156
1163
  startedAt: now,
1157
1164
  lastTargetOffset: offset,
1158
1165
  stableFrames: 0,
1159
- hasReachedTarget: false,
1160
- startedSmooth: behavior === "smooth"
1166
+ phase: initialScrollPhase(behavior, false)
1161
1167
  };
1162
1168
  this._scrollToOffset(offset, { adjustments: void 0, behavior });
1163
1169
  this.scheduleScrollReconcile();
@@ -1169,13 +1175,14 @@ class Virtualizer {
1169
1175
  this.scrollState = {
1170
1176
  index: this.options.count - 1,
1171
1177
  align: "end",
1172
- behavior,
1173
1178
  toEnd: true,
1174
1179
  startedAt: this.now(),
1175
1180
  lastTargetOffset: offset,
1176
1181
  stableFrames: 0,
1177
- hasReachedTarget: utils.approxEqual(offset, this.getScrollOffset()),
1178
- startedSmooth: behavior === "smooth"
1182
+ phase: initialScrollPhase(
1183
+ behavior,
1184
+ utils.approxEqual(offset, this.getScrollOffset())
1185
+ )
1179
1186
  };
1180
1187
  this._scrollToOffset(offset, { adjustments: void 0, behavior });
1181
1188
  this.scheduleScrollReconcile();
@@ -1321,7 +1328,11 @@ class Virtualizer {
1321
1328
  const STABLE_FRAMES = this.scrollState.index == null ? 1 : this.options.useAnimationFrameWithResizeObserver ? 3 : 2;
1322
1329
  const targetChanged = targetOffset !== this.scrollState.lastTargetOffset;
1323
1330
  if (!targetChanged && utils.approxEqual(targetOffset, this.getScrollOffset())) {
1324
- this.scrollState.hasReachedTarget = true;
1331
+ if (this.scrollState.phase === "smooth") {
1332
+ this.scrollState.phase = "smoothLanded";
1333
+ } else if (this.scrollState.phase === "smoothFinishing") {
1334
+ this.scrollState.phase = "instant";
1335
+ }
1325
1336
  this.scrollState.stableFrames++;
1326
1337
  if (this.scrollState.stableFrames >= STABLE_FRAMES) {
1327
1338
  if (this.getScrollOffset() !== targetOffset) {
@@ -1338,12 +1349,14 @@ class Virtualizer {
1338
1349
  if (targetChanged) {
1339
1350
  const viewport = this.getSize() || 600;
1340
1351
  const distance = Math.abs(targetOffset - this.getScrollOffset());
1341
- const keepSmooth = this.scrollState.behavior === "smooth" && distance > viewport;
1352
+ const keepSmooth = isSmoothScroll(this.scrollState) && distance > viewport;
1342
1353
  this.scrollState.lastTargetOffset = targetOffset;
1343
1354
  if (keepSmooth) {
1344
- this.scrollState.hasReachedTarget = false;
1345
- } else {
1346
- this.scrollState.behavior = "auto";
1355
+ this.scrollState.phase = "smooth";
1356
+ } else if (this.scrollState.phase === "smooth") {
1357
+ this.scrollState.phase = "smoothFinishing";
1358
+ } else if (this.scrollState.phase === "smoothLanded") {
1359
+ this.scrollState.phase = "instant";
1347
1360
  }
1348
1361
  this._scrollToOffset(targetOffset, {
1349
1362
  adjustments: void 0,