@tanstack/virtual-core 3.19.2 → 3.19.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/virtual-core",
3
- "version": "3.19.2",
3
+ "version": "3.19.3",
4
4
  "description": "Headless UI for virtualizing scrollable elements in TS/JS + Frameworks",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
package/src/index.ts CHANGED
@@ -413,14 +413,6 @@ type ScrollState = {
413
413
  // settling
414
414
  stableFrames: number
415
415
  phase: ScrollPhase
416
-
417
- // Where our last non-smooth write for this command put the element, until
418
- // its scroll event arrives. Until then `scrollOffset` still holds the old
419
- // position, and the rows on screen are the ones rendered for it. A smooth
420
- // write leaves it null: the element is somewhere between the two offsets.
421
- // It goes with the scroll state, so a write the browser ignored (no read-back)
422
- // does not outlive its command.
423
- writtenOffset?: number | null
424
416
  }
425
417
 
426
418
  const initialScrollPhase = (
@@ -439,12 +431,14 @@ const isSmoothScroll = (state: ScrollState | null): state is ScrollState =>
439
431
  const isAnimating = (state: ScrollState) =>
440
432
  state.phase === 'smooth' || state.phase === 'smoothFinishing'
441
433
 
442
- type PendingScrollAnchor = [
443
- key: Key | null,
444
- offset: number,
445
- followOnAppend: ScrollBehavior | null,
446
- anchorDelta: number,
447
- ]
434
+ // What `setOptions` decided about the scroll position for `_willUpdate` to
435
+ // carry out once the consumer has rendered: keep the reading anchor in place
436
+ // after a prepend (`scrollOffset` already moved by `delta`, the element has
437
+ // not), or follow an append to the end. A prepend that qualifies for both
438
+ // only follows.
439
+ type PendingScrollAnchor =
440
+ | { kind: 'anchor'; delta: number }
441
+ | { kind: 'follow'; behavior: ScrollBehavior }
448
442
 
449
443
  function isAppendWithTrim(
450
444
  prevCount: number,
@@ -505,30 +499,49 @@ export class Virtualizer<
505
499
  scrollRect: Rect | null = null
506
500
  scrollOffset: number | null = null
507
501
  scrollDirection: ScrollDirection | null = null
508
- scrollAdjustments = 0
502
+ // Size compensation written to the element but not yet reflected in
503
+ // `scrollOffset`: the element is expected at `scrollOffset +
504
+ // scrollAdjustments` until the next scroll event reports it.
505
+ get scrollAdjustments() {
506
+ return this.pendingWrite?.adjustments ?? 0
507
+ }
509
508
  // Sum of size-change deltas above-viewport that were skipped during
510
509
  // iOS momentum scroll (writing scrollTop mid-momentum cancels it).
511
510
  // Flushed in a single scrollTo when iOS is fully settled.
512
511
  private _iosDeferredAdjustment = 0
513
- // Touch state. iOS WebKit cancels momentum when scrollTop is written, so
514
- // we defer adjustments not only during `isScrolling` but also through the
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).
519
- private _iosTouching = false
520
- private _iosJustTouchEnded = false
521
- private _iosTouchEndTimerId: number | null = null
522
- // Subpixel reconciliation. Safari (and Chrome/Firefox under certain DPRs)
523
- // round scrollTop/scrollLeft writes to integer pixels. If we wrote 12345.5
524
- // but the browser reports back 12346, the next reconcileScroll sees a
525
- // "target changed" and re-fires scrollTo — a feedback loop that the
526
- // approxEqual(<1.01) tolerance otherwise absorbs as a workaround.
527
- // By remembering the intended value of our most-recent self-driven
528
- // scrollTo, we can match the browser's rounded read back to the intended
529
- // value when the diff is < 1.5 px, distinguishing it from a real user
530
- // scroll. The +0.5 over Math.abs lets us also absorb the +1 / -1 cases.
531
- private _intendedScrollOffset: number | null = null
512
+ // The touch gesture in progress. iOS WebKit cancels momentum when
513
+ // scrollTop is written, so adjustments are deferred not only during
514
+ // `isScrolling` but through the whole gesture: `down` from touchstart to
515
+ // touchend/touchcancel (active drag), then `tail` for a short grace window
516
+ // after the finger lifts (early momentum — iOS fires touch events only
517
+ // once at the start of momentum, so the tail is a timer rather than
518
+ // another event). Null between gestures, and after touchend on non-iOS,
519
+ // which arms no tail. The tail record owns its timer: `_endIosTouch` is
520
+ // the only way to drop either, so a cleared record never leaves a timer
521
+ // behind, and a live timer always has its record.
522
+ private _iosTouch:
523
+ | { phase: 'down' }
524
+ | { phase: 'tail'; timerId: number }
525
+ | null = null
526
+ // Our last write to the element, from `_scrollToOffset` until the next
527
+ // scroll event reports it (the only place it is cleared). Until then
528
+ // `scrollOffset` may still hold the old position.
529
+ //
530
+ // - `target`: where the write puts the element (offset + adjustments).
531
+ // Safari (and Chrome/Firefox under certain DPRs) round writes to whole
532
+ // pixels: wrote 12345.5, read back 12346. A read-back within 1.5px of
533
+ // `target` is taken as our own and adopts the exact value, so reconcile
534
+ // does not see a moved target and write again; a user scroll has moved
535
+ // further by the time it reports.
536
+ // - `smooth`: the write animates, so until it lands the element is
537
+ // somewhere between the old offset and `target`.
538
+ // - `adjustments`: see `scrollAdjustments`. Writes that pass none keep the
539
+ // previous value.
540
+ private pendingWrite: {
541
+ target: number
542
+ smooth: boolean
543
+ adjustments: number
544
+ } | null = null
532
545
  // A compensation write from `applyScrollAdjustment` whose target exceeded
533
546
  // the element's scroll max at the moment of the write. The browser clamps
534
547
  // such a write because the consumer's sizer has not grown yet: an
@@ -759,8 +772,7 @@ export class Virtualizer<
759
772
  // the virtualizer would render the wrong items for one frame (the
760
773
  // estimate-based positions are stale) and then correct in the next
761
774
  // frame, producing a visible "jump" on prepend with dynamic sizes.
762
- let anchorResolved = false
763
- let anchorDelta = 0
775
+ let anchorDelta: number | null = null
764
776
  if (anchor && this.scrollOffset !== null) {
765
777
  const [anchorKey, anchorOffset] = anchor
766
778
  const newMeasurements = this.getMeasurements()
@@ -781,19 +793,15 @@ export class Virtualizer<
781
793
  if (!followOnAppend && newOffset !== this.scrollOffset) {
782
794
  anchorDelta = newOffset - this.scrollOffset
783
795
  this.scrollOffset = newOffset
784
- anchorResolved = true
785
796
  }
786
797
  }
787
798
  }
