@tanstack/virtual-core 3.19.2 → 3.19.4

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
@@ -15,6 +15,22 @@ const getRect = (element) => {
15
15
  const { offsetWidth, offsetHeight } = element;
16
16
  return { width: offsetWidth, height: offsetHeight };
17
17
  };
18
+ const createFrameQueue = (targetWindow) => {
19
+ const ids = /* @__PURE__ */ new Set();
20
+ return {
21
+ schedule: (fn) => {
22
+ const id = targetWindow.requestAnimationFrame(() => {
23
+ ids.delete(id);
24
+ fn();
25
+ });
26
+ ids.add(id);
27
+ },
28
+ cancel: () => {
29
+ ids.forEach((id) => targetWindow.cancelAnimationFrame(id));
30
+ ids.clear();
31
+ }
32
+ };
33
+ };
18
34
  const defaultKeyExtractor = (index) => index;
19
35
  const defaultRangeExtractor = (range) => {
20
36
  const overscan = range.overscan * Math.max(range.lanes ?? 1, 1);
@@ -45,6 +61,7 @@ const observeElementRect = (instance, cb) => {
45
61
  return () => {
46
62
  };
47
63
  }
64
+ const frames = createFrameQueue(targetWindow);
48
65
  const observer = new targetWindow.ResizeObserver((entries) => {
49
66
  const run = () => {
50
67
  const entry = entries[0];
@@ -57,11 +74,12 @@ const observeElementRect = (instance, cb) => {
57
74
  }
58
75
  handler(getRect(element));
59
76
  };
60
- instance.options.useAnimationFrameWithResizeObserver ? requestAnimationFrame(run) : run();
77
+ instance.options.useAnimationFrameWithResizeObserver ? frames.schedule(run) : run();
61
78
  });
62
79
  observer.observe(element, { box: "border-box" });
63
80
  return () => {
64
81
  observer.unobserve(element);
82
+ frames.cancel();
65
83
  };
66
84
  };
