@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/src/index.ts CHANGED
@@ -388,11 +388,19 @@ export interface VirtualizerOptions<
388
388
  useCachedMeasurements?: boolean
389
389
  }
390
390
 
391
+ // Where an in-flight scroll command is in its lifecycle, which decides how
392
+ // its scroll events and measurements are read:
393
+ // smooth a smooth animation is travelling to the target
394
+ // smoothLanded a smooth scroll is at its target, waiting to retire
395
+ // smoothFinishing a smooth scroll switched to instant writes for the
396
+ // final approach; its animation can still report positions
397
+ // instant instant writes
398
+ type ScrollPhase = 'smooth' | 'smoothLanded' | 'smoothFinishing' | 'instant'
399
+
391
400
  type ScrollState = {
392
401
  // what we want
393
402
  index: number | null
394
403
  align: ScrollAlignment
395
- behavior: ScrollBehavior
396
404
  // scrollToEnd: track the DOM max scroll offset instead of the item's offset
397
405
  toEnd?: boolean
398
406
 
@@ -404,12 +412,7 @@ type ScrollState = {
404
412
 
405
413
  // settling
406
414
  stableFrames: number
407
- // Keep arrival across measurement-driven retargets so external scrolls can cancel.
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
415
+ phase: ScrollPhase
413
416
 
414
417
  // Where our last non-smooth write for this command put the element, until
415
418
  // its scroll event arrives. Until then `scrollOffset` still holds the old
@@ -420,6 +423,22 @@ type ScrollState = {
420
423
  writtenOffset?: number | null
421
424
  }
422
425
 
426
+ const initialScrollPhase = (
427
+ behavior: ScrollBehavior,
428
+ atTarget: boolean,
429
+ ): ScrollPhase =>
430
+ behavior === 'smooth' ? (atTarget ? 'smoothLanded' : 'smooth') : 'instant'
431
+
432
+ // A smooth scroll owns the position: size compensation, the prepend anchor
433
+ // sync and far measurements hold off until it switches to instant writes.
434
+ const isSmoothScroll = (state: ScrollState | null): state is ScrollState =>
435
+ state?.phase === 'smooth' || state?.phase === 'smoothLanded'
436
+
437
+ // The smooth animation may still be reporting positions, so a scroll event
438
+ // that isn't the read-back of our own write may not be the user.
439
+ const isAnimating = (state: ScrollState) =>
440
+ state.phase === 'smooth' || state.phase === 'smoothFinishing'
441
+
423
442
  type PendingScrollAnchor = [
424
443
  key: Key | null,
425
444
  offset: number,
@@ -493,9 +512,10 @@ export class Virtualizer<
493
512
  private _iosDeferredAdjustment = 0
494
513
  // Touch state. iOS WebKit cancels momentum when scrollTop is written, so
495
514
  // we defer adjustments not only during `isScrolling` but also through the
496
- // touchstart→touchend window (active drag) and a short tail after
497
- // touchend (early-momentum window — iOS only fires touch events once at
498
- // the start of momentum, so we use a timer rather than another event).
515
+ // touchstart→touchend/touchcancel window (active drag) and a short tail
516
+ // after the touch ends (early-momentum window — iOS only fires touch
517
+ // events once at the start of momentum, so we use a timer rather than
518
+ // another event).
499
519
  private _iosTouching = false
500
520
  private _iosJustTouchEnded = false
501
521
  private _iosTouchEndTimerId: number | null = null
@@ -984,10 +1004,9 @@ export class Virtualizer<
984
1004
  // like the user and the scroll stops at the max. That needs the
985
1005
  // sizer re-rendered before the scroll event, which browsers have
986
1006
  // 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) &&
1007
+ // While a smooth scroll's animation may still report, its events
1008
+ // look like a user's, so it yields only once it has landed.
1009
+ !isAnimating(this.scrollState) &&
991
1010
  !approxEqual(offset, this.scrollState.lastTargetOffset) &&
992
1011
  !approxEqual(offset, this.getScrollStateTarget(this.scrollState)) &&
993
1012
  (this._clampedAdjustment === null ||
@@ -1080,9 +1099,17 @@ export class Virtualizer<
1080
1099
  onTouchEnd,
1081
1100
  addEventListenerOptions,
1082
1101
  )
1102
+ // Same handler as touchend: iOS can end a touch with touchcancel
1103
+ // (system gesture, interrupted scroll) and never fire touchend.
1104
+ scrollEl.addEventListener(
1105
+ 'touchcancel',
1106
+ onTouchEnd,
1107
+ addEventListenerOptions,
1108
+ )
1083
1109
  this.unsubs.push(() => {
1084
1110
  scrollEl.removeEventListener('touchstart', onTouchStart)
1085
1111
  scrollEl.removeEventListener('touchend', onTouchEnd)
1112
+ scrollEl.removeEventListener('touchcancel', onTouchEnd)
1086
1113
  if (this._iosTouchEndTimerId !== null && this.targetWindow != null) {
1087
1114
  this.targetWindow.clearTimeout(this._iosTouchEndTimerId)
1088
1115
  this._iosTouchEndTimerId = null
@@ -1120,7 +1147,7 @@ export class Virtualizer<
1120
1147
  this._iosDeferredAdjustment += anchorDelta
1121
1148
  }
1122
1149
  } else if (
1123
- this.scrollState?.behavior === 'smooth' &&
1150
+ isSmoothScroll(this.scrollState) &&
1124
1151
  !approxEqual(
1125
1152
  this.getScrollOffset() - anchorDelta,
1126
1153
  this.scrollState.lastTargetOffset,
@@ -1270,7 +1297,13 @@ export class Virtualizer<
1270
1297
  const targetChanged = targetOffset !== this.scrollState.lastTargetOffset
1271
1298
 
1272
1299
  if (!targetChanged && approxEqual(targetOffset, this.getScrollOffset())) {
1273
- this.scrollState.hasReachedTarget = true
1300
+ // At the target: a travelling smooth scroll has landed, and a
1301
+ // finishing one is now an instant scroll.
1302
+ if (this.scrollState.phase === 'smooth') {
1303
+ this.scrollState.phase = 'smoothLanded'
1304
+ } else if (this.scrollState.phase === 'smoothFinishing') {
1305
+ this.scrollState.phase = 'instant'
1306
+ }
1274
1307
  this.scrollState.stableFrames++
1275
1308
  if (this.scrollState.stableFrames >= STABLE_FRAMES) {
1276
1309
  // Final-pass exact landing. The reconcile-stable check uses a 1.01px
@@ -1302,16 +1335,21 @@ export class Virtualizer<
1302
1335
  const viewport = this.getSize() || 600
1303
1336
  const distance = Math.abs(targetOffset - this.getScrollOffset())
1304
1337
  const keepSmooth =
1305
- this.scrollState.behavior === 'smooth' && distance > viewport
1338
+ isSmoothScroll(this.scrollState) && distance > viewport
1306
1339
 
1307
1340
  this.scrollState.lastTargetOffset = targetOffset
1308
1341
  if (keepSmooth) {
1309
1342
  // A smooth retarget travels again, so its intermediate scroll
1310
1343
  // events must not read as an external scroll away from the target.
1311
- this.scrollState.hasReachedTarget = false
1312
- } else {
1313
- this.scrollState.behavior = 'auto'
1344
+ this.scrollState.phase = 'smooth'
1345
+ } else if (this.scrollState.phase === 'smooth') {
1346
+ // The final approach is instant, but the animation can still report.
1347
+ this.scrollState.phase = 'smoothFinishing'
1348
+ } else if (this.scrollState.phase === 'smoothLanded') {
1349
+ this.scrollState.phase = 'instant'
1314
1350
  }
1351
+ // 'smoothFinishing' stays as it is (its animation may still report)
1352
+ // until reconcile sees it at the target; 'instant' stays instant.
1315
1353
 
1316
1354
  this._scrollToOffset(targetOffset, {
1317
1355
  adjustments: undefined,
@@ -1713,7 +1751,7 @@ export class Virtualizer<
1713
1751
  */
1714
1752
  private shouldMeasureDuringScroll = (index: number): boolean => {
1715
1753
  // No scroll state or not smooth scroll - always allow measurements
1716
- if (!this.scrollState || this.scrollState.behavior !== 'smooth') {
1754
+ if (!isSmoothScroll(this.scrollState)) {
1717
1755
  return true
1718
1756
  }
1719
1757
 
@@ -1815,7 +1853,7 @@ export class Virtualizer<
1815
1853
  if (delta !== 0) {
1816
1854
  const wasAtEnd =
1817
1855
  this.options.anchorTo === 'end' &&
1818
- this.scrollState?.behavior !== 'smooth' &&
1856
+ !isSmoothScroll(this.scrollState) &&
1819
1857
  this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold
1820
1858
  const prevTotalSize = wasAtEnd ? this.getTotalSize() : 0
1821
1859
  // Default anchoring predicate (used unless the consumer supplies a
@@ -1838,7 +1876,7 @@ export class Virtualizer<
1838
1876
  itemStart + itemSize <= scrollOffsetWithAdj &&
1839
1877
  this.scrollDirection !== 'backward'
1840
1878
  const shouldAdjustScroll =
1841
- this.scrollState?.behavior !== 'smooth' &&
1879
+ !isSmoothScroll(this.scrollState) &&
1842
1880
  (this.shouldAdjustScrollPositionOnItemSizeChange !== undefined
1843
1881
  ? this.shouldAdjustScrollPositionOnItemSizeChange(
1844
1882
  // The callback expects a VirtualItem; build one lazily only
@@ -2076,12 +2114,10 @@ export class Virtualizer<
2076
2114
  this.scrollState = {
2077
2115
  index: null,
2078
2116
  align,
2079
- behavior,
2080
2117
  startedAt: now,
2081
2118
  lastTargetOffset: offset,
2082
2119
  stableFrames: 0,
2083
- hasReachedTarget: false,
2084
- startedSmooth: behavior === 'smooth',
2120
+ phase: initialScrollPhase(behavior, false),
2085
2121
  }
2086
2122
 
2087
2123
  this._scrollToOffset(offset, { adjustments: undefined, behavior })
@@ -2112,12 +2148,13 @@ export class Virtualizer<
2112
2148
  this.scrollState = {
2113
2149
  index,
2114
2150
  align,
2115
- behavior,
2116
2151
  startedAt: now,
2117
2152
  lastTargetOffset: offset,
2118
2153
  stableFrames: 0,
2119
- hasReachedTarget: approxEqual(offset, this.getScrollOffset()),
2120
- startedSmooth: behavior === 'smooth',
2154
+ phase: initialScrollPhase(
2155
+ behavior,
2156
+ approxEqual(offset, this.getScrollOffset()),
2157
+ ),
2121
2158
  }
2122
2159
 
2123
2160
  this._scrollToOffset(offset, { adjustments: undefined, behavior })
@@ -2141,12 +2178,10 @@ export class Virtualizer<
2141
2178
  this.scrollState = {
2142
2179
  index: null,
2143
2180
  align: 'start',
2144
- behavior,
2145
2181
  startedAt: now,
2146
2182
  lastTargetOffset: offset,
2147
2183
  stableFrames: 0,
2148
- hasReachedTarget: false,
2149
- startedSmooth: behavior === 'smooth',
2184
+ phase: initialScrollPhase(behavior, false),
2150
2185
  }
2151
2186
 
2152
2187
  this._scrollToOffset(offset, { adjustments: undefined, behavior })
@@ -2165,13 +2200,14 @@ export class Virtualizer<
2165
2200
  this.scrollState = {
2166
2201
  index: this.options.count - 1,
2167
2202
  align: 'end',
2168
- behavior,
2169
2203
  toEnd: true,
2170
2204
  startedAt: this.now(),
2171
2205
  lastTargetOffset: offset,
2172
2206
  stableFrames: 0,
2173
- hasReachedTarget: approxEqual(offset, this.getScrollOffset()),
2174
- startedSmooth: behavior === 'smooth',
2207
+ phase: initialScrollPhase(
2208
+ behavior,
2209
+ approxEqual(offset, this.getScrollOffset()),
2210
+ ),
2175
2211
  }
2176
2212
 
2177
2213
  this._scrollToOffset(offset, { adjustments: undefined, behavior })