@tanstack/virtual-core 3.15.0 → 3.16.1
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/dist/cjs/index.cjs +131 -15
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +12 -0
- package/dist/esm/index.d.ts +12 -0
- package/dist/esm/index.js +131 -15
- package/dist/esm/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +227 -21
package/src/index.ts
CHANGED
|
@@ -33,6 +33,10 @@ type ScrollAlignment = 'start' | 'center' | 'end' | 'auto'
|
|
|
33
33
|
|
|
34
34
|
type ScrollBehavior = 'auto' | 'smooth' | 'instant'
|
|
35
35
|
|
|
36
|
+
type ScrollAnchor = 'start' | 'end'
|
|
37
|
+
|
|
38
|
+
type FollowOnAppend = boolean | ScrollBehavior
|
|
39
|
+
|
|
36
40
|
export interface ScrollToOptions {
|
|
37
41
|
align?: ScrollAlignment
|
|
38
42
|
behavior?: ScrollBehavior
|
|
@@ -42,6 +46,8 @@ type ScrollToOffsetOptions = ScrollToOptions
|
|
|
42
46
|
|
|
43
47
|
type ScrollToIndexOptions = ScrollToOptions
|
|
44
48
|
|
|
49
|
+
type ScrollToEndOptions = Pick<ScrollToOptions, 'behavior'>
|
|
50
|
+
|
|
45
51
|
export interface Range {
|
|
46
52
|
startIndex: number
|
|
47
53
|
endIndex: number
|
|
@@ -328,6 +334,9 @@ export interface VirtualizerOptions<
|
|
|
328
334
|
indexAttribute?: string
|
|
329
335
|
initialMeasurementsCache?: Array<VirtualItem>
|
|
330
336
|
lanes?: number
|
|
337
|
+
anchorTo?: ScrollAnchor
|
|
338
|
+
followOnAppend?: FollowOnAppend
|
|
339
|
+
scrollEndThreshold?: number
|
|
331
340
|
isScrollingResetDelay?: number
|
|
332
341
|
useScrollendEvent?: boolean
|
|
333
342
|
enabled?: boolean
|
|
@@ -352,6 +361,13 @@ type ScrollState = {
|
|
|
352
361
|
stableFrames: number
|
|
353
362
|
}
|
|
354
363
|
|
|
364
|
+
type PendingScrollAnchor = [
|
|
365
|
+
key: Key | null,
|
|
366
|
+
offset: number,
|
|
367
|
+
followOnAppend: ScrollBehavior | null,
|
|
368
|
+
anchorDelta: number,
|
|
369
|
+
]
|
|
370
|
+
|
|
355
371
|
export class Virtualizer<
|
|
356
372
|
TScrollElement extends Element | Window,
|
|
357
373
|
TItemElement extends Element,
|
|
@@ -374,6 +390,7 @@ export class Virtualizer<
|
|
|
374
390
|
private prevLanes: number | undefined = undefined
|
|
375
391
|
private lanesChangedFlag = false
|
|
376
392
|
private lanesSettling = false
|
|
393
|
+
private pendingScrollAnchor: PendingScrollAnchor | null = null
|
|
377
394
|
scrollRect: Rect | null = null
|
|
378
395
|
scrollOffset: number | null = null
|
|
379
396
|
scrollDirection: ScrollDirection | null = null
|
|
@@ -498,6 +515,9 @@ export class Virtualizer<
|
|
|
498
515
|
indexAttribute: 'data-index',
|
|
499
516
|
initialMeasurementsCache: [],
|
|
500
517
|
lanes: 1,
|
|
518
|
+
anchorTo: 'start',
|
|
519
|
+
followOnAppend: false,
|
|
520
|
+
scrollEndThreshold: 1,
|
|
501
521
|
isScrollingResetDelay: 150,
|
|
502
522
|
enabled: true,
|
|
503
523
|
isRtl: false,
|
|
@@ -511,13 +531,143 @@ export class Virtualizer<
|
|
|
511
531
|
if (v !== undefined) (merged as any)[key] = v
|
|
512
532
|
}
|
|
513
533
|
|
|
534
|
+
const prevOptions = this.options as
|
|
535
|
+
| Required<VirtualizerOptions<TScrollElement, TItemElement>>
|
|
536
|
+
| undefined
|
|
537
|
+
let anchor: [Key, number] | null = null
|
|
538
|
+
let followOnAppend: ScrollBehavior | null = null
|
|
539
|
+
let edgeKeysChanged = false
|
|
540
|
+
|
|
541
|
+
if (
|
|
542
|
+
prevOptions !== undefined &&
|
|
543
|
+
prevOptions.enabled &&
|
|
544
|
+
merged.enabled &&
|
|
545
|
+
merged.anchorTo === 'end' &&
|
|
546
|
+
this.scrollElement !== null
|
|
547
|
+
) {
|
|
548
|
+
const prevCount = prevOptions.count
|
|
549
|
+
const nextCount = merged.count
|
|
550
|
+
const measurements = this.getMeasurements()
|
|
551
|
+
const prevFirstKey =
|
|
552
|
+
prevCount > 0
|
|
553
|
+
? (measurements[0]?.key ?? prevOptions.getItemKey(0))
|
|
554
|
+
: null
|
|
555
|
+
const prevLastKey =
|
|
556
|
+
prevCount > 0
|
|
557
|
+
? (measurements[prevCount - 1]?.key ??
|
|
558
|
+
prevOptions.getItemKey(prevCount - 1))
|
|
559
|
+
: null
|
|
560
|
+
const didCountChange = nextCount !== prevCount
|
|
561
|
+
const didEdgeKeysChange =
|
|
562
|
+
didCountChange ||
|
|
563
|
+
(prevCount > 0 &&
|
|
564
|
+
nextCount > 0 &&
|
|
565
|
+
(merged.getItemKey(0) !== prevFirstKey ||
|
|
566
|
+
merged.getItemKey(nextCount - 1) !== prevLastKey))
|
|
567
|
+
|
|
568
|
+
if (didEdgeKeysChange) {
|
|
569
|
+
edgeKeysChanged = true
|
|
570
|
+
const item =
|
|
571
|
+
prevCount > 0
|
|
572
|
+
? (this.getVirtualItemForOffset(this.getScrollOffset()) ??
|
|
573
|
+
measurements[0])
|
|
574
|
+
: null
|
|
575
|
+
|
|
576
|
+
if (item) {
|
|
577
|
+
anchor = [item.key, this.getScrollOffset() - item.start]
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
const behavior =
|
|
581
|
+
merged.followOnAppend === true
|
|
582
|
+
? 'auto'
|
|
583
|
+
: merged.followOnAppend || null
|
|
584
|
+
|
|
585
|
+
if (
|
|
586
|
+
behavior &&
|
|
587
|
+
nextCount > prevCount &&
|
|
588
|
+
this.isAtEnd(prevOptions.scrollEndThreshold) &&
|
|
589
|
+
(prevCount === 0 || merged.getItemKey(nextCount - 1) !== prevLastKey)
|
|
590
|
+
) {
|
|
591
|
+
followOnAppend = behavior
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
|
|
514
596
|
this.options = merged
|
|
597
|
+
|
|
598
|
+
// When edge keys changed (prepend, trim, reorder, etc.) the key→index
|
|
599
|
+
// mapping has shifted. Force a full measurement rebuild so the anchor
|
|
600
|
+
// resolution below reads positions from the new layout, not the stale
|
|
601
|
+
// memoised cache. Without this, a stable `getItemKey` reference +
|
|
602
|
+
// unchanged `count` would let getMeasurements() return the old layout.
|
|
603
|
+
if (edgeKeysChanged) {
|
|
604
|
+
this.pendingMin = 0
|
|
605
|
+
this.itemSizeCacheVersion++
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
// Eagerly adjust scrollOffset so the virtualizer computes the correct
|
|
609
|
+
// visible range during the current render pass — before _willUpdate
|
|
610
|
+
// syncs the DOM scroll position in a layout effect. Without this,
|
|
611
|
+
// the virtualizer would render the wrong items for one frame (the
|
|
612
|
+
// estimate-based positions are stale) and then correct in the next
|
|
613
|
+
// frame, producing a visible "jump" on prepend with dynamic sizes.
|
|
614
|
+
let anchorResolved = false
|
|
615
|
+
let anchorDelta = 0
|
|
616
|
+
if (anchor && this.scrollOffset !== null) {
|
|
617
|
+
const [anchorKey, anchorOffset] = anchor
|
|
618
|
+
const newMeasurements = this.getMeasurements()
|
|
619
|
+
const { count, getItemKey } = this.options
|
|
620
|
+
let idx = 0
|
|
621
|
+
while (idx < count && getItemKey(idx) !== anchorKey) {
|
|
622
|
+
idx++
|
|
623
|
+
}
|
|
624
|
+
if (idx < count) {
|
|
625
|
+
const anchorItem = newMeasurements[idx]
|
|
626
|
+
if (anchorItem) {
|
|
627
|
+
const newOffset = anchorItem.start + anchorOffset
|
|
628
|
+
if (newOffset !== this.scrollOffset) {
|
|
629
|
+
anchorDelta = newOffset - this.scrollOffset
|
|
630
|
+
this.scrollOffset = newOffset
|
|
631
|
+
anchorResolved = true
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
if (anchorResolved || followOnAppend) {
|
|
638
|
+
this.pendingScrollAnchor = [
|
|
639
|
+
anchorResolved ? anchor![0] : null,
|
|
640
|
+
anchorResolved ? anchor![1] : 0,
|
|
641
|
+
followOnAppend,
|
|
642
|
+
anchorDelta,
|
|
643
|
+
]
|
|
644
|
+
}
|
|
515
645
|
}
|
|
516
646
|
|
|
517
647
|
private notify = (sync: boolean) => {
|
|
518
648
|
this.options.onChange?.(this, sync)
|
|
519
649
|
}
|
|
520
650
|
|
|
651
|
+
private applyScrollAdjustment(delta: number, behavior?: ScrollBehavior) {
|
|
652
|
+
if (delta === 0) return
|
|
653
|
+
|
|
654
|
+
if (process.env.NODE_ENV !== 'production' && this.options.debug) {
|
|
655
|
+
console.info('correction', delta)
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
if (
|
|
659
|
+
isIOSWebKit() &&
|
|
660
|
+
(this.isScrolling || this._iosTouching || this._iosJustTouchEnded)
|
|
661
|
+
) {
|
|
662
|
+
this._iosDeferredAdjustment += delta
|
|
663
|
+
} else {
|
|
664
|
+
this._scrollToOffset(this.getScrollOffset(), {
|
|
665
|
+
adjustments: (this.scrollAdjustments += delta),
|
|
666
|
+
behavior,
|
|
667
|
+
})
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
|
|
521
671
|
private maybeNotify = memo(
|
|
522
672
|
() => {
|
|
523
673
|
this.calculateRange()
|
|
@@ -686,6 +836,42 @@ export class Virtualizer<
|
|
|
686
836
|
behavior: undefined,
|
|
687
837
|
})
|
|
688
838
|
}
|
|
839
|
+
|
|
840
|
+
const anchor = this.pendingScrollAnchor
|
|
841
|
+
this.pendingScrollAnchor = null
|
|
842
|
+
|
|
843
|
+
if (anchor && this.scrollElement && this.options.enabled) {
|
|
844
|
+
const [key, _offset, followOnAppend, anchorDelta] = anchor
|
|
845
|
+
|
|
846
|
+
if (key !== null && !followOnAppend) {
|
|
847
|
+
// scrollOffset was eagerly adjusted in setOptions so the
|
|
848
|
+
// virtualizer already computed the correct range during render.
|
|
849
|
+
// Now sync the browser's actual scroll position to match.
|
|
850
|
+
// Skip when followOnAppend is set — scrollToEnd will handle it.
|
|
851
|
+
//
|
|
852
|
+
// On iOS WebKit, writing scrollTop during touch/momentum cancels
|
|
853
|
+
// the in-flight scroll. Defer the DOM sync the same way
|
|
854
|
+
// applyScrollAdjustment does — accumulate the delta and let
|
|
855
|
+
// _flushIosDeferredIfReady handle it once the scroll settles.
|
|
856
|
+
if (
|
|
857
|
+
isIOSWebKit() &&
|
|
858
|
+
(this.isScrolling || this._iosTouching || this._iosJustTouchEnded)
|
|
859
|
+
) {
|
|
860
|
+
if (anchorDelta !== 0) {
|
|
861
|
+
this._iosDeferredAdjustment += anchorDelta
|
|
862
|
+
}
|
|
863
|
+
} else {
|
|
864
|
+
this._scrollToOffset(this.getScrollOffset(), {
|
|
865
|
+
adjustments: undefined,
|
|
866
|
+
behavior: undefined,
|
|
867
|
+
})
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
if (followOnAppend) {
|
|
872
|
+
this.scrollToEnd({ behavior: followOnAppend })
|
|
873
|
+
}
|
|
874
|
+
}
|
|
689
875
|
}
|
|
690
876
|
|
|
691
877
|
// Apply any accumulated iOS-deferred scroll adjustment, but only when we're
|
|
@@ -1284,7 +1470,12 @@ export class Virtualizer<
|
|
|
1284
1470
|
const delta = size - itemSize
|
|
1285
1471
|
|
|
1286
1472
|
if (delta !== 0) {
|
|
1287
|
-
|
|
1473
|
+
const wasAtEnd =
|
|
1474
|
+
this.options.anchorTo === 'end' &&
|
|
1475
|
+
this.scrollState?.behavior !== 'smooth' &&
|
|
1476
|
+
this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold
|
|
1477
|
+
const prevTotalSize = wasAtEnd ? this.getTotalSize() : 0
|
|
1478
|
+
const shouldAdjustScroll =
|
|
1288
1479
|
this.scrollState?.behavior !== 'smooth' &&
|
|
1289
1480
|
(this.shouldAdjustScrollPositionOnItemSizeChange !== undefined
|
|
1290
1481
|
? this.shouldAdjustScrollPositionOnItemSizeChange(
|
|
@@ -1309,26 +1500,6 @@ export class Virtualizer<
|
|
|
1309
1500
|
// behavior can pass shouldAdjustScrollPositionOnItemSizeChange.
|
|
1310
1501
|
itemStart < this.getScrollOffset() + this.scrollAdjustments &&
|
|
1311
1502
|
this.scrollDirection !== 'backward')
|
|
1312
|
-
) {
|
|
1313
|
-
if (process.env.NODE_ENV !== 'production' && this.options.debug) {
|
|
1314
|
-
console.info('correction', delta)
|
|
1315
|
-
}
|
|
1316
|
-
// On iOS WebKit, writing scrollTop while a finger is on screen or
|
|
1317
|
-
// momentum-scroll is running cancels the in-flight scroll. Defer
|
|
1318
|
-
// the adjustment until iOS is fully settled — flushed by either
|
|
1319
|
-
// the scroll callback or the touchend grace-timer.
|
|
1320
|
-
if (
|
|
1321
|
-
isIOSWebKit() &&
|
|
1322
|
-
(this.isScrolling || this._iosTouching || this._iosJustTouchEnded)
|
|
1323
|
-
) {
|
|
1324
|
-
this._iosDeferredAdjustment += delta
|
|
1325
|
-
} else {
|
|
1326
|
-
this._scrollToOffset(this.getScrollOffset(), {
|
|
1327
|
-
adjustments: (this.scrollAdjustments += delta),
|
|
1328
|
-
behavior: undefined,
|
|
1329
|
-
})
|
|
1330
|
-
}
|
|
1331
|
-
}
|
|
1332
1503
|
|
|
1333
1504
|
if (this.pendingMin === null || index < this.pendingMin) {
|
|
1334
1505
|
this.pendingMin = index
|
|
@@ -1336,6 +1507,12 @@ export class Virtualizer<
|
|
|
1336
1507
|
this.itemSizeCache.set(key, size)
|
|
1337
1508
|
this.itemSizeCacheVersion++
|
|
1338
1509
|
|
|
1510
|
+
if (wasAtEnd) {
|
|
1511
|
+
this.applyScrollAdjustment(this.getTotalSize() - prevTotalSize)
|
|
1512
|
+
} else if (shouldAdjustScroll) {
|
|
1513
|
+
this.applyScrollAdjustment(delta)
|
|
1514
|
+
}
|
|
1515
|
+
|
|
1339
1516
|
this.notify(false)
|
|
1340
1517
|
}
|
|
1341
1518
|
}
|
|
@@ -1398,6 +1575,21 @@ export class Virtualizer<
|
|
|
1398
1575
|
}
|
|
1399
1576
|
}
|
|
1400
1577
|
|
|
1578
|
+
private getVirtualDistanceFromEnd = () => {
|
|
1579
|
+
return Math.max(
|
|
1580
|
+
this.getTotalSize() - this.getSize() - this.getScrollOffset(),
|
|
1581
|
+
0,
|
|
1582
|
+
)
|
|
1583
|
+
}
|
|
1584
|
+
|
|
1585
|
+
getDistanceFromEnd = () => {
|
|
1586
|
+
return Math.max(this.getMaxScrollOffset() - this.getScrollOffset(), 0)
|
|
1587
|
+
}
|
|
1588
|
+
|
|
1589
|
+
isAtEnd = (threshold = this.options.scrollEndThreshold) => {
|
|
1590
|
+
return this.getDistanceFromEnd() <= threshold
|
|
1591
|
+
}
|
|
1592
|
+
|
|
1401
1593
|
getOffsetForAlignment = (
|
|
1402
1594
|
toOffset: number,
|
|
1403
1595
|
align: ScrollAlignment,
|
|
@@ -1533,6 +1725,20 @@ export class Virtualizer<
|
|
|
1533
1725
|
this.scheduleScrollReconcile()
|
|
1534
1726
|
}
|
|
1535
1727
|
|
|
1728
|
+
scrollToEnd = ({ behavior = 'auto' }: ScrollToEndOptions = {}) => {
|
|
1729
|
+
if (this.options.count > 0) {
|
|
1730
|
+
this.scrollToIndex(this.options.count - 1, {
|
|
1731
|
+
align: 'end',
|
|
1732
|
+
behavior,
|
|
1733
|
+
})
|
|
1734
|
+
return
|
|
1735
|
+
}
|
|
1736
|
+
|
|
1737
|
+
this.scrollToOffset(Math.max(this.getTotalSize() - this.getSize(), 0), {
|
|
1738
|
+
behavior,
|
|
1739
|
+
})
|
|
1740
|
+
}
|
|
1741
|
+
|
|
1536
1742
|
getTotalSize = () => {
|
|
1537
1743
|
const measurements = this.getMeasurements()
|
|
1538
1744
|
|