788
799
  }
789
800
 
790
- if (anchorResolved || followOnAppend) {
791
- this.pendingScrollAnchor = [
792
- anchorResolved ? anchor![0] : null,
793
- anchorResolved ? anchor![1] : 0,
794
- followOnAppend,
795
- anchorDelta,
796
- ]
801
+ if (followOnAppend) {
802
+ this.pendingScrollAnchor = { kind: 'follow', behavior: followOnAppend }
803
+ } else if (anchorDelta !== null) {
804
+ this.pendingScrollAnchor = { kind: 'anchor', delta: anchorDelta }
797
805
  }
798
806
  }
799
807
 
@@ -815,10 +823,7 @@ export class Virtualizer<
815
823
  console.info('correction', delta)
816
824
  }
817
825
 
818
- if (
819
- isIOSWebKit() &&
820
- (this.isScrolling || this._iosTouching || this._iosJustTouchEnded)
821
- ) {
826
+ if (isIOSWebKit() && this._iosInMotion()) {
822
827
  // An index scroll retargets from fresh measurements in reconcileScroll;
823
828
  // replaying this delta after it lands would count it twice.
824
829
  if (this.scrollState?.index == null) {
@@ -839,7 +844,7 @@ export class Virtualizer<
839
844
  ? { target, maxAtWrite }
840
845
  : null
841
846
  this._scrollToOffset(this.getScrollOffset(), {
842
- adjustments: (this.scrollAdjustments += delta),
847
+ adjustments: this.scrollAdjustments + delta,
843
848
  behavior,
844
849
  })
845
850
  // Eagerly carry the intended target in `scrollOffset` so callers that
@@ -860,7 +865,7 @@ export class Virtualizer<
860
865
  // Upper-bound overflow stays untouched — it is transiently
861
866
  // legitimate mid-prepend while the consumer's sizer catches up.
862
867
  if (this.scrollOffset < 0) this.scrollOffset = 0
863
- this.scrollAdjustments = 0
868
+ if (this.pendingWrite) this.pendingWrite.adjustments = 0
864
869
  }
865
870
  return true
866
871
  }
@@ -912,14 +917,14 @@ export class Virtualizer<
912
917
  // capture), so the new element never reports it. Carrying the state
913
918
  // over would replay a stale deferred delta on the new element's first
914
919
  // flush, and a cleanup that lands mid-touch or inside the post-touchend
915
- // grace window would strand _iosTouching / _iosJustTouchEnded as true
916
- // (the listener unsub clears the grace timer, and with it the only
917
- // pending reset of the flag), deferring every adjustment on the new
918
- // element until its next touch cycle.
920
+ // grace window would strand `_iosTouch` (the listeners that end it were
921
+ // just removed), deferring every adjustment on the new element until
922
+ // its next touch cycle.
919
923
  this._iosDeferredAdjustment = 0
920
- this._iosTouching = false
921
- this._iosJustTouchEnded = false
924
+ this._endIosTouch()
922
925
  this._clampedAdjustment = null
926
+ // A write to the old element will never be read back.
927
+ this.pendingWrite = null
923
928
  this.scrollElement = null
924
929
  this.targetWindow = null
925
930
  }
