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