@tanstack/virtual-core 3.19.0 → 3.19.1

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
@@ -465,10 +465,24 @@ class Virtualizer {
465
465
  }
466
466
  if (this._intendedScrollOffset !== null && Math.abs(offset - this._intendedScrollOffset) < 1.5) {
467
467
  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)) {
468
+ } else if (((_a2 = this.scrollState) == null ? void 0 : _a2.index) != null && // A non-smooth write lands at once, so any other scroll event is
469
+ // the user's, even before reconcile saw the target reached (the
470
+ // target may keep moving, or iOS ignored the write under a
471
+ // finger). A write the browser clamped because the sizer hasn't
472
+ // grown yet reads back at the max, which is also where the fresh
473
+ // target clamps, so the check below keeps it reconciling (#1294).
474
+ // Not when rows above the target shrink before the read-back and
475
+ // pull the target below the new max: the read-back then looks
476
+ // like the user and the scroll stops at the max. That needs the
477
+ // sizer re-rendered before the scroll event, which browsers have
478
+ // 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)) {
469
482
  this.scrollState = null;
470
483
  }
471
484
  this._intendedScrollOffset = null;
485
+ if (this.scrollState) this.scrollState.writtenOffset = null;
472
486
  if (this._clampedAdjustment !== null && Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5) {
473
487
  this._clampedAdjustment = null;
474
488
  }
@@ -908,7 +922,7 @@ class Virtualizer {
908
922
  }
909
923
  };
910
924
  this.resizeItem = (index, size) => {
911
- var _a, _b, _c, _d, _e;
925
+ var _a, _b, _c, _d, _e, _f;
912
926
  if (!this.isIndexInRange(index)) return;
913
927
  if ((_a = this._resizeBatch) == null ? void 0 : _a.sync) this.getMeasurements();
914
928
  let cachedSize;
@@ -967,17 +981,23 @@ class Virtualizer {
967
981
  }
968
982
  this.itemSizeCache.set(key, size);
969
983
  this.itemSizeCacheVersion++;
984
+ const awaitingCommandReadBack = ((_e = this.scrollState) == null ? void 0 : _e.writtenOffset) != null && !approxEqual(
985
+ this.scrollState.writtenOffset,
986
+ this.getScrollOffset() + this.scrollAdjustments
987
+ );
970
988
  let adjustedSync = false;
971
- if (wasAtEnd) {
972
- adjustedSync = this.applyScrollAdjustment(
973
- this.getTotalSize() - prevTotalSize
974
- );
975
- } else if (shouldAdjustScroll) {
976
- adjustedSync = this.applyScrollAdjustment(delta);
989
+ if (!awaitingCommandReadBack) {
990
+ if (wasAtEnd) {
991
+ adjustedSync = this.applyScrollAdjustment(
992
+ this.getTotalSize() - prevTotalSize
993
+ );
994
+ } else if (shouldAdjustScroll) {
995
+ adjustedSync = this.applyScrollAdjustment(delta);
996
+ }
977
997
  }
978
998
  if (this._resizeBatch) {
979
999
  this._resizeBatch.notify = true;
980
- (_e = this._resizeBatch).sync || (_e.sync = adjustedSync);
1000
+ (_f = this._resizeBatch).sync || (_f.sync = adjustedSync);
981
1001
  return;
982
1002
  }
983
1003
  this.notify(adjustedSync);
@@ -1089,7 +1109,8 @@ class Virtualizer {
1089
1109
  startedAt: now,
1090
1110
  lastTargetOffset: offset,
1091
1111
  stableFrames: 0,
1092
- hasReachedTarget: false
1112
+ hasReachedTarget: false,
1113
+ startedSmooth: behavior === "smooth"
1093
1114
  };
1094
1115
  this._scrollToOffset(offset, { adjustments: void 0, behavior });
1095
1116
  this.scheduleScrollReconcile();
@@ -1113,13 +1134,18 @@ class Virtualizer {
1113
1134
  startedAt: now,
1114
1135
  lastTargetOffset: offset,
1115
1136
  stableFrames: 0,
1116
- hasReachedTarget: approxEqual(offset, this.getScrollOffset())
1137
+ hasReachedTarget: approxEqual(offset, this.getScrollOffset()),
1138
+ startedSmooth: behavior === "smooth"
1117
1139
  };
1118
1140
  this._scrollToOffset(offset, { adjustments: void 0, behavior });
1119
1141
  this.scheduleScrollReconcile();
1120
1142
  };
1121
1143
  this.scrollBy = (delta, { behavior = "auto" } = {}) => {
1122
- const offset = this.getScrollOffset() + delta;
1144
+ var _a;
1145
+ const offset = this.getOffsetForAlignment(
1146
+ (((_a = this.scrollState) == null ? void 0 : _a.writtenOffset) ?? this.getScrollOffset()) + delta,
1147
+ "start"
1148
+ );
1123
1149
  const now = this.now();
1124
1150
  this.scrollState = {
1125
1151
  index: null,
@@ -1128,7 +1154,8 @@ class Virtualizer {
1128
1154
  startedAt: now,
1129
1155
  lastTargetOffset: offset,
1130
1156
  stableFrames: 0,
1131
- hasReachedTarget: false
1157
+ hasReachedTarget: false,
1158
+ startedSmooth: behavior === "smooth"
1132
1159
  };
1133
1160
  this._scrollToOffset(offset, { adjustments: void 0, behavior });
1134
1161
  this.scheduleScrollReconcile();
@@ -1145,7 +1172,8 @@ class Virtualizer {
1145
1172
  startedAt: this.now(),
1146
1173
  lastTargetOffset: offset,
1147
1174
  stableFrames: 0,
1148
- hasReachedTarget: approxEqual(offset, this.getScrollOffset())
1175
+ hasReachedTarget: approxEqual(offset, this.getScrollOffset()),
1176
+ startedSmooth: behavior === "smooth"
1149
1177
  };
1150
1178
  this._scrollToOffset(offset, { adjustments: void 0, behavior });
1151
1179
  this.scheduleScrollReconcile();
@@ -1216,6 +1244,9 @@ class Virtualizer {
1216
1244
  behavior
1217
1245
  }) => {
1218
1246
  this._intendedScrollOffset = offset + (adjustments ?? 0);
1247
+ if (this.scrollState) {
1248
+ this.scrollState.writtenOffset = behavior === "smooth" ? null : this._intendedScrollOffset;
1249
+ }
1219
1250
  this.options.scrollToFn(offset, { behavior, adjustments }, this);
1220
1251
  };
1221
1252
  this.measure = () => {