@tanstack/virtual-core 3.17.8 → 3.17.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.ts CHANGED
@@ -447,6 +447,16 @@ export class Virtualizer<
447
447
  // value when the diff is < 1.5 px, distinguishing it from a real user
448
448
  // scroll. The +0.5 over Math.abs lets us also absorb the +1 / -1 cases.
449
449
  private _intendedScrollOffset: number | null = null
450
+ // A compensation write from `applyScrollAdjustment` whose target exceeded
451
+ // the element's scroll max at the moment of the write. The browser clamps
452
+ // such a write because the consumer's sizer has not grown yet: an
453
+ // end-anchored item growing at the bottom only extends `scrollHeight` to
454
+ // its own end, so with `paddingEnd > 0` the clamp lands exactly
455
+ // `paddingEnd` short of the target (#1258). `_willUpdate` re-issues the
456
+ // write once the sizer has caught up; the clamped read-back keeps it
457
+ // pending, any other scroll event (a real gesture) cancels it.
458
+ private _clampedAdjustment: { target: number; maxAtWrite: number } | null =
459
+ null
450
460
  shouldAdjustScrollPositionOnItemSizeChange:
451
461
  | undefined
452
462
  | ((
@@ -706,6 +716,18 @@ export class Virtualizer<
706
716
  this._iosDeferredAdjustment += delta
707
717
  return false
708
718
  } else {
719
+ const target = this.getScrollOffset() + this.scrollAdjustments + delta
720
+ // Guarded so a bare test double without `scrollHeight` / `document`
721
+ // does not crash in `getMaxScrollOffset`.
722
+ const el = this.scrollElement
723
+ const maxAtWrite =
724
+ el !== null && ('scrollHeight' in el || 'document' in el)
725
+ ? this.getMaxScrollOffset()
726
+ : null
727
+ this._clampedAdjustment =
728
+ maxAtWrite !== null && target > maxAtWrite + 0.5
729
+ ? { target, maxAtWrite }
730
+ : null
709
731
  this._scrollToOffset(this.getScrollOffset(), {
710
732
  adjustments: (this.scrollAdjustments += delta),
711
733
  behavior,
@@ -787,6 +809,7 @@ export class Virtualizer<
787
809
  this._iosDeferredAdjustment = 0
788
810
  this._iosTouching = false
789
811
  this._iosJustTouchEnded = false
812
+ this._clampedAdjustment = null
790
813
  this.scrollElement = null
791
814
  this.targetWindow = null
792
815
  }
@@ -861,6 +884,17 @@ export class Virtualizer<
861
884
  }
862
885
  this._intendedScrollOffset = null
863
886
 
887
+ // A pending clamped compensation write (#1258) survives only its
888
+ // own read-back, which the browser reports at the scroll max we
889
+ // saw at write time. Anything else is a real gesture (or the write
890
+ // landing after all), so drop it rather than yank the user later.
891
+ if (
892
+ this._clampedAdjustment !== null &&
893
+ Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5
894
+ ) {
895
+ this._clampedAdjustment = null
896
+ }
897
+
864
898
  this.scrollAdjustments = 0
865
899
  // If the offset hasn't moved, this is the echo of our own
866
900
  // adjustment write — `applyScrollAdjustment` already folded it
@@ -980,6 +1014,39 @@ export class Virtualizer<
980
1014
  this.scrollToEnd({ behavior: followOnAppend })
981
1015
  }
982
1016
  }
1017
+
1018
+ // The consumer has committed the new total size by now, so a clamped
1019
+ // compensation write may have room (#1258).
1020
+ this._retryClampedAdjustment()
1021
+ }
1022
+
1023
+ // Re-issue a compensation write the browser clamped because the sizer had
1024
+ // not grown yet (#1258, #1266). Called after `notify` in `resizeItem`,
1025
+ // which covers consumers that size the container synchronously inside
1026
+ // `onChange` (direct DOM updates, flushSync renders — where no re-render
1027
+ // may follow at all), and from `_willUpdate` for consumers that size it
1028
+ // during an asynchronous render. Both the clamped read-back and the
1029
+ // absence of one leave `_clampedAdjustment` set, so timing does not matter.
1030
+ private _retryClampedAdjustment = () => {
1031
+ if (
1032
+ this._clampedAdjustment === null ||
1033
+ !this.scrollElement ||
1034
+ !this.options.enabled
1035
+ ) {
1036
+ return
1037
+ }
1038
+ const { target, maxAtWrite } = this._clampedAdjustment
1039
+ const max = this.getMaxScrollOffset()
1040
+ if (max > maxAtWrite + 0.5) {
1041
+ // Still short (the sizer grew only partially): stay pending against
1042
+ // the new max so the next opportunity retries.
1043
+ this._clampedAdjustment =
1044
+ target > max + 0.5 ? { target, maxAtWrite: max } : null
1045
+ this._scrollToOffset(target, {
1046
+ adjustments: undefined,
1047
+ behavior: undefined,
1048
+ })
1049
+ }
983
1050
  }
984
1051
 
985
1052
  // Apply any accumulated iOS-deferred scroll adjustment, but only when we're
@@ -1646,6 +1713,11 @@ export class Virtualizer<
1646
1713
  // land in one paint. When nothing moved (or the write was deferred on
1647
1714
  // iOS), keep the cheaper async notify.
1648
1715
  this.notify(adjustedSync)
1716
+ // A consumer that grows the sizer synchronously inside `onChange`
1717
+ // (direct DOM updates) may never re-render when the range is
1718
+ // unchanged, so retry a clamped write here rather than only in
1719
+ // `_willUpdate` (#1266).
1720
+ this._retryClampedAdjustment()
1649
1721
  }
1650
1722
  }
1651
1723