@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/src/index.ts CHANGED
@@ -406,6 +406,18 @@ type ScrollState = {
406
406
  stableFrames: number
407
407
  // Keep arrival across measurement-driven retargets so external scrolls can cancel.
408
408
  hasReachedTarget: boolean
409
+ // The command asked for a smooth scroll. Unlike `behavior`, which reconcile
410
+ // switches to 'auto' for the final approach, this stays set: the smooth
411
+ // animation can still deliver scroll events after that switch.
412
+ startedSmooth: boolean
413
+
414
+ // Where our last non-smooth write for this command put the element, until
415
+ // its scroll event arrives. Until then `scrollOffset` still holds the old
416
+ // position, and the rows on screen are the ones rendered for it. A smooth
417
+ // write leaves it null: the element is somewhere between the two offsets.
418
+ // It goes with the scroll state, so a write the browser ignored (no read-back)
419
+ // does not outlive its command.
420
+ writtenOffset?: number | null
409
421
  }
410
422
 
411
423
  type PendingScrollAnchor = [
@@ -961,18 +973,33 @@ export class Virtualizer<
961
973
  offset = this._intendedScrollOffset
962
974
  } else if (
963
975
  this.scrollState?.index != null &&
964
- this.scrollState.hasReachedTarget &&
976
+ // A non-smooth write lands at once, so any other scroll event is
977
+ // the user's, even before reconcile saw the target reached (the
978
+ // target may keep moving, or iOS ignored the write under a
979
+ // finger). A write the browser clamped because the sizer hasn't
980
+ // grown yet reads back at the max, which is also where the fresh
981
+ // target clamps, so the check below keeps it reconciling (#1294).
982
+ // Not when rows above the target shrink before the read-back and
983
+ // pull the target below the new max: the read-back then looks
984
+ // like the user and the scroll stops at the max. That needs the
985
+ // sizer re-rendered before the scroll event, which browsers have
986
+ // not been seen to do (pinned by a `.fails` test).
987
+ // A smooth scroll's own animation events look like a user's, so
988
+ // it only yields once it has reached the target.
989
+ (this.scrollState.hasReachedTarget ||
990
+ !this.scrollState.startedSmooth) &&
965
991
  !approxEqual(offset, this.scrollState.lastTargetOffset) &&
966
992
  !approxEqual(offset, this.getScrollStateTarget(this.scrollState)) &&
967
993
  (this._clampedAdjustment === null ||
968
994
  Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5)
969
995
  ) {
970
- // The index scroll reached its target, but an external scroll moved
971
- // away while we waited for measurements. Our own measurement updates
972
- // (including clamped writes) still need to reconcile.
996
+ // An external scroll moved away from the index scroll's target.
997
+ // Our own measurement updates (including clamped writes) still
998
+ // need to reconcile.
973
999
  this.scrollState = null
974
1000
  }
975
1001
  this._intendedScrollOffset = null
1002
+ if (this.scrollState) this.scrollState.writtenOffset = null
976
1003
 
977
1004
  // A pending clamped compensation write (#1258) survives only its
978
1005
  // own read-back, which the browser reports at the scroll max we
@@ -1835,13 +1862,33 @@ export class Virtualizer<
1835
1862
  this.itemSizeCache.set(key, size)
1836
1863
  this.itemSizeCacheVersion++
1837
1864
 
