@tanstack/virtual-core 3.19.1 → 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.
@@ -113,12 +113,10 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
113
113
  scrollRect: Rect | null;
114
114
  scrollOffset: number | null;
115
115
  scrollDirection: ScrollDirection | null;
116
- scrollAdjustments: number;
116
+ get scrollAdjustments(): number;
117
117
  private _iosDeferredAdjustment;
118
- private _iosTouching;
119
- private _iosJustTouchEnded;
120
- private _iosTouchEndTimerId;
121
- private _intendedScrollOffset;
118
+ private _iosTouch;
119
+ private pendingWrite;
122
120
  private _clampedAdjustment;
123
121
  shouldAdjustScrollPositionOnItemSizeChange: undefined | ((item: VirtualItem, delta: number, instance: Virtualizer<TScrollElement, TItemElement>) => boolean);
124
122
  elementsCache: Map<Key, TItemElement>;
@@ -138,6 +136,8 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
138
136
  _didMount: () => () => void;
139
137
  _willUpdate: () => void;
140
138
  private _retryClampedAdjustment;
139
+ private _iosInMotion;
140
+ private _endIosTouch;
141
141
  private _flushIosDeferredIfReady;
142
142
  private rafId;
143
143
  private scheduleScrollReconcile;
@@ -183,6 +183,7 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
183
183
  scrollToIndex: (index: number, { align: initialAlign, behavior, }?: ScrollToIndexOptions) => void;
184
184
  scrollBy: (delta: number, { behavior }?: ScrollToOffsetOptions) => void;
185
185
  scrollToEnd: ({ behavior }?: ScrollToEndOptions) => void;
186
+ private _startScroll;
186
187
  cancelScroll: () => void;
187
188
  getTotalSize: () => number;
188
189
  /**
@@ -197,5 +198,6 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
197
198
  */
198
199
  takeSnapshot: () => Array<VirtualItem>;
199
200
  private _scrollToOffset;
201
+ private getCommandWrittenOffset;
200
202
  measure: () => void;
201
203
  }
@@ -113,12 +113,10 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
113
113
  scrollRect: Rect | null;
114
114
  scrollOffset: number | null;
115
115
  scrollDirection: ScrollDirection | null;
116
- scrollAdjustments: number;
116
+ get scrollAdjustments(): number;
117
117
  private _iosDeferredAdjustment;
118
- private _iosTouching;
119
- private _iosJustTouchEnded;
120
- private _iosTouchEndTimerId;
121
- private _intendedScrollOffset;
118
+ private _iosTouch;
119
+ private pendingWrite;
122
120
  private _clampedAdjustment;
123
121
  shouldAdjustScrollPositionOnItemSizeChange: undefined | ((item: VirtualItem, delta: number, instance: Virtualizer<TScrollElement, TItemElement>) => boolean);
124
122
  elementsCache: Map<Key, TItemElement>;
@@ -138,6 +136,8 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
138
136
  _didMount: () => () => void;
139
137
  _willUpdate: () => void;
140
138
  private _retryClampedAdjustment;
139
+ private _iosInMotion;
140
+ private _endIosTouch;
141
141
  private _flushIosDeferredIfReady;
142
142
  private rafId;
143
143
  private scheduleScrollReconcile;
@@ -183,6 +183,7 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
183
183
  scrollToIndex: (index: number, { align: initialAlign, behavior, }?: ScrollToIndexOptions) => void;
184
184
  scrollBy: (delta: number, { behavior }?: ScrollToOffsetOptions) => void;
185
185
  scrollToEnd: ({ behavior }?: ScrollToEndOptions) => void;
186
+ private _startScroll;
186
187
  cancelScroll: () => void;
187
188
  getTotalSize: () => number;
188
189
  /**
@@ -197,5 +198,6 @@ export declare class Virtualizer<TScrollElement extends Element | Window, TItemE
197
198
  */
198
199
  takeSnapshot: () => Array<VirtualItem>;
199
200
  private _scrollToOffset;
201
+ private getCommandWrittenOffset;
200
202
  measure: () => void;
