@tanstack/virtual-core 3.17.7 → 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
@@ -217,6 +217,10 @@ const observeOffset = <T extends Element | Window>(
217
217
  if (registerScrollendEvent) {
218
218
  element.removeEventListener('scrollend', endHandler)
219
219
  }
220
+ // Removing the listener doesn't retract a reset already queued by the
221
+ // last scroll, and that call would land on a virtualizer that has been
222
+ // torn down — in React, a dispatch into an unmounted tree.
223
+ fallback?.cancel()
220
224
  }
221
225
  }
222
226
 
@@ -443,6 +447,16 @@ export class Virtualizer<
443
447
  // value when the diff is < 1.5 px, distinguishing it from a real user
444
448
  // scroll. The +0.5 over Math.abs lets us also absorb the +1 / -1 cases.
445
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
446
460
  shouldAdjustScrollPositionOnItemSizeChange:
447
461
  | undefined
448
462
  | ((
@@ -489,6 +503,8 @@ export class Virtualizer<
489
503
  return
490
504
  }
491
505
 
506
+ if (!this.isIndexInRange(index)) return
507
+
492
508
  if (this.shouldMeasureDuringScroll(index)) {
493
509
  this.resizeItem(
494
510
  index,
@@ -700,6 +716,18 @@ export class Virtualizer<
700
716
  this._iosDeferredAdjustment += delta
701
717
  return false
702
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
703
731
  this._scrollToOffset(this.getScrollOffset(), {
704
732
  adjustments: (this.scrollAdjustments += delta),
705
733
  behavior,
@@ -761,6 +789,13 @@ export class Virtualizer<
761
789
  this.rafId = null
762
790
  }
763
791
  this.scrollState = null
792
+ // The debounce cancelled above is the only thing that writes `isScrolling`
793
+ // back to false, so a cleanup inside the reset window would strand it, and
794
+ // the direction derived from it, as true. That matters because `cleanup`
795
+ // also runs when the scroll element changes or `enabled` goes false, where
796
+ // the instance lives on.
797
+ this.isScrolling = false
798
+ this.scrollDirection = null
764
799
  // The iOS gesture/deferral state is scoped to the current scroll
765
800
  // element: the touch listeners that maintain it were just removed, and
766
801
  // an in-flight touch keeps targeting the old element (implicit touch
@@ -774,6 +809,7 @@ export class Virtualizer<
774
809
  this._iosDeferredAdjustment = 0
775
810
  this._iosTouching = false
776
811
  this._iosJustTouchEnded = false
812
+ this._clampedAdjustment = null
777
813
  this.scrollElement = null
778
814
  this.targetWindow = null
779
815
  }
@@ -848,6 +884,17 @@ export class Virtualizer<
848
884
  }
849
885
  this._intendedScrollOffset = null
850
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
+
851
898
  this.scrollAdjustments = 0
852
899
  // If the offset hasn't moved, this is the echo of our own
853
900
  // adjustment write — `applyScrollAdjustment` already folded it
@@ -967,6 +1014,39 @@ export class Virtualizer<
967
1014
  this.scrollToEnd({ behavior: followOnAppend })
968
1015
  }
969
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
+ }
970
1050
  }
971
1051
 
972
1052
  // Apply any accumulated iOS-deferred scroll adjustment, but only when we're
@@ -1171,6 +1251,9 @@ export class Virtualizer<
1171
1251
  },
1172
1252
  )
1173
1253
 
1254
+ private isIndexInRange = (index: number): boolean =>
1255
+ index >= 0 && index < this.options.count
1256
+
1174
1257
  private getMeasurements = memo(
1175
1258
  () => [this.getMeasurementOptions(), this.itemSizeCacheVersion],
1176
1259
  (
@@ -1511,6 +1594,8 @@ export class Virtualizer<
1511
1594
  }
1512
1595
 
1513
1596
  const index = this.indexFromElement(node)
1597
+ if (!this.isIndexInRange(index)) return
1598
+
1514
1599
  const key = this.options.getItemKey(index)
1515
1600
  const prevNode = this.elementsCache.get(key)
1516
1601
 
@@ -1534,7 +1619,7 @@ export class Virtualizer<
1534
1619
  }
1535
1620
 
1536
1621
  resizeItem = (index: number, size: number) => {
1537
- if (index < 0 || index >= this.options.count) return
1622
+ if (!this.isIndexInRange(index)) return
1538
1623
 
1539
1624
  // Fast field reads. For lanes===1 we read raw start/size from the flat
1540
1625
  // typed array, avoiding a Proxy.get + VirtualItem allocation per call.
@@ -1628,6 +1713,11 @@ export class Virtualizer<
1628
1713
  // land in one paint. When nothing moved (or the write was deferred on
1629
1714
  // iOS), keep the cheaper async notify.
1630
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()
1631
1721
  }
1632
1722
  }
1633
1723
 
package/src/utils.ts CHANGED
@@ -102,8 +102,18 @@ export const debounce = (
102
102
  ms: number,
103
103
  ) => {
104
104
  let timeoutId: number
105
- return function (this: any, ...args: Array<any>) {
106
- targetWindow.clearTimeout(timeoutId)
107
- timeoutId = targetWindow.setTimeout(() => fn.apply(this, args), ms)
108
- }
105
+ return Object.assign(
106
+ function (this: any, ...args: Array<any>) {
107
+ targetWindow.clearTimeout(timeoutId)
108
+ timeoutId = targetWindow.setTimeout(() => fn.apply(this, args), ms)
109
+ },
110
+ {
111
+ // The handle is closure-local, so a caller that has already
112
+ // unsubscribed has no way to stop a queued call. Teardown paths use
113
+ // this to drop the pending invocation instead of letting it land.
114
+ cancel: () => {
115
+ targetWindow.clearTimeout(timeoutId)
116
+ },
117
+ },
118
+ )
109
119
  }