@tanstack/virtual-core 3.19.4 → 3.19.5

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.
@@ -2,17 +2,8 @@
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const lazyMeasurements = require("./lazy-measurements.cjs");
4
4
  const utils = require("./utils.cjs");
5
- let _isIOSResult;
6
- const isIOSWebKit = () => {
7
- if (_isIOSResult !== void 0) return _isIOSResult;
8
- if (typeof navigator === "undefined") return _isIOSResult = false;
9
- if (/iP(hone|od|ad)/.test(navigator.userAgent)) return _isIOSResult = true;
10
- const mtp = navigator.maxTouchPoints;
11
- return _isIOSResult = navigator.platform === "MacIntel" && mtp !== void 0 && mtp > 0;
12
- };
13
- const _resetIOSDetectionForTests = () => {
14
- _isIOSResult = void 0;
15
- };
5
+ const scrollClassifier = require("./scroll-classifier.cjs");
6
+ const iosGesture = require("./ios-gesture.cjs");
16
7
  const getRect = (element) => {
17
8
  const { offsetWidth, offsetHeight } = element;
18
9
  return { width: offsetWidth, height: offsetHeight };
@@ -185,7 +176,6 @@ const windowScroll = scrollWithAdjustments;
185
176
  const elementScroll = scrollWithAdjustments;
186
177
  const initialScrollPhase = (behavior, atTarget) => behavior === "smooth" ? atTarget ? "smoothLanded" : "smooth" : "instant";
187
178
  const isSmoothScroll = (state) => (state == null ? void 0 : state.phase) === "smooth" || (state == null ? void 0 : state.phase) === "smoothLanded";
188
- const isAnimating = (state) => state.phase === "smooth" || state.phase === "smoothFinishing";
189
179
  function isAppendWithTrim(prevCount, nextCount, getPreviousKey, getNextKey) {
190
180
  if (nextCount === 0) return false;
191
181
  const firstKey = getNextKey(0);
@@ -227,8 +217,40 @@ class Virtualizer {
227
217
  this.scrollRect = null;
228
218
  this.scrollOffset = null;
229
219
  this.scrollDirection = null;
230
- this._iosDeferredAdjustment = 0;
231
- this._iosTouch = null;
220
+ this._holdOffset = 0;
221
+ this._reportedOverscroll = false;
222
+ this._iosGesture = new iosGesture.IosGesture({
223
+ getWindow: () => this.targetWindow,
224
+ now: () => this.now(),
225
+ // The end of a fling can move less than a pixel per scroll event, so
226
+ // events can stop for 150ms while the fling is still running (seen on
227
+ // an iPhone, 27.0.1). A command waiting for it would then be written
228
+ // into the fling and overridden. The element having moved since the
229
+ // last event means it has not ended: wait again. Not while our own
230
+ // write has yet to report: that moved it.
231
+ tailStillMoving: () => {
232
+ const state = this.scrollState;
233
+ if ((state == null ? void 0 : state.phase) !== "queued" || this.pendingWrite !== null) {
234
+ return false;
235
+ }
236
+ const now = this._readElementOffset();
237
+ if (now === null || this._lastElementOffset === null || Math.abs(now - this._lastElementOffset) < 1) {
238
+ return false;
239
+ }
240
+ this._lastElementOffset = now;
241
+ state.samples = [...state.samples ?? [], { t: this.now(), offset: now }];
242
+ return true;
243
+ },
244
+ onTailClosed: () => {
245
+ const state = this.scrollState;
246
+ if ((state == null ? void 0 : state.phase) === "queued" && scrollClassifier.flingCaught(state.samples ?? [])) {
247
+ this.scrollState = null;
248
+ }
249
+ this._flushHoldIfReady(true);
250
+ this._writeAwaitingCommand();
251
+ }
252
+ });
253
+ this._lastElementOffset = null;
232
254
  this.pendingWrite = null;
233
255
  this._clampedAdjustment = null;
234
256
  this.elementsCache = /* @__PURE__ */ new Map();
@@ -259,6 +281,8 @@ class Virtualizer {
259
281
  this._resizeBatch = null;
260
282
  if (batch.notify) {
261
283
  this.notify(batch.sync);
284
+ this._noteClampMax();
285
+ this._flushHoldIfReady(true);
262
286
  this._retryClampedAdjustment();
263
287
  }
264
288
  }
@@ -379,7 +403,7 @@ class Virtualizer {
379
403
  this.itemSizeCacheVersion++;
380
404
  }
381
405
  let anchorDelta = null;
382
- if (anchor && this.scrollOffset !== null) {
406
+ if (anchor && this.scrollOffset !== null && !this._awaitingCommandReadBack()) {
383
407
  const [anchorKey, anchorOffset] = anchor;
384
408
  const newMeasurements = this.getMeasurements();
385
409
  const { count, getItemKey } = this.options;
@@ -401,7 +425,12 @@ class Virtualizer {
401
425
  if (followOnAppend) {
402
426
  this.pendingScrollAnchor = { kind: "follow", behavior: followOnAppend };
403
427
  } else if (anchorDelta !== null) {
404
- this.pendingScrollAnchor = { kind: "anchor", delta: anchorDelta };
428
+ if (this._canWriteScroll()) {
429
+ this.pendingScrollAnchor = { kind: "anchor", delta: anchorDelta };
430
+ } else {
431
+ this._holdOffset += anchorDelta;
432
+ this._followOffsetTarget(anchorDelta);
433
+ }
405
434
  }
406
435
  };
407
436
  this.notify = (sync) => {
@@ -431,6 +460,7 @@ class Virtualizer {
431
460
  }
432
461
  );
433
462
  this.cleanup = () => {
463
+ var _a;
434
464
  this.unsubs.filter(Boolean).forEach((d) => d());
435
465
  this.unsubs = [];
436
466
  this.observer.disconnect();
@@ -439,10 +469,12 @@ class Virtualizer {
439
469
  this.rafId = null;
440
470
  }
441
471
  this.scrollState = null;
472
+ (_a = this._momentumStop) == null ? void 0 : _a.restore();
473
+ this._momentumStop = null;
442
474
  this.isScrolling = false;
443
475
  this.scrollDirection = null;
444
- this._iosDeferredAdjustment = 0;
445
- this._endIosTouch();
476
+ this._holdOffset = 0;
477
+ this._reportedOverscroll = false;
446
478
  this._clampedAdjustment = null;
447
479
  this.pendingWrite = null;
448
480
  this.scrollElement = null;
@@ -479,27 +511,30 @@ class Virtualizer {
479
511
  );
480
512
  this.unsubs.push(
481
513
  this.options.observeElementOffset(this, (offset, isScrolling) => {
482
- var _a2;
483
- if (isScrolling && this.pendingWrite === null && offset === this.scrollOffset) {
484
- return;
485
- }
486
- const pending = this.pendingWrite;
487
- if (pending !== null && Math.abs(offset - pending.target) < 1.5) {
488
- offset = pending.target;
489
- } else if (((_a2 = this.scrollState) == null ? void 0 : _a2.index) != null && // A non-smooth write lands at once, so any other scroll event is
490
- // the user's, even before reconcile saw the target reached (the
491
- // target may keep moving, or iOS ignored the write under a
492
- // finger). A write the browser clamped because the sizer hasn't
493
- // grown yet reads back at the max, which is also where the fresh
494
- // target clamps, so the check below keeps it reconciling (#1294).
495
- // Not when rows above the target shrink before the read-back and
496
- // pull the target below the new max: the read-back then looks
497
- // like the user and the scroll stops at the max. That needs the
498
- // sizer re-rendered before the scroll event, which browsers have
499
- // not been seen to do (pinned by a `.fails` test).
500
- // While a smooth scroll's animation may still report, its events
501
- // look like a user's, so it yields only once it has landed.
502
- !isAnimating(this.scrollState) && !utils.approxEqual(offset, this.scrollState.lastTargetOffset) && !utils.approxEqual(offset, this.getScrollStateTarget(this.scrollState)) && (this._clampedAdjustment === null || Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5)) {
514
+ var _a2, _b, _c, _d;
515
+ const elementMax = isScrolling ? this._elementMaxScrollOffset() : null;
516
+ const wasOverscrolled = this._reportedOverscroll;
517
+ this._reportedOverscroll = offset < 0 || elementMax !== null && offset > elementMax + 1;
518
+ const elementOffset = offset;
519
+ this._lastElementOffset = elementOffset;
520
+ offset = this._toLayoutOffset(offset);
521
+ const kind = this._classifyScrollEvent(
522
+ offset,
523
+ isScrolling,
524
+ elementMax
525
+ );
526
+ if (kind === "spurious") return;
527
+ if (kind === "readBack") {
528
+ offset = this.pendingWrite.target;
529
+ } else if (kind === "clampedReadBack" && ((_a2 = this.scrollState) == null ? void 0 : _a2.index) != null && // Not `elementMax`: an adapter can report the read-back at rest.
530
+ offset < this.getMaxScrollOffset() - 1.5) {
531
+ this.scrollState.lastTargetOffset = offset;
532
+ } else if (scrollClassifier.externalRetiresCommand({
533
+ kind,
534
+ offset,
535
+ command: this.scrollState,
536
+ target: () => this.getScrollStateTarget(this.scrollState)
537
+ })) {
503
538
  this.scrollState = null;
504
539
  }
505
540
  this.pendingWrite = null;
@@ -507,10 +542,40 @@ class Virtualizer {
507
542
  this._clampedAdjustment = null;
508
543
  }
509
544
  const prevOffset = this.getScrollOffset();
545
+ const prevDirection = this.scrollDirection;
510
546
  this.scrollDirection = isScrolling ? prevOffset === offset ? this.scrollDirection : prevOffset < offset ? "forward" : "backward" : null;
511
547
  this.scrollOffset = offset;
512
548
  this.isScrolling = isScrolling;
513
- this._flushIosDeferredIfReady();
549
+ const queued = this.scrollState;
550
+ if ((queued == null ? void 0 : queued.phase) === "queued" && kind === "external" && isScrolling) {
551
+ if (wasOverscrolled || this._reportedOverscroll) {
552
+ queued.samples = [];
553
+ } else {
554
+ const t = this.now();
555
+ queued.samples = [
556
+ ...(queued.samples ?? []).filter(
557
+ (sample) => t - sample.t <= 200
558
+ ),
559
+ { t, offset: elementOffset }
560
+ ];
561
+ if (prevDirection !== null && this.scrollDirection !== prevDirection || scrollClassifier.flingSpedUp(queued.samples)) {
562
+ this.scrollState = null;
563
+ }
564
+ }
565
+ }
566
+ if (this._iosGesture.onScroll({
567
+ isScrolling,
568
+ ownReadBack: kind === "readBack",
569
+ moved: offset !== prevOffset,
570
+ // Neither our write's read-back nor a frame of our smooth
571
+ // animation: the user or a fling moved it.
572
+ external: kind === "external" && ((_b = this.scrollState) == null ? void 0 : _b.phase) !== "smooth" && ((_c = this.scrollState) == null ? void 0 : _c.phase) !== "smoothFinishing"
573
+ }) && // The user scrolls: a command still waiting for the tail is
574
+ // theirs to override, like a written one would be.
575
+ ((_d = this.scrollState) == null ? void 0 : _d.phase) === "queued") {
576
+ this.scrollState = null;
577
+ }
578
+ this._flushHoldIfReady(true);
514
579
  if (this.scrollState) {
515
580
  this.scheduleScrollReconcile();
516
581
  }
@@ -518,74 +583,48 @@ class Virtualizer {
518
583
  })
519
584
  );
520
585
  if ("addEventListener" in this.scrollElement) {
521
- const scrollEl = this.scrollElement;
522
- const onTouchStart = () => {
523
- this._endIosTouch();
524
- this._iosTouch = { phase: "down" };
525
- };
526
- const onTouchEnd = () => {
527
- this._endIosTouch();
528
- if (!isIOSWebKit() || this.targetWindow == null) {
529
- return;
530
- }
531
- const tail = {
532
- phase: "tail",
533
- timerId: this.targetWindow.setTimeout(() => {
534
- if (this._iosTouch === tail) this._iosTouch = null;
535
- this._flushIosDeferredIfReady();
536
- }, 150)
537
- };
538
- this._iosTouch = tail;
539
- };
540
- scrollEl.addEventListener(
541
- "touchstart",
542
- onTouchStart,
543
- addEventListenerOptions
586
+ this.unsubs.push(
587
+ this._iosGesture.listen(this.scrollElement)
544
588
  );
545
- scrollEl.addEventListener(
546
- "touchend",
547
- onTouchEnd,
548
- addEventListenerOptions
549
- );
550
- scrollEl.addEventListener(
551
- "touchcancel",
552
- onTouchEnd,
553
- addEventListenerOptions
554
- );
555
- this.unsubs.push(() => {
556
- scrollEl.removeEventListener("touchstart", onTouchStart);
557
- scrollEl.removeEventListener("touchend", onTouchEnd);
558
- scrollEl.removeEventListener("touchcancel", onTouchEnd);
559
- this._endIosTouch();
560
- });
561
589
  }
562
- this._scrollToOffset(this.getScrollOffset(), {
563
- adjustments: void 0,
564
- behavior: void 0
565
- });
590
+ this._scrollToOffset(this.getScrollOffset());
566
591
  }
592
+ this._noteClampMax();
567
593
  const anchor = this.pendingScrollAnchor;
568
594
  this.pendingScrollAnchor = null;
569
595
  if (anchor && this.scrollElement && this.options.enabled) {
570
596
  if (anchor.kind === "anchor") {
571
- if (isIOSWebKit() && this._iosInMotion()) {
572
- this._iosDeferredAdjustment += anchor.delta;
597
+ if (!this._canWriteScroll()) {
598
+ this._holdOffset += anchor.delta;
599
+ this._followOffsetTarget(anchor.delta);
600
+ this.notify(false);
573
601
  } else if (isSmoothScroll(this.scrollState) && !utils.approxEqual(
574
602
  this.getScrollOffset() - anchor.delta,
575
603
  this.scrollState.lastTargetOffset
576
604
  )) ;
577
605
  else {
578
- this._scrollToOffset(this.getScrollOffset(), {
579
- adjustments: void 0,
580
- behavior: void 0
581
- });
606
+ this._followOffsetTarget(anchor.delta);
607
+ this._scrollToOffset(this.getScrollOffset());
582
608
  }
583
609
  } else {
584
610
  this.scrollToEnd({ behavior: anchor.behavior });
585
611
  }
586
612
  }
613
+ this._flushHoldIfReady(false);
587
614
  this._retryClampedAdjustment();
588
615
  };
616
+ this._classifyScrollEvent = (offset, isScrolling, elementMax) => {
617
+ var _a;
618
+ return scrollClassifier.classifyScrollEvent({
619
+ offset,
620
+ isScrolling,
621
+ elementMax,
622
+ holdOffset: this._holdOffset,
623
+ scrollOffset: this.scrollOffset,
624
+ pendingWrite: this.pendingWrite,
625
+ clampedMaxAtWrite: ((_a = this._clampedAdjustment) == null ? void 0 : _a.maxAtWrite) ?? null
626
+ });
627
+ };
589
628
  this._retryClampedAdjustment = () => {
590
629
  if (this._clampedAdjustment === null || !this.scrollElement || !this.options.enabled) {
591
630
  return;
@@ -594,36 +633,51 @@ class Virtualizer {
594
633
  const max = this.getMaxScrollOffset();
595
634
  if (max > maxAtWrite + 0.5) {
596
635
  this._clampedAdjustment = target > max + 0.5 ? { target, maxAtWrite: max } : null;
597
- this._scrollToOffset(target, {
598
- adjustments: void 0,
599
- behavior: void 0
600
- });
636
+ this._scrollToOffset(target);
601
637
  }
602
638
  };
603
- this._iosInMotion = () => this.isScrolling || this._iosTouch !== null;
604
- this._endIosTouch = () => {
605
- var _a;
606
- if (((_a = this._iosTouch) == null ? void 0 : _a.phase) === "tail" && this.targetWindow != null) {
607
- this.targetWindow.clearTimeout(this._iosTouch.timerId);
639
+ this._writeAwaitingCommand = () => {
640
+ const state = this.scrollState;
641
+ if (state === null || state.phase !== "queued") return;
642
+ const { behavior } = state;
643
+ this.getMeasurements();
644
+ const offset = this.getScrollStateTarget(state);
645
+ state.lastTargetOffset = offset;
646
+ this._clampedAdjustment = null;
647
+ state.phase = initialScrollPhase(
648
+ behavior,
649
+ state.index != null && utils.approxEqual(offset, this.getScrollOffset())
650
+ );
651
+ this._scrollToOffset(offset, { behavior });
652
+ this.scheduleScrollReconcile();
653
+ };
654
+ this._readElementOffset = () => {
655
+ const el = this.scrollElement;
656
+ if (el == null) return null;
657
+ const { horizontal, isRtl } = this.options;
658
+ if (typeof el.scrollTop === "number") {
659
+ return horizontal ? el.scrollLeft * (isRtl && -1 || 1) : el.scrollTop;
608
660
  }
609
- this._iosTouch = null;
661
+ if (typeof el.scrollY === "number") {
662
+ return horizontal ? el.scrollX : el.scrollY;
663
+ }
664
+ return null;
610
665
  };
611
- this._flushIosDeferredIfReady = () => {
612
- if (this._iosDeferredAdjustment === 0) return;
613
- if (this._iosInMotion()) return;
614
- const cur = this.getScrollOffset();
615
- const max = this.getMaxScrollOffset();
616
- if (cur < 0 || cur > max) return;
617
- if (this._iosDeferredAdjustment < 0 && cur >= max - 1) {
618
- this._iosDeferredAdjustment = 0;
619
- return;
666
+ this._canWriteScroll = () => !(iosGesture.isIOSWebKit() && this._iosGesture.inMotion()) && !this._reportedOverscroll;
667
+ this._flushHoldIfReady = (sync) => {
668
+ var _a;
669
+ if (this._holdOffset === 0) return;
670
+ if (!this.scrollElement || !this.options.enabled) return;
671
+ if (!this._canWriteScroll()) return;
672
+ if (((_a = this.scrollState) == null ? void 0 : _a.phase) === "smooth") return;
673
+ this._holdOffset = 0;
674
+ if (this.scrollOffset !== null && this.scrollOffset < 0) {
675
+ this.scrollOffset = 0;
620
676
  }
621
- const delta = this._iosDeferredAdjustment;
622
- this._iosDeferredAdjustment = 0;
623
- this._scrollToOffset(cur, {
624
- adjustments: this.scrollAdjustments + delta,
625
- behavior: void 0
626
- });
677
+ this.notify(sync);
678
+ const target = this.getCommandWrittenOffset() ?? this.getScrollOffset();
679
+ this._clampedAdjustment = this._clampedBy(target);
680
+ this._scrollToOffset(target);
627
681
  };
628
682
  this.rafId = null;
629
683
  this.getSize = () => {
@@ -966,14 +1020,14 @@ class Virtualizer {
966
1020
  const delta = size - itemSize;
967
1021
  if (delta !== 0) {
968
1022
  const wasAtEnd = this.options.anchorTo === "end" && !isSmoothScroll(this.scrollState) && this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold;
969
- const prevTotalSize = wasAtEnd ? this.getTotalSize() : 0;
970
- const scrollOffsetWithAdj = this.getScrollOffset() + this.scrollAdjustments;
1023
+ const prevTotalSize = wasAtEnd ? this._getLayoutTotalSize() : 0;
1024
+ const scrollOffset = this.getScrollOffset();
971
1025
  const isFirstMeasure = !this.itemSizeCache.has(key);
972
1026
  const defaultShouldAdjust = isFirstMeasure ? (
973
1027
  // First measurement: compensate any item whose top sits above the
974
1028
  // fold — the estimate→actual delta must be corrected regardless of
975
1029
  // scroll direction, since the whole estimated block was above it.
976
- itemStart < scrollOffsetWithAdj
1030
+ itemStart < scrollOffset
977
1031
  ) : (
978
1032
  // Re-measurement: only compensate an item that is ENTIRELY above the
979
1033
  // fold. An item that merely *spans* the fold (top above, bottom
@@ -982,7 +1036,7 @@ class Virtualizer {
982
1036
  // delta would drag the viewport downward on every growth (#1218).
983
1037
  // Also skip during backward scroll to avoid the "items jump while
984
1038
  // scrolling up" cascade.
985
- itemStart + itemSize <= scrollOffsetWithAdj && this.scrollDirection !== "backward"
1039
+ itemStart + itemSize <= scrollOffset && this.scrollDirection !== "backward"
986
1040
  );
987
1041
  const shouldAdjustScroll = !isSmoothScroll(this.scrollState) && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
988
1042
  // The callback expects a VirtualItem; build one lazily only
@@ -1003,16 +1057,11 @@ class Virtualizer {
1003
1057
  }
1004
1058
  this.itemSizeCache.set(key, size);
1005
1059
  this.itemSizeCacheVersion++;
1006
- const writtenOffset = this.getCommandWrittenOffset();
1007
- const awaitingCommandReadBack = writtenOffset !== null && !utils.approxEqual(
1008
- writtenOffset,
1009
- this.getScrollOffset() + this.scrollAdjustments
1010
- );
1011
1060
  let adjustedSync = false;
1012
- if (!awaitingCommandReadBack) {
1061
+ if (!this._awaitingCommandReadBack()) {
1013
1062
  if (wasAtEnd) {
1014
1063
  adjustedSync = this.applyScrollAdjustment(
1015
- this.getTotalSize() - prevTotalSize
1064
+ this._getLayoutTotalSize() - prevTotalSize
1016
1065
  );
1017
1066
  } else if (shouldAdjustScroll) {
1018
1067
  adjustedSync = this.applyScrollAdjustment(delta);
@@ -1024,17 +1073,25 @@ class Virtualizer {
1024
1073
  return;
1025
1074
  }
1026
1075
  this.notify(adjustedSync);
1076
+ this._noteClampMax();
1077
+ this._flushHoldIfReady(true);
1027
1078
  this._retryClampedAdjustment();
1028
1079
  }
1029
1080
  };
1030
1081
  this.getVirtualItems = utils.memo(
1031
- () => [this.getVirtualIndexes(), this.getMeasurements()],
1032
- (indexes, measurements) => {
1082
+ () => [this.getVirtualIndexes(), this.getMeasurements(), this._holdOffset],
1083
+ (indexes, measurements, hold) => {
1033
1084
  const virtualItems = [];
1034
1085
  for (let k = 0, len = indexes.length; k < len; k++) {
1035
1086
  const i = indexes[k];
1036
1087
  const measurement = measurements[i];
1037
- virtualItems.push(measurement);
1088
+ virtualItems.push(
1089
+ hold === 0 ? measurement : {
1090
+ ...measurement,
1091
+ start: measurement.start - hold,
1092
+ end: measurement.end - hold
1093
+ }
1094
+ );
1038
1095
  }
1039
1096
  return virtualItems;
1040
1097
  },
@@ -1059,18 +1116,83 @@ class Virtualizer {
1059
1116
  );
1060
1117
  return utils.notUndefined(measurements[idx]);
1061
1118
  };
1062
- this.getMaxScrollOffset = () => {
1063
- if (!this.scrollElement) return 0;
1064
- if ("scrollHeight" in this.scrollElement) {
1065
- return this.options.horizontal ? this.scrollElement.scrollWidth - this.scrollElement.clientWidth : this.scrollElement.scrollHeight - this.scrollElement.clientHeight;
1066
- } else {
1067
- const doc = this.scrollElement.document.documentElement;
1068
- return this.options.horizontal ? doc.scrollWidth - this.scrollElement.innerWidth : doc.scrollHeight - this.scrollElement.innerHeight;
1119
+ this._noteClampMax = () => {
1120
+ if (!this._awaitingCommandReadBack()) return;
1121
+ const elementMax = this._elementMaxScrollOffset();
1122
+ if (elementMax === null) return;
1123
+ const write = this.pendingWrite;
1124
+ const max = Math.max(elementMax, 0) + this._holdOffset;
1125
+ if (max >= write.target) return;
1126
+ const lowMaxes = write.lowMaxes ?? (write.lowMaxes = []);
1127
+ if (lowMaxes[lowMaxes.length - 1] !== max) lowMaxes.push(max);
1128
+ };
1129
+ this.getMaxScrollOffset = () => (this._elementMaxScrollOffset() ?? 0) + this._holdOffset;
1130
+ this._toLayoutOffset = (elementOffset) => {
1131
+ var _a;
1132
+ let offset = elementOffset + this._holdOffset;
1133
+ if (((_a = this.pendingScrollAnchor) == null ? void 0 : _a.kind) === "anchor") {
1134
+ offset += this.pendingScrollAnchor.delta;
1135
+ }
1136
+ return offset;
1137
+ };
1138
+ this._elementMaxScrollOffset = () => {
1139
+ const el = this.scrollElement;
1140
+ if (el === null) return null;
1141
+ if ("scrollHeight" in el) {
1142
+ return this.options.horizontal ? el.scrollWidth - el.clientWidth : el.scrollHeight - el.clientHeight;
1069
1143
  }
1144
+ if ("document" in el) {
1145
+ const doc = el.document.documentElement;
1146
+ return this.options.horizontal ? doc.scrollWidth - el.innerWidth : doc.scrollHeight - el.innerHeight;
1147
+ }
1148
+ return null;
1149
+ };
1150
+ this._momentumStop = null;
1151
+ this._stopMomentum = (done) => {
1152
+ const el = this.scrollElement;
1153
+ const win = this.targetWindow;
1154
+ if (el === null || win === null) return;
1155
+ const scroller = "document" in el ? el.document.scrollingElement : el;
1156
+ if (!(scroller == null ? void 0 : scroller.style)) return;
1157
+ if (this._momentumStop) {
1158
+ this._momentumStop.done = done;
1159
+ return;
1160
+ }
1161
+ const { style } = scroller;
1162
+ const property = this.options.horizontal ? "overflow-x" : "overflow-y";
1163
+ const value = style.getPropertyValue(property);
1164
+ const priority = style.getPropertyPriority(property);
1165
+ style.setProperty(property, "hidden", "important");
1166
+ const stop = {
1167
+ // False when the app replaced the hidden value: the fling may not
1168
+ // have stopped.
1169
+ restore: () => {
1170
+ if (style.getPropertyValue(property) !== "hidden" || style.getPropertyPriority(property) !== "important") {
1171
+ return false;
1172
+ }
1173
+ style.setProperty(property, value, priority);
1174
+ return true;
1175
+ },
1176
+ done
1177
+ };
1178
+ this._momentumStop = stop;
1179
+ win.requestAnimationFrame(
1180
+ () => win.requestAnimationFrame(() => {
1181
+ if (this._momentumStop !== stop) return;
1182
+ this._momentumStop = null;
1183
+ if (stop.restore()) stop.done();
1184
+ })
1185
+ );
1186
+ };
1187
+ this._clampedBy = (target) => {
1188
+ const elementMax = this._elementMaxScrollOffset();
1189
+ if (elementMax === null) return null;
1190
+ const maxAtWrite = elementMax + this._holdOffset;
1191
+ return target > maxAtWrite + 0.5 ? { target, maxAtWrite } : null;
1070
1192
  };
1071
1193
  this.getVirtualDistanceFromEnd = () => {
1072
1194
  return Math.max(
1073
- this.getTotalSize() - this.getSize() - this.getScrollOffset(),
1195
+ this._getLayoutTotalSize() - this.getSize() - this.getScrollOffset(),
1074
1196
  0
1075
1197
  );
1076
1198
  };
@@ -1135,10 +1257,7 @@ class Virtualizer {
1135
1257
  } = {}) => {
1136
1258
  index = Math.max(0, Math.min(index, this.options.count - 1));
1137
1259
  const offsetInfo = this.getOffsetForIndex(index, initialAlign);
1138
- if (!offsetInfo) {
1139
- this._iosDeferredAdjustment = 0;
1140
- return;
1141
- }
1260
+ if (!offsetInfo) return;
1142
1261
  const [offset, align] = offsetInfo;
1143
1262
  this._startScroll({ index, align }, offset, behavior, "absolute");
1144
1263
  };
@@ -1175,7 +1294,8 @@ class Virtualizer {
1175
1294
  }
1176
1295
  this.scrollState = null;
1177
1296
  };
1178
- this.getTotalSize = () => {
1297
+ this.getTotalSize = () => Math.max(this._getLayoutTotalSize() - this._holdOffset, 0);
1298
+ this._getLayoutTotalSize = () => {
1179
1299
  var _a, _b;
1180
1300
  const measurements = this.getMeasurements();
1181
1301
  let end;
@@ -1227,15 +1347,29 @@ class Virtualizer {
1227
1347
  this._scrollToOffset = (offset, {
1228
1348
  adjustments,
1229
1349
  behavior
1230
- }) => {
1231
- this.pendingWrite = {
1350
+ } = {}) => {
1351
+ const write = {
1232
1352
  target: offset + (adjustments ?? 0),
1233
- smooth: behavior === "smooth",
1234
- adjustments: adjustments ?? this.scrollAdjustments
1353
+ smooth: behavior === "smooth"
1235
1354
  };
1236
- this.options.scrollToFn(offset, { behavior, adjustments }, this);
1355
+ this.pendingWrite = write;
1356
+ this.options.scrollToFn(
1357
+ offset - this._holdOffset,
1358
+ { behavior, adjustments },
1359
+ this
1360
+ );
1361
+ return write;
1237
1362
  };
1238
1363
  this.getCommandWrittenOffset = () => this.scrollState !== null && this.pendingWrite !== null && !this.pendingWrite.smooth ? this.pendingWrite.target : null;
1364
+ this._awaitingCommandReadBack = () => {
1365
+ const writtenOffset = this.getCommandWrittenOffset();
1366
+ return !(iosGesture.isIOSWebKit() && this._iosGesture.dragging()) && writtenOffset !== null && !utils.approxEqual(writtenOffset, this.getScrollOffset());
1367
+ };
1368
+ this._followOffsetTarget = (delta) => {
1369
+ if (this.scrollState !== null && this.scrollState.index == null) {
1370
+ this.scrollState.lastTargetOffset += delta;
1371
+ }
1372
+ };
1239
1373
  this.measure = () => {
1240
1374
  this.pendingMin = null;
1241
1375
  this.itemSizeCache.clear();
@@ -1245,41 +1379,38 @@ class Virtualizer {
1245
1379
  };
1246
1380
  this.setOptions(opts);
1247
1381
  }
1248
- // Size compensation written to the element but not yet reflected in
1249
- // `scrollOffset`: the element is expected at `scrollOffset +
1250
- // scrollAdjustments` until the next scroll event reports it.
1382
+ /**
1383
+ * @deprecated Always 0: a scroll correction is folded into `scrollOffset`
1384
+ * at once, so there is never an outstanding adjustment to add to it. Read
1385
+ * `scrollOffset` alone.
1386
+ */
1251
1387
  get scrollAdjustments() {
1252
- var _a;
1253
- return ((_a = this.pendingWrite) == null ? void 0 : _a.adjustments) ?? 0;
1388
+ return 0;
1254
1389
  }
1255
1390
  // Returns `true` when it performed a synchronous `scrollTop` write this
1256
1391
  // tick, `false` when the delta was zero or the write was deferred (iOS).
1257
1392
  // `resizeItem` uses that to decide whether the follow-up `notify` must be
1258
1393
  // synchronous so the grown transforms commit in the same paint (#1227).
1259
1394
  applyScrollAdjustment(delta, behavior) {
1260
- var _a;
1261
1395
  if (delta === 0) return false;
1262
1396
  if (process.env.NODE_ENV !== "production" && this.options.debug) {
1263
1397
  console.info("correction", delta);
1264
1398
  }
1265
- if (isIOSWebKit() && this._iosInMotion()) {
1266
- if (((_a = this.scrollState) == null ? void 0 : _a.index) == null) {
1267
- this._iosDeferredAdjustment += delta;
1268
- }
1269
- return false;
1399
+ this._followOffsetTarget(delta);
1400
+ if (!this._canWriteScroll()) {
1401
+ this._holdOffset += delta;
1402
+ if (this.scrollOffset !== null) this.scrollOffset += delta;
1403
+ return true;
1270
1404
  } else {
1271
- const target = this.getScrollOffset() + this.scrollAdjustments + delta;
1272
- const el = this.scrollElement;
1273
- const maxAtWrite = el !== null && ("scrollHeight" in el || "document" in el) ? this.getMaxScrollOffset() : null;
1274
- this._clampedAdjustment = maxAtWrite !== null && target > maxAtWrite + 0.5 ? { target, maxAtWrite } : null;
1275
- this._scrollToOffset(this.getScrollOffset(), {
1276
- adjustments: this.scrollAdjustments + delta,
1405
+ const offset = this.getScrollOffset();
1406
+ this._clampedAdjustment = this._clampedBy(offset + delta);
1407
+ const write = this._scrollToOffset(offset, {
1408
+ adjustments: delta,
1277
1409
  behavior
1278
1410
  });
1279
1411
  if (this.scrollOffset !== null) {
1280
- this.scrollOffset += this.scrollAdjustments;
1412
+ if (this.pendingWrite === write) this.scrollOffset += delta;
1281
1413
  if (this.scrollOffset < 0) this.scrollOffset = 0;
1282
- if (this.pendingWrite) this.pendingWrite.adjustments = 0;
1283
1414
  }
1284
1415
  return true;
1285
1416
  }
@@ -1308,6 +1439,10 @@ class Virtualizer {
1308
1439
  this.scrollState = null;
1309
1440
  return;
1310
1441
  }
1442
+ if (this.scrollState.phase === "queued") {
1443
+ this.scheduleScrollReconcile();
1444
+ return;
1445
+ }
1311
1446
  this.getMeasurements();
1312
1447
  const targetOffset = this.getScrollStateTarget(this.scrollState);
1313
1448
  const STABLE_FRAMES = this.scrollState.index == null ? 1 : this.options.useAnimationFrameWithResizeObserver ? 3 : 2;
@@ -1321,10 +1456,7 @@ class Virtualizer {
1321
1456
  this.scrollState.stableFrames++;
1322
1457
  if (this.scrollState.stableFrames >= STABLE_FRAMES) {
1323
1458
  if (this.getScrollOffset() !== targetOffset) {
1324
- this._scrollToOffset(targetOffset, {
1325
- adjustments: void 0,
1326
- behavior: "auto"
1327
- });
1459
+ this._scrollToOffset(targetOffset, { behavior: "auto" });
1328
1460
  }
1329
1461
  this.scrollState = null;
1330
1462
  return;
@@ -1344,7 +1476,6 @@ class Virtualizer {
1344
1476
  this.scrollState.phase = "instant";
1345
1477
  }
1346
1478
  this._scrollToOffset(targetOffset, {
1347
- adjustments: void 0,
1348
1479
  behavior: keepSmooth ? "smooth" : "auto"
1349
1480
  });
1350
1481
  }
@@ -1354,31 +1485,58 @@ class Virtualizer {
1354
1485
  // Every scroll command starts here: it becomes the in-flight command,
1355
1486
  // writes its first target and leaves reconcile to retarget and retire it.
1356
1487
  //
1357
- // An absolute command derives its target from current measurements, so
1358
- // any iOS-deferred compensation still pending is stale by definition —
1359
- // the command already accounts for the measurements the delta was
1360
- // compensating for. Drop it so _flushIosDeferredIfReady doesn't replay it
1361
- // onto the just-established position. A relative command (scrollBy)
1362
- // builds on the current offset, so it keeps the deferral.
1488
+ // Targets are layout offsets. A held correction is not dropped by a
1489
+ // command: the write lands the element `_holdOffset` short of the target,
1490
+ // under the rows exposed shifted by the same amount, and the flush moves
1491
+ // both together once a write is possible.
1363
1492
  //
1364
1493
  // An index or end target that is already at its offset starts a smooth
1365
1494
  // scroll as landed; an offset target never does, so its first scroll
1366
1495
  // event is read as the user's (see the scroll handler in `_willUpdate`).
1367
1496
  _startScroll(target, offset, behavior, kind) {
1368
- if (kind === "absolute") {
1369
- this._iosDeferredAdjustment = 0;
1497
+ const tail = this._iosGesture.tail();
1498
+ if (kind === "absolute" && tail !== null) {
1499
+ if (tail === "fling" && iosGesture.iosMomentumOverridesWrites()) {
1500
+ this._stopMomentum(() => {
1501
+ const state = this.scrollState;
1502
+ if ((state == null ? void 0 : state.phase) !== "queued" || // A finger came down: it owns the scroll until it lifts.
1503
+ this._iosGesture.tail() !== "fling" || // In a bounce WebKit can drop the write (#1287): the tail
1504
+ // decides, as without the stop. (Not `flingCaught`: our stop
1505
+ // also ends the fling at speed, so a slow frame would read as
1506
+ // a catch and the tail would drop the command.)
1507
+ this._reportedOverscroll) {
1508
+ return;
1509
+ }
1510
+ this._iosGesture.end();
1511
+ this._flushHoldIfReady(true);
1512
+ this._writeAwaitingCommand();
1513
+ });
1514
+ this.scrollState = {
1515
+ ...target,
1516
+ startedAt: this.now(),
1517
+ lastTargetOffset: offset,
1518
+ stableFrames: 0,
1519
+ phase: "queued",
1520
+ behavior,
1521
+ samples: []
1522
+ };
1523
+ this.scheduleScrollReconcile();
1524
+ return;
1525
+ }
1526
+ this._iosGesture.end();
1370
1527
  }
1371
1528
  this.scrollState = {
1372
1529
  ...target,
1373
1530
  startedAt: this.now(),
1374
1531
  lastTargetOffset: offset,
1375
1532
  stableFrames: 0,
1533
+ behavior,
1376
1534
  phase: initialScrollPhase(
1377
1535
  behavior,
1378
1536
  target.index != null && utils.approxEqual(offset, this.getScrollOffset())
1379
1537
  )
1380
1538
  };
1381
- this._scrollToOffset(offset, { adjustments: void 0, behavior });
1539
+ this._scrollToOffset(offset, { behavior });
1382
1540
  this.scheduleScrollReconcile();
1383
1541
  }
1384
1542
  }
@@ -1462,8 +1620,8 @@ exports.approxEqual = utils.approxEqual;
1462
1620
  exports.debounce = utils.debounce;
1463
1621
  exports.memo = utils.memo;
1464
1622
  exports.notUndefined = utils.notUndefined;
1623
+ exports._resetIOSDetectionForTests = iosGesture._resetIOSDetectionForTests;
1465
1624
  exports.Virtualizer = Virtualizer;
1466
- exports._resetIOSDetectionForTests = _resetIOSDetectionForTests;
1467
1625
  exports.defaultKeyExtractor = defaultKeyExtractor;
1468
1626
  exports.defaultRangeExtractor = defaultRangeExtractor;
1469
1627
  exports.elementScroll = elementScroll;