67
85
  const addEventListenerOptions = {
@@ -207,12 +225,9 @@ class Virtualizer {
207
225
  this.scrollRect = null;
208
226
  this.scrollOffset = null;
209
227
  this.scrollDirection = null;
210
- this.scrollAdjustments = 0;
211
228
  this._iosDeferredAdjustment = 0;
212
- this._iosTouching = false;
213
- this._iosJustTouchEnded = false;
214
- this._iosTouchEndTimerId = null;
215
- this._intendedScrollOffset = null;
229
+ this._iosTouch = null;
230
+ this.pendingWrite = null;
216
231
  this._clampedAdjustment = null;
217
232
  this.elementsCache = /* @__PURE__ */ new Map();
218
233
  this._resizeBatch = null;
@@ -222,14 +237,17 @@ class Virtualizer {
222
237
  };
223
238
  this.observer = /* @__PURE__ */ (() => {
224
239
  let _ro = null;
240
+ let _frames = null;
225
241
  const get = () => {
226
242
  if (_ro) {
227
243
  return _ro;
228
244
  }
229
- if (!this.targetWindow || !this.targetWindow.ResizeObserver) {
245
+ const targetWindow = this.targetWindow;
246
+ if (!targetWindow || !targetWindow.ResizeObserver) {
230
247
  return null;
231
248
  }
232
- return _ro = new this.targetWindow.ResizeObserver((entries) => {
249
+ const frames = _frames = createFrameQueue(targetWindow);
250
+ return _ro = new targetWindow.ResizeObserver((entries) => {
233
251
  const run = () => {
234
252
  const batch = { sync: false };
235
253
  this._resizeBatch = batch;
@@ -243,7 +261,7 @@ class Virtualizer {
243
261
  }
244
262
  }
245
263
  };
246
- this.options.useAnimationFrameWithResizeObserver ? requestAnimationFrame(run) : run();
264
+ this.options.useAnimationFrameWithResizeObserver ? frames.schedule(run) : run();
247
265
  });
248
266
  };
249
267
  const measureEntry = (entry) => {
@@ -268,7 +286,9 @@ class Virtualizer {
268
286
  disconnect: () => {
269
287
  var _a;
270
288
  (_a = get()) == null ? void 0 : _a.disconnect();
289
+ _frames == null ? void 0 : _frames.cancel();
271
290
  _ro = null;
291
+ _frames = null;
272
292
  },
273
293
  observe: (target) => {
274
294
  var _a;
@@ -356,8 +376,7 @@ class Virtualizer {
356
376
  this.pendingMin = 0;
357
377
  this.itemSizeCacheVersion++;
358
378
  }
359
- let anchorResolved = false;
360
- let anchorDelta = 0;
379
+ let anchorDelta = null;
361
380
  if (anchor && this.scrollOffset !== null) {
362
381
  const [anchorKey, anchorOffset] = anchor;
363
382
  const newMeasurements = this.getMeasurements();
@@ -373,18 +392,14 @@ class Virtualizer {
373
392
  if (!followOnAppend && newOffset !== this.scrollOffset) {
374
393
  anchorDelta = newOffset - this.scrollOffset;
375
394
  this.scrollOffset = newOffset;
376
- anchorResolved = true;
377
395
  }
378
396
  }
379
397
  }
380
398
  }
381
- if (anchorResolved || followOnAppend) {
382
- this.pendingScrollAnchor = [
383
- anchorResolved ? anchor[0] : null,
384
- anchorResolved ? anchor[1] : 0,
385
- followOnAppend,
386
- anchorDelta
387
- ];
399
+ if (followOnAppend) {
400
+ this.pendingScrollAnchor = { kind: "follow", behavior: followOnAppend };
401
+ } else if (anchorDelta !== null) {
402
+ this.pendingScrollAnchor = { kind: "anchor", delta: anchorDelta };
388
403
  }
389
404
  };
390
405
  this.notify = (sync) => {
@@ -425,9 +440,9 @@ class Virtualizer {
425
440
  this.isScrolling = false;
426
441
  this.scrollDirection = null;
427
442
  this._iosDeferredAdjustment = 0;
428
- this._iosTouching = false;
429
- this._iosJustTouchEnded = false;
443
+ this._endIosTouch();
430
444
  this._clampedAdjustment = null;
445
+ this.pendingWrite = null;
431
446
  this.scrollElement = null;
432
447
  this.targetWindow = null;
433
448
  };
@@ -463,11 +478,12 @@ class Virtualizer {
463
478
  this.unsubs.push(
464
479
  this.options.observeElementOffset(this, (offset, isScrolling) => {
465
480
  var _a2;
466
- if (isScrolling && this._intendedScrollOffset === null && offset === this.scrollOffset) {
481
+ if (isScrolling && this.pendingWrite === null && offset === this.scrollOffset) {
467
482
  return;
468
483
  }
469
- if (this._intendedScrollOffset !== null && Math.abs(offset - this._intendedScrollOffset) < 1.5) {
470
- offset = this._intendedScrollOffset;
484
+ const pending = this.pendingWrite;
485
+ if (pending !== null && Math.abs(offset - pending.target) < 1.5) {
486
+ offset = pending.target;
471
487
  } else if (((_a2 = this.scrollState) == null ? void 0 : _a2.index) != null && // A non-smooth write lands at once, so any other scroll event is
472
488
  // the user's, even before reconcile saw the target reached (the
473
489
  // target may keep moving, or iOS ignored the write under a
@@ -484,12 +500,10 @@ class Virtualizer {
484
500
  !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)) {
485
501
  this.scrollState = null;
486
502
  }
487
- this._intendedScrollOffset = null;
488
- if (this.scrollState) this.scrollState.writtenOffset = null;
503
+ this.pendingWrite = null;
489
504
  if (this._clampedAdjustment !== null && Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5) {
490
505
  this._clampedAdjustment = null;
491
506
  }
492
- this.scrollAdjustments = 0;
493
507
  const prevOffset = this.getScrollOffset();
494
508
  this.scrollDirection = isScrolling ? prevOffset === offset ? this.scrollDirection : prevOffset < offset ? "forward" : "backward" : null;
495
509
  this.scrollOffset = offset;
@@ -504,24 +518,22 @@ class Virtualizer {
504
518
  if ("addEventListener" in this.scrollElement) {
505
519
  const scrollEl = this.scrollElement;
506
520
  const onTouchStart = () => {
507
- this._iosTouching = true;
508
- this._iosJustTouchEnded = false;
509
- if (this._iosTouchEndTimerId !== null && this.targetWindow != null) {
510
- this.targetWindow.clearTimeout(this._iosTouchEndTimerId);
511
- this._iosTouchEndTimerId = null;
512
- }
521
+ this._endIosTouch();
522
+ this._iosTouch = { phase: "down" };
513
523
  };
514
524
  const onTouchEnd = () => {
515
- this._iosTouching = false;
525
+ this._endIosTouch();
516
526
  if (!isIOSWebKit() || this.targetWindow == null) {
517
527
  return;
518
528
  }
519
- this._iosJustTouchEnded = true;
520
- this._iosTouchEndTimerId = this.targetWindow.setTimeout(() => {
521
- this._iosJustTouchEnded = false;
522
- this._iosTouchEndTimerId = null;
523
- this._flushIosDeferredIfReady();
524
- }, 150);
529
+ const tail = {
530
+ phase: "tail",
531
+ timerId: this.targetWindow.setTimeout(() => {
532
+ if (this._iosTouch === tail) this._iosTouch = null;
533
+ this._flushIosDeferredIfReady();
534
+ }, 150)
535
+ };
536
+ this._iosTouch = tail;
525
537
  };
526
538
  scrollEl.addEventListener(
527
539
  "touchstart",
@@ -542,10 +554,7 @@ class Virtualizer {
542
554
  scrollEl.removeEventListener("touchstart", onTouchStart);
543
555
  scrollEl.removeEventListener("touchend", onTouchEnd);
544
556
  scrollEl.removeEventListener("touchcancel", onTouchEnd);
545
- if (this._iosTouchEndTimerId !== null && this.targetWindow != null) {
546
- this.targetWindow.clearTimeout(this._iosTouchEndTimerId);
547
- this._iosTouchEndTimerId = null;
548
- }
557
+ this._endIosTouch();
549
558
  });
550
559
  }
551
560
  this._scrollToOffset(this.getScrollOffset(), {
@@ -556,14 +565,11 @@ class Virtualizer {
556
565
  const anchor = this.pendingScrollAnchor;
557
566
  this.pendingScrollAnchor = null;
558
567
  if (anchor && this.scrollElement && this.options.enabled) {
559
- const [key, _offset, followOnAppend, anchorDelta] = anchor;
560
- if (key !== null && !followOnAppend) {
561
- if (isIOSWebKit() && (this.isScrolling || this._iosTouching || this._iosJustTouchEnded)) {
562
- if (anchorDelta !== 0) {
563
- this._iosDeferredAdjustment += anchorDelta;
564
- }
568
+ if (anchor.kind === "anchor") {
569
+ if (isIOSWebKit() && this._iosInMotion()) {
570
+ this._iosDeferredAdjustment += anchor.delta;
565
571
  } else if (isSmoothScroll(this.scrollState) && !approxEqual(
566
- this.getScrollOffset() - anchorDelta,
572
+ this.getScrollOffset() - anchor.delta,
567
573
  this.scrollState.lastTargetOffset
568
574
  )) ;
569
575
  else {
@@ -572,9 +578,8 @@ class Virtualizer {
572
578
  behavior: void 0
573
579
  });
574
580
  }
575
- }
576
- if (followOnAppend) {
577
- this.scrollToEnd({ behavior: followOnAppend });
581
+ } else {
582
+ this.scrollToEnd({ behavior: anchor.behavior });
578
583
  }
579
584
  }
580
585
  this._retryClampedAdjustment();
@@ -593,11 +598,17 @@ class Virtualizer {
593
598
  });
594
599
  }
595
600
  };
601
+ this._iosInMotion = () => this.isScrolling || this._iosTouch !== null;
602
+ this._endIosTouch = () => {
603
+ var _a;
604
+ if (((_a = this._iosTouch) == null ? void 0 : _a.phase) === "tail" && this.targetWindow != null) {
605
+ this.targetWindow.clearTimeout(this._iosTouch.timerId);
606
+ }
607
+ this._iosTouch = null;
608
+ };
596
609
  this._flushIosDeferredIfReady = () => {
597
610
  if (this._iosDeferredAdjustment === 0) return;
598
- if (this.isScrolling) return;
599
- if (this._iosTouching) return;
600
- if (this._iosJustTouchEnded) return;
611
+ if (this._iosInMotion()) return;
601
612
  const cur = this.getScrollOffset();
602
613
  const max = this.getMaxScrollOffset();
603
614
  if (cur < 0 || cur > max) return;
@@ -608,7 +619,7 @@ class Virtualizer {
608
619
  const delta = this._iosDeferredAdjustment;
609
620
  this._iosDeferredAdjustment = 0;
610
621
  this._scrollToOffset(cur, {
611
- adjustments: this.scrollAdjustments += delta,
622
+ adjustments: this.scrollAdjustments + delta,
612
623
  behavior: void 0
613
624
  });
614
625
  };
@@ -931,7 +942,7 @@ class Virtualizer {
931
942
  }
932
943
  };
933
944
  this.resizeItem = (index, size) => {
934
- var _a, _b, _c, _d;
945
+ var _a, _b, _c;
935
946
  if (!this.isIndexInRange(index)) return;
936
947
  if ((_a = this._resizeBatch) == null ? void 0 : _a.sync) this.getMeasurements();
937
948
  let cachedSize;
@@ -990,8 +1001,9 @@ class Virtualizer {
990
1001
  }
991
1002
  this.itemSizeCache.set(key, size);
992
1003
  this.itemSizeCacheVersion++;
993
- const awaitingCommandReadBack = ((_c = this.scrollState) == null ? void 0 : _c.writtenOffset) != null && !approxEqual(
994
- this.scrollState.writtenOffset,
1004
+ const writtenOffset = this.getCommandWrittenOffset();
1005
+ const awaitingCommandReadBack = writtenOffset !== null && !approxEqual(
1006
+ writtenOffset,
995
1007
  this.getScrollOffset() + this.scrollAdjustments
996
1008
  );
997
1009
  let adjustedSync = false;
@@ -1006,7 +1018,7 @@ class Virtualizer {
1006
1018
  }
1007
1019
  if (this._resizeBatch) {
1008
1020
  this._resizeBatch.notify = true;
1009
- (_d = this._resizeBatch).sync || (_d.sync = adjustedSync);
1021
+ (_c = this._resizeBatch).sync || (_c.sync = adjustedSync);
1010
1022
  return;
1011
1023
  }
1012
1024
  this.notify(adjustedSync);
@@ -1108,82 +1120,46 @@ class Virtualizer {
1108
1120
  ];
1109
1121
  };
1110
1122
  this.scrollToOffset = (toOffset, { align = "start", behavior = "auto" } = {}) => {
1111
- this._iosDeferredAdjustment = 0;
1112
- const offset = this.getOffsetForAlignment(toOffset, align);
1113
- const now = this.now();
1114
- this.scrollState = {
1115
- index: null,
1116
- align,
1117
- startedAt: now,
1118
- lastTargetOffset: offset,
1119
- stableFrames: 0,
1120
- phase: initialScrollPhase(behavior, false)
1121
- };
1122
- this._scrollToOffset(offset, { adjustments: void 0, behavior });
1123
- this.scheduleScrollReconcile();
1123
+ this._startScroll(
1124
+ { index: null, align },
1125
+ this.getOffsetForAlignment(toOffset, align),
1126
+ behavior,
1127
+ "absolute"
1128
+ );
1124
1129
  };
1125
1130
  this.scrollToIndex = (index, {
1126
1131
  align: initialAlign = "auto",
1127
1132
  behavior = "auto"
1128
1133
  } = {}) => {
1129
- this._iosDeferredAdjustment = 0;
1130
1134
  index = Math.max(0, Math.min(index, this.options.count - 1));
1131
1135
  const offsetInfo = this.getOffsetForIndex(index, initialAlign);
1132
1136
  if (!offsetInfo) {
1137
+ this._iosDeferredAdjustment = 0;
1133
1138
  return;
1134
1139
  }
1135
1140
  const [offset, align] = offsetInfo;
1136
- const now = this.now();
1137
- this.scrollState = {
1138
- index,
1139
- align,
1140
- startedAt: now,
1141
- lastTargetOffset: offset,
1142
- stableFrames: 0,
1143
- phase: initialScrollPhase(
1144
- behavior,
1145
- approxEqual(offset, this.getScrollOffset())
1146
- )
1147
- };
1148
- this._scrollToOffset(offset, { adjustments: void 0, behavior });
1149
- this.scheduleScrollReconcile();
1141
+ this._startScroll({ index, align }, offset, behavior, "absolute");
1150
1142
  };
1151
1143
  this.scrollBy = (delta, { behavior = "auto" } = {}) => {
1152
- var _a;
1153
1144
  const offset = this.getOffsetForAlignment(
1154
- (((_a = this.scrollState) == null ? void 0 : _a.writtenOffset) ?? this.getScrollOffset()) + delta,
1145
+ (this.getCommandWrittenOffset() ?? this.getScrollOffset()) + delta,
1155
1146
  "start"
1156
1147
  );
1157
- const now = this.now();
1158
- this.scrollState = {
1159
- index: null,
1160
- align: "start",
1161
- startedAt: now,
1162
- lastTargetOffset: offset,
1163
- stableFrames: 0,
1164
- phase: initialScrollPhase(behavior, false)
1165
- };
1166
- this._scrollToOffset(offset, { adjustments: void 0, behavior });
1167
- this.scheduleScrollReconcile();
1148
+ this._startScroll(
1149
+ { index: null, align: "start" },
1150
+ offset,
1151
+ behavior,
1152
+ "relative"
1153
+ );
1168
1154
  };
1169
1155
  this.scrollToEnd = ({ behavior = "auto" } = {}) => {
1170
1156
  if (this.options.count > 0) {
1171
- this._iosDeferredAdjustment = 0;
1172
- const offset = Math.max(this.getMaxScrollOffset(), 0);
1173
- this.scrollState = {
1174
- index: this.options.count - 1,
1175
- align: "end",
1176
- toEnd: true,
1177
- startedAt: this.now(),
1178
- lastTargetOffset: offset,
1179
- stableFrames: 0,
1180
- phase: initialScrollPhase(
1181
- behavior,
1182
- approxEqual(offset, this.getScrollOffset())
1183
- )
1184
- };
1185
- this._scrollToOffset(offset, { adjustments: void 0, behavior });
1186
- this.scheduleScrollReconcile();
1157
+ this._startScroll(
1158
+ { index: this.options.count - 1, align: "end", toEnd: true },
1159
+ Math.max(this.getMaxScrollOffset(), 0),
1160
+ behavior,
1161
+ "absolute"
1162
+ );
1187
1163
  return;
1188
1164
  }
1189
1165
  this.scrollToOffset(Math.max(this.getTotalSize() - this.getSize(), 0), {
@@ -1250,12 +1226,14 @@ class Virtualizer {
1250
1226
  adjustments,
1251
1227
  behavior
1252
1228
  }) => {
1253
- this._intendedScrollOffset = offset + (adjustments ?? 0);
1254
- if (this.scrollState) {
1255
- this.scrollState.writtenOffset = behavior === "smooth" ? null : this._intendedScrollOffset;
1256
- }
1229
+ this.pendingWrite = {
1230
+ target: offset + (adjustments ?? 0),
1231
+ smooth: behavior === "smooth",
1232
+ adjustments: adjustments ?? this.scrollAdjustments
1233
+ };
1257
1234
  this.options.scrollToFn(offset, { behavior, adjustments }, this);
1258
1235
  };
1236
+ this.getCommandWrittenOffset = () => this.scrollState !== null && this.pendingWrite !== null && !this.pendingWrite.smooth ? this.pendingWrite.target : null;
1259
1237
  this.measure = () => {
1260
1238
  this.pendingMin = null;
1261
1239
  this.itemSizeCache.clear();
@@ -1265,6 +1243,13 @@ class Virtualizer {
1265
1243
  };
1266
1244
  this.setOptions(opts);
1267
1245
  }
1246
+ // Size compensation written to the element but not yet reflected in
1247
+ // `scrollOffset`: the element is expected at `scrollOffset +
1248
+ // scrollAdjustments` until the next scroll event reports it.
1249
+ get scrollAdjustments() {
1250
+ var _a;
1251
+ return ((_a = this.pendingWrite) == null ? void 0 : _a.adjustments) ?? 0;
1252
+ }
1268
1253
  // Returns `true` when it performed a synchronous `scrollTop` write this
1269
1254
  // tick, `false` when the delta was zero or the write was deferred (iOS).
1270
1255
  // `resizeItem` uses that to decide whether the follow-up `notify` must be
@@ -1275,7 +1260,7 @@ class Virtualizer {
1275
1260
  if (process.env.NODE_ENV !== "production" && this.options.debug) {
1276
1261
  console.info("correction", delta);
1277
1262
  }
1278
- if (isIOSWebKit() && (this.isScrolling || this._iosTouching || this._iosJustTouchEnded)) {
1263
+ if (isIOSWebKit() && this._iosInMotion()) {
1279
1264
  if (((_a = this.scrollState) == null ? void 0 : _a.index) == null) {
1280
1265
  this._iosDeferredAdjustment += delta;
1281
1266
  }
@@ -1286,13 +1271,13 @@ class Virtualizer {
1286
1271
  const maxAtWrite = el !== null && ("scrollHeight" in el || "document" in el) ? this.getMaxScrollOffset() : null;
1287
1272
  this._clampedAdjustment = maxAtWrite !== null && target > maxAtWrite + 0.5 ? { target, maxAtWrite } : null;
1288
1273
  this._scrollToOffset(this.getScrollOffset(), {
1289
- adjustments: this.scrollAdjustments += delta,
1274
+ adjustments: this.scrollAdjustments + delta,
1290
1275
  behavior
1291
1276
  });
1292
1277
  if (this.scrollOffset !== null) {
1293
1278
  this.scrollOffset += this.scrollAdjustments;
1294
1279
  if (this.scrollOffset < 0) this.scrollOffset = 0;
1295
- this.scrollAdjustments = 0;
1280
+ if (this.pendingWrite) this.pendingWrite.adjustments = 0;
1296
1281
  }
1297
1282
  return true;
1298
1283
  }
@@ -1364,6 +1349,36 @@ class Virtualizer {
1364
1349
  }
1365
1350
  this.scheduleScrollReconcile();
1366
1351
  }
1352
+ // Every scroll command starts here: it becomes the in-flight command,
1353
+ // writes its first target and leaves reconcile to retarget and retire it.
1354
+ //
1355
+ // An absolute command derives its target from current measurements, so
1356
+ // any iOS-deferred compensation still pending is stale by definition —
1357
+ // the command already accounts for the measurements the delta was
1358
+ // compensating for. Drop it so _flushIosDeferredIfReady doesn't replay it
1359
+ // onto the just-established position. A relative command (scrollBy)
1360
+ // builds on the current offset, so it keeps the deferral.
1361
+ //
1362
+ // An index or end target that is already at its offset starts a smooth
1363
+ // scroll as landed; an offset target never does, so its first scroll
1364
+ // event is read as the user's (see the scroll handler in `_willUpdate`).
1365
+ _startScroll(target, offset, behavior, kind) {
1366
+ if (kind === "absolute") {
1367
+ this._iosDeferredAdjustment = 0;
1368
+ }
1369
+ this.scrollState = {
1370
+ ...target,
1371
+ startedAt: this.now(),
1372
+ lastTargetOffset: offset,
1373
+ stableFrames: 0,
1374
+ phase: initialScrollPhase(
1375
+ behavior,
1376
+ target.index != null && approxEqual(offset, this.getScrollOffset())
1377
+ )
1378
+ };
1379
+ this._scrollToOffset(offset, { adjustments: void 0, behavior });
1380
+ this.scheduleScrollReconcile();
1381
+ }
1367
1382
  }
1368
1383
  const findNearestBinarySearch = (low, high, getCurrentValue, value) => {
1369
1384
  while (low <= high) {