@tanstack/virtual-core 3.19.2 → 3.19.4
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 +141 -126
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +7 -5
- package/dist/esm/index.d.ts +7 -5
- package/dist/esm/index.js +141 -126
- package/dist/esm/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +215 -203
package/src/index.ts
CHANGED
|
@@ -86,6 +86,25 @@ const getRect = (element: HTMLElement): Rect => {
|
|
|
86
86
|
return { width: offsetWidth, height: offsetHeight }
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
+
// Animation frames for `useAnimationFrameWithResizeObserver`, requested from
|
|
90
|
+
// the scroll element's own window rather than the global one.
|
|
91
|
+
const createFrameQueue = (targetWindow: Window) => {
|
|
92
|
+
const ids = new Set<number>()
|
|
93
|
+
return {
|
|
94
|
+
schedule: (fn: () => void) => {
|
|
95
|
+
const id = targetWindow.requestAnimationFrame(() => {
|
|
96
|
+
ids.delete(id)
|
|
97
|
+
fn()
|
|
98
|
+
})
|
|
99
|
+
ids.add(id)
|
|
100
|
+
},
|
|
101
|
+
cancel: () => {
|
|
102
|
+
ids.forEach((id) => targetWindow.cancelAnimationFrame(id))
|
|
103
|
+
ids.clear()
|
|
104
|
+
},
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
89
108
|
export const defaultKeyExtractor = (index: number) => index
|
|
90
109
|
|
|
91
110
|
export const defaultRangeExtractor = (range: Range) => {
|
|
@@ -129,6 +148,7 @@ export const observeElementRect = <T extends Element>(
|
|
|
129
148
|
return () => {}
|
|
130
149
|
}
|
|
131
150
|
|
|
151
|
+
const frames = createFrameQueue(targetWindow)
|
|
132
152
|
const observer = new targetWindow.ResizeObserver((entries) => {
|
|
133
153
|
const run = () => {
|
|
134
154
|
const entry = entries[0]
|
|
@@ -143,7 +163,7 @@ export const observeElementRect = <T extends Element>(
|
|
|
143
163
|
}
|
|
144
164
|
|
|
145
165
|
instance.options.useAnimationFrameWithResizeObserver
|
|
146
|
-
?
|
|
166
|
+
? frames.schedule(run)
|
|
147
167
|
: run()
|
|
148
168
|
})
|
|
149
169
|
|
|
@@ -151,6 +171,7 @@ export const observeElementRect = <T extends Element>(
|
|
|
151
171
|
|
|
152
172
|
return () => {
|
|
153
173
|
observer.unobserve(element)
|
|
174
|
+
frames.cancel()
|
|
154
175
|
}
|
|
155
176
|
}
|
|
156
177
|
|
|
@@ -413,14 +434,6 @@ type ScrollState = {
|
|
|
413
434
|
// settling
|
|
414
435
|
stableFrames: number
|
|
415
436
|
phase: ScrollPhase
|
|
416
|
-
|
|
417
|
-
// Where our last non-smooth write for this command put the element, until
|
|
418
|
-
// its scroll event arrives. Until then `scrollOffset` still holds the old
|
|
419
|
-
// position, and the rows on screen are the ones rendered for it. A smooth
|
|
420
|
-
// write leaves it null: the element is somewhere between the two offsets.
|
|
421
|
-
// It goes with the scroll state, so a write the browser ignored (no read-back)
|
|
422
|
-
// does not outlive its command.
|
|
423
|
-
writtenOffset?: number | null
|
|
424
437
|
}
|
|
425
438
|
|
|
426
439
|
const initialScrollPhase = (
|
|
@@ -439,12 +452,14 @@ const isSmoothScroll = (state: ScrollState | null): state is ScrollState =>
|
|
|
439
452
|
const isAnimating = (state: ScrollState) =>
|
|
440
453
|
state.phase === 'smooth' || state.phase === 'smoothFinishing'
|
|
441
454
|
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
455
|
+
// What `setOptions` decided about the scroll position for `_willUpdate` to
|
|
456
|
+
// carry out once the consumer has rendered: keep the reading anchor in place
|
|
457
|
+
// after a prepend (`scrollOffset` already moved by `delta`, the element has
|
|
458
|
+
// not), or follow an append to the end. A prepend that qualifies for both
|
|
459
|
+
// only follows.
|
|
460
|
+
type PendingScrollAnchor =
|
|
461
|
+
| { kind: 'anchor'; delta: number }
|
|
462
|
+
| { kind: 'follow'; behavior: ScrollBehavior }
|
|
448
463
|
|
|
449
464
|
function isAppendWithTrim(
|
|
450
465
|
prevCount: number,
|
|
@@ -505,30 +520,49 @@ export class Virtualizer<
|
|
|
505
520
|
scrollRect: Rect | null = null
|
|
506
521
|
scrollOffset: number | null = null
|
|
507
522
|
scrollDirection: ScrollDirection | null = null
|
|
508
|
-
|
|
523
|
+
// Size compensation written to the element but not yet reflected in
|
|
524
|
+
// `scrollOffset`: the element is expected at `scrollOffset +
|
|
525
|
+
// scrollAdjustments` until the next scroll event reports it.
|
|
526
|
+
get scrollAdjustments() {
|
|
527
|
+
return this.pendingWrite?.adjustments ?? 0
|
|
528
|
+
}
|
|
509
529
|
// Sum of size-change deltas above-viewport that were skipped during
|
|
510
530
|
// iOS momentum scroll (writing scrollTop mid-momentum cancels it).
|
|
511
531
|
// Flushed in a single scrollTo when iOS is fully settled.
|
|
512
532
|
private _iosDeferredAdjustment = 0
|
|
513
|
-
//
|
|
514
|
-
//
|
|
515
|
-
//
|
|
516
|
-
//
|
|
517
|
-
//
|
|
518
|
-
//
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
//
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
//
|
|
528
|
-
//
|
|
529
|
-
//
|
|
530
|
-
//
|
|
531
|
-
|
|
533
|
+
// The touch gesture in progress. iOS WebKit cancels momentum when
|
|
534
|
+
// scrollTop is written, so adjustments are deferred not only during
|
|
535
|
+
// `isScrolling` but through the whole gesture: `down` from touchstart to
|
|
536
|
+
// touchend/touchcancel (active drag), then `tail` for a short grace window
|
|
537
|
+
// after the finger lifts (early momentum — iOS fires touch events only
|
|
538
|
+
// once at the start of momentum, so the tail is a timer rather than
|
|
539
|
+
// another event). Null between gestures, and after touchend on non-iOS,
|
|
540
|
+
// which arms no tail. The tail record owns its timer: `_endIosTouch` is
|
|
541
|
+
// the only way to drop either, so a cleared record never leaves a timer
|
|
542
|
+
// behind, and a live timer always has its record.
|
|
543
|
+
private _iosTouch:
|
|
544
|
+
| { phase: 'down' }
|
|
545
|
+
| { phase: 'tail'; timerId: number }
|
|
546
|
+
| null = null
|
|
547
|
+
// Our last write to the element, from `_scrollToOffset` until the next
|
|
548
|
+
// scroll event reports it (the only place it is cleared). Until then
|
|
549
|
+
// `scrollOffset` may still hold the old position.
|
|
550
|
+
//
|
|
551
|
+
// - `target`: where the write puts the element (offset + adjustments).
|
|
552
|
+
// Safari (and Chrome/Firefox under certain DPRs) round writes to whole
|
|
553
|
+
// pixels: wrote 12345.5, read back 12346. A read-back within 1.5px of
|
|
554
|
+
// `target` is taken as our own and adopts the exact value, so reconcile
|
|
555
|
+
// does not see a moved target and write again; a user scroll has moved
|
|
556
|
+
// further by the time it reports.
|
|
557
|
+
// - `smooth`: the write animates, so until it lands the element is
|
|
558
|
+
// somewhere between the old offset and `target`.
|
|
559
|
+
// - `adjustments`: see `scrollAdjustments`. Writes that pass none keep the
|
|
560
|
+
// previous value.
|
|
561
|
+
private pendingWrite: {
|
|
562
|
+
target: number
|
|
563
|
+
smooth: boolean
|
|
564
|
+
adjustments: number
|
|
565
|
+
} | null = null
|
|
532
566
|
// A compensation write from `applyScrollAdjustment` whose target exceeded
|
|
533
567
|
// the element's scroll max at the moment of the write. The browser clamps
|
|
534
568
|
// such a write because the consumer's sizer has not grown yet: an
|
|
@@ -553,17 +587,20 @@ export class Virtualizer<
|
|
|
553
587
|
private now = () => this.targetWindow?.performance?.now?.() ?? Date.now()
|
|
554
588
|
private observer = (() => {
|
|
555
589
|
let _ro: ResizeObserver | null = null
|
|
590
|
+
let _frames: ReturnType<typeof createFrameQueue> | null = null
|
|
556
591
|
|
|
557
592
|
const get = () => {
|
|
558
593
|
if (_ro) {
|
|
559
594
|
return _ro
|
|
560
595
|
}
|
|
561
596
|
|
|
562
|
-
|
|
597
|
+
const targetWindow = this.targetWindow
|
|
598
|
+
if (!targetWindow || !targetWindow.ResizeObserver) {
|
|
563
599
|
return null
|
|
564
600
|
}
|
|
565
601
|
|
|
566
|
-
|
|
602
|
+
const frames = (_frames = createFrameQueue(targetWindow))
|
|
603
|
+
return (_ro = new targetWindow.ResizeObserver((entries) => {
|
|
567
604
|
const run = () => {
|
|
568
605
|
// Measure every entry before notifying. A notify that adjusted
|
|
569
606
|
// scroll re-renders synchronously, and that render can unmount
|
|
@@ -583,7 +620,7 @@ export class Virtualizer<
|
|
|
583
620
|
}
|
|
584
621
|
}
|
|
585
622
|
this.options.useAnimationFrameWithResizeObserver
|
|
586
|
-
?
|
|
623
|
+
? frames.schedule(run)
|
|
587
624
|
: run()
|
|
588
625
|
}))
|
|
589
626
|
}
|
|
@@ -621,7 +658,9 @@ export class Virtualizer<
|
|
|
621
658
|
return {
|
|
622
659
|
disconnect: () => {
|
|
623
660
|
get()?.disconnect()
|
|
661
|
+
_frames?.cancel()
|
|
624
662
|
_ro = null
|
|
663
|
+
_frames = null
|
|
625
664
|
},
|
|
626
665
|
observe: (target: Element) =>
|
|
627
666
|
get()?.observe(target, { box: 'border-box' }),
|
|
@@ -759,8 +798,7 @@ export class Virtualizer<
|
|
|
759
798
|
// the virtualizer would render the wrong items for one frame (the
|
|
760
799
|
// estimate-based positions are stale) and then correct in the next
|
|
761
800
|
// frame, producing a visible "jump" on prepend with dynamic sizes.
|
|
762
|
-
let
|
|
763
|
-
let anchorDelta = 0
|
|
801
|
+
let anchorDelta: number | null = null
|
|
764
802
|
if (anchor && this.scrollOffset !== null) {
|
|
765
803
|
const [anchorKey, anchorOffset] = anchor
|
|
766
804
|
const newMeasurements = this.getMeasurements()
|
|
@@ -781,19 +819,15 @@ export class Virtualizer<
|
|
|
781
819
|
if (!followOnAppend && newOffset !== this.scrollOffset) {
|
|
782
820
|
anchorDelta = newOffset - this.scrollOffset
|
|
783
821
|
this.scrollOffset = newOffset
|
|
784
|
-
anchorResolved = true
|
|
785
822
|
}
|
|
786
823
|
}
|
|
787
824
|
}
|
|
788
825
|
}
|
|
789
826
|
|
|
790
|
-
if (
|
|
791
|
-
this.pendingScrollAnchor =
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
followOnAppend,
|
|
795
|
-
anchorDelta,
|
|
796
|
-
]
|
|
827
|
+
if (followOnAppend) {
|
|
828
|
+
this.pendingScrollAnchor = { kind: 'follow', behavior: followOnAppend }
|
|
829
|
+
} else if (anchorDelta !== null) {
|
|
830
|
+
this.pendingScrollAnchor = { kind: 'anchor', delta: anchorDelta }
|
|
797
831
|
}
|
|
798
832
|
}
|
|
799
833
|
|
|
@@ -815,10 +849,7 @@ export class Virtualizer<
|
|
|
815
849
|
console.info('correction', delta)
|
|
816
850
|
}
|
|
817
851
|
|
|
818
|
-
if (
|
|
819
|
-
isIOSWebKit() &&
|
|
820
|
-
(this.isScrolling || this._iosTouching || this._iosJustTouchEnded)
|
|
821
|
-
) {
|
|
852
|
+
if (isIOSWebKit() && this._iosInMotion()) {
|
|
822
853
|
// An index scroll retargets from fresh measurements in reconcileScroll;
|
|
823
854
|
// replaying this delta after it lands would count it twice.
|
|
824
855
|
if (this.scrollState?.index == null) {
|
|
@@ -839,7 +870,7 @@ export class Virtualizer<
|
|
|
839
870
|
? { target, maxAtWrite }
|
|
840
871
|
: null
|
|
841
872
|
this._scrollToOffset(this.getScrollOffset(), {
|
|
842
|
-
adjustments:
|
|
873
|
+
adjustments: this.scrollAdjustments + delta,
|
|
843
874
|
behavior,
|
|
844
875
|
})
|
|
845
876
|
// Eagerly carry the intended target in `scrollOffset` so callers that
|
|
@@ -860,7 +891,7 @@ export class Virtualizer<
|
|
|
860
891
|
// Upper-bound overflow stays untouched — it is transiently
|
|
861
892
|
// legitimate mid-prepend while the consumer's sizer catches up.
|
|
862
893
|
if (this.scrollOffset < 0) this.scrollOffset = 0
|
|
863
|
-
this.
|
|
894
|
+
if (this.pendingWrite) this.pendingWrite.adjustments = 0
|
|
864
895
|
}
|
|
865
896
|
return true
|
|
866
897
|
}
|
|
@@ -912,14 +943,14 @@ export class Virtualizer<
|
|
|
912
943
|
// capture), so the new element never reports it. Carrying the state
|
|
913
944
|
// over would replay a stale deferred delta on the new element's first
|
|
914
945
|
// flush, and a cleanup that lands mid-touch or inside the post-touchend
|
|
915
|
-
// grace window would strand
|
|
916
|
-
//
|
|
917
|
-
//
|
|
918
|
-
// element until its next touch cycle.
|
|
946
|
+
// grace window would strand `_iosTouch` (the listeners that end it were
|
|
947
|
+
// just removed), deferring every adjustment on the new element until
|
|
948
|
+
// its next touch cycle.
|
|
919
949
|
this._iosDeferredAdjustment = 0
|
|
920
|
-
this.
|
|
921
|
-
this._iosJustTouchEnded = false
|
|
950
|
+
this._endIosTouch()
|
|
922
951
|
this._clampedAdjustment = null
|
|
952
|
+
// A write to the old element will never be read back.
|
|
953
|
+
this.pendingWrite = null
|
|
923
954
|
this.scrollElement = null
|
|
924
955
|
this.targetWindow = null
|
|
925
956
|
}
|
|
@@ -969,28 +1000,21 @@ export class Virtualizer<
|
|
|
969
1000
|
// no-op re-emit that Safari/Firefox fire after a re-render's layout
|
|
970
1001
|
// (Chrome doesn't). Treating it as scrolling re-arms `isScrolling`,
|
|
971
1002
|
// which forces a render that triggers another such event: an
|
|
972
|
-
// infinite re-render loop. Ignore it. (Self-writes are
|
|
973
|
-
//
|
|
1003
|
+
// infinite re-render loop. Ignore it. (Self-writes are matched to
|
|
1004
|
+
// `pendingWrite` just below.)
|
|
974
1005
|
if (
|
|
975
1006
|
isScrolling &&
|
|
976
|
-
this.
|
|
1007
|
+
this.pendingWrite === null &&
|
|
977
1008
|
offset === this.scrollOffset
|
|
978
1009
|
) {
|
|
979
1010
|
return
|
|
980
1011
|
}
|
|
981
1012
|
|
|
982
|
-
//
|
|
983
|
-
//
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
// intended value will already have been consumed by a prior
|
|
988
|
-
// scroll event and cleared.
|
|
989
|
-
if (
|
|
990
|
-
this._intendedScrollOffset !== null &&
|
|
991
|
-
Math.abs(offset - this._intendedScrollOffset) < 1.5
|
|
992
|
-
) {
|
|
993
|
-
offset = this._intendedScrollOffset
|
|
1013
|
+
// The read-back of our own write: adopt the exact target over the
|
|
1014
|
+
// browser's rounded value (see `pendingWrite`).
|
|
1015
|
+
const pending = this.pendingWrite
|
|
1016
|
+
if (pending !== null && Math.abs(offset - pending.target) < 1.5) {
|
|
1017
|
+
offset = pending.target
|
|
994
1018
|
} else if (
|
|
995
1019
|
this.scrollState?.index != null &&
|
|
996
1020
|
// A non-smooth write lands at once, so any other scroll event is
|
|
@@ -1017,8 +1041,7 @@ export class Virtualizer<
|
|
|
1017
1041
|
// need to reconcile.
|
|
1018
1042
|
this.scrollState = null
|
|
1019
1043
|
}
|
|
1020
|
-
this.
|
|
1021
|
-
if (this.scrollState) this.scrollState.writtenOffset = null
|
|
1044
|
+
this.pendingWrite = null
|
|
1022
1045
|
|
|
1023
1046
|
// A pending clamped compensation write (#1258) survives only its
|
|
1024
1047
|
// own read-back, which the browser reports at the scroll max we
|
|
@@ -1031,7 +1054,6 @@ export class Virtualizer<
|
|
|
1031
1054
|
this._clampedAdjustment = null
|
|
1032
1055
|
}
|
|
1033
1056
|
|
|
1034
|
-
this.scrollAdjustments = 0
|
|
1035
1057
|
// If the offset hasn't moved, this is the echo of our own
|
|
1036
1058
|
// adjustment write — `applyScrollAdjustment` already folded it
|
|
1037
1059
|
// into `scrollOffset`. There's no direction to infer, so leave
|
|
@@ -1066,28 +1088,27 @@ export class Virtualizer<
|
|
|
1066
1088
|
if ('addEventListener' in this.scrollElement) {
|
|
1067
1089
|
const scrollEl = this.scrollElement as unknown as EventTarget
|
|
1068
1090
|
const onTouchStart = () => {
|
|
1069
|
-
|
|
1070
|
-
this.
|
|
1071
|
-
|
|
1072
|
-
this.targetWindow.clearTimeout(this._iosTouchEndTimerId)
|
|
1073
|
-
this._iosTouchEndTimerId = null
|
|
1074
|
-
}
|
|
1091
|
+
// A finger back down inside the grace window cancels its tail.
|
|
1092
|
+
this._endIosTouch()
|
|
1093
|
+
this._iosTouch = { phase: 'down' }
|
|
1075
1094
|
}
|
|
1076
1095
|
const onTouchEnd = () => {
|
|
1077
|
-
this.
|
|
1096
|
+
this._endIosTouch()
|
|
1078
1097
|
if (!isIOSWebKit() || this.targetWindow == null) {
|
|
1079
|
-
// Non-iOS: nothing more to track.
|
|
1098
|
+
// Non-iOS: nothing more to track.
|
|
1080
1099
|
return
|
|
1081
1100
|
}
|
|
1082
|
-
this._iosJustTouchEnded = true
|
|
1083
1101
|
// After ~150 ms with no scroll/touch events, momentum is done.
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
this.
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1102
|
+
const tail = {
|
|
1103
|
+
phase: 'tail' as const,
|
|
1104
|
+
timerId: this.targetWindow.setTimeout(() => {
|
|
1105
|
+
if (this._iosTouch === tail) this._iosTouch = null
|
|
1106
|
+
// After the grace window, attempt to flush. The scroll event
|
|
1107
|
+
// for momentum decay may have already fired before our timer.
|
|
1108
|
+
this._flushIosDeferredIfReady()
|
|
1109
|
+
}, 150),
|
|
1110
|
+
}
|
|
1111
|
+
this._iosTouch = tail
|
|
1091
1112
|
}
|
|
1092
1113
|
scrollEl.addEventListener(
|
|
1093
1114
|
'touchstart',
|
|
@@ -1110,10 +1131,7 @@ export class Virtualizer<
|
|
|
1110
1131
|
scrollEl.removeEventListener('touchstart', onTouchStart)
|
|
1111
1132
|
scrollEl.removeEventListener('touchend', onTouchEnd)
|
|
1112
1133
|
scrollEl.removeEventListener('touchcancel', onTouchEnd)
|
|
1113
|
-
|
|
1114
|
-
this.targetWindow.clearTimeout(this._iosTouchEndTimerId)
|
|
1115
|
-
this._iosTouchEndTimerId = null
|
|
1116
|
-
}
|
|
1134
|
+
this._endIosTouch()
|
|
1117
1135
|
})
|
|
1118
1136
|
}
|
|
1119
1137
|
|
|
@@ -1127,29 +1145,21 @@ export class Virtualizer<
|
|
|
1127
1145
|
this.pendingScrollAnchor = null
|
|
1128
1146
|
|
|
1129
1147
|
if (anchor && this.scrollElement && this.options.enabled) {
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
if (key !== null && !followOnAppend) {
|
|
1148
|
+
if (anchor.kind === 'anchor') {
|
|
1133
1149
|
// scrollOffset was eagerly adjusted in setOptions so the
|
|
1134
1150
|
// virtualizer already computed the correct range during render.
|
|
1135
1151
|
// Now sync the browser's actual scroll position to match.
|
|
1136
|
-
// Skip when followOnAppend is set — scrollToEnd will handle it.
|
|
1137
1152
|
//
|
|
1138
1153
|
// On iOS WebKit, writing scrollTop during touch/momentum cancels
|
|
1139
1154
|
// the in-flight scroll. Defer the DOM sync the same way
|
|
1140
1155
|
// applyScrollAdjustment does — accumulate the delta and let
|
|
1141
1156
|
// _flushIosDeferredIfReady handle it once the scroll settles.
|
|
1142
|
-
if (
|
|
1143
|
-
|
|
1144
|
-
(this.isScrolling || this._iosTouching || this._iosJustTouchEnded)
|
|
1145
|
-
) {
|
|
1146
|
-
if (anchorDelta !== 0) {
|
|
1147
|
-
this._iosDeferredAdjustment += anchorDelta
|
|
1148
|
-
}
|
|
1157
|
+
if (isIOSWebKit() && this._iosInMotion()) {
|
|
1158
|
+
this._iosDeferredAdjustment += anchor.delta
|
|
1149
1159
|
} else if (
|
|
1150
1160
|
isSmoothScroll(this.scrollState) &&
|
|
1151
1161
|
!approxEqual(
|
|
1152
|
-
this.getScrollOffset() -
|
|
1162
|
+
this.getScrollOffset() - anchor.delta,
|
|
1153
1163
|
this.scrollState.lastTargetOffset,
|
|
1154
1164
|
)
|
|
1155
1165
|
) {
|
|
@@ -1167,10 +1177,8 @@ export class Virtualizer<
|
|
|
1167
1177
|
behavior: undefined,
|
|
1168
1178
|
})
|
|
1169
1179
|
}
|
|
1170
|
-
}
|
|
1171
|
-
|
|
1172
|
-
if (followOnAppend) {
|
|
1173
|
-
this.scrollToEnd({ behavior: followOnAppend })
|
|
1180
|
+
} else {
|
|
1181
|
+
this.scrollToEnd({ behavior: anchor.behavior })
|
|
1174
1182
|
}
|
|
1175
1183
|
}
|
|
1176
1184
|
|
|
@@ -1208,15 +1216,25 @@ export class Virtualizer<
|
|
|
1208
1216
|
}
|
|
1209
1217
|
}
|
|
1210
1218
|
|
|
1219
|
+
// The element is moving or about to: a scroll in progress, or a touch
|
|
1220
|
+
// gesture that may still turn into one (see `_iosTouch`). On iOS a write
|
|
1221
|
+
// now would cancel the momentum, so adjustments wait for the flush below.
|
|
1222
|
+
private _iosInMotion = () => this.isScrolling || this._iosTouch !== null
|
|
1223
|
+
|
|
1224
|
+
private _endIosTouch = () => {
|
|
1225
|
+
if (this._iosTouch?.phase === 'tail' && this.targetWindow != null) {
|
|
1226
|
+
this.targetWindow.clearTimeout(this._iosTouch.timerId)
|
|
1227
|
+
}
|
|
1228
|
+
this._iosTouch = null
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1211
1231
|
// Apply any accumulated iOS-deferred scroll adjustment, but only when we're
|
|
1212
1232
|
// truly settled — not actively scrolling, not under an active touch, and
|
|
1213
1233
|
// past the post-touchend grace window. Called from the scroll callback
|
|
1214
1234
|
// and the touchend grace-timer.
|
|
1215
1235
|
private _flushIosDeferredIfReady = () => {
|
|
1216
1236
|
if (this._iosDeferredAdjustment === 0) return
|
|
1217
|
-
if (this.
|
|
1218
|
-
if (this._iosTouching) return
|
|
1219
|
-
if (this._iosJustTouchEnded) return
|
|
1237
|
+
if (this._iosInMotion()) return
|
|
1220
1238
|
// Phase 2b: Safari elastic-overscroll (rubber-band) lets scrollTop go
|
|
1221
1239
|
// negative or beyond scrollHeight - clientHeight. Writing scrollTop
|
|
1222
1240
|
// while in that zone snaps the page back to the clamped value at the
|
|
@@ -1239,7 +1257,7 @@ export class Virtualizer<
|
|
|
1239
1257
|
// landing between now and the resulting scroll event computes from the
|
|
1240
1258
|
// post-flush offset rather than the stale one.
|
|
1241
1259
|
this._scrollToOffset(cur, {
|
|
1242
|
-
adjustments:
|
|
1260
|
+
adjustments: this.scrollAdjustments + delta,
|
|
1243
1261
|
behavior: undefined,
|
|
1244
1262
|
})
|
|
1245
1263
|
}
|
|
@@ -1911,10 +1929,11 @@ export class Virtualizer<
|
|
|
1911
1929
|
// command. Compensation writes and the prepend anchor sync update
|
|
1912
1930
|
// `scrollOffset` eagerly, and the iOS flush writes exactly
|
|
1913
1931
|
// `scrollOffset + scrollAdjustments`, so none of them count.
|
|
1932
|
+
const writtenOffset = this.getCommandWrittenOffset()
|
|
1914
1933
|
const awaitingCommandReadBack =
|
|
1915
|
-
|
|
1934
|
+
writtenOffset !== null &&
|
|
1916
1935
|
!approxEqual(
|
|
1917
|
-
|
|
1936
|
+
writtenOffset,
|
|
1918
1937
|
this.getScrollOffset() + this.scrollAdjustments,
|
|
1919
1938
|
)
|
|
1920
1939
|
|
|
@@ -2100,29 +2119,12 @@ export class Virtualizer<
|
|
|
2100
2119
|
toOffset: number,
|
|
2101
2120
|
{ align = 'start', behavior = 'auto' }: ScrollToOffsetOptions = {},
|
|
2102
2121
|
) => {
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
this._iosDeferredAdjustment = 0
|
|
2110
|
-
|
|
2111
|
-
const offset = this.getOffsetForAlignment(toOffset, align)
|
|
2112
|
-
|
|
2113
|
-
const now = this.now()
|
|
2114
|
-
this.scrollState = {
|
|
2115
|
-
index: null,
|
|
2116
|
-
align,
|
|
2117
|
-
startedAt: now,
|
|
2118
|
-
lastTargetOffset: offset,
|
|
2119
|
-
stableFrames: 0,
|
|
2120
|
-
phase: initialScrollPhase(behavior, false),
|
|
2121
|
-
}
|
|
2122
|
-
|
|
2123
|
-
this._scrollToOffset(offset, { adjustments: undefined, behavior })
|
|
2124
|
-
|
|
2125
|
-
this.scheduleScrollReconcile()
|
|
2122
|
+
this._startScroll(
|
|
2123
|
+
{ index: null, align },
|
|
2124
|
+
this.getOffsetForAlignment(toOffset, align),
|
|
2125
|
+
behavior,
|
|
2126
|
+
'absolute',
|
|
2127
|
+
)
|
|
2126
2128
|
}
|
|
2127
2129
|
|
|
2128
2130
|
scrollToIndex = (
|
|
@@ -2132,34 +2134,19 @@ export class Virtualizer<
|
|
|
2132
2134
|
behavior = 'auto',
|
|
2133
2135
|
}: ScrollToIndexOptions = {},
|
|
2134
2136
|
) => {
|
|
2135
|
-
// See scrollToOffset: an absolute target invalidates any pending
|
|
2136
|
-
// iOS-deferred compensation.
|
|
2137
|
-
this._iosDeferredAdjustment = 0
|
|
2138
|
-
|
|
2139
2137
|
index = Math.max(0, Math.min(index, this.options.count - 1))
|
|
2140
2138
|
|
|
2141
2139
|
const offsetInfo = this.getOffsetForIndex(index, initialAlign)
|
|
2142
2140
|
if (!offsetInfo) {
|
|
2141
|
+
// No target (an empty list), so nothing is written; still drop the
|
|
2142
|
+
// deferral like every absolute command, or it replays once the list
|
|
2143
|
+
// refills.
|
|
2144
|
+
this._iosDeferredAdjustment = 0
|
|
2143
2145
|
return
|
|
2144
2146
|
}
|
|
2145
2147
|
const [offset, align] = offsetInfo
|
|
2146
2148
|
|
|
2147
|
-
|
|
2148
|
-
this.scrollState = {
|
|
2149
|
-
index,
|
|
2150
|
-
align,
|
|
2151
|
-
startedAt: now,
|
|
2152
|
-
lastTargetOffset: offset,
|
|
2153
|
-
stableFrames: 0,
|
|
2154
|
-
phase: initialScrollPhase(
|
|
2155
|
-
behavior,
|
|
2156
|
-
approxEqual(offset, this.getScrollOffset()),
|
|
2157
|
-
),
|
|
2158
|
-
}
|
|
2159
|
-
|
|
2160
|
-
this._scrollToOffset(offset, { adjustments: undefined, behavior })
|
|
2161
|
-
|
|
2162
|
-
this.scheduleScrollReconcile()
|
|
2149
|
+
this._startScroll({ index, align }, offset, behavior, 'absolute')
|
|
2163
2150
|
}
|
|
2164
2151
|
|
|
2165
2152
|
scrollBy = (
|
|
@@ -2170,49 +2157,28 @@ export class Virtualizer<
|
|
|
2170
2157
|
// Clamp: a write past either edge that leaves the element where it was
|
|
2171
2158
|
// fires no scroll event, so nothing would clear an unreachable offset.
|
|
2172
2159
|
const offset = this.getOffsetForAlignment(
|
|
2173
|
-
(this.
|
|
2160
|
+
(this.getCommandWrittenOffset() ?? this.getScrollOffset()) + delta,
|
|
2174
2161
|
'start',
|
|
2175
2162
|
)
|
|
2176
|
-
const now = this.now()
|
|
2177
2163
|
|
|
2178
|
-
this.
|
|
2179
|
-
index: null,
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
phase: initialScrollPhase(behavior, false),
|
|
2185
|
-
}
|
|
2186
|
-
|
|
2187
|
-
this._scrollToOffset(offset, { adjustments: undefined, behavior })
|
|
2188
|
-
|
|
2189
|
-
this.scheduleScrollReconcile()
|
|
2164
|
+
this._startScroll(
|
|
2165
|
+
{ index: null, align: 'start' },
|
|
2166
|
+
offset,
|
|
2167
|
+
behavior,
|
|
2168
|
+
'relative',
|
|
2169
|
+
)
|
|
2190
2170
|
}
|
|
2191
2171
|
|
|
2192
2172
|
scrollToEnd = ({ behavior = 'auto' }: ScrollToEndOptions = {}) => {
|
|
2193
2173
|
if (this.options.count > 0) {
|
|
2194
2174
|
// Target the browser's max scroll rather than the last item's end-align
|
|
2195
2175
|
// offset, so paddingEnd is included and isAtEnd() holds afterwards.
|
|
2196
|
-
this.
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
align: 'end',
|
|
2203
|
-
toEnd: true,
|
|
2204
|
-
startedAt: this.now(),
|
|
2205
|
-
lastTargetOffset: offset,
|
|
2206
|
-
stableFrames: 0,
|
|
2207
|
-
phase: initialScrollPhase(
|
|
2208
|
-
behavior,
|
|
2209
|
-
approxEqual(offset, this.getScrollOffset()),
|
|
2210
|
-
),
|
|
2211
|
-
}
|
|
2212
|
-
|
|
2213
|
-
this._scrollToOffset(offset, { adjustments: undefined, behavior })
|
|
2214
|
-
|
|
2215
|
-
this.scheduleScrollReconcile()
|
|
2176
|
+
this._startScroll(
|
|
2177
|
+
{ index: this.options.count - 1, align: 'end', toEnd: true },
|
|
2178
|
+
Math.max(this.getMaxScrollOffset(), 0),
|
|
2179
|
+
behavior,
|
|
2180
|
+
'absolute',
|
|
2181
|
+
)
|
|
2216
2182
|
return
|
|
2217
2183
|
}
|
|
2218
2184
|
|
|
@@ -2221,6 +2187,42 @@ export class Virtualizer<
|
|
|
2221
2187
|
})
|
|
2222
2188
|
}
|
|
2223
2189
|
|
|
2190
|
+
// Every scroll command starts here: it becomes the in-flight command,
|
|
2191
|
+
// writes its first target and leaves reconcile to retarget and retire it.
|
|
2192
|
+
//
|
|
2193
|
+
// An absolute command derives its target from current measurements, so
|
|
2194
|
+
// any iOS-deferred compensation still pending is stale by definition —
|
|
2195
|
+
// the command already accounts for the measurements the delta was
|
|
2196
|
+
// compensating for. Drop it so _flushIosDeferredIfReady doesn't replay it
|
|
2197
|
+
// onto the just-established position. A relative command (scrollBy)
|
|
2198
|
+
// builds on the current offset, so it keeps the deferral.
|
|
2199
|
+
//
|
|
2200
|
+
// An index or end target that is already at its offset starts a smooth
|
|
2201
|
+
// scroll as landed; an offset target never does, so its first scroll
|
|
2202
|
+
// event is read as the user's (see the scroll handler in `_willUpdate`).
|
|
2203
|
+
private _startScroll(
|
|
2204
|
+
target: Pick<ScrollState, 'index' | 'align' | 'toEnd'>,
|
|
2205
|
+
offset: number,
|
|
2206
|
+
behavior: ScrollBehavior,
|
|
2207
|
+
kind: 'absolute' | 'relative',
|
|
2208
|
+
) {
|
|
2209
|
+
if (kind === 'absolute') {
|
|
2210
|
+
this._iosDeferredAdjustment = 0
|
|
2211
|
+
}
|
|
2212
|
+
this.scrollState = {
|
|
2213
|
+
...target,
|
|
2214
|
+
startedAt: this.now(),
|
|
2215
|
+
lastTargetOffset: offset,
|
|
2216
|
+
stableFrames: 0,
|
|
2217
|
+
phase: initialScrollPhase(
|
|
2218
|
+
behavior,
|
|
2219
|
+
target.index != null && approxEqual(offset, this.getScrollOffset()),
|
|
2220
|
+
),
|
|
2221
|
+
}
|
|
2222
|
+
this._scrollToOffset(offset, { adjustments: undefined, behavior })
|
|
2223
|
+
this.scheduleScrollReconcile()
|
|
2224
|
+
}
|
|
2225
|
+
|
|
2224
2226
|
// Drops the target of an in-flight scrollToIndex / scrollToOffset /
|
|
2225
2227
|
// scrollBy / scrollToEnd so a user gesture can take over the viewport.
|
|
2226
2228
|
// The scroll position is left untouched: writing it would stop iOS
|
|
@@ -2317,16 +2319,26 @@ export class Virtualizer<
|
|
|
2317
2319
|
behavior: ScrollBehavior | undefined
|
|
2318
2320
|
},
|
|
2319
2321
|
) => {
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
this.scrollState.writtenOffset =
|
|
2325
|
-
behavior === 'smooth' ? null : this._intendedScrollOffset
|
|
2322
|
+
this.pendingWrite = {
|
|
2323
|
+
target: offset + (adjustments ?? 0),
|
|
2324
|
+
smooth: behavior === 'smooth',
|
|
2325
|
+
adjustments: adjustments ?? this.scrollAdjustments,
|
|
2326
2326
|
}
|
|
2327
2327
|
this.options.scrollToFn(offset, { behavior, adjustments }, this)
|
|
2328
2328
|
}
|
|
2329
2329
|
|
|
2330
|
+
// Where the in-flight command's last instant write put the element, until
|
|
2331
|
+
// its scroll event arrives; the rows on screen are still the ones rendered
|
|
2332
|
+
// for the old `scrollOffset`. Null after a smooth write (the element is
|
|
2333
|
+
// somewhere on the way) and without a command, so a write the browser
|
|
2334
|
+
// ignored (no read-back) does not outlive its command.
|
|
2335
|
+
private getCommandWrittenOffset = () =>
|
|
2336
|
+
this.scrollState !== null &&
|
|
2337
|
+
this.pendingWrite !== null &&
|
|
2338
|
+
!this.pendingWrite.smooth
|
|
2339
|
+
? this.pendingWrite.target
|
|
2340
|
+
: null
|
|
2341
|
+
|
|
2330
2342
|
measure = () => {
|
|
2331
2343
|
// Reset pendingMin so the next getMeasurements rebuilds from index 0.
|
|
2332
2344
|
// Without this, a prior resizeItem() that left pendingMin > 0 would
|