1838
- let adjustedSync = false
1839
- if (wasAtEnd) {
1840
- adjustedSync = this.applyScrollAdjustment(
1841
- this.getTotalSize() - prevTotalSize,
1865
+ // A scroll command (or reconcile) wrote a new position that neither a
1866
+ // scroll event nor an eager update has reflected in `scrollOffset` yet.
1867
+ // The rows being measured were rendered for the old position, so
1868
+ // "above the viewport" means nothing against either offset. An index
1869
+ // or end target is retargeted from fresh measurements by
1870
+ // reconcileScroll next frame; an offset target (scrollToOffset,
1871
+ // scrollBy) lands where it was written and the delta is not applied,
1872
+ // since compensating against the stale offset would overwrite the
1873
+ // command. Compensation writes and the prepend anchor sync update
1874
+ // `scrollOffset` eagerly, and the iOS flush writes exactly
1875
+ // `scrollOffset + scrollAdjustments`, so none of them count.
1876
+ const awaitingCommandReadBack =
1877
+ this.scrollState?.writtenOffset != null &&
1878
+ !approxEqual(
1879
+ this.scrollState.writtenOffset,
1880
+ this.getScrollOffset() + this.scrollAdjustments,
1842
1881
  )
1843
- } else if (shouldAdjustScroll) {
1844
- adjustedSync = this.applyScrollAdjustment(delta)
1882
+
1883
+ let adjustedSync = false
1884
+ if (!awaitingCommandReadBack) {
1885
+ if (wasAtEnd) {
1886
+ adjustedSync = this.applyScrollAdjustment(
1887
+ this.getTotalSize() - prevTotalSize,
1888
+ )
1889
+ } else if (shouldAdjustScroll) {
1890
+ adjustedSync = this.applyScrollAdjustment(delta)
1891
+ }
1845
1892
  }
1846
1893
 
1847
1894
  // When we just moved `scrollTop` to compensate for an above-viewport
@@ -2034,6 +2081,7 @@ export class Virtualizer<
2034
2081
  lastTargetOffset: offset,
2035
2082
  stableFrames: 0,
2036
2083
  hasReachedTarget: false,
2084
+ startedSmooth: behavior === 'smooth',
2037
2085
  }
2038
2086
 
2039
2087
  this._scrollToOffset(offset, { adjustments: undefined, behavior })
@@ -2069,6 +2117,7 @@ export class Virtualizer<
2069
2117
  lastTargetOffset: offset,
2070
2118
  stableFrames: 0,
2071
2119
  hasReachedTarget: approxEqual(offset, this.getScrollOffset()),
2120
+ startedSmooth: behavior === 'smooth',
2072
2121
  }
2073
2122
 
2074
2123
  this._scrollToOffset(offset, { adjustments: undefined, behavior })
@@ -2080,7 +2129,13 @@ export class Virtualizer<
2080
2129
  delta: number,
2081
2130
  { behavior = 'auto' }: ScrollToOffsetOptions = {},
2082
2131
  ) => {
2083
- const offset = this.getScrollOffset() + delta
2132
+ // Build on a position we just wrote even before its scroll event arrives.
2133
+ // Clamp: a write past either edge that leaves the element where it was
2134
+ // fires no scroll event, so nothing would clear an unreachable offset.
2135
+ const offset = this.getOffsetForAlignment(
2136
+ (this.scrollState?.writtenOffset ?? this.getScrollOffset()) + delta,
2137
+ 'start',
2138
+ )
2084
2139
  const now = this.now()
2085
2140
 
2086
2141
  this.scrollState = {
@@ -2091,6 +2146,7 @@ export class Virtualizer<
2091
2146
  lastTargetOffset: offset,
2092
2147
  stableFrames: 0,
2093
2148
  hasReachedTarget: false,
2149
+ startedSmooth: behavior === 'smooth',
2094
2150
  }
2095
2151
 
2096
2152
  this._scrollToOffset(offset, { adjustments: undefined, behavior })
@@ -2115,6 +2171,7 @@ export class Virtualizer<
2115
2171
  lastTargetOffset: offset,
2116
2172
  stableFrames: 0,
2117
2173
  hasReachedTarget: approxEqual(offset, this.getScrollOffset()),
2174
+ startedSmooth: behavior === 'smooth',
2118
2175
  }
2119
2176
 
2120
2177
  this._scrollToOffset(offset, { adjustments: undefined, behavior })
@@ -2227,6 +2284,10 @@ export class Virtualizer<
2227
2284
  // Record the intended logical scroll target so the next scroll event
2228
2285
  // can reconcile against subpixel rounding by the browser.
2229
2286
  this._intendedScrollOffset = offset + (adjustments ?? 0)
2287
+ if (this.scrollState) {
2288
+ this.scrollState.writtenOffset =
2289
+ behavior === 'smooth' ? null : this._intendedScrollOffset
2290
+ }
2230
2291
  this.options.scrollToFn(offset, { behavior, adjustments }, this)
2231
2292
  }
2232
2293