@@ -969,28 +974,21 @@ export class Virtualizer<
969
974
  // no-op re-emit that Safari/Firefox fire after a re-render's layout
970
975
  // (Chrome doesn't). Treating it as scrolling re-arms `isScrolling`,
971
976
  // which forces a render that triggers another such event: an
972
- // infinite re-render loop. Ignore it. (Self-writes are handled by
973
- // the `_intendedScrollOffset` reconciliation just below.)
977
+ // infinite re-render loop. Ignore it. (Self-writes are matched to
978
+ // `pendingWrite` just below.)
974
979
  if (
975
980
  isScrolling &&
976
- this._intendedScrollOffset === null &&
981
+ this.pendingWrite === null &&
977
982
  offset === this.scrollOffset
978
983
  ) {
979
984
  return
980
985
  }
981
986
 
982
- // If this scroll event looks like the browser's read-back of a
983
- // value we just wrote, prefer our intended (sub-pixel-accurate)
984
- // value over the browser's rounded one. The 1.5 px tolerance is
985
- // tight enough to avoid mistaking a real user scroll for a
986
- // self-write — by the time the user has moved 1.5 px, the
987
- // intended value will already have been consumed by a prior
988
- // scroll event and cleared.
989
- if (
990
- this._intendedScrollOffset !== null &&
991
- Math.abs(offset - this._intendedScrollOffset) < 1.5
992
- ) {
993
- offset = this._intendedScrollOffset
987
+ // The read-back of our own write: adopt the exact target over the
988
+ // browser's rounded value (see `pendingWrite`).
989
+ const pending = this.pendingWrite
990
+ if (pending !== null && Math.abs(offset - pending.target) < 1.5) {
991
+ offset = pending.target
994
992
  } else if (
995
993
  this.scrollState?.index != null &&
996
994
  // A non-smooth write lands at once, so any other scroll event is
@@ -1017,8 +1015,7 @@ export class Virtualizer<
1017
1015
  // need to reconcile.
1018
1016
  this.scrollState = null
1019
1017
  }
1020
- this._intendedScrollOffset = null
1021
- if (this.scrollState) this.scrollState.writtenOffset = null
1018
+ this.pendingWrite = null
1022
1019
 
1023
1020
  // A pending clamped compensation write (#1258) survives only its
1024
1021
  // own read-back, which the browser reports at the scroll max we
@@ -1031,7 +1028,6 @@ export class Virtualizer<
1031
1028
  this._clampedAdjustment = null
1032
1029
  }
1033
1030
 
1034
- this.scrollAdjustments = 0
1035
1031
  // If the offset hasn't moved, this is the echo of our own
1036
1032
  // adjustment write — `applyScrollAdjustment` already folded it
1037
1033
  // into `scrollOffset`. There's no direction to infer, so leave
