@tanstack/virtual-core 3.15.0 → 3.16.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/dist/cjs/index.cjs +104 -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 +105 -16
- package/dist/esm/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +176 -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,12 @@ 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
|
+
]
|
|
369
|
+
|
|
355
370
|
export class Virtualizer<
|
|
356
371
|
TScrollElement extends Element | Window,
|
|
357
372
|
TItemElement extends Element,
|
|
@@ -374,6 +389,7 @@ export class Virtualizer<
|
|
|
374
389
|
private prevLanes: number | undefined = undefined
|
|
375
390
|
private lanesChangedFlag = false
|
|
376
391
|
private lanesSettling = false
|
|
392
|
+
private pendingScrollAnchor: PendingScrollAnchor | null = null
|
|
377
393
|
scrollRect: Rect | null = null
|
|
378
394
|
scrollOffset: number | null = null
|
|
379
395
|
scrollDirection: ScrollDirection | null = null
|
|
@@ -498,6 +514,9 @@ export class Virtualizer<
|
|
|
498
514
|
indexAttribute: 'data-index',
|
|
499
515
|
initialMeasurementsCache: [],
|
|
500
516
|
lanes: 1,
|
|
517
|
+
anchorTo: 'start',
|
|
518
|
+
followOnAppend: false,
|
|
519
|
+
scrollEndThreshold: 1,
|
|
501
520
|
isScrollingResetDelay: 150,
|
|
502
521
|
enabled: true,
|
|
503
522
|
isRtl: false,
|
|
@@ -511,13 +530,101 @@ export class Virtualizer<
|
|
|
511
530
|
if (v !== undefined) (merged as any)[key] = v
|
|
512
531
|
}
|
|
513
532
|
|
|
533
|
+
const prevOptions = this.options as
|
|
534
|
+
| Required<VirtualizerOptions<TScrollElement, TItemElement>>
|
|
535
|
+
| undefined
|
|
536
|
+
let anchor: [Key, number] | null = null
|
|
537
|
+
let followOnAppend: ScrollBehavior | null = null
|
|
538
|
+
|
|
539
|
+
if (
|
|
540
|
+
prevOptions !== undefined &&
|
|
541
|
+
prevOptions.enabled &&
|
|
542
|
+
merged.enabled &&
|
|
543
|
+
merged.anchorTo === 'end' &&
|
|
544
|
+
this.scrollElement !== null
|
|
545
|
+
) {
|
|
546
|
+
const prevCount = prevOptions.count
|
|
547
|
+
const nextCount = merged.count
|
|
548
|
+
const measurements = this.getMeasurements()
|
|
549
|
+
const prevFirstKey =
|
|
550
|
+
prevCount > 0
|
|
551
|
+
? (measurements[0]?.key ?? prevOptions.getItemKey(0))
|
|
552
|
+
: null
|
|
553
|
+
const prevLastKey =
|
|
554
|
+
prevCount > 0
|
|
555
|
+
? (measurements[prevCount - 1]?.key ??
|
|
556
|
+
prevOptions.getItemKey(prevCount - 1))
|
|
557
|
+
: null
|
|
558
|
+
const didCountChange = nextCount !== prevCount
|
|
559
|
+
const didEdgeKeysChange =
|
|
560
|
+
didCountChange ||
|
|
561
|
+
(prevCount > 0 &&
|
|
562
|
+
nextCount > 0 &&
|
|
563
|
+
(merged.getItemKey(0) !== prevFirstKey ||
|
|
564
|
+
merged.getItemKey(nextCount - 1) !== prevLastKey))
|
|
565
|
+
|
|
566
|
+
if (didEdgeKeysChange) {
|
|
567
|
+
const item =
|
|
568
|
+
prevCount > 0
|
|
569
|
+
? (this.getVirtualItemForOffset(this.getScrollOffset()) ??
|
|
570
|
+
measurements[0])
|
|
571
|
+
: null
|
|
572
|
+
|
|
573
|
+
if (item) {
|
|
574
|
+
anchor = [item.key, this.getScrollOffset() - item.start]
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
const behavior =
|
|
578
|
+
merged.followOnAppend === true
|
|
579
|
+
? 'auto'
|
|
580
|
+
: merged.followOnAppend || null
|
|
581
|
+
|
|
582
|
+
if (
|
|
583
|
+
behavior &&
|
|
584
|
+
nextCount > prevCount &&
|
|
585
|
+
this.isAtEnd(prevOptions.scrollEndThreshold) &&
|
|
586
|
+
(prevCount === 0 || merged.getItemKey(nextCount - 1) !== prevLastKey)
|
|
587
|
+
) {
|
|
588
|
+
followOnAppend = behavior
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
|
|
514
593
|
this.options = merged
|
|
594
|
+
|
|
595
|
+
if (anchor || followOnAppend) {
|
|
596
|
+
this.pendingScrollAnchor = [
|
|
597
|
+
anchor?.[0] ?? null,
|
|
598
|
+
anchor?.[1] ?? 0,
|
|
599
|
+
followOnAppend,
|
|
600
|
+
]
|
|
601
|
+
}
|
|
515
602
|
}
|
|
516
603
|
|
|
517
604
|
private notify = (sync: boolean) => {
|
|
518
605
|
this.options.onChange?.(this, sync)
|
|
519
606
|
}
|
|
520
607
|
|
|
608
|
+
private applyScrollAdjustment(delta: number, behavior?: ScrollBehavior) {
|
|
609
|
+
if (delta === 0) return
|
|
610
|
+
|
|
611
|
+
if (process.env.NODE_ENV !== 'production' && this.options.debug) {
|
|
612
|
+
console.info('correction', delta)
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
if (
|
|
616
|
+
isIOSWebKit() &&
|
|
617
|
+
(this.isScrolling || this._iosTouching || this._iosJustTouchEnded)
|
|
618
|
+
) {
|
|
619
|
+
this._iosDeferredAdjustment += delta
|
|
620
|
+
} else {
|
|
621
|
+
this._scrollToOffset(this.getScrollOffset(), {
|
|
622
|
+
adjustments: (this.scrollAdjustments += delta),
|
|
623
|
+
behavior,
|
|
624
|
+
})
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
|
|
521
628
|
private maybeNotify = memo(
|
|
522
629
|
() => {
|
|
523
630
|
this.calculateRange()
|
|
@@ -686,6 +793,34 @@ export class Virtualizer<
|
|
|
686
793
|
behavior: undefined,
|
|
687
794
|
})
|
|
688
795
|
}
|
|
796
|
+
|
|
797
|
+
const anchor = this.pendingScrollAnchor
|
|
798
|
+
this.pendingScrollAnchor = null
|
|
799
|
+
|
|
800
|
+
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)
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
if (followOnAppend) {
|
|
821
|
+
this.scrollToEnd({ behavior: followOnAppend })
|
|
822
|
+
}
|
|
823
|
+
}
|
|
689
824
|
}
|
|
690
825
|
|
|
691
826
|
// Apply any accumulated iOS-deferred scroll adjustment, but only when we're
|
|
@@ -1284,7 +1419,12 @@ export class Virtualizer<
|
|
|
1284
1419
|
const delta = size - itemSize
|
|
1285
1420
|
|
|
1286
1421
|
if (delta !== 0) {
|
|
1287
|
-
|
|
1422
|
+
const wasAtEnd =
|
|
1423
|
+
this.options.anchorTo === 'end' &&
|
|
1424
|
+
this.scrollState?.behavior !== 'smooth' &&
|
|
1425
|
+
this.getVirtualDistanceFromEnd() <= this.options.scrollEndThreshold
|
|
1426
|
+
const prevTotalSize = wasAtEnd ? this.getTotalSize() : 0
|
|
1427
|
+
const shouldAdjustScroll =
|
|
1288
1428
|
this.scrollState?.behavior !== 'smooth' &&
|
|
1289
1429
|
(this.shouldAdjustScrollPositionOnItemSizeChange !== undefined
|
|
1290
1430
|
? this.shouldAdjustScrollPositionOnItemSizeChange(
|
|
@@ -1309,26 +1449,6 @@ export class Virtualizer<
|
|
|
1309
1449
|
// behavior can pass shouldAdjustScrollPositionOnItemSizeChange.
|
|
1310
1450
|
itemStart < this.getScrollOffset() + this.scrollAdjustments &&
|
|
1311
1451
|
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
1452
|
|
|
1333
1453
|
if (this.pendingMin === null || index < this.pendingMin) {
|
|
1334
1454
|
this.pendingMin = index
|
|
@@ -1336,6 +1456,12 @@ export class Virtualizer<
|
|
|
1336
1456
|
this.itemSizeCache.set(key, size)
|
|
1337
1457
|
this.itemSizeCacheVersion++
|
|
1338
1458
|
|
|
1459
|
+
if (wasAtEnd) {
|
|
1460
|
+
this.applyScrollAdjustment(this.getTotalSize() - prevTotalSize)
|
|
1461
|
+
} else if (shouldAdjustScroll) {
|
|
1462
|
+
this.applyScrollAdjustment(delta)
|
|
1463
|
+
}
|
|
1464
|
+
|
|
1339
1465
|
this.notify(false)
|
|
1340
1466
|
}
|
|
1341
1467
|
}
|
|
@@ -1398,6 +1524,21 @@ export class Virtualizer<
|
|
|
1398
1524
|
}
|
|
1399
1525
|
}
|
|
1400
1526
|
|
|
1527
|
+
private getVirtualDistanceFromEnd = () => {
|
|
1528
|
+
return Math.max(
|
|
1529
|
+
this.getTotalSize() - this.getSize() - this.getScrollOffset(),
|
|
1530
|
+
0,
|
|
1531
|
+
)
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1534
|
+
getDistanceFromEnd = () => {
|
|
1535
|
+
return Math.max(this.getMaxScrollOffset() - this.getScrollOffset(), 0)
|
|
1536
|
+
}
|
|
1537
|
+
|
|
1538
|
+
isAtEnd = (threshold = this.options.scrollEndThreshold) => {
|
|
1539
|
+
return this.getDistanceFromEnd() <= threshold
|
|
1540
|
+
}
|
|
1541
|
+
|
|
1401
1542
|
getOffsetForAlignment = (
|
|
1402
1543
|
toOffset: number,
|
|
1403
1544
|
align: ScrollAlignment,
|
|
@@ -1533,6 +1674,20 @@ export class Virtualizer<
|
|
|
1533
1674
|
this.scheduleScrollReconcile()
|
|
1534
1675
|
}
|
|
1535
1676
|
|
|
1677
|
+
scrollToEnd = ({ behavior = 'auto' }: ScrollToEndOptions = {}) => {
|
|
1678
|
+
if (this.options.count > 0) {
|
|
1679
|
+
this.scrollToIndex(this.options.count - 1, {
|
|
1680
|
+
align: 'end',
|
|
1681
|
+
behavior,
|
|
1682
|
+
})
|
|
1683
|
+
return
|
|
1684
|
+
}
|
|
1685
|
+
|
|
1686
|
+
this.scrollToOffset(Math.max(this.getTotalSize() - this.getSize(), 0), {
|
|
1687
|
+
behavior,
|
|
1688
|
+
})
|
|
1689
|
+
}
|
|
1690
|
+
|
|
1536
1691
|
getTotalSize = () => {
|
|
1537
1692
|
const measurements = this.getMeasurements()
|
|
1538
1693
|
|