201
203
  }
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);
@@ -204,12 +207,9 @@ class Virtualizer {
204
207
  this.scrollRect = null;
205
208
  this.scrollOffset = null;
206
209
  this.scrollDirection = null;
207
- this.scrollAdjustments = 0;
208
210
  this._iosDeferredAdjustment = 0;
209
- this._iosTouching = false;
210
- this._iosJustTouchEnded = false;
211
- this._iosTouchEndTimerId = null;
212
- this._intendedScrollOffset = null;
211
+ this._iosTouch = null;
212
+ this.pendingWrite = null;
213
213
  this._clampedAdjustment = null;
214
214
  this.elementsCache = /* @__PURE__ */ new Map();
215
215
  this._resizeBatch = null;
@@ -353,8 +353,7 @@ class Virtualizer {
353
353
  this.pendingMin = 0;
354
354
  this.itemSizeCacheVersion++;
355
355
  }
356
- let anchorResolved = false;
357
- let anchorDelta = 0;
356
+ let anchorDelta = null;
358
357
  if (anchor && this.scrollOffset !== null) {
359
358
  const [anchorKey, anchorOffset] = anchor;
360
359
  const newMeasurements = this.getMeasurements();
@@ -370,18 +369,14 @@ class Virtualizer {
370
369
  if (!followOnAppend && newOffset !== this.scrollOffset) {
371
370
  anchorDelta = newOffset - this.scrollOffset;
372
371
  this.scrollOffset = newOffset;
373
- anchorResolved = true;
374
372
  }
375
373
  }
376
374
  }
377
375
  }
378
- if (anchorResolved || followOnAppend) {
379
- this.pendingScrollAnchor = [
380
- anchorResolved ? anchor[0] : null,
381
- anchorResolved ? anchor[1] : 0,
382
- followOnAppend,
383
- anchorDelta
384
- ];
376
+ if (followOnAppend) {
377
+ this.pendingScrollAnchor = { kind: "follow", behavior: followOnAppend };
378
+ } else if (anchorDelta !== null) {
379
+ this.pendingScrollAnchor = { kind: "anchor", delta: anchorDelta };
385
380
  }
386
381
  };
387
382
  this.notify = (sync) => {
@@ -422,9 +417,9 @@ class Virtualizer {
422
417
  this.isScrolling = false;
423
418
  this.scrollDirection = null;
424
419
  this._iosDeferredAdjustment = 0;
425
- this._iosTouching = false;
426
- this._iosJustTouchEnded = false;
420
+ this._endIosTouch();
427
421
  this._clampedAdjustment = null;
422
+ this.pendingWrite = null;
428
423
  this.scrollElement = null;
429
424
  this.targetWindow = null;
430
425
  };
@@ -434,7 +429,7 @@ class Virtualizer {
434
429
  };
435
430
  };