@@ -1066,28 +1062,27 @@ export class Virtualizer<
1066
1062
  if ('addEventListener' in this.scrollElement) {
1067
1063
  const scrollEl = this.scrollElement as unknown as EventTarget
1068
1064
  const onTouchStart = () => {
1069
- this._iosTouching = true
1070
- this._iosJustTouchEnded = false
1071
- if (this._iosTouchEndTimerId !== null && this.targetWindow != null) {
1072
- this.targetWindow.clearTimeout(this._iosTouchEndTimerId)
1073
- this._iosTouchEndTimerId = null
1074
- }
1065
+ // A finger back down inside the grace window cancels its tail.
1066
+ this._endIosTouch()
1067
+ this._iosTouch = { phase: 'down' }
1075
1068
  }
1076
1069
  const onTouchEnd = () => {
1077
- this._iosTouching = false
1070
+ this._endIosTouch()
1078
1071
  if (!isIOSWebKit() || this.targetWindow == null) {
1079
- // Non-iOS: nothing more to track. Just clear the touching flag.
1072
+ // Non-iOS: nothing more to track.
1080
1073
  return
1081
1074
  }
1082
- this._iosJustTouchEnded = true
1083
1075
  // After ~150 ms with no scroll/touch events, momentum is done.
1084
- this._iosTouchEndTimerId = this.targetWindow.setTimeout(() => {
1085
- this._iosJustTouchEnded = false
1086
- this._iosTouchEndTimerId = null
1087
- // After the grace window, attempt to flush. The scroll event
1088
- // for momentum decay may have already fired before our timer.
1089
- this._flushIosDeferredIfReady()
1090
- }, 150)
1076
+ const tail = {
1077
+ phase: 'tail' as const,
1078
+ timerId: this.targetWindow.setTimeout(() => {
1079
+ if (this._iosTouch === tail) this._iosTouch = null
1080
+ // After the grace window, attempt to flush. The scroll event
1081
+ // for momentum decay may have already fired before our timer.
1082
+ this._flushIosDeferredIfReady()
1083
+ }, 150),
1084
+ }
1085
+ this._iosTouch = tail
1091
1086
  }
1092
1087
  scrollEl.addEventListener(
1093
1088
  'touchstart',
@@ -1110,10 +1105,7 @@ export class Virtualizer<
1110
1105
  scrollEl.removeEventListener('touchstart', onTouchStart)
1111
1106
  scrollEl.removeEventListener('touchend', onTouchEnd)
1112
1107
  scrollEl.removeEventListener('touchcancel', onTouchEnd)
1113
- if (this._iosTouchEndTimerId !== null && this.targetWindow != null) {
1114
- this.targetWindow.clearTimeout(this._iosTouchEndTimerId)
1115
- this._iosTouchEndTimerId = null
1116
- }
1108
+ this._endIosTouch()
1117
1109
  })
1118
1110
  }
1119
1111
 
@@ -1127,29 +1119,21 @@ export class Virtualizer<
1127
1119
  this.pendingScrollAnchor = null
1128
1120
 
1129
1121
  if (anchor && this.scrollElement && this.options.enabled) {
1130
- const [key, _offset, followOnAppend, anchorDelta] = anchor
1131
-
1132
- if (key !== null && !followOnAppend) {
1122
+ if (anchor.kind === 'anchor') {
1133
1123
  // scrollOffset was eagerly adjusted in setOptions so the
1134
1124
  // virtualizer already computed the correct range during render.
1135
1125
  // Now sync the browser's actual scroll position to match.
1136
- // Skip when followOnAppend is set — scrollToEnd will handle it.
1137
1126
  //
1138
1127
  // On iOS WebKit, writing scrollTop during touch/momentum cancels
1139
1128
  // the in-flight scroll. Defer the DOM sync the same way
1140
1129
  // applyScrollAdjustment does — accumulate the delta and let
1141
1130
  // _flushIosDeferredIfReady handle it once the scroll settles.
1142
- if (
1143
- isIOSWebKit() &&
1144
- (this.isScrolling || this._iosTouching || this._iosJustTouchEnded)
1145
- ) {
1146
- if (anchorDelta !== 0) {
1147
- this._iosDeferredAdjustment += anchorDelta
1148
- }
1131
+ if (isIOSWebKit() && this._iosInMotion()) {
1132
+ this._iosDeferredAdjustment += anchor.delta
1149
1133
  } else if (
1150
1134
  isSmoothScroll(this.scrollState) &&
1151
1135
  !approxEqual(
1152
- this.getScrollOffset() - anchorDelta,
1136
+ this.getScrollOffset() - anchor.delta,
1153
1137
  this.scrollState.lastTargetOffset,
1154
1138
  )
1155
1139
  ) {
@@ -1167,10 +1151,8 @@ export class Virtualizer<
1167
1151
  behavior: undefined,
1168
1152
  })
1169
1153
  }
1170
- }
1171
-
1172
- if (followOnAppend) {
1173
- this.scrollToEnd({ behavior: followOnAppend })
1154
+ } else {
1155
+ this.scrollToEnd({ behavior: anchor.behavior })
1174
1156
  }
1175
1157
  }
