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