@tanstack/virtual-core 3.19.0 → 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();
@@ -467,10 +470,24 @@ class Virtualizer {
467
470
  }
468
471
  if (this._intendedScrollOffset !== null && Math.abs(offset - this._intendedScrollOffset) < 1.5) {
469
472
  offset = this._intendedScrollOffset;
470
- } else if (((_a2 = this.scrollState) == null ? void 0 : _a2.index) != null && this.scrollState.hasReachedTarget && !utils.approxEqual(offset, this.scrollState.lastTargetOffset) && !utils.approxEqual(offset, this.getScrollStateTarget(this.scrollState)) && (this._clampedAdjustment === null || Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5)) {
473
+ } else if (((_a2 = this.scrollState) == null ? void 0 : _a2.index) != null && // A non-smooth write lands at once, so any other scroll event is
474
+ // the user's, even before reconcile saw the target reached (the
475
+ // target may keep moving, or iOS ignored the write under a
476
+ // finger). A write the browser clamped because the sizer hasn't
477
+ // grown yet reads back at the max, which is also where the fresh
478
+ // target clamps, so the check below keeps it reconciling (#1294).
479
+ // Not when rows above the target shrink before the read-back and
480
+ // pull the target below the new max: the read-back then looks
481
+ // like the user and the scroll stops at the max. That needs the
482
+ // sizer re-rendered before the scroll event, which browsers have
483
+ // not been seen to do (pinned by a `.fails` test).
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)) {
471
487
  this.scrollState = null;
472
488
  }
473
489
  this._intendedScrollOffset = null;
490
+ if (this.scrollState) this.scrollState.writtenOffset = null;
474
491
  if (this._clampedAdjustment !== null && Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5) {
475
492
  this._clampedAdjustment = null;
476
493
  }
@@ -518,9 +535,15 @@ class Virtualizer {
518
535
  onTouchEnd,
519
536
  addEventListenerOptions
520
537
  );