1176
1158
 
@@ -1208,15 +1190,25 @@ export class Virtualizer<
1208
1190
  }
1209
1191
  }
1210
1192
 
1193
+ // The element is moving or about to: a scroll in progress, or a touch
1194
+ // gesture that may still turn into one (see `_iosTouch`). On iOS a write
1195
+ // now would cancel the momentum, so adjustments wait for the flush below.
1196
+ private _iosInMotion = () => this.isScrolling || this._iosTouch !== null
1197
+
1198
+ private _endIosTouch = () => {
1199
+ if (this._iosTouch?.phase === 'tail' && this.targetWindow != null) {
1200
+ this.targetWindow.clearTimeout(this._iosTouch.timerId)
1201
+ }
1202
+ this._iosTouch = null
1203
+ }
1204
+
1211
1205
  // Apply any accumulated iOS-deferred scroll adjustment, but only when we're
1212
1206
  // truly settled — not actively scrolling, not under an active touch, and
1213
1207
  // past the post-touchend grace window. Called from the scroll callback
1214
1208
  // and the touchend grace-timer.
1215
1209
  private _flushIosDeferredIfReady = () => {
1216
1210
  if (this._iosDeferredAdjustment === 0) return
1217
- if (this.isScrolling) return
1218
- if (this._iosTouching) return
1219
- if (this._iosJustTouchEnded) return
1211
+ if (this._iosInMotion()) return
1220
1212
  // Phase 2b: Safari elastic-overscroll (rubber-band) lets scrollTop go
1221
1213
  // negative or beyond scrollHeight - clientHeight. Writing scrollTop
1222
1214
  // while in that zone snaps the page back to the clamped value at the
@@ -1239,7 +1231,7 @@ export class Virtualizer<
1239
1231
  // landing between now and the resulting scroll event computes from the
1240
1232
  // post-flush offset rather than the stale one.
1241
1233
  this._scrollToOffset(cur, {
1242
- adjustments: (this.scrollAdjustments += delta),
1234
+ adjustments: this.scrollAdjustments + delta,
1243
1235
  behavior: undefined,
1244
1236
  })
1245
1237
  }
@@ -1911,10 +1903,11 @@ export class Virtualizer<
1911
1903
  // command. Compensation writes and the prepend anchor sync update
1912
1904
  // `scrollOffset` eagerly, and the iOS flush writes exactly
1913
1905
  // `scrollOffset + scrollAdjustments`, so none of them count.
1906
+ const writtenOffset = this.getCommandWrittenOffset()
1914
1907
  const awaitingCommandReadBack =
1915
- this.scrollState?.writtenOffset != null &&
1908
+ writtenOffset !== null &&
1916
1909
  !approxEqual(
1917
- this.scrollState.writtenOffset,
1910
+ writtenOffset,
1918
1911
  this.getScrollOffset() + this.scrollAdjustments,
1919
1912
  )
1920
1913
 
@@ -2100,29 +2093,12 @@ export class Virtualizer<
2100
2093
  toOffset: number,
2101
2094
  { align = 'start', behavior = 'auto' }: ScrollToOffsetOptions = {},
2102
2095
  ) => {
2103
- // An absolute scroll command derives its target from current
2104
- // measurements, so any iOS-deferred compensation still pending is stale by
2105
- // definition — the command already accounts for the measurements the delta
2106
- // was compensating for. Drop it so _flushIosDeferredIfReady doesn't replay
2107
- // it onto the just-established position (relative commands like scrollBy
2108
- // intentionally keep the deferral, since they build on the current offset).
2109
- this._iosDeferredAdjustment = 0
2110
-
2111
- const offset = this.getOffsetForAlignment(toOffset, align)
2112
-
2113
- const now = this.now()
2114
- this.scrollState = {
2115
- index: null,
2116
- align,
2117
- startedAt: now,
2118
- lastTargetOffset: offset,
2119
- stableFrames: 0,
2120
- phase: initialScrollPhase(behavior, false),
2121
- }
2122
-
2123
- this._scrollToOffset(offset, { adjustments: undefined, behavior })
2124
-
2125
- this.scheduleScrollReconcile()
2096
+ this._startScroll(
2097
+ { index: null, align },
2098
+ this.getOffsetForAlignment(toOffset, align),
2099
+ behavior,
2100
+ 'absolute',
2101
+ )
2126
2102
  }
