@tanstack/virtual-core 3.16.0 → 3.17.0

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
@@ -242,6 +242,16 @@ export const measureElement = <TItemElement extends Element>(
242
242
  entry: ResizeObserverEntry | undefined,
243
243
  instance: Virtualizer<any, TItemElement>,
244
244
  ) => {
245
+ // When useCachedMeasurements is enabled, return the cached size
246
+ // (or estimateSize as fallback) instead of measuring the DOM.
247
+ if (instance.options.useCachedMeasurements) {
248
+ const index = instance.indexFromElement(element)
249
+ const key = instance.options.getItemKey(index)
250
+ return (
251
+ instance.itemSizeCache.get(key) ?? instance.options.estimateSize(index)
252
+ )
253
+ }
254
+
245
255
  if (entry?.borderBoxSize) {
246
256
  const box = entry.borderBoxSize[0]
247
257
  if (box) {
@@ -252,6 +262,21 @@ export const measureElement = <TItemElement extends Element>(
252
262
  }
253
263
  }
254
264
 
265
+ // When called without a ResizeObserverEntry (sync measurement path),
266
+ // return the previously measured size if available. This avoids a
267
+ // synchronous layout read (offsetWidth/offsetHeight) on re-renders.
268
+ // The ResizeObserver is already observing the element and will deliver
269
+ // the accurate size asynchronously if it changed.
270
+ // Users who need synchronous DOM reads can provide a custom measureElement.
271
+ if (!entry) {
272
+ const index = instance.indexFromElement(element)
273
+ const key = instance.options.getItemKey(index)
274
+ const cachedSize = instance.itemSizeCache.get(key)
275
+ if (cachedSize !== undefined) {
276
+ return cachedSize
277
+ }
278
+ }
279
+
255
280
  return (element as unknown as HTMLElement)[
256
281
  instance.options.horizontal ? 'offsetWidth' : 'offsetHeight'
257
282
  ]
@@ -343,6 +368,7 @@ export interface VirtualizerOptions<
343
368
  isRtl?: boolean
344
369
  useAnimationFrameWithResizeObserver?: boolean
345
370
  laneAssignmentMode?: LaneAssignmentMode
371
+ useCachedMeasurements?: boolean
346
372
  }
347
373
 
348
374
  type ScrollState = {
@@ -365,6 +391,7 @@ type PendingScrollAnchor = [
365
391
  key: Key | null,
366
392
  offset: number,
367
393
  followOnAppend: ScrollBehavior | null,
394
+ anchorDelta: number,
368
395
  ]
369
396
 
370
397
  export class Virtualizer<
@@ -381,7 +408,7 @@ export class Virtualizer<
381
408
  // Flat backing store for the lanes===1 fast path: [start_0, size_0, start_1, size_1, ...].
382
409
  // null until the first single-lane build; reused (and grown) across rebuilds.
383
410
  private _flatMeasurements: Float64Array | null = null
384
- private itemSizeCache = new Map<Key, number>()
411
+ itemSizeCache = new Map<Key, number>()
385
412
  private itemSizeCacheVersion = 0
386
413
  private laneAssignments = new Map<number, number>() // index → lane cache
387
414
  // Earliest index dirtied since last getMeasurements() rebuild, or null.
@@ -523,6 +550,7 @@ export class Virtualizer<
523
550
  useScrollendEvent: false,
524
551
  useAnimationFrameWithResizeObserver: false,
525
552
  laneAssignmentMode: 'estimate',
553
+ useCachedMeasurements: false,
526
554
  } as unknown as Required<VirtualizerOptions<TScrollElement, TItemElement>>
527
555
 
528
556
  for (const key in opts) {
@@ -535,6 +563,7 @@ export class Virtualizer<
535
563
  | undefined
536
564
  let anchor: [Key, number] | null = null
537
565
  let followOnAppend: ScrollBehavior | null = null
566
+ let edgeKeysChanged = false
538
567
 
539
568
  if (
540
569
  prevOptions !== undefined &&
@@ -564,6 +593,7 @@ export class Virtualizer<
564
593
  merged.getItemKey(nextCount - 1) !== prevLastKey))
565
594
 
566
595
  if (didEdgeKeysChange) {
596
+ edgeKeysChanged = true
567
597
  const item =
568
598
  prevCount > 0
569
599
  ? (this.getVirtualItemForOffset(this.getScrollOffset()) ??
@@ -592,11 +622,51 @@ export class Virtualizer<
592
622
 
593
623
  this.options = merged
594
624
 
595
- if (anchor || followOnAppend) {
625
+ // When edge keys changed (prepend, trim, reorder, etc.) the key→index
626
+ // mapping has shifted. Force a full measurement rebuild so the anchor
627
+ // resolution below reads positions from the new layout, not the stale
628
+ // memoised cache. Without this, a stable `getItemKey` reference +
629
+ // unchanged `count` would let getMeasurements() return the old layout.
630
+ if (edgeKeysChanged) {
631
+ this.pendingMin = 0
632
+ this.itemSizeCacheVersion++
633
+ }
634
+
635
+ // Eagerly adjust scrollOffset so the virtualizer computes the correct
636
+ // visible range during the current render pass — before _willUpdate
637
+ // syncs the DOM scroll position in a layout effect. Without this,
638
+ // the virtualizer would render the wrong items for one frame (the
639
+ // estimate-based positions are stale) and then correct in the next
640
+ // frame, producing a visible "jump" on prepend with dynamic sizes.
641
+ let anchorResolved = false
642
+ let anchorDelta = 0
643
+ if (anchor && this.scrollOffset !== null) {
644
+ const [anchorKey, anchorOffset] = anchor
645
+ const newMeasurements = this.getMeasurements()
646
+ const { count, getItemKey } = this.options
647
+ let idx = 0
648
+ while (idx < count && getItemKey(idx) !== anchorKey) {
649
+ idx++
650
+ }
651
+ if (idx < count) {
652
+ const anchorItem = newMeasurements[idx]
653
+ if (anchorItem) {
654
+ const newOffset = anchorItem.start + anchorOffset
655
+ if (newOffset !== this.scrollOffset) {
656
+ anchorDelta = newOffset - this.scrollOffset
657
+ this.scrollOffset = newOffset
658
+ anchorResolved = true
659
+ }
660
+ }
661
+ }
662
+ }
663
+
664
+ if (anchorResolved || followOnAppend) {
596
665
  this.pendingScrollAnchor = [
597
- anchor?.[0] ?? null,
598
- anchor?.[1] ?? 0,
666
+ anchorResolved ? anchor![0] : null,
667
+ anchorResolved ? anchor![1] : 0,
599
668
  followOnAppend,
669
+ anchorDelta,
600
670
  ]
601
671
  }
602
672
  }
@@ -798,22 +868,30 @@ export class Virtualizer<
798
868
  this.pendingScrollAnchor = null
799
869
 
800
870
  if (anchor && this.scrollElement && this.options.enabled) {
801
- const [key, offset, followOnAppend] = anchor
802
-
803
- if (key !== null) {
804
- const { count, getItemKey } = this.options
805
- let index = 0
806
- while (index < count && getItemKey(index) !== key) {
807
- index++
808
- }
809
-
810
- const item = index < count ? this.getMeasurements()[index] : undefined
811
- if (item) {
812
- const delta = item.start + offset - this.getScrollOffset()
813
-
814
- if (!approxEqual(delta, 0)) {
815
- this.applyScrollAdjustment(delta)
871
+ const [key, _offset, followOnAppend, anchorDelta] = anchor
872
+
873
+ if (key !== null && !followOnAppend) {
874
+ // scrollOffset was eagerly adjusted in setOptions so the
875
+ // virtualizer already computed the correct range during render.
876
+ // Now sync the browser's actual scroll position to match.
877
+ // Skip when followOnAppend is set — scrollToEnd will handle it.
878
+ //
879
+ // On iOS WebKit, writing scrollTop during touch/momentum cancels
880
+ // the in-flight scroll. Defer the DOM sync the same way
881
+ // applyScrollAdjustment does — accumulate the delta and let
882
+ // _flushIosDeferredIfReady handle it once the scroll settles.
883
+ if (
884
+ isIOSWebKit() &&
885
+ (this.isScrolling || this._iosTouching || this._iosJustTouchEnded)
886
+ ) {
887
+ if (anchorDelta !== 0) {
888
+ this._iosDeferredAdjustment += anchorDelta
816
889
  }
890
+ } else {
891
+ this._scrollToOffset(this.getScrollOffset(), {
892
+ adjustments: undefined,
893
+ behavior: undefined,
894
+ })
817
895
  }
818
896
  }
819
897