436
431
  this._willUpdate = () => {
437
- var _a, _b;
432
+ var _a;
438
433
  const scrollElement = this.options.enabled ? this.options.getScrollElement() : null;
439
434
  if (this.scrollElement !== scrollElement) {
440
435
  this.cleanup();
@@ -460,11 +455,12 @@ class Virtualizer {
460
455
  this.unsubs.push(
461
456
  this.options.observeElementOffset(this, (offset, isScrolling) => {
462
457
  var _a2;
463
- if (isScrolling && this._intendedScrollOffset === null && offset === this.scrollOffset) {
458
+ if (isScrolling && this.pendingWrite === null && offset === this.scrollOffset) {
464
459
  return;
465
460
  }
466
- if (this._intendedScrollOffset !== null && Math.abs(offset - this._intendedScrollOffset) < 1.5) {
467
- offset = this._intendedScrollOffset;
461
+ const pending = this.pendingWrite;
462
+ if (pending !== null && Math.abs(offset - pending.target) < 1.5) {
463
+ offset = pending.target;
468
464
  } 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
465
  // the user's, even before reconcile saw the target reached (the
470
466
  // target may keep moving, or iOS ignored the write under a
@@ -476,17 +472,15 @@ class Virtualizer {
476
472
  // like the user and the scroll stops at the max. That needs the
477
473
  // sizer re-rendered before the scroll event, which browsers have
478
474
  // 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)) {
475
+ // While a smooth scroll's animation may still report, its events
476
+ // look like a user's, so it yields only once it has landed.
477
+ !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)) {
482
478
  this.scrollState = null;
483
479
  }
484
- this._intendedScrollOffset = null;
485
- if (this.scrollState) this.scrollState.writtenOffset = null;
480
+ this.pendingWrite = null;
486
481
  if (this._clampedAdjustment !== null && Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5) {
487
482
  this._clampedAdjustment = null;
488
483
  }
489
- this.scrollAdjustments = 0;
490
484
  const prevOffset = this.getScrollOffset();
491
485
  this.scrollDirection = isScrolling ? prevOffset === offset ? this.scrollDirection : prevOffset < offset ? "forward" : "backward" : null;
492
486
  this.scrollOffset = offset;
@@ -501,24 +495,22 @@ class Virtualizer {
501
495
  if ("addEventListener" in this.scrollElement) {
502
496
  const scrollEl = this.scrollElement;
503
497
  const onTouchStart = () => {
504
- this._iosTouching = true;
505
- this._iosJustTouchEnded = false;
506
- if (this._iosTouchEndTimerId !== null && this.targetWindow != null) {
507
- this.targetWindow.clearTimeout(this._iosTouchEndTimerId);
508
- this._iosTouchEndTimerId = null;
509
- }
498
+ this._endIosTouch();
499
+ this._iosTouch = { phase: "down" };
510
500
  };
511
501
  const onTouchEnd = () => {
512
- this._iosTouching = false;
502
+ this._endIosTouch();
513
503
  if (!isIOSWebKit() || this.targetWindow == null) {
514
504
  return;
515
505
  }
516
- this._iosJustTouchEnded = true;
517
- this._iosTouchEndTimerId = this.targetWindow.setTimeout(() => {
518
- this._iosJustTouchEnded = false;
519
- this._iosTouchEndTimerId = null;
520
- this._flushIosDeferredIfReady();
521
- }, 150);
506
+ const tail = {
507
+ phase: "tail",
508
+ timerId: this.targetWindow.setTimeout(() => {
509
+ if (this._iosTouch === tail) this._iosTouch = null;
510
+ this._flushIosDeferredIfReady();
511
+ }, 150)
512
+ };
513
+ this._iosTouch = tail;
522
514
  };
523
515
  scrollEl.addEventListener(
524
516
  "touchstart",
@@ -530,13 +522,16 @@ class Virtualizer {
530
522
  onTouchEnd,
531
523
  addEventListenerOptions
532
524
  );
525
+ scrollEl.addEventListener(
526
+ "touchcancel",
527
+ onTouchEnd,
528
+ addEventListenerOptions
529
+ );
533
530
  this.unsubs.push(() => {
534
531
  scrollEl.removeEventListener("touchstart", onTouchStart);
535
532
  scrollEl.removeEventListener("touchend", onTouchEnd);
536
- if (this._iosTouchEndTimerId !== null && this.targetWindow != null) {
537
- this.targetWindow.clearTimeout(this._iosTouchEndTimerId);
538
- this._iosTouchEndTimerId = null;
539
- }
533
+ scrollEl.removeEventListener("touchcancel", onTouchEnd);
534
+ this._endIosTouch();
540
535
  });
541
536
  }
542
537
  this._scrollToOffset(this.getScrollOffset(), {
@@ -547,14 +542,11 @@ class Virtualizer {
547
542
  const anchor = this.pendingScrollAnchor;
548
543
  this.pendingScrollAnchor = null;
549
544
  if (anchor && this.scrollElement && this.options.enabled) {
550
- const [key, _offset, followOnAppend, anchorDelta] = anchor;
551
- if (key !== null && !followOnAppend) {
552
- if (isIOSWebKit() && (this.isScrolling || this._iosTouching || this._iosJustTouchEnded)) {
553
- if (anchorDelta !== 0) {
554
- this._iosDeferredAdjustment += anchorDelta;
555
- }
556
- } else if (((_b = this.scrollState) == null ? void 0 : _b.behavior) === "smooth" && !approxEqual(
557
- this.getScrollOffset() - anchorDelta,
545
+ if (anchor.kind === "anchor") {
546
+ if (isIOSWebKit() && this._iosInMotion()) {
547
+ this._iosDeferredAdjustment += anchor.delta;
548
+ } else if (isSmoothScroll(this.scrollState) && !approxEqual(
549
+ this.getScrollOffset() - anchor.delta,
558
550
  this.scrollState.lastTargetOffset
559
551
  )) ;
560
552
  else {
@@ -563,9 +555,8 @@ class Virtualizer {
563
555
  behavior: void 0
564
556
  });
565
557
  }
566
- }
567
- if (followOnAppend) {
568
- this.scrollToEnd({ behavior: followOnAppend });
558
+ } else {
559
+ this.scrollToEnd({ behavior: anchor.behavior });
569
560
  }
570
561
  }
571
562
  this._retryClampedAdjustment();
@@ -584,11 +575,17 @@ class Virtualizer {
584
575
  });
585
576
  }
586
577
  };
578
+ this._iosInMotion = () => this.isScrolling || this._iosTouch !== null;
579
+ this._endIosTouch = () => {
580
+ var _a;
581
+ if (((_a = this._iosTouch) == null ? void 0 : _a.phase) === "tail" && this.targetWindow != null) {
582
+ this.targetWindow.clearTimeout(this._iosTouch.timerId);
583
+ }
584
+ this._iosTouch = null;
585
+ };
587
586
  this._flushIosDeferredIfReady = () => {
588
587
  if (this._iosDeferredAdjustment === 0) return;
589
- if (this.isScrolling) return;
590
- if (this._iosTouching) return;
591
- if (this._iosJustTouchEnded) return;
588
+ if (this._iosInMotion()) return;
592
589
  const cur = this.getScrollOffset();
593
590
  const max = this.getMaxScrollOffset();
594
591
  if (cur < 0 || cur > max) return;
@@ -599,7 +596,7 @@ class Virtualizer {
599
596
  const delta = this._iosDeferredAdjustment;
600
597
  this._iosDeferredAdjustment = 0;
601
598
  this._scrollToOffset(cur, {
602
- adjustments: this.scrollAdjustments += delta,
599
+ adjustments: this.scrollAdjustments + delta,
603
600
  behavior: void 0
604
601
  });
605
602
  };
@@ -878,7 +875,7 @@ class Virtualizer {
878
875
  };
879
876
  this.shouldMeasureDuringScroll = (index) => {
880
877
  var _a;
881
- if (!this.scrollState || this.scrollState.behavior !== "smooth") {
878
+ if (!isSmoothScroll(this.scrollState)) {
882
879
  return true;
883
880
  }
884
881
  const scrollIndex = this.scrollState.index ?? ((_a = this.getVirtualItemForOffset(this.scrollState.lastTargetOffset)) == null ? void 0 : _a.index);
@@ -922,7 +919,7 @@ class Virtualizer {
922
919
  }
923
920
  };
924
921
  this.resizeItem = (index, size) => {
925
- var _a, _b, _c, _d, _e, _f;
922
+ var _a, _b, _c;
926
923
  if (!this.isIndexInRange(index)) return;
927
924
  if ((_a = this._resizeBatch) == null ? void 0 : _a.sync) this.getMeasurements();
928
925
  let cachedSize;
@@ -943,7 +940,7 @@ class Virtualizer {
943
940
  const itemSize = this.itemSizeCache.get(key) ?? cachedSize;
944
941
  const delta = size - itemSize;
945
942
  if (delta !== 0) {
946
- const wasAtEnd = this.options.anchorTo === "end" && ((_c = this.scrollState) == null ? void 0 : _c.behavior) !== "smooth" && this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold;
943
+ const wasAtEnd = this.options.anchorTo === "end" && !isSmoothScroll(this.scrollState) && this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold;
947
944
  const prevTotalSize = wasAtEnd ? this.getTotalSize() : 0;
948
945
  const scrollOffsetWithAdj = this.getScrollOffset() + this.scrollAdjustments;
949
946
  const isFirstMeasure = !this.itemSizeCache.has(key);
@@ -962,7 +959,7 @@ class Virtualizer {
962
959
  // scrolling up" cascade.
963
960
  itemStart + itemSize <= scrollOffsetWithAdj && this.scrollDirection !== "backward"
964
961
  );
965
- const shouldAdjustScroll = ((_d = this.scrollState) == null ? void 0 : _d.behavior) !== "smooth" && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
962
+ const shouldAdjustScroll = !isSmoothScroll(this.scrollState) && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
966
963
  // The callback expects a VirtualItem; build one lazily only
967
964
  // when the consumer actually supplied a custom predicate.
968
965
  this.measurementsCache[index] ?? {
@@ -981,8 +978,9 @@ class Virtualizer {
981
978
  }
982
979
  this.itemSizeCache.set(key, size);
983
980
  this.itemSizeCacheVersion++;
984
- const awaitingCommandReadBack = ((_e = this.scrollState) == null ? void 0 : _e.writtenOffset) != null && !approxEqual(
985
- this.scrollState.writtenOffset,
981
+ const writtenOffset = this.getCommandWrittenOffset();
982
+ const awaitingCommandReadBack = writtenOffset !== null && !approxEqual(
983
+ writtenOffset,
986
984
  this.getScrollOffset() + this.scrollAdjustments
987
985
  );
988
986
  let adjustedSync = false;
@@ -997,7 +995,7 @@ class Virtualizer {
997
995
  }
998
996
  if (this._resizeBatch) {
999
997
  this._resizeBatch.notify = true;
1000
- (_f = this._resizeBatch).sync || (_f.sync = adjustedSync);
998
+ (_c = this._resizeBatch).sync || (_c.sync = adjustedSync);
1001
999
  return;
1002
1000
  }
1003
1001
  this.notify(adjustedSync);
@@ -1099,84 +1097,46 @@ class Virtualizer {
1099
1097
  ];
1100
1098
  };
1101
1099
  this.scrollToOffset = (toOffset, { align = "start", behavior = "auto" } = {}) => {
1102
- this._iosDeferredAdjustment = 0;
1103
- const offset = this.getOffsetForAlignment(toOffset, align);
1104
- const now = this.now();
1105
- this.scrollState = {
1106
- index: null,
1107
- align,
1100
+ this._startScroll(
1101
+ { index: null, align },
1102
+ this.getOffsetForAlignment(toOffset, align),
1108
1103
  behavior,
1109
- startedAt: now,
1110
- lastTargetOffset: offset,
1111
- stableFrames: 0,
1112
- hasReachedTarget: false,
1113
- startedSmooth: behavior === "smooth"
1114
- };
1115
- this._scrollToOffset(offset, { adjustments: void 0, behavior });
1116
- this.scheduleScrollReconcile();
1104
+ "absolute"
1105
+ );
1117
1106
  };
1118
1107
  this.scrollToIndex = (index, {
1119
1108
  align: initialAlign = "auto",
1120
1109
  behavior = "auto"
1121
1110
  } = {}) => {
1122
- this._iosDeferredAdjustment = 0;
1123
1111
  index = Math.max(0, Math.min(index, this.options.count - 1));
1124
1112
  const offsetInfo = this.getOffsetForIndex(index, initialAlign);
1125
1113
  if (!offsetInfo) {
1114
+ this._iosDeferredAdjustment = 0;
1126
1115
  return;
1127
1116
  }
1128
1117
  const [offset, align] = offsetInfo;
1129
- const now = this.now();
1130
- this.scrollState = {
1131
- index,
1132
- align,
1133
- behavior,
1134
- startedAt: now,
1135
- lastTargetOffset: offset,
1136
- stableFrames: 0,
1137
- hasReachedTarget: approxEqual(offset, this.getScrollOffset()),
1138
- startedSmooth: behavior === "smooth"
1139
- };
1140
- this._scrollToOffset(offset, { adjustments: void 0, behavior });
1141
- this.scheduleScrollReconcile();
1118
+ this._startScroll({ index, align }, offset, behavior, "absolute");
1142
1119
  };
1143
1120
  this.scrollBy = (delta, { behavior = "auto" } = {}) => {
1144
- var _a;
1145
1121
  const offset = this.getOffsetForAlignment(
1146
- (((_a = this.scrollState) == null ? void 0 : _a.writtenOffset) ?? this.getScrollOffset()) + delta,
1122
+ (this.getCommandWrittenOffset() ?? this.getScrollOffset()) + delta,
1147
1123
  "start"
1148
1124
  );
1149
- const now = this.now();
1150
- this.scrollState = {
1151
- index: null,
1152
- align: "start",
1125
+ this._startScroll(
1126
+ { index: null, align: "start" },
1127
+ offset,
1153
1128
  behavior,
1154
- startedAt: now,
1155
- lastTargetOffset: offset,
1156
- stableFrames: 0,
1157
- hasReachedTarget: false,
1158
- startedSmooth: behavior === "smooth"
1159
- };
1160
- this._scrollToOffset(offset, { adjustments: void 0, behavior });
1161
- this.scheduleScrollReconcile();
1129
+ "relative"
1130
+ );
1162
1131
  };
1163
1132
  this.scrollToEnd = ({ behavior = "auto" } = {}) => {
1164
1133
  if (this.options.count > 0) {
1165
- this._iosDeferredAdjustment = 0;
1166
- const offset = Math.max(this.getMaxScrollOffset(), 0);
1167
- this.scrollState = {
1168
- index: this.options.count - 1,
1169
- align: "end",
1134
+ this._startScroll(
1135
+ { index: this.options.count - 1, align: "end", toEnd: true },
1136
+ Math.max(this.getMaxScrollOffset(), 0),
1170
1137
  behavior,
1171
- toEnd: true,
1172
- startedAt: this.now(),
1173
- lastTargetOffset: offset,
1174
- stableFrames: 0,
1175
- hasReachedTarget: approxEqual(offset, this.getScrollOffset()),
1176
- startedSmooth: behavior === "smooth"
1177
- };
1178
- this._scrollToOffset(offset, { adjustments: void 0, behavior });
1179
- this.scheduleScrollReconcile();
1138
+ "absolute"
1139
+ );
1180
1140
  return;
1181
1141
  }
1182
1142
  this.scrollToOffset(Math.max(this.getTotalSize() - this.getSize(), 0), {
@@ -1243,12 +1203,14 @@ class Virtualizer {
1243
1203
  adjustments,
1244
1204
  behavior
1245
1205
  }) => {
1246
- this._intendedScrollOffset = offset + (adjustments ?? 0);
1247
- if (this.scrollState) {
1248
- this.scrollState.writtenOffset = behavior === "smooth" ? null : this._intendedScrollOffset;
1249
- }
1206
+ this.pendingWrite = {
1207
+ target: offset + (adjustments ?? 0),
1208
+ smooth: behavior === "smooth",
1209
+ adjustments: adjustments ?? this.scrollAdjustments
1210
+ };
1250
1211
  this.options.scrollToFn(offset, { behavior, adjustments }, this);
1251
1212
  };
1213
+ this.getCommandWrittenOffset = () => this.scrollState !== null && this.pendingWrite !== null && !this.pendingWrite.smooth ? this.pendingWrite.target : null;
1252
1214
  this.measure = () => {
1253
1215
  this.pendingMin = null;
1254
1216
  this.itemSizeCache.clear();
@@ -1258,6 +1220,13 @@ class Virtualizer {
1258
1220
  };
1259
1221
  this.setOptions(opts);
1260
1222
  }
1223
+ // Size compensation written to the element but not yet reflected in
1224
+ // `scrollOffset`: the element is expected at `scrollOffset +
1225
+ // scrollAdjustments` until the next scroll event reports it.
1226
+ get scrollAdjustments() {
1227
+ var _a;
1228
+ return ((_a = this.pendingWrite) == null ? void 0 : _a.adjustments) ?? 0;
1229
+ }
1261
1230
  // Returns `true` when it performed a synchronous `scrollTop` write this
1262
1231
  // tick, `false` when the delta was zero or the write was deferred (iOS).
1263
1232
  // `resizeItem` uses that to decide whether the follow-up `notify` must be
@@ -1268,7 +1237,7 @@ class Virtualizer {
1268
1237
  if (process.env.NODE_ENV !== "production" && this.options.debug) {
1269
1238
  console.info("correction", delta);
1270
1239
  }
1271
- if (isIOSWebKit() && (this.isScrolling || this._iosTouching || this._iosJustTouchEnded)) {
1240
+ if (isIOSWebKit() && this._iosInMotion()) {
1272
1241
  if (((_a = this.scrollState) == null ? void 0 : _a.index) == null) {
1273
1242
  this._iosDeferredAdjustment += delta;
1274
1243
  }
@@ -1279,13 +1248,13 @@ class Virtualizer {
1279
1248
  const maxAtWrite = el !== null && ("scrollHeight" in el || "document" in el) ? this.getMaxScrollOffset() : null;
1280
1249
  this._clampedAdjustment = maxAtWrite !== null && target > maxAtWrite + 0.5 ? { target, maxAtWrite } : null;
1281
1250
  this._scrollToOffset(this.getScrollOffset(), {
1282
- adjustments: this.scrollAdjustments += delta,
1251
+ adjustments: this.scrollAdjustments + delta,
1283
1252
  behavior
1284
1253
  });
1285
1254
  if (this.scrollOffset !== null) {
1286
1255
  this.scrollOffset += this.scrollAdjustments;
1287
1256
  if (this.scrollOffset < 0) this.scrollOffset = 0;
1288
- this.scrollAdjustments = 0;
1257
+ if (this.pendingWrite) this.pendingWrite.adjustments = 0;
1289
1258
  }
1290
1259
  return true;
1291
1260
  }
@@ -1319,7 +1288,11 @@ class Virtualizer {
1319
1288
  const STABLE_FRAMES = this.scrollState.index == null ? 1 : this.options.useAnimationFrameWithResizeObserver ? 3 : 2;
1320
1289
  const targetChanged = targetOffset !== this.scrollState.lastTargetOffset;
1321
1290
  if (!targetChanged && approxEqual(targetOffset, this.getScrollOffset())) {
1322
- this.scrollState.hasReachedTarget = true;
1291
+ if (this.scrollState.phase === "smooth") {
1292
+ this.scrollState.phase = "smoothLanded";
1293
+ } else if (this.scrollState.phase === "smoothFinishing") {
1294
+ this.scrollState.phase = "instant";
1295
+ }
1323
1296
  this.scrollState.stableFrames++;
1324
1297
  if (this.scrollState.stableFrames >= STABLE_FRAMES) {
1325
1298
  if (this.getScrollOffset() !== targetOffset) {
@@ -1336,12 +1309,14 @@ class Virtualizer {
1336
1309
  if (targetChanged) {
1337
1310
  const viewport = this.getSize() || 600;
1338
1311
  const distance = Math.abs(targetOffset - this.getScrollOffset());
1339
- const keepSmooth = this.scrollState.behavior === "smooth" && distance > viewport;
1312
+ const keepSmooth = isSmoothScroll(this.scrollState) && distance > viewport;
1340
1313
  this.scrollState.lastTargetOffset = targetOffset;
1341
1314
  if (keepSmooth) {
1342
- this.scrollState.hasReachedTarget = false;
1343
- } else {
1344
- this.scrollState.behavior = "auto";
1315
+ this.scrollState.phase = "smooth";
1316
+ } else if (this.scrollState.phase === "smooth") {
1317
+ this.scrollState.phase = "smoothFinishing";
1318
+ } else if (this.scrollState.phase === "smoothLanded") {
1319
+ this.scrollState.phase = "instant";
1345
1320
  }
1346
1321
  this._scrollToOffset(targetOffset, {
1347
1322
  adjustments: void 0,
@@ -1351,6 +1326,36 @@ class Virtualizer {
1351
1326
  }
1352
1327
  this.scheduleScrollReconcile();
1353
1328
  }
1329
+ // Every scroll command starts here: it becomes the in-flight command,
1330
+ // writes its first target and leaves reconcile to retarget and retire it.
1331
+ //
1332
+ // An absolute command derives its target from current measurements, so
1333
+ // any iOS-deferred compensation still pending is stale by definition —
1334
+ // the command already accounts for the measurements the delta was
1335
+ // compensating for. Drop it so _flushIosDeferredIfReady doesn't replay it
1336
+ // onto the just-established position. A relative command (scrollBy)
1337
+ // builds on the current offset, so it keeps the deferral.
1338
+ //
1339
+ // An index or end target that is already at its offset starts a smooth
1340
+ // scroll as landed; an offset target never does, so its first scroll
1341
+ // event is read as the user's (see the scroll handler in `_willUpdate`).
1342
+ _startScroll(target, offset, behavior, kind) {
1343
+ if (kind === "absolute") {
1344
+ this._iosDeferredAdjustment = 0;
1345
+ }
1346
+ this.scrollState = {
1347
+ ...target,
1348
+ startedAt: this.now(),
1349
+ lastTargetOffset: offset,
1350
+ stableFrames: 0,
1351
+ phase: initialScrollPhase(
1352
+ behavior,
1353
+ target.index != null && approxEqual(offset, this.getScrollOffset())
1354
+ )
1355
+ };
1356
+ this._scrollToOffset(offset, { adjustments: void 0, behavior });
1357
+ this.scheduleScrollReconcile();
1358
+ }
1354
1359
  }
1355
1360
  const findNearestBinarySearch = (low, high, getCurrentValue, value) => {
1356
1361
  while (low <= high) {