2127
2103
 
2128
2104
  scrollToIndex = (
@@ -2132,34 +2108,19 @@ export class Virtualizer<
2132
2108
  behavior = 'auto',
2133
2109
  }: ScrollToIndexOptions = {},
2134
2110
  ) => {
2135
- // See scrollToOffset: an absolute target invalidates any pending
2136
- // iOS-deferred compensation.
2137
- this._iosDeferredAdjustment = 0
2138
-
2139
2111
  index = Math.max(0, Math.min(index, this.options.count - 1))
2140
2112
 
2141
2113
  const offsetInfo = this.getOffsetForIndex(index, initialAlign)
2142
2114
  if (!offsetInfo) {
2115
+ // No target (an empty list), so nothing is written; still drop the
2116
+ // deferral like every absolute command, or it replays once the list
2117
+ // refills.
2118
+ this._iosDeferredAdjustment = 0
2143
2119
  return
2144
2120
  }
2145
2121
  const [offset, align] = offsetInfo
2146
2122
 
2147
- const now = this.now()
2148
- this.scrollState = {
2149
- index,
2150
- align,
2151
- startedAt: now,
2152
- lastTargetOffset: offset,
2153
- stableFrames: 0,
2154
- phase: initialScrollPhase(
2155
- behavior,
2156
- approxEqual(offset, this.getScrollOffset()),
2157
- ),
2158
- }
2159
-
2160
- this._scrollToOffset(offset, { adjustments: undefined, behavior })
2161
-
2162
- this.scheduleScrollReconcile()
2123
+ this._startScroll({ index, align }, offset, behavior, 'absolute')
2163
2124
  }
2164
2125
 
2165
2126
  scrollBy = (
@@ -2170,49 +2131,28 @@ export class Virtualizer<
2170
2131
  // Clamp: a write past either edge that leaves the element where it was
2171
2132
  // fires no scroll event, so nothing would clear an unreachable offset.
2172
2133
  const offset = this.getOffsetForAlignment(
2173
- (this.scrollState?.writtenOffset ?? this.getScrollOffset()) + delta,
2134
+ (this.getCommandWrittenOffset() ?? this.getScrollOffset()) + delta,
2174
2135
  'start',
2175
2136
  )
2176
- const now = this.now()
2177
-
2178
- this.scrollState = {
2179
- index: null,
2180
- align: 'start',
2181
- startedAt: now,
2182
- lastTargetOffset: offset,
2183
- stableFrames: 0,
2184
- phase: initialScrollPhase(behavior, false),
2185
- }
2186
2137
 
2187
- this._scrollToOffset(offset, { adjustments: undefined, behavior })
2188
-
2189
- this.scheduleScrollReconcile()
2138
+ this._startScroll(
2139
+ { index: null, align: 'start' },
2140
+ offset,
2141
+ behavior,
2142
+ 'relative',
2143
+ )
2190
2144
  }
2191
2145
 
2192
2146
  scrollToEnd = ({ behavior = 'auto' }: ScrollToEndOptions = {}) => {
2193
2147
  if (this.options.count > 0) {
2194
2148
  // Target the browser's max scroll rather than the last item's end-align
2195
2149
  // offset, so paddingEnd is included and isAtEnd() holds afterwards.
2196
- this._iosDeferredAdjustment = 0
2197
-
2198
- const offset = Math.max(this.getMaxScrollOffset(), 0)
2199
-
2200
- this.scrollState = {
2201
- index: this.options.count - 1,
2202
- align: 'end',
2203
- toEnd: true,
2204
- startedAt: this.now(),
2205
- lastTargetOffset: offset,
2206
- stableFrames: 0,
2207
- phase: initialScrollPhase(
2208
- behavior,
2209
- approxEqual(offset, this.getScrollOffset()),
2210
- ),
2211
- }
2212
-
2213
- this._scrollToOffset(offset, { adjustments: undefined, behavior })
2214
-
2215
- this.scheduleScrollReconcile()
2150
+ this._startScroll(
2151
+ { index: this.options.count - 1, align: 'end', toEnd: true },
2152
+ Math.max(this.getMaxScrollOffset(), 0),
2153
+ behavior,
2154
+ 'absolute',
2155
+ )
2216
2156
  return
2217
2157
  }
2218
2158
 
@@ -2221,6 +2161,42 @@ export class Virtualizer<
2221
2161
  })