538
+ scrollEl.addEventListener(
539
+ "touchcancel",
540
+ onTouchEnd,
541
+ addEventListenerOptions
542
+ );
521
543
  this.unsubs.push(() => {
522
544
  scrollEl.removeEventListener("touchstart", onTouchStart);
523
545
  scrollEl.removeEventListener("touchend", onTouchEnd);
546
+ scrollEl.removeEventListener("touchcancel", onTouchEnd);
524
547
  if (this._iosTouchEndTimerId !== null && this.targetWindow != null) {
525
548
  this.targetWindow.clearTimeout(this._iosTouchEndTimerId);
526
549
  this._iosTouchEndTimerId = null;
@@ -541,7 +564,7 @@ class Virtualizer {
541
564
  if (anchorDelta !== 0) {
542
565
  this._iosDeferredAdjustment += anchorDelta;
543
566
  }
544
- } else if (((_b = this.scrollState) == null ? void 0 : _b.behavior) === "smooth" && !utils.approxEqual(
567
+ } else if (isSmoothScroll(this.scrollState) && !utils.approxEqual(
545
568
  this.getScrollOffset() - anchorDelta,
546
569
  this.scrollState.lastTargetOffset
547
570
  )) ;
@@ -866,7 +889,7 @@ class Virtualizer {
866
889
  };
867
890
  this.shouldMeasureDuringScroll = (index) => {
868
891
  var _a;
869
- if (!this.scrollState || this.scrollState.behavior !== "smooth") {
892
+ if (!isSmoothScroll(this.scrollState)) {
870
893
  return true;
871
894
  }
872
895
  const scrollIndex = this.scrollState.index ?? ((_a = this.getVirtualItemForOffset(this.scrollState.lastTargetOffset)) == null ? void 0 : _a.index);
@@ -910,7 +933,7 @@ class Virtualizer {
910
933
  }
911
934
  };
912
935
  this.resizeItem = (index, size) => {
913
- var _a, _b, _c, _d, _e;
936
+ var _a, _b, _c, _d;
914
937
  if (!this.isIndexInRange(index)) return;
915
938
  if ((_a = this._resizeBatch) == null ? void 0 : _a.sync) this.getMeasurements();
916
939
  let cachedSize;
@@ -931,7 +954,7 @@ class Virtualizer {
931
954
  const itemSize = this.itemSizeCache.get(key) ?? cachedSize;
932
955
  const delta = size - itemSize;
933
956
  if (delta !== 0) {
934
- 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;
935
958
  const prevTotalSize = wasAtEnd ? this.getTotalSize() : 0;
936
959
  const scrollOffsetWithAdj = this.getScrollOffset() + this.scrollAdjustments;
937
960
  const isFirstMeasure = !this.itemSizeCache.has(key);
@@ -950,7 +973,7 @@ class Virtualizer {
950
973
  // scrolling up" cascade.
951
974
  itemStart + itemSize <= scrollOffsetWithAdj && this.scrollDirection !== "backward"
952
975
  );
953
- 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(
954
977
  // The callback expects a VirtualItem; build one lazily only
955
978
  // when the consumer actually supplied a custom predicate.
956
979
  this.measurementsCache[index] ?? {
@@ -969,17 +992,23 @@ class Virtualizer {
969
992
  }
970
993
  this.itemSizeCache.set(key, size);
971
994
  this.itemSizeCacheVersion++;
995
+ const awaitingCommandReadBack = ((_c = this.scrollState) == null ? void 0 : _c.writtenOffset) != null && !utils.approxEqual(
996
+ this.scrollState.writtenOffset,
997
+ this.getScrollOffset() + this.scrollAdjustments
998
+ );
972
999
  let adjustedSync = false;
973
- if (wasAtEnd) {
974
- adjustedSync = this.applyScrollAdjustment(
975
- this.getTotalSize() - prevTotalSize
976
- );
977
- } else if (shouldAdjustScroll) {
978
- adjustedSync = this.applyScrollAdjustment(delta);
1000
+ if (!awaitingCommandReadBack) {
1001
+ if (wasAtEnd) {
1002
+ adjustedSync = this.applyScrollAdjustment(
1003
+ this.getTotalSize() - prevTotalSize
1004
+ );
1005
+ } else if (shouldAdjustScroll) {
1006
+ adjustedSync = this.applyScrollAdjustment(delta);
1007
+ }
979
1008
  }
980
1009
  if (this._resizeBatch) {
981
1010
  this._resizeBatch.notify = true;
982
- (_e = this._resizeBatch).sync || (_e.sync = adjustedSync);
1011
+ (_d = this._resizeBatch).sync || (_d.sync = adjustedSync);
983
1012
  return;
984
1013
  }
985
1014
  this.notify(adjustedSync);
@@ -1087,11 +1116,10 @@ class Virtualizer {
1087
1116
  this.scrollState = {
1088
1117
  index: null,
1089
1118
  align,
1090
- behavior,
1091
1119
  startedAt: now,
1092
1120
  lastTargetOffset: offset,
1093
1121
  stableFrames: 0,
1094
- hasReachedTarget: false
1122
+ phase: initialScrollPhase(behavior, false)
1095
1123
  };
1096
1124
  this._scrollToOffset(offset, { adjustments: void 0, behavior });
1097
1125
  this.scheduleScrollReconcile();
@@ -1111,26 +1139,31 @@ class Virtualizer {
1111
1139
  this.scrollState = {
1112
1140
  index,
1113
1141
  align,
1114
- behavior,
1115
1142
  startedAt: now,
1116
1143
  lastTargetOffset: offset,
1117
1144
  stableFrames: 0,
1118
- hasReachedTarget: utils.approxEqual(offset, this.getScrollOffset())
1145
+ phase: initialScrollPhase(
1146
+ behavior,
1147
+ utils.approxEqual(offset, this.getScrollOffset())
1148
+ )
1119
1149
  };
1120
1150
  this._scrollToOffset(offset, { adjustments: void 0, behavior });
1121
1151
  this.scheduleScrollReconcile();
1122
1152
  };
1123
1153
  this.scrollBy = (delta, { behavior = "auto" } = {}) => {
1124
- const offset = this.getScrollOffset() + delta;
1154
+ var _a;
1155
+ const offset = this.getOffsetForAlignment(
1156
+ (((_a = this.scrollState) == null ? void 0 : _a.writtenOffset) ?? this.getScrollOffset()) + delta,
1157
+ "start"
1158
+ );
1125
1159
  const now = this.now();
1126
1160
  this.scrollState = {
1127
1161
  index: null,
1128
1162
  align: "start",
1129
- behavior,
1130
1163
  startedAt: now,
1131
1164
  lastTargetOffset: offset,
1132
1165
  stableFrames: 0,
1133
- hasReachedTarget: false
1166
+ phase: initialScrollPhase(behavior, false)
1134
1167
  };
1135
1168
  this._scrollToOffset(offset, { adjustments: void 0, behavior });
1136
1169
  this.scheduleScrollReconcile();
@@ -1142,12 +1175,14 @@ class Virtualizer {
1142
1175
  this.scrollState = {
1143
1176
  index: this.options.count - 1,
1144
1177
  align: "end",
1145
- behavior,
1146
1178
  toEnd: true,
1147
1179
  startedAt: this.now(),
1148
1180
  lastTargetOffset: offset,
1149
1181
  stableFrames: 0,
1150
- hasReachedTarget: utils.approxEqual(offset, this.getScrollOffset())
1182
+ phase: initialScrollPhase(
1183
+ behavior,
1184
+ utils.approxEqual(offset, this.getScrollOffset())
1185
+ )
1151
1186
  };
1152
1187
  this._scrollToOffset(offset, { adjustments: void 0, behavior });
1153
1188
  this.scheduleScrollReconcile();
@@ -1218,6 +1253,9 @@ class Virtualizer {
1218
1253
  behavior
1219
1254
  }) => {
1220
1255
  this._intendedScrollOffset = offset + (adjustments ?? 0);
1256
+ if (this.scrollState) {
1257
+ this.scrollState.writtenOffset = behavior === "smooth" ? null : this._intendedScrollOffset;
1258
+ }
1221
1259
  this.options.scrollToFn(offset, { behavior, adjustments }, this);
1222
1260
  };
1223
1261
  this.measure = () => {
@@ -1290,7 +1328,11 @@ class Virtualizer {
1290
1328
  const STABLE_FRAMES = this.scrollState.index == null ? 1 : this.options.useAnimationFrameWithResizeObserver ? 3 : 2;
1291
1329
  const targetChanged = targetOffset !== this.scrollState.lastTargetOffset;
1292
1330
  if (!targetChanged && utils.approxEqual(targetOffset, this.getScrollOffset())) {
1293
- 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
+ }
1294
1336
  this.scrollState.stableFrames++;
1295
1337
  if (this.scrollState.stableFrames >= STABLE_FRAMES) {
1296
1338
  if (this.getScrollOffset() !== targetOffset) {
@@ -1307,12 +1349,14 @@ class Virtualizer {
1307
1349
  if (targetChanged) {
1308
1350
  const viewport = this.getSize() || 600;
1309
1351
  const distance = Math.abs(targetOffset - this.getScrollOffset());
1310
- const keepSmooth = this.scrollState.behavior === "smooth" && distance > viewport;
1352
+ const keepSmooth = isSmoothScroll(this.scrollState) && distance > viewport;
1311
1353
  this.scrollState.lastTargetOffset = targetOffset;
1312
1354
  if (keepSmooth) {
1313
- this.scrollState.hasReachedTarget = false;
1314
- } else {
1315
- 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";
1316
1360
  }
1317
1361
  this._scrollToOffset(targetOffset, {
1318
1362
  adjustments: void 0,