@tanstack/virtual-core 3.19.4 → 3.19.6

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,20 +2,11 @@
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
- const { offsetWidth, offsetHeight } = element;
18
- return { width: offsetWidth, height: offsetHeight };
8
+ const { clientWidth, clientHeight } = element;
9
+ return { width: clientWidth, height: clientHeight };
19
10
  };
20
11
  const createFrameQueue = (targetWindow) => {
21
12
  const ids = /* @__PURE__ */ new Set();
@@ -64,21 +55,13 @@ const observeElementRect = (instance, cb) => {
64
55
  };
65
56
  }
66
57
  const frames = createFrameQueue(targetWindow);
67
- const observer = new targetWindow.ResizeObserver((entries) => {
58
+ const observer = new targetWindow.ResizeObserver(() => {
68
59
  const run = () => {
69
- const entry = entries[0];
70
- if (entry == null ? void 0 : entry.borderBoxSize) {
71
- const box = entry.borderBoxSize[0];
72
- if (box) {
73
- handler({ width: box.inlineSize, height: box.blockSize });
74
- return;
75
- }
76
- }
77
60
  handler(getRect(element));
78
61
  };
79
62
  instance.options.useAnimationFrameWithResizeObserver ? frames.schedule(run) : run();
80
63
  });
81
- observer.observe(element, { box: "border-box" });
64
+ observer.observe(element, { box: "content-box" });
82
65
  return () => {
83
66
  observer.unobserve(element);
84
67
  frames.cancel();
@@ -185,7 +168,6 @@ const windowScroll = scrollWithAdjustments;
185
168
  const elementScroll = scrollWithAdjustments;
186
169
  const initialScrollPhase = (behavior, atTarget) => behavior === "smooth" ? atTarget ? "smoothLanded" : "smooth" : "instant";
187
170
  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
171
  function isAppendWithTrim(prevCount, nextCount, getPreviousKey, getNextKey) {
190
172
  if (nextCount === 0) return false;
191
173
  const firstKey = getNextKey(0);
@@ -227,8 +209,40 @@ class Virtualizer {
227
209
  this.scrollRect = null;
228
210
  this.scrollOffset = null;
229
211
  this.scrollDirection = null;
230
- this._iosDeferredAdjustment = 0;
231
- this._iosTouch = null;
212
+ this._holdOffset = 0;
213
+ this._reportedOverscroll = false;
214
+ this._iosGesture = new iosGesture.IosGesture({
215
+ getWindow: () => this.targetWindow,
216
+ now: () => this.now(),
217
+ // The end of a fling can move less than a pixel per scroll event, so
218
+ // events can stop for 150ms while the fling is still running (seen on
219
+ // an iPhone, 27.0.1). A command waiting for it would then be written
220
+ // into the fling and overridden. The element having moved since the
221
+ // last event means it has not ended: wait again. Not while our own
222
+ // write has yet to report: that moved it.
223
+ tailStillMoving: () => {
224
+ const state = this.scrollState;
225
+ if ((state == null ? void 0 : state.phase) !== "queued" || this.pendingWrite !== null) {
226
+ return false;
227
+ }
228
+ const now = this._readElementOffset();
229
+ if (now === null || this._lastElementOffset === null || Math.abs(now - this._lastElementOffset) < 1) {
230
+ return false;
231
+ }
232
+ this._lastElementOffset = now;
233
+ state.samples = [...state.samples ?? [], { t: this.now(), offset: now }];
234
+ return true;
235
+ },
236
+ onTailClosed: () => {
237
+ const state = this.scrollState;
238
+ if ((state == null ? void 0 : state.phase) === "queued" && scrollClassifier.flingCaught(state.samples ?? [])) {
239
+ this.scrollState = null;
240
+ }
241
+ this._flushHoldIfReady(true);
242
+ this._writeAwaitingCommand();
243
+ }
244
+ });
245
+ this._lastElementOffset = null;
232
246
  this.pendingWrite = null;
233
247
  this._clampedAdjustment = null;
234
248
  this.elementsCache = /* @__PURE__ */ new Map();
@@ -259,6 +273,8 @@ class Virtualizer {
259
273
  this._resizeBatch = null;
260
274
  if (batch.notify) {
261
275
  this.notify(batch.sync);
276
+ this._noteClampMax();
277
+ this._flushHoldIfReady(true);
262
278
  this._retryClampedAdjustment();
263
279
  }
264
280
  }
@@ -379,7 +395,7 @@ class Virtualizer {
379
395
  this.itemSizeCacheVersion++;
380
396
  }
381
397
  let anchorDelta = null;
382
- if (anchor && this.scrollOffset !== null) {
398
+ if (anchor && this.scrollOffset !== null && !this._awaitingCommandReadBack()) {
383
399
  const [anchorKey, anchorOffset] = anchor;
384
400
  const newMeasurements = this.getMeasurements();
385
401
  const { count, getItemKey } = this.options;
@@ -401,7 +417,12 @@ class Virtualizer {
401
417
  if (followOnAppend) {
402
418
  this.pendingScrollAnchor = { kind: "follow", behavior: followOnAppend };
403
419
  } else if (anchorDelta !== null) {
404
- this.pendingScrollAnchor = { kind: "anchor", delta: anchorDelta };
420
+ if (this._canWriteScroll()) {
421
+ this.pendingScrollAnchor = { kind: "anchor", delta: anchorDelta };
422
+ } else {
423
+ this._holdOffset += anchorDelta;
424
+ this._followOffsetTarget(anchorDelta);
425
+ }
405
426
  }
406
427
  };
407
428
  this.notify = (sync) => {
@@ -431,6 +452,7 @@ class Virtualizer {
431
452
  }
432
453
  );
433
454
  this.cleanup = () => {
455
+ var _a;
434
456
  this.unsubs.filter(Boolean).forEach((d) => d());
435
457
  this.unsubs = [];
436
458
  this.observer.disconnect();
@@ -439,10 +461,12 @@ class Virtualizer {
439
461
  this.rafId = null;
440
462
  }
441
463
  this.scrollState = null;
464
+ (_a = this._momentumStop) == null ? void 0 : _a.restore();
465
+ this._momentumStop = null;
442
466
  this.isScrolling = false;
443
467
  this.scrollDirection = null;
444
- this._iosDeferredAdjustment = 0;
445
- this._endIosTouch();
468
+ this._holdOffset = 0;
469
+ this._reportedOverscroll = false;
446
470
  this._clampedAdjustment = null;
447
471
  this.pendingWrite = null;
448
472
  this.scrollElement = null;
@@ -479,27 +503,30 @@ class Virtualizer {
479
503
  );
480
504
  this.unsubs.push(
481
505
  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)) {
506
+ var _a2, _b, _c, _d;
507
+ const elementMax = isScrolling ? this._elementMaxScrollOffset() : null;
508
+ const wasOverscrolled = this._reportedOverscroll;
509
+ this._reportedOverscroll = offset < 0 || elementMax !== null && offset > elementMax + 1;
510
+ const elementOffset = offset;
511
+ this._lastElementOffset = elementOffset;
512
+ offset = this._toLayoutOffset(offset);
513
+ const kind = this._classifyScrollEvent(
514
+ offset,
515
+ isScrolling,
516
+ elementMax
517
+ );
518
+ if (kind === "spurious") return;
519
+ if (kind === "readBack") {
520
+ offset = this.pendingWrite.target;
521
+ } else if (kind === "clampedReadBack" && ((_a2 = this.scrollState) == null ? void 0 : _a2.index) != null && // Not `elementMax`: an adapter can report the read-back at rest.
522
+ offset < this.getMaxScrollOffset() - 1.5) {
523
+ this.scrollState.lastTargetOffset = offset;
524
+ } else if (scrollClassifier.externalRetiresCommand({
525
+ kind,
526
+ offset,
527
+ command: this.scrollState,
528
+ target: () => this.getScrollStateTarget(this.scrollState)
529
+ })) {
503
530
  this.scrollState = null;
504
531
  }
505
532
  this.pendingWrite = null;
@@ -507,10 +534,40 @@ class Virtualizer {
507
534
  this._clampedAdjustment = null;
508
535
  }
509
536
  const prevOffset = this.getScrollOffset();
537
+ const prevDirection = this.scrollDirection;
510
538
  this.scrollDirection = isScrolling ? prevOffset === offset ? this.scrollDirection : prevOffset < offset ? "forward" : "backward" : null;
511
539
  this.scrollOffset = offset;
512
540
  this.isScrolling = isScrolling;
513
- this._flushIosDeferredIfReady();
541
+ const queued = this.scrollState;
542
+ if ((queued == null ? void 0 : queued.phase) === "queued" && kind === "external" && isScrolling) {
543
+ if (wasOverscrolled || this._reportedOverscroll) {
544
+ queued.samples = [];
545
+ } else {
546
+ const t = this.now();
547
+ queued.samples = [
548
+ ...(queued.samples ?? []).filter(
549
+ (sample) => t - sample.t <= 200
550
+ ),
551
+ { t, offset: elementOffset }
552
+ ];
553
+ if (prevDirection !== null && this.scrollDirection !== prevDirection || scrollClassifier.flingSpedUp(queued.samples)) {
554
+ this.scrollState = null;
555
+ }
556
+ }
557
+ }
558
+ if (this._iosGesture.onScroll({
559
+ isScrolling,
560
+ ownReadBack: kind === "readBack",
561
+ moved: offset !== prevOffset,
562
+ // Neither our write's read-back nor a frame of our smooth
563
+ // animation: the user or a fling moved it.
564
+ external: kind === "external" && ((_b = this.scrollState) == null ? void 0 : _b.phase) !== "smooth" && ((_c = this.scrollState) == null ? void 0 : _c.phase) !== "smoothFinishing"
565
+ }) && // The user scrolls: a command still waiting for the tail is
566
+ // theirs to override, like a written one would be.
567
+ ((_d = this.scrollState) == null ? void 0 : _d.phase) === "queued") {
568
+ this.scrollState = null;
569
+ }
570
+ this._flushHoldIfReady(true);
514
571
  if (this.scrollState) {
515
572
  this.scheduleScrollReconcile();
516
573
  }
@@ -518,74 +575,48 @@ class Virtualizer {
518
575
  })
519
576
  );
520
577
  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
578
+ this.unsubs.push(
579
+ this._iosGesture.listen(this.scrollElement)
544
580
  );
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
581
  }
562
- this._scrollToOffset(this.getScrollOffset(), {
563
- adjustments: void 0,
564
- behavior: void 0
565
- });
582
+ this._scrollToOffset(this.getScrollOffset());
566
583
  }
584
+ this._noteClampMax();
567
585
  const anchor = this.pendingScrollAnchor;
568
586
  this.pendingScrollAnchor = null;
569
587
  if (anchor && this.scrollElement && this.options.enabled) {
570
588
  if (anchor.kind === "anchor") {
571
- if (isIOSWebKit() && this._iosInMotion()) {
572
- this._iosDeferredAdjustment += anchor.delta;
589
+ if (!this._canWriteScroll()) {
590
+ this._holdOffset += anchor.delta;
591
+ this._followOffsetTarget(anchor.delta);
592
+ this.notify(false);
573
593
  } else if (isSmoothScroll(this.scrollState) && !utils.approxEqual(
574
594
  this.getScrollOffset() - anchor.delta,
575
595
  this.scrollState.lastTargetOffset
576
596
  )) ;
577
597
  else {
578
- this._scrollToOffset(this.getScrollOffset(), {
579
- adjustments: void 0,
580
- behavior: void 0
581
- });
598
+ this._followOffsetTarget(anchor.delta);
599
+ this._scrollToOffset(this.getScrollOffset());
582
600
  }
583
601
  } else {
584
602
  this.scrollToEnd({ behavior: anchor.behavior });
585
603
  }
586
604
  }
605
+ this._flushHoldIfReady(false);
587
606
  this._retryClampedAdjustment();
588
607
  };
608
+ this._classifyScrollEvent = (offset, isScrolling, elementMax) => {
609
+ var _a;
610
+ return scrollClassifier.classifyScrollEvent({
611
+ offset,
612
+ isScrolling,
613
+ elementMax,
614
+ holdOffset: this._holdOffset,
615
+ scrollOffset: this.scrollOffset,
616
+ pendingWrite: this.pendingWrite,
617
+ clampedMaxAtWrite: ((_a = this._clampedAdjustment) == null ? void 0 : _a.maxAtWrite) ?? null
618
+ });
619
+ };
589
620
  this._retryClampedAdjustment = () => {
590
621
  if (this._clampedAdjustment === null || !this.scrollElement || !this.options.enabled) {
591
622
  return;
@@ -594,36 +625,51 @@ class Virtualizer {
594
625
  const max = this.getMaxScrollOffset();
595
626
  if (max > maxAtWrite + 0.5) {
596
627
  this._clampedAdjustment = target > max + 0.5 ? { target, maxAtWrite: max } : null;
597
- this._scrollToOffset(target, {
598
- adjustments: void 0,
599
- behavior: void 0
600
- });
628
+ this._scrollToOffset(target);
601
629
  }
602
630
  };
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);
631
+ this._writeAwaitingCommand = () => {
632
+ const state = this.scrollState;
633
+ if (state === null || state.phase !== "queued") return;
634
+ const { behavior } = state;
635
+ this.getMeasurements();
636
+ const offset = this.getScrollStateTarget(state);
637
+ state.lastTargetOffset = offset;
638
+ this._clampedAdjustment = null;
639
+ state.phase = initialScrollPhase(
640
+ behavior,
641
+ state.index != null && utils.approxEqual(offset, this.getScrollOffset())
642
+ );
643
+ this._scrollToOffset(offset, { behavior });
644
+ this.scheduleScrollReconcile();
645
+ };
646
+ this._readElementOffset = () => {
647
+ const el = this.scrollElement;
648
+ if (el == null) return null;
649
+ const { horizontal, isRtl } = this.options;
650
+ if (typeof el.scrollTop === "number") {
651
+ return horizontal ? el.scrollLeft * (isRtl && -1 || 1) : el.scrollTop;
652
+ }
653
+ if (typeof el.scrollY === "number") {
654
+ return horizontal ? el.scrollX : el.scrollY;
608
655
  }
609
- this._iosTouch = null;
656
+ return null;
610
657
  };
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;
658
+ this._canWriteScroll = () => !(iosGesture.isIOSWebKit() && this._iosGesture.inMotion()) && !this._reportedOverscroll;
659
+ this._flushHoldIfReady = (sync) => {
660
+ var _a;
661
+ if (this._holdOffset === 0) return;
662
+ if (!this.scrollElement || !this.options.enabled) return;
663
+ if (!this._canWriteScroll()) return;
664
+ if (((_a = this.scrollState) == null ? void 0 : _a.phase) === "smooth") return;
665
+ this._holdOffset = 0;
666
+ if (this.scrollOffset !== null && this.scrollOffset < 0) {
667
+ this.scrollOffset = 0;
620
668
  }
621
- const delta = this._iosDeferredAdjustment;
622
- this._iosDeferredAdjustment = 0;
623
- this._scrollToOffset(cur, {
624
- adjustments: this.scrollAdjustments + delta,
625
- behavior: void 0
626
- });
669
+ this.notify(sync);
670
+ const target = this.getCommandWrittenOffset() ?? this.getScrollOffset();
671
+ this._clampedAdjustment = this._clampedBy(target);
672
+ this._scrollToOffset(target);
627
673
  };
628
674
  this.rafId = null;
629
675
  this.getSize = () => {
@@ -966,14 +1012,14 @@ class Virtualizer {
966
1012
  const delta = size - itemSize;
967
1013
  if (delta !== 0) {
968
1014
  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;
1015
+ const prevTotalSize = wasAtEnd ? this._getLayoutTotalSize() : 0;
1016
+ const scrollOffset = this.getScrollOffset();
971
1017
  const isFirstMeasure = !this.itemSizeCache.has(key);
972
1018
  const defaultShouldAdjust = isFirstMeasure ? (
973
1019
  // First measurement: compensate any item whose top sits above the
974
1020
  // fold — the estimate→actual delta must be corrected regardless of
975
1021
  // scroll direction, since the whole estimated block was above it.
976
- itemStart < scrollOffsetWithAdj
1022
+ itemStart < scrollOffset
977
1023
  ) : (
978
1024
  // Re-measurement: only compensate an item that is ENTIRELY above the
979
1025
  // fold. An item that merely *spans* the fold (top above, bottom
@@ -982,7 +1028,7 @@ class Virtualizer {
982
1028
  // delta would drag the viewport downward on every growth (#1218).
983
1029
  // Also skip during backward scroll to avoid the "items jump while
984
1030
  // scrolling up" cascade.
985
- itemStart + itemSize <= scrollOffsetWithAdj && this.scrollDirection !== "backward"
1031
+ itemStart + itemSize <= scrollOffset && this.scrollDirection !== "backward"
986
1032
  );
987
1033
  const shouldAdjustScroll = !isSmoothScroll(this.scrollState) && (this.shouldAdjustScrollPositionOnItemSizeChange !== void 0 ? this.shouldAdjustScrollPositionOnItemSizeChange(
988
1034
  // The callback expects a VirtualItem; build one lazily only
@@ -1003,16 +1049,11 @@ class Virtualizer {
1003
1049
  }
1004
1050
  this.itemSizeCache.set(key, size);
1005
1051
  this.itemSizeCacheVersion++;
1006
- const writtenOffset = this.getCommandWrittenOffset();
1007
- const awaitingCommandReadBack = writtenOffset !== null && !utils.approxEqual(
1008
- writtenOffset,
1009
- this.getScrollOffset() + this.scrollAdjustments
1010
- );
1011
1052
  let adjustedSync = false;
1012
- if (!awaitingCommandReadBack) {
1053
+ if (!this._awaitingCommandReadBack()) {
1013
1054
  if (wasAtEnd) {
1014
1055
  adjustedSync = this.applyScrollAdjustment(
1015
- this.getTotalSize() - prevTotalSize
1056
+ this._getLayoutTotalSize() - prevTotalSize
1016
1057
  );
1017
1058
  } else if (shouldAdjustScroll) {
1018
1059
  adjustedSync = this.applyScrollAdjustment(delta);
@@ -1024,17 +1065,25 @@ class Virtualizer {
1024
1065
  return;
1025
1066
  }
1026
1067
  this.notify(adjustedSync);
1068
+ this._noteClampMax();
1069
+ this._flushHoldIfReady(true);
1027
1070
  this._retryClampedAdjustment();
1028
1071
  }
1029
1072
  };
1030
1073
  this.getVirtualItems = utils.memo(
1031
- () => [this.getVirtualIndexes(), this.getMeasurements()],
1032
- (indexes, measurements) => {
1074
+ () => [this.getVirtualIndexes(), this.getMeasurements(), this._holdOffset],
1075
+ (indexes, measurements, hold) => {
1033
1076
  const virtualItems = [];
1034
1077
  for (let k = 0, len = indexes.length; k < len; k++) {
1035
1078
  const i = indexes[k];
1036
1079
  const measurement = measurements[i];
1037
- virtualItems.push(measurement);
1080
+ virtualItems.push(
1081
+ hold === 0 ? measurement : {
1082
+ ...measurement,
1083
+ start: measurement.start - hold,
1084
+ end: measurement.end - hold
1085
+ }
1086
+ );
1038
1087
  }
1039
1088
  return virtualItems;
1040
1089
  },
@@ -1059,18 +1108,83 @@ class Virtualizer {
1059
1108
  );
1060
1109
  return utils.notUndefined(measurements[idx]);
1061
1110
  };
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;
1111
+ this._noteClampMax = () => {
1112
+ if (!this._awaitingCommandReadBack()) return;
1113
+ const elementMax = this._elementMaxScrollOffset();
1114
+ if (elementMax === null) return;
1115
+ const write = this.pendingWrite;
1116
+ const max = Math.max(elementMax, 0) + this._holdOffset;
1117
+ if (max >= write.target) return;
1118
+ const lowMaxes = write.lowMaxes ?? (write.lowMaxes = []);
1119
+ if (lowMaxes[lowMaxes.length - 1] !== max) lowMaxes.push(max);
1120
+ };
1121
+ this.getMaxScrollOffset = () => (this._elementMaxScrollOffset() ?? 0) + this._holdOffset;
1122
+ this._toLayoutOffset = (elementOffset) => {
1123
+ var _a;
1124
+ let offset = elementOffset + this._holdOffset;
1125
+ if (((_a = this.pendingScrollAnchor) == null ? void 0 : _a.kind) === "anchor") {
1126
+ offset += this.pendingScrollAnchor.delta;
1069
1127
  }
1128
+ return offset;
1129
+ };
1130
+ this._elementMaxScrollOffset = () => {
1131
+ const el = this.scrollElement;
1132
+ if (el === null) return null;
1133
+ if ("scrollHeight" in el) {
1134
+ return this.options.horizontal ? el.scrollWidth - el.clientWidth : el.scrollHeight - el.clientHeight;
1135
+ }
1136
+ if ("document" in el) {
1137
+ const doc = el.document.documentElement;
1138
+ return this.options.horizontal ? doc.scrollWidth - el.innerWidth : doc.scrollHeight - el.innerHeight;
1139
+ }
1140
+ return null;
1141
+ };
1142
+ this._momentumStop = null;
1143
+ this._stopMomentum = (done) => {
1144
+ const el = this.scrollElement;
1145
+ const win = this.targetWindow;
1146
+ if (el === null || win === null) return;
1147
+ const scroller = "document" in el ? el.document.scrollingElement : el;
1148
+ if (!(scroller == null ? void 0 : scroller.style)) return;
1149
+ if (this._momentumStop) {
1150
+ this._momentumStop.done = done;
1151
+ return;
1152
+ }
1153
+ const { style } = scroller;
1154
+ const property = this.options.horizontal ? "overflow-x" : "overflow-y";
1155
+ const value = style.getPropertyValue(property);
1156
+ const priority = style.getPropertyPriority(property);
1157
+ style.setProperty(property, "hidden", "important");
1158
+ const stop = {
1159
+ // False when the app replaced the hidden value: the fling may not
1160
+ // have stopped.
1161
+ restore: () => {
1162
+ if (style.getPropertyValue(property) !== "hidden" || style.getPropertyPriority(property) !== "important") {
1163
+ return false;
1164
+ }
1165
+ style.setProperty(property, value, priority);
1166
+ return true;
1167
+ },
1168
+ done
1169
+ };
1170
+ this._momentumStop = stop;
1171
+ win.requestAnimationFrame(
1172
+ () => win.requestAnimationFrame(() => {
1173
+ if (this._momentumStop !== stop) return;
1174
+ this._momentumStop = null;
1175
+ if (stop.restore()) stop.done();
1176
+ })
1177
+ );
1178
+ };
1179
+ this._clampedBy = (target) => {
1180
+ const elementMax = this._elementMaxScrollOffset();
1181
+ if (elementMax === null) return null;
1182
+ const maxAtWrite = elementMax + this._holdOffset;
1183
+ return target > maxAtWrite + 0.5 ? { target, maxAtWrite } : null;
1070
1184
  };
1071
1185
  this.getVirtualDistanceFromEnd = () => {
1072
1186
  return Math.max(
1073
- this.getTotalSize() - this.getSize() - this.getScrollOffset(),
1187
+ this._getLayoutTotalSize() - this.getSize() - this.getScrollOffset(),
1074
1188
  0
1075
1189
  );
1076
1190
  };
@@ -1135,10 +1249,7 @@ class Virtualizer {
1135
1249
  } = {}) => {
1136
1250
  index = Math.max(0, Math.min(index, this.options.count - 1));
1137
1251
  const offsetInfo = this.getOffsetForIndex(index, initialAlign);
1138
- if (!offsetInfo) {
1139
- this._iosDeferredAdjustment = 0;
1140
- return;
1141
- }
1252
+ if (!offsetInfo) return;
1142
1253
  const [offset, align] = offsetInfo;
1143
1254
  this._startScroll({ index, align }, offset, behavior, "absolute");
1144
1255
  };
@@ -1175,7 +1286,8 @@ class Virtualizer {
1175
1286
  }
1176
1287
  this.scrollState = null;
1177
1288
  };
1178
- this.getTotalSize = () => {
1289
+ this.getTotalSize = () => Math.max(this._getLayoutTotalSize() - this._holdOffset, 0);
1290
+ this._getLayoutTotalSize = () => {
1179
1291
  var _a, _b;
1180
1292
  const measurements = this.getMeasurements();
1181
1293
  let end;
@@ -1227,15 +1339,29 @@ class Virtualizer {
1227
1339
  this._scrollToOffset = (offset, {
1228
1340
  adjustments,
1229
1341
  behavior
1230
- }) => {
1231
- this.pendingWrite = {
1342
+ } = {}) => {
1343
+ const write = {
1232
1344
  target: offset + (adjustments ?? 0),
1233
- smooth: behavior === "smooth",
1234
- adjustments: adjustments ?? this.scrollAdjustments
1345
+ smooth: behavior === "smooth"
1235
1346
  };
1236
- this.options.scrollToFn(offset, { behavior, adjustments }, this);
1347
+ this.pendingWrite = write;
1348
+ this.options.scrollToFn(
1349
+ offset - this._holdOffset,
1350
+ { behavior, adjustments },
1351
+ this
1352
+ );
1353
+ return write;
1237
1354
  };
1238
1355
  this.getCommandWrittenOffset = () => this.scrollState !== null && this.pendingWrite !== null && !this.pendingWrite.smooth ? this.pendingWrite.target : null;
1356
+ this._awaitingCommandReadBack = () => {
1357
+ const writtenOffset = this.getCommandWrittenOffset();
1358
+ return !(iosGesture.isIOSWebKit() && this._iosGesture.dragging()) && writtenOffset !== null && !utils.approxEqual(writtenOffset, this.getScrollOffset());
1359
+ };
1360
+ this._followOffsetTarget = (delta) => {
1361
+ if (this.scrollState !== null && this.scrollState.index == null) {
1362
+ this.scrollState.lastTargetOffset += delta;
1363
+ }
1364
+ };
1239
1365
  this.measure = () => {
1240
1366
  this.pendingMin = null;
1241
1367
  this.itemSizeCache.clear();
@@ -1245,41 +1371,38 @@ class Virtualizer {
1245
1371
  };
1246
1372
  this.setOptions(opts);
1247
1373
  }
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.
1374
+ /**
1375
+ * @deprecated Always 0: a scroll correction is folded into `scrollOffset`
1376
+ * at once, so there is never an outstanding adjustment to add to it. Read
1377
+ * `scrollOffset` alone.
1378
+ */
1251
1379
  get scrollAdjustments() {
1252
- var _a;
1253
- return ((_a = this.pendingWrite) == null ? void 0 : _a.adjustments) ?? 0;
1380
+ return 0;
1254
1381
  }
1255
1382
  // Returns `true` when it performed a synchronous `scrollTop` write this
1256
1383
  // tick, `false` when the delta was zero or the write was deferred (iOS).
1257
1384
  // `resizeItem` uses that to decide whether the follow-up `notify` must be
1258
1385
  // synchronous so the grown transforms commit in the same paint (#1227).
1259
1386
  applyScrollAdjustment(delta, behavior) {
1260
- var _a;
1261
1387
  if (delta === 0) return false;
1262
1388
  if (process.env.NODE_ENV !== "production" && this.options.debug) {
1263
1389
  console.info("correction", delta);
1264
1390
  }
1265
- if (isIOSWebKit() && this._iosInMotion()) {
1266
- if (((_a = this.scrollState) == null ? void 0 : _a.index) == null) {
1267
- this._iosDeferredAdjustment += delta;
1268
- }
1269
- return false;
1391
+ this._followOffsetTarget(delta);
1392
+ if (!this._canWriteScroll()) {
1393
+ this._holdOffset += delta;
1394
+ if (this.scrollOffset !== null) this.scrollOffset += delta;
1395
+ return true;
1270
1396
  } 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,
1397
+ const offset = this.getScrollOffset();
1398
+ this._clampedAdjustment = this._clampedBy(offset + delta);
1399
+ const write = this._scrollToOffset(offset, {
1400
+ adjustments: delta,
1277
1401
  behavior
1278
1402
  });
1279
1403
  if (this.scrollOffset !== null) {
1280
- this.scrollOffset += this.scrollAdjustments;
1404
+ if (this.pendingWrite === write) this.scrollOffset += delta;
1281
1405
  if (this.scrollOffset < 0) this.scrollOffset = 0;
1282
- if (this.pendingWrite) this.pendingWrite.adjustments = 0;
1283
1406
  }
1284
1407
  return true;
1285
1408
  }
@@ -1308,6 +1431,10 @@ class Virtualizer {
1308
1431
  this.scrollState = null;
1309
1432
  return;
1310
1433
  }
1434
+ if (this.scrollState.phase === "queued") {
1435
+ this.scheduleScrollReconcile();
1436
+ return;
1437
+ }
1311
1438
  this.getMeasurements();
1312
1439
  const targetOffset = this.getScrollStateTarget(this.scrollState);
1313
1440
  const STABLE_FRAMES = this.scrollState.index == null ? 1 : this.options.useAnimationFrameWithResizeObserver ? 3 : 2;
@@ -1321,10 +1448,7 @@ class Virtualizer {
1321
1448
  this.scrollState.stableFrames++;
1322
1449
  if (this.scrollState.stableFrames >= STABLE_FRAMES) {
1323
1450
  if (this.getScrollOffset() !== targetOffset) {
1324
- this._scrollToOffset(targetOffset, {
1325
- adjustments: void 0,
1326
- behavior: "auto"
1327
- });
1451
+ this._scrollToOffset(targetOffset, { behavior: "auto" });
1328
1452
  }
1329
1453
  this.scrollState = null;
1330
1454
  return;
@@ -1344,7 +1468,6 @@ class Virtualizer {
1344
1468
  this.scrollState.phase = "instant";
1345
1469
  }
1346
1470
  this._scrollToOffset(targetOffset, {
1347
- adjustments: void 0,
1348
1471
  behavior: keepSmooth ? "smooth" : "auto"
1349
1472
  });
1350
1473
  }
@@ -1354,31 +1477,58 @@ class Virtualizer {
1354
1477
  // Every scroll command starts here: it becomes the in-flight command,
1355
1478
  // writes its first target and leaves reconcile to retarget and retire it.
1356
1479
  //
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.
1480
+ // Targets are layout offsets. A held correction is not dropped by a
1481
+ // command: the write lands the element `_holdOffset` short of the target,
1482
+ // under the rows exposed shifted by the same amount, and the flush moves
1483
+ // both together once a write is possible.
1363
1484
  //
1364
1485
  // An index or end target that is already at its offset starts a smooth
1365
1486
  // scroll as landed; an offset target never does, so its first scroll
1366
1487
  // event is read as the user's (see the scroll handler in `_willUpdate`).
1367
1488
  _startScroll(target, offset, behavior, kind) {
1368
- if (kind === "absolute") {
1369
- this._iosDeferredAdjustment = 0;
1489
+ const tail = this._iosGesture.tail();
1490
+ if (kind === "absolute" && tail !== null) {
1491
+ if (tail === "fling" && iosGesture.iosMomentumOverridesWrites()) {
1492
+ this._stopMomentum(() => {
1493
+ const state = this.scrollState;
1494
+ if ((state == null ? void 0 : state.phase) !== "queued" || // A finger came down: it owns the scroll until it lifts.
1495
+ this._iosGesture.tail() !== "fling" || // In a bounce WebKit can drop the write (#1287): the tail
1496
+ // decides, as without the stop. (Not `flingCaught`: our stop
1497
+ // also ends the fling at speed, so a slow frame would read as
1498
+ // a catch and the tail would drop the command.)
1499
+ this._reportedOverscroll) {
1500
+ return;
1501
+ }
1502
+ this._iosGesture.end();
1503
+ this._flushHoldIfReady(true);
1504
+ this._writeAwaitingCommand();
1505
+ });
1506
+ this.scrollState = {
1507
+ ...target,
1508
+ startedAt: this.now(),
1509
+ lastTargetOffset: offset,
1510
+ stableFrames: 0,
1511
+ phase: "queued",
1512
+ behavior,
1513
+ samples: []
1514
+ };
1515
+ this.scheduleScrollReconcile();
1516
+ return;
1517
+ }
1518
+ this._iosGesture.end();
1370
1519
  }
1371
1520
  this.scrollState = {
1372
1521
  ...target,
1373
1522
  startedAt: this.now(),
1374
1523
  lastTargetOffset: offset,
1375
1524
  stableFrames: 0,
1525
+ behavior,
1376
1526
  phase: initialScrollPhase(
1377
1527
  behavior,
1378
1528
  target.index != null && utils.approxEqual(offset, this.getScrollOffset())
1379
1529
  )
1380
1530
  };
1381
- this._scrollToOffset(offset, { adjustments: void 0, behavior });
1531
+ this._scrollToOffset(offset, { behavior });
1382
1532
  this.scheduleScrollReconcile();
1383
1533
  }
1384
1534
  }
@@ -1462,8 +1612,8 @@ exports.approxEqual = utils.approxEqual;
1462
1612
  exports.debounce = utils.debounce;
1463
1613
  exports.memo = utils.memo;
1464
1614
  exports.notUndefined = utils.notUndefined;
1615
+ exports._resetIOSDetectionForTests = iosGesture._resetIOSDetectionForTests;
1465
1616
  exports.Virtualizer = Virtualizer;
1466
- exports._resetIOSDetectionForTests = _resetIOSDetectionForTests;
1467
1617
  exports.defaultKeyExtractor = defaultKeyExtractor;
1468
1618
  exports.defaultRangeExtractor = defaultRangeExtractor;
1469
1619
  exports.elementScroll = elementScroll;