2222
2162
  }
2223
2163
 
2164
+ // Every scroll command starts here: it becomes the in-flight command,
2165
+ // writes its first target and leaves reconcile to retarget and retire it.
2166
+ //
2167
+ // An absolute command derives its target from current measurements, so
2168
+ // any iOS-deferred compensation still pending is stale by definition —
2169
+ // the command already accounts for the measurements the delta was
2170
+ // compensating for. Drop it so _flushIosDeferredIfReady doesn't replay it
2171
+ // onto the just-established position. A relative command (scrollBy)
2172
+ // builds on the current offset, so it keeps the deferral.
2173
+ //
2174
+ // An index or end target that is already at its offset starts a smooth
2175
+ // scroll as landed; an offset target never does, so its first scroll
2176
+ // event is read as the user's (see the scroll handler in `_willUpdate`).
2177
+ private _startScroll(
2178
+ target: Pick<ScrollState, 'index' | 'align' | 'toEnd'>,
2179
+ offset: number,
2180
+ behavior: ScrollBehavior,
2181
+ kind: 'absolute' | 'relative',
2182
+ ) {
2183
+ if (kind === 'absolute') {
2184
+ this._iosDeferredAdjustment = 0
2185
+ }
2186
+ this.scrollState = {
2187
+ ...target,
2188
+ startedAt: this.now(),
2189
+ lastTargetOffset: offset,
2190
+ stableFrames: 0,
2191
+ phase: initialScrollPhase(
2192
+ behavior,
2193
+ target.index != null && approxEqual(offset, this.getScrollOffset()),
2194
+ ),
2195
+ }
2196
+ this._scrollToOffset(offset, { adjustments: undefined, behavior })
2197
+ this.scheduleScrollReconcile()
2198
+ }
2199
+
2224
2200
  // Drops the target of an in-flight scrollToIndex / scrollToOffset /
2225
2201
  // scrollBy / scrollToEnd so a user gesture can take over the viewport.
2226
2202
  // The scroll position is left untouched: writing it would stop iOS
@@ -2317,16 +2293,26 @@ export class Virtualizer<
2317
2293
  behavior: ScrollBehavior | undefined
2318
2294
  },
2319
2295
  ) => {
2320
- // Record the intended logical scroll target so the next scroll event
2321
- // can reconcile against subpixel rounding by the browser.
2322
- this._intendedScrollOffset = offset + (adjustments ?? 0)
2323
- if (this.scrollState) {
2324
- this.scrollState.writtenOffset =
2325
- behavior === 'smooth' ? null : this._intendedScrollOffset
2296
+ this.pendingWrite = {
2297
+ target: offset + (adjustments ?? 0),
2298
+ smooth: behavior === 'smooth',
2299
+ adjustments: adjustments ?? this.scrollAdjustments,
2326
2300
  }
2327
2301
  this.options.scrollToFn(offset, { behavior, adjustments }, this)
2328
2302
  }
2329
2303
 
2304
+ // Where the in-flight command's last instant write put the element, until
2305
+ // its scroll event arrives; the rows on screen are still the ones rendered
2306
+ // for the old `scrollOffset`. Null after a smooth write (the element is
2307
+ // somewhere on the way) and without a command, so a write the browser
2308
+ // ignored (no read-back) does not outlive its command.
2309
+ private getCommandWrittenOffset = () =>
2310
+ this.scrollState !== null &&
2311
+ this.pendingWrite !== null &&
2312
+ !this.pendingWrite.smooth
2313
+ ? this.pendingWrite.target
2314
+ : null
2315
+
2330
2316
  measure = () => {
2331
2317
  // Reset pendingMin so the next getMeasurements rebuilds from index 0.
2332
2318
  // Without this, a prior resizeItem() that left pendingMin > 0 would