@tanstack/virtual-core 3.19.4 → 3.19.5
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 +338 -180
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +32 -7
- package/dist/cjs/ios-gesture.cjs +129 -0
- package/dist/cjs/ios-gesture.cjs.map +1 -0
- package/dist/cjs/ios-gesture.d.cts +42 -0
- package/dist/cjs/scroll-classifier.cjs +85 -0
- package/dist/cjs/scroll-classifier.cjs.map +1 -0
- package/dist/cjs/scroll-classifier.d.cts +34 -0
- package/dist/esm/index.d.ts +32 -7
- package/dist/esm/index.js +338 -179
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/ios-gesture.d.ts +42 -0
- package/dist/esm/ios-gesture.js +129 -0
- package/dist/esm/ios-gesture.js.map +1 -0
- package/dist/esm/scroll-classifier.d.ts +34 -0
- package/dist/esm/scroll-classifier.js +85 -0
- package/dist/esm/scroll-classifier.js.map +1 -0
- package/package.json +1 -1
- package/src/index.ts +635 -317
- package/src/ios-gesture.ts +229 -0
- package/src/scroll-classifier.ts +232 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export declare const isIOSWebKit: () => boolean;
|
|
2
|
+
export declare const iosMomentumOverridesWrites: () => boolean;
|
|
3
|
+
export declare const _resetIOSDetectionForTests: () => void;
|
|
4
|
+
type IosTouch = {
|
|
5
|
+
phase: 'down';
|
|
6
|
+
moved: boolean;
|
|
7
|
+
} | {
|
|
8
|
+
phase: 'tail';
|
|
9
|
+
moved: boolean;
|
|
10
|
+
awaitingFling: boolean;
|
|
11
|
+
timerId: number;
|
|
12
|
+
} | {
|
|
13
|
+
phase: 'lateFling';
|
|
14
|
+
until: number;
|
|
15
|
+
} | null;
|
|
16
|
+
type TimerWindow = Pick<Window, 'setTimeout' | 'clearTimeout'>;
|
|
17
|
+
export declare class IosGesture {
|
|
18
|
+
private readonly host;
|
|
19
|
+
private _touch;
|
|
20
|
+
constructor(host: {
|
|
21
|
+
getWindow: () => TimerWindow | null;
|
|
22
|
+
now: () => number;
|
|
23
|
+
tailStillMoving: () => boolean;
|
|
24
|
+
onTailClosed: () => void;
|
|
25
|
+
});
|
|
26
|
+
get touch(): IosTouch;
|
|
27
|
+
inMotion: () => boolean;
|
|
28
|
+
dragging: () => boolean;
|
|
29
|
+
tail: () => "tap" | "fling" | null;
|
|
30
|
+
onScroll: ({ isScrolling, ownReadBack, moved, external, }: {
|
|
31
|
+
isScrolling: boolean;
|
|
32
|
+
ownReadBack: boolean;
|
|
33
|
+
moved: boolean;
|
|
34
|
+
external: boolean;
|
|
35
|
+
}) => boolean;
|
|
36
|
+
touchStart: () => void;
|
|
37
|
+
touchEnd: () => void;
|
|
38
|
+
end: () => void;
|
|
39
|
+
listen: (target: EventTarget) => () => void;
|
|
40
|
+
private _openTail;
|
|
41
|
+
}
|
|
42
|
+
export {};
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
let _isIOSResult;
|
|
2
|
+
const isIOSWebKit = () => {
|
|
3
|
+
if (_isIOSResult !== void 0) return _isIOSResult;
|
|
4
|
+
if (typeof navigator === "undefined") return _isIOSResult = false;
|
|
5
|
+
if (/iP(hone|od|ad)/.test(navigator.userAgent)) return _isIOSResult = true;
|
|
6
|
+
const mtp = navigator.maxTouchPoints;
|
|
7
|
+
return _isIOSResult = navigator.platform === "MacIntel" && mtp !== void 0 && mtp > 0;
|
|
8
|
+
};
|
|
9
|
+
let _iosMomentumOverridesWritesResult;
|
|
10
|
+
const iosMomentumOverridesWrites = () => {
|
|
11
|
+
if (_iosMomentumOverridesWritesResult !== void 0) {
|
|
12
|
+
return _iosMomentumOverridesWritesResult;
|
|
13
|
+
}
|
|
14
|
+
return _iosMomentumOverridesWritesResult = isIOSWebKit() && typeof CSS !== "undefined" && typeof CSS.supports === "function" && CSS.supports("overflow-anchor: auto");
|
|
15
|
+
};
|
|
16
|
+
const _resetIOSDetectionForTests = () => {
|
|
17
|
+
_isIOSResult = void 0;
|
|
18
|
+
_iosMomentumOverridesWritesResult = void 0;
|
|
19
|
+
};
|
|
20
|
+
const touchListenerOptions = {
|
|
21
|
+
capture: true,
|
|
22
|
+
passive: true
|
|
23
|
+
};
|
|
24
|
+
class IosGesture {
|
|
25
|
+
constructor(host) {
|
|
26
|
+
this.host = host;
|
|
27
|
+
this._touch = null;
|
|
28
|
+
this.inMotion = () => this._touch !== null && this._touch.phase !== "lateFling";
|
|
29
|
+
this.dragging = () => {
|
|
30
|
+
var _a;
|
|
31
|
+
return ((_a = this._touch) == null ? void 0 : _a.phase) === "down" && this._touch.moved;
|
|
32
|
+
};
|
|
33
|
+
this.tail = () => {
|
|
34
|
+
var _a;
|
|
35
|
+
return ((_a = this._touch) == null ? void 0 : _a.phase) === "tail" ? this._touch.moved ? "fling" : "tap" : null;
|
|
36
|
+
};
|
|
37
|
+
this.onScroll = ({
|
|
38
|
+
isScrolling,
|
|
39
|
+
ownReadBack,
|
|
40
|
+
moved,
|
|
41
|
+
external
|
|
42
|
+
}) => {
|
|
43
|
+
var _a, _b, _c;
|
|
44
|
+
if (isScrolling && ((_a = this._touch) == null ? void 0 : _a.phase) === "tail") {
|
|
45
|
+
this._openTail(this._touch.moved || !ownReadBack, false);
|
|
46
|
+
} else if (((_b = this._touch) == null ? void 0 : _b.phase) === "lateFling") {
|
|
47
|
+
if (this.host.now() > this._touch.until) {
|
|
48
|
+
this._touch = null;
|
|
49
|
+
} else if (external && isScrolling && moved) {
|
|
50
|
+
this._openTail(true, false);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (!ownReadBack && isScrolling && moved && ((_c = this._touch) == null ? void 0 : _c.phase) === "down") {
|
|
54
|
+
this._touch.moved = true;
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
return false;
|
|
58
|
+
};
|
|
59
|
+
this.touchStart = () => {
|
|
60
|
+
this.end();
|
|
61
|
+
this._touch = { phase: "down", moved: false };
|
|
62
|
+
};
|
|
63
|
+
this.touchEnd = () => {
|
|
64
|
+
var _a;
|
|
65
|
+
const moved = ((_a = this._touch) == null ? void 0 : _a.phase) === "down" && this._touch.moved;
|
|
66
|
+
this.end();
|
|
67
|
+
this._openTail(moved, moved);
|
|
68
|
+
};
|
|
69
|
+
this.end = () => {
|
|
70
|
+
var _a;
|
|
71
|
+
const win = this.host.getWindow();
|
|
72
|
+
if (((_a = this._touch) == null ? void 0 : _a.phase) === "tail" && win != null) {
|
|
73
|
+
win.clearTimeout(this._touch.timerId);
|
|
74
|
+
}
|
|
75
|
+
this._touch = null;
|
|
76
|
+
};
|
|
77
|
+
this.listen = (target) => {
|
|
78
|
+
const onTouchStart = () => this.touchStart();
|
|
79
|
+
const onTouchEnd = () => this.touchEnd();
|
|
80
|
+
target.addEventListener("touchstart", onTouchStart, touchListenerOptions);
|
|
81
|
+
target.addEventListener("touchend", onTouchEnd, touchListenerOptions);
|
|
82
|
+
target.addEventListener("touchcancel", onTouchEnd, touchListenerOptions);
|
|
83
|
+
return () => {
|
|
84
|
+
target.removeEventListener(
|
|
85
|
+
"touchstart",
|
|
86
|
+
onTouchStart,
|
|
87
|
+
touchListenerOptions
|
|
88
|
+
);
|
|
89
|
+
target.removeEventListener("touchend", onTouchEnd, touchListenerOptions);
|
|
90
|
+
target.removeEventListener(
|
|
91
|
+
"touchcancel",
|
|
92
|
+
onTouchEnd,
|
|
93
|
+
touchListenerOptions
|
|
94
|
+
);
|
|
95
|
+
this.end();
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
this._openTail = (moved, awaitingFling) => {
|
|
99
|
+
const win = this.host.getWindow();
|
|
100
|
+
if (!isIOSWebKit() || win == null) return;
|
|
101
|
+
this.end();
|
|
102
|
+
const tail = {
|
|
103
|
+
phase: "tail",
|
|
104
|
+
moved,
|
|
105
|
+
awaitingFling,
|
|
106
|
+
// Only fires while this tail is current: `end` clears it otherwise.
|
|
107
|
+
timerId: win.setTimeout(() => {
|
|
108
|
+
if (this.host.tailStillMoving()) {
|
|
109
|
+
this._openTail(tail.moved, tail.awaitingFling);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
this._touch = tail.awaitingFling ? { phase: "lateFling", until: this.host.now() + 1e3 } : null;
|
|
113
|
+
this.host.onTailClosed();
|
|
114
|
+
}, 150)
|
|
115
|
+
};
|
|
116
|
+
this._touch = tail;
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
get touch() {
|
|
120
|
+
return this._touch;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
export {
|
|
124
|
+
IosGesture,
|
|
125
|
+
_resetIOSDetectionForTests,
|
|
126
|
+
iosMomentumOverridesWrites,
|
|
127
|
+
isIOSWebKit
|
|
128
|
+
};
|
|
129
|
+
//# sourceMappingURL=ios-gesture.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ios-gesture.js","sources":["../../src/ios-gesture.ts"],"sourcesContent":["// Browser-aware iOS detection. Programmatic `scrollTo`/`scrollTop` writes\n// during a momentum-scroll cancel the momentum on iOS WebKit, so we defer\n// scroll-position adjustments triggered by mid-scroll resizes until the\n// scroll settles. SSR-safe (returns false when navigator is unavailable).\nlet _isIOSResult: boolean | undefined\nexport const isIOSWebKit = (): boolean => {\n if (_isIOSResult !== undefined) return _isIOSResult\n if (typeof navigator === 'undefined') return (_isIOSResult = false)\n if (/iP(hone|od|ad)/.test(navigator.userAgent)) return (_isIOSResult = true)\n // iPadOS 13+ reports as MacIntel; touch-points distinguishes it from desktop.\n const mtp = (navigator as Navigator & { maxTouchPoints?: number })\n .maxTouchPoints\n return (_isIOSResult =\n navigator.platform === 'MacIntel' && mtp !== undefined && mtp > 0)\n}\n\n// iOS 27 changed what a scrollTop write does to a fling: it no longer stops\n// the momentum, the fling carries on along its own path and the write shows\n// for one frame (measured on the 27.0 simulator in #1307, device check still\n// open). WebKit shipped `overflow-anchor` in the same release, so that is\n// the feature check; iOS 26 and earlier report false. Cached like the iOS\n// check; SSR-safe.\nlet _iosMomentumOverridesWritesResult: boolean | undefined\nexport const iosMomentumOverridesWrites = (): boolean => {\n if (_iosMomentumOverridesWritesResult !== undefined) {\n return _iosMomentumOverridesWritesResult\n }\n return (_iosMomentumOverridesWritesResult =\n isIOSWebKit() &&\n typeof CSS !== 'undefined' &&\n typeof CSS.supports === 'function' &&\n CSS.supports('overflow-anchor: auto'))\n}\n\n// Test hook: reset the iOS detection caches. Not exported.\nexport const _resetIOSDetectionForTests = () => {\n _isIOSResult = undefined\n _iosMomentumOverridesWritesResult = undefined\n}\n\n// Touch listeners on the scroll element run in the capture phase, so a row\n// that stops a touch event's propagation cannot hide the gesture from the\n// iOS deferral.\nconst touchListenerOptions = {\n capture: true,\n passive: true,\n}\n\n// The touch gesture in progress. iOS WebKit ignores a scrollTop write while\n// a finger drags the content (one resting on it does not stop the write)\n// and cancels momentum on one after it, so adjustments are deferred through\n// the whole gesture: `down` from touchstart to touchend/touchcancel, then\n// `tail` while momentum runs. iOS fires no touch events during momentum, so\n// every scroll event re-arms the tail and it closes ~150ms after the last\n// one. Null between gestures, and after touchend on non-iOS, which arms no\n// tail. The tail record owns its timer: every replacement of the state goes\n// through `end`, which clears it, so a live timer always has its record.\n//\n// `moved`: the content moved under or after the finger other than by our\n// own write's read-back. While `down`, a scroll event that changed the\n// position sets it: the finger is dragging. The tail inherits it from the\n// drag, and any other scroll event re-arming the tail sets it: a fling can\n// be running. A tap's tail has it unset.\n//\n// `awaitingFling`: the tail follows a drag whose fling has not reported\n// yet. The first frame of a fling can come 150–250ms after touchend when\n// the main thread is busy (27.0 simulator, #1307); if the tail closes\n// first, `lateFling` waits until `until` for that frame to reopen it. It\n// defers nothing: other scroll events with no touch before them must not\n// defer corrections (#1250).\ntype IosTouch =\n | { phase: 'down'; moved: boolean }\n | { phase: 'tail'; moved: boolean; awaitingFling: boolean; timerId: number }\n | { phase: 'lateFling'; until: number }\n | null\n\ntype TimerWindow = Pick<Window, 'setTimeout' | 'clearTimeout'>\n\nexport class IosGesture {\n private _touch: IosTouch = null\n\n constructor(\n private readonly host: {\n // The scroll element's window, which runs the tail timer; null when\n // there is no element.\n getWindow: () => TimerWindow | null\n now: () => number\n // Asked when the tail's timer fires: true keeps it open another\n // 150ms (the fling still moves between scroll events).\n tailStillMoving: () => boolean\n // The tail closed on its own: a write is possible again.\n onTailClosed: () => void\n },\n ) {}\n\n get touch(): IosTouch {\n return this._touch\n }\n\n // A touch gesture owns the scroll: on iOS a write now would be ignored or\n // would cancel the momentum, so adjustments wait for the tail to close.\n // `isScrolling` is not part of it: any scroll event sets it, including\n // the read-back of our own write, and a programmatic scroll has no\n // momentum to protect (#1250).\n inMotion = () => this._touch !== null && this._touch.phase !== 'lateFling'\n\n // A finger is dragging the content, so iOS ignores our writes and no\n // read-back will come. A resting finger does not: the write lands.\n dragging = () => this._touch?.phase === 'down' && this._touch.moved\n\n // The open tail, if any: after a tap, or after a drag or fling. On iOS 27\n // a fling's momentum overrides a write within a frame (see\n // `iosMomentumOverridesWrites`).\n tail = (): 'tap' | 'fling' | null =>\n this._touch?.phase === 'tail' ? (this._touch.moved ? 'fling' : 'tap') : null\n\n // A scroll event reached the handler. `ownReadBack`: it is our own\n // write's read-back; `moved`: it changed the layout position; `external`:\n // the user or a fling moved it (not our write nor our smooth animation).\n // Returns whether it shows a finger dragging the content.\n onScroll = ({\n isScrolling,\n ownReadBack,\n moved,\n external,\n }: {\n isScrolling: boolean\n ownReadBack: boolean\n moved: boolean\n external: boolean\n }): boolean => {\n // Momentum after touchend fires a scroll event per frame but no touch\n // events, so each one re-arms the tail to span the fling. Only an open\n // tail is extended: a scroll event with no touch before it (our own\n // write's read-back) never opens one.\n if (isScrolling && this._touch?.phase === 'tail') {\n this._openTail(this._touch.moved || !ownReadBack, false)\n } else if (this._touch?.phase === 'lateFling') {\n // A drag's fling reporting after its tail closed reopens it.\n if (this.host.now() > this._touch.until) {\n this._touch = null\n } else if (external && isScrolling && moved) {\n this._openTail(true, false)\n }\n }\n // A finger that moved the content is dragging. Our own write landing\n // under a resting finger is not, and neither is a report at rest that\n // leaves the position where it was.\n if (!ownReadBack && isScrolling && moved && this._touch?.phase === 'down') {\n this._touch.moved = true\n return true\n }\n return false\n }\n\n touchStart = () => {\n // A finger back down inside the grace window cancels its tail.\n this.end()\n this._touch = { phase: 'down', moved: false }\n }\n\n touchEnd = () => {\n const moved = this._touch?.phase === 'down' && this._touch.moved\n this.end()\n this._openTail(moved, moved)\n }\n\n // Drop the gesture, and the tail's timer with it.\n end = () => {\n const win = this.host.getWindow()\n if (this._touch?.phase === 'tail' && win != null) {\n win.clearTimeout(this._touch.timerId)\n }\n this._touch = null\n }\n\n // Follow the touch events on `target`; the returned function stops and\n // drops the gesture. Listening is unconditional: the listeners are\n // passive and cheap, and on non-touch devices they never fire. The gating\n // by `isIOSWebKit()` lives where a write is decided.\n listen = (target: EventTarget) => {\n const onTouchStart = () => this.touchStart()\n const onTouchEnd = () => this.touchEnd()\n target.addEventListener('touchstart', onTouchStart, touchListenerOptions)\n target.addEventListener('touchend', onTouchEnd, touchListenerOptions)\n // Same handler as touchend: iOS can end a touch with touchcancel (system\n // gesture, interrupted scroll) and never fire touchend.\n target.addEventListener('touchcancel', onTouchEnd, touchListenerOptions)\n return () => {\n target.removeEventListener(\n 'touchstart',\n onTouchStart,\n touchListenerOptions,\n )\n target.removeEventListener('touchend', onTouchEnd, touchListenerOptions)\n target.removeEventListener(\n 'touchcancel',\n onTouchEnd,\n touchListenerOptions,\n )\n this.end()\n }\n }\n\n // (Re)open the tail after the finger lifts, closing ~150ms after the last\n // touchend or momentum frame. On non-iOS there is nothing to protect.\n private _openTail = (moved: boolean, awaitingFling: boolean) => {\n const win = this.host.getWindow()\n if (!isIOSWebKit() || win == null) return\n this.end()\n const tail = {\n phase: 'tail' as const,\n moved,\n awaitingFling,\n // Only fires while this tail is current: `end` clears it otherwise.\n timerId: win.setTimeout(() => {\n if (this.host.tailStillMoving()) {\n this._openTail(tail.moved, tail.awaitingFling)\n return\n }\n this._touch = tail.awaitingFling\n ? { phase: 'lateFling', until: this.host.now() + 1000 }\n : null\n this.host.onTailClosed()\n }, 150),\n }\n this._touch = tail\n }\n}\n"],"names":[],"mappings":"AAIA,IAAI;AACG,MAAM,cAAc,MAAe;AACxC,MAAI,iBAAiB,OAAW,QAAO;AACvC,MAAI,OAAO,cAAc,YAAa,QAAQ,eAAe;AAC7D,MAAI,iBAAiB,KAAK,UAAU,SAAS,UAAW,eAAe;AAEvE,QAAM,MAAO,UACV;AACH,SAAQ,eACN,UAAU,aAAa,cAAc,QAAQ,UAAa,MAAM;AACpE;AAQA,IAAI;AACG,MAAM,6BAA6B,MAAe;AACvD,MAAI,sCAAsC,QAAW;AACnD,WAAO;AAAA,EACT;AACA,SAAQ,oCACN,iBACA,OAAO,QAAQ,eACf,OAAO,IAAI,aAAa,cACxB,IAAI,SAAS,uBAAuB;AACxC;AAGO,MAAM,6BAA6B,MAAM;AAC9C,iBAAe;AACf,sCAAoC;AACtC;AAKA,MAAM,uBAAuB;AAAA,EAC3B,SAAS;AAAA,EACT,SAAS;AACX;AAgCO,MAAM,WAAW;AAAA,EAGtB,YACmB,MAWjB;AAXiB,SAAA,OAAA;AAHnB,SAAQ,SAAmB;AAyB3B,SAAA,WAAW,MAAM,KAAK,WAAW,QAAQ,KAAK,OAAO,UAAU;AAI/D,SAAA,WAAW,MAAA;AAxGb;AAwGmB,yBAAK,WAAL,mBAAa,WAAU,UAAU,KAAK,OAAO;AAAA;AAK9D,SAAA,OAAO;AA7GT;AA8GI,yBAAK,WAAL,mBAAa,WAAU,SAAU,KAAK,OAAO,QAAQ,UAAU,QAAS;AAAA;AAM1E,SAAA,WAAW,CAAC;AAAA,MACV;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IAAA,MAMa;AA9HjB;AAmII,UAAI,iBAAe,UAAK,WAAL,mBAAa,WAAU,QAAQ;AAChD,aAAK,UAAU,KAAK,OAAO,SAAS,CAAC,aAA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K,IAAA;AAAA,MACP;AAAA,IACF;AAIA,SAAQ,YAAY,CAAC,OAAgB,kBAA2B;AAC9D,YAAM,MAAM,KAAK,KAAK,UAAA;AACtB,UAAI,CAAC,YAAA,KAAiB,OAAO,KAAM;AACnC,WAAK,IAAA;AACL,YAAM,OAAO;AAAA,QACX,OAAO;AAAA,QACP;AAAA,QACA;AAAA;AAAA,QAEA,SAAS,IAAI,WAAW,MAAM;AAC5B,cAAI,KAAK,KAAK,mBAAmB;AAC/B,iBAAK,UAAU,KAAK,OAAO,KAAK,aAAa;AAC7C;AAAA,UACF;AACA,eAAK,SAAS,KAAK,gBACf,EAAE,OAAO,aAAa,OAAO,KAAK,KAAK,IAAA,IAAQ,IAAA,IAC/C;AACJ,eAAK,KAAK,aAAA;AAAA,QACZ,GAAG,GAAG;AAAA,MAAA;AAER,WAAK,SAAS;AAAA,IAChB;AAAA,EAtIG;AAAA,EAEH,IAAI,QAAkB;AACpB,WAAO,KAAK;AAAA,EACd;AAmIF;"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
type ScrollEventKind = 'spurious' | 'readBack' | 'clampedReadBack' | 'clampedEcho' | 'external';
|
|
2
|
+
export interface ScrollEventInput {
|
|
3
|
+
offset: number;
|
|
4
|
+
isScrolling: boolean;
|
|
5
|
+
elementMax: number | null;
|
|
6
|
+
holdOffset: number;
|
|
7
|
+
scrollOffset: number | null;
|
|
8
|
+
pendingWrite: {
|
|
9
|
+
target: number;
|
|
10
|
+
smooth: boolean;
|
|
11
|
+
lowMaxes?: Array<number>;
|
|
12
|
+
} | null;
|
|
13
|
+
clampedMaxAtWrite: number | null;
|
|
14
|
+
}
|
|
15
|
+
export declare function classifyScrollEvent({ offset, isScrolling, elementMax, holdOffset, scrollOffset, pendingWrite, clampedMaxAtWrite, }: ScrollEventInput): ScrollEventKind;
|
|
16
|
+
export type ScrollPhase = 'queued' | 'smooth' | 'smoothLanded' | 'smoothFinishing' | 'instant';
|
|
17
|
+
export interface RetireInput {
|
|
18
|
+
kind: ScrollEventKind;
|
|
19
|
+
offset: number;
|
|
20
|
+
command: {
|
|
21
|
+
index: number | null;
|
|
22
|
+
phase: ScrollPhase;
|
|
23
|
+
lastTargetOffset: number;
|
|
24
|
+
} | null;
|
|
25
|
+
target: () => number;
|
|
26
|
+
}
|
|
27
|
+
export declare function externalRetiresCommand({ kind, offset, command, target, }: RetireInput): boolean;
|
|
28
|
+
export interface ScrollSample {
|
|
29
|
+
t: number;
|
|
30
|
+
offset: number;
|
|
31
|
+
}
|
|
32
|
+
export declare function flingSpedUp(samples: ReadonlyArray<ScrollSample>): boolean;
|
|
33
|
+
export declare function flingCaught(samples: ReadonlyArray<ScrollSample>): boolean;
|
|
34
|
+
export {};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { approxEqual } from "./utils.js";
|
|
2
|
+
function classifyScrollEvent({
|
|
3
|
+
offset,
|
|
4
|
+
isScrolling,
|
|
5
|
+
elementMax,
|
|
6
|
+
holdOffset,
|
|
7
|
+
scrollOffset,
|
|
8
|
+
pendingWrite,
|
|
9
|
+
clampedMaxAtWrite
|
|
10
|
+
}) {
|
|
11
|
+
var _a;
|
|
12
|
+
if (isScrolling && pendingWrite === null && offset === scrollOffset) {
|
|
13
|
+
return "spurious";
|
|
14
|
+
}
|
|
15
|
+
if (pendingWrite !== null && Math.abs(offset - pendingWrite.target) < 1.5) {
|
|
16
|
+
return "readBack";
|
|
17
|
+
}
|
|
18
|
+
if (pendingWrite !== null && !pendingWrite.smooth && elementMax !== null && pendingWrite.target > elementMax + holdOffset + 0.5 && Math.abs(offset - (elementMax + holdOffset)) < 1.5) {
|
|
19
|
+
return "clampedReadBack";
|
|
20
|
+
}
|
|
21
|
+
if (pendingWrite !== null && !pendingWrite.smooth && ((_a = pendingWrite.lowMaxes) == null ? void 0 : _a.some((max) => Math.abs(offset - max) < 1.5))) {
|
|
22
|
+
return "clampedReadBack";
|
|
23
|
+
}
|
|
24
|
+
if (clampedMaxAtWrite !== null && Math.abs(offset - clampedMaxAtWrite) < 1.5) {
|
|
25
|
+
return "clampedEcho";
|
|
26
|
+
}
|
|
27
|
+
return "external";
|
|
28
|
+
}
|
|
29
|
+
const isAnimating = (phase) => phase === "smooth" || phase === "smoothFinishing";
|
|
30
|
+
function externalRetiresCommand({
|
|
31
|
+
kind,
|
|
32
|
+
offset,
|
|
33
|
+
command,
|
|
34
|
+
target
|
|
35
|
+
}) {
|
|
36
|
+
return kind !== "spurious" && kind !== "readBack" && // Our own write, clamped (`clampedReadBack`, #1310): the command is not
|
|
37
|
+
// retired by it, and reconcile retargets below the new max.
|
|
38
|
+
kind !== "clampedReadBack" && (command == null ? void 0 : command.index) != null && // Nothing has been written for a command waiting for the tail; these
|
|
39
|
+
// are the fling's events.
|
|
40
|
+
command.phase !== "queued" && // A non-smooth write lands at once, so any other scroll event is the
|
|
41
|
+
// user's, even before reconcile saw the target reached (the target may
|
|
42
|
+
// keep moving, or iOS ignored the write under a finger). A write the
|
|
43
|
+
// browser clamped because the sizer hasn't grown yet reads back at the
|
|
44
|
+
// max, which is also where the fresh target clamps, so the check below
|
|
45
|
+
// keeps it reconciling (#1294). While a smooth scroll's animation may
|
|
46
|
+
// still report, its events look like a user's, so it yields only once it
|
|
47
|
+
// has landed.
|
|
48
|
+
!isAnimating(command.phase) && !approxEqual(offset, command.lastTargetOffset) && !approxEqual(offset, target()) && // A compensation write's clamped echo (#1258) keeps the command. The
|
|
49
|
+
// order is deliberate, not tidiness: this clause stays after `target()`
|
|
50
|
+
// (and `clampedReadBack` before it) because `target()` can rebuild the
|
|
51
|
+
// measurements, so it must run for exactly the events it always did.
|
|
52
|
+
kind !== "clampedEcho";
|
|
53
|
+
}
|
|
54
|
+
const FLING_WINDOW_MS = 40;
|
|
55
|
+
function flingSpedUp(samples) {
|
|
56
|
+
const last = samples[samples.length - 1];
|
|
57
|
+
if (last === void 0) return false;
|
|
58
|
+
let j = samples.length - 2;
|
|
59
|
+
while (j >= 0 && last.t - samples[j].t < FLING_WINDOW_MS) j--;
|
|
60
|
+
if (j < 0) return false;
|
|
61
|
+
const mid = samples[j];
|
|
62
|
+
let k = j - 1;
|
|
63
|
+
while (k >= 0 && mid.t - samples[k].t < FLING_WINDOW_MS) k--;
|
|
64
|
+
if (k < 0) return false;
|
|
65
|
+
const first = samples[k];
|
|
66
|
+
const recent = (last.offset - mid.offset) / (last.t - mid.t);
|
|
67
|
+
const before = (mid.offset - first.offset) / (mid.t - first.t);
|
|
68
|
+
return recent * before >= 0 && Math.abs(recent) >= 2 * Math.abs(before) && Math.abs(recent) - Math.abs(before) >= 0.8;
|
|
69
|
+
}
|
|
70
|
+
function flingCaught(samples) {
|
|
71
|
+
const last = samples[samples.length - 1];
|
|
72
|
+
if (last === void 0) return false;
|
|
73
|
+
let j = samples.length - 2;
|
|
74
|
+
while (j >= 0 && last.t - samples[j].t < FLING_WINDOW_MS) j--;
|
|
75
|
+
if (j < 0) return false;
|
|
76
|
+
const from = samples[j];
|
|
77
|
+
return Math.abs(last.offset - from.offset) / (last.t - from.t) >= 0.05;
|
|
78
|
+
}
|
|
79
|
+
export {
|
|
80
|
+
classifyScrollEvent,
|
|
81
|
+
externalRetiresCommand,
|
|
82
|
+
flingCaught,
|
|
83
|
+
flingSpedUp
|
|
84
|
+
};
|
|
85
|
+
//# sourceMappingURL=scroll-classifier.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scroll-classifier.js","sources":["../../src/scroll-classifier.ts"],"sourcesContent":["import { approxEqual } from './utils'\n\n// Why a scroll event arrived, decided from the virtualizer's state as it was\n// before the event, before the scroll handler mutates anything. The first\n// match in this order wins:\n// spurious Safari/Firefox re-emit at the position we hold, nothing\n// pending\n// readBack our last write reporting back (within 1.5px of its\n// target)\n// clampedReadBack our last non-smooth write, clamped at the element's\n// current max or at a smaller max the layout passed\n// through since the write\n// clampedEcho a compensation write the browser clamped (#1258)\n// reporting at the max seen at write time\n// external anything else: the user, a fling, a smooth animation\n// frame\ntype ScrollEventKind =\n | 'spurious'\n | 'readBack'\n | 'clampedReadBack'\n | 'clampedEcho'\n | 'external'\n\nexport interface ScrollEventInput {\n // The reported position in layout coordinates (the element's offset plus\n // the held correction and a pending prepend anchor's delta).\n offset: number\n isScrolling: boolean\n // The element's own scroll max, read only while scrolling (null otherwise\n // or where it cannot be read).\n elementMax: number | null\n // How far the element sits behind the layout while a correction is held.\n holdOffset: number\n // The layout position the virtualizer holds.\n scrollOffset: number | null\n // Our last write to the element, until a scroll event reports it, with\n // the scroll maxima below its target the element had since (see\n // `lowMaxes` on `pendingWrite` in the virtualizer).\n pendingWrite: {\n target: number\n smooth: boolean\n lowMaxes?: Array<number>\n } | null\n // The element's max when a clamped compensation write was made (#1258),\n // or null when none is pending.\n clampedMaxAtWrite: number | null\n}\n\nexport function classifyScrollEvent({\n offset,\n isScrolling,\n elementMax,\n holdOffset,\n scrollOffset,\n pendingWrite,\n clampedMaxAtWrite,\n}: ScrollEventInput): ScrollEventKind {\n // A scroll event that reports movement but lands on the offset we already\n // hold — and isn't a self-write read-back — is a spurious no-op re-emit\n // that Safari/Firefox fire after a re-render's layout (Chrome doesn't).\n // Treating it as scrolling re-arms `isScrolling`, which forces a render\n // that triggers another such event: an infinite re-render loop.\n if (isScrolling && pendingWrite === null && offset === scrollOffset) {\n return 'spurious'\n }\n if (pendingWrite !== null && Math.abs(offset - pendingWrite.target) < 1.5) {\n return 'readBack'\n }\n // A non-smooth write past the element's max reads back at the max: ours,\n // clamped because the sizer shrank between the write and the report (rows\n // above the target measured smaller in a same-task render, #1310). The\n // element is where it says. A finger that drags to the max while such a\n // write is pending (iOS ignored it) is read the same way; the command then\n // retires at the next frame, its target clamped to the same max.\n if (\n pendingWrite !== null &&\n !pendingWrite.smooth &&\n elementMax !== null &&\n pendingWrite.target > elementMax + holdOffset + 0.5 &&\n Math.abs(offset - (elementMax + holdOffset)) < 1.5\n ) {\n return 'clampedReadBack'\n }\n // The same, clamped to a smaller max than the element's current one: the\n // sizer shrank below the target after the write (rows measured smaller,\n // a count change) and the browser clamped there, then it grew back, all\n // before the report. The clamp lands on one of those maxima; a position\n // between them is not one.\n if (\n pendingWrite !== null &&\n !pendingWrite.smooth &&\n pendingWrite.lowMaxes?.some((max) => Math.abs(offset - max) < 1.5)\n ) {\n return 'clampedReadBack'\n }\n if (\n clampedMaxAtWrite !== null &&\n Math.abs(offset - clampedMaxAtWrite) < 1.5\n ) {\n return 'clampedEcho'\n }\n return 'external'\n}\n\n// Where an in-flight scroll command is in its lifecycle, which decides how\n// its scroll events and measurements are read:\n// queued the first write waits for the momentum tail to close\n// (iOS 27, see `_startScroll`). Nothing has been written:\n// the fling's scroll events do not retire the command, a\n// drag does, and reconcile only keeps the safety valve\n// smooth a smooth animation is travelling to the target\n// smoothLanded a smooth scroll is at its target, waiting to retire\n// smoothFinishing a smooth scroll switched to instant writes for the\n// final approach; its animation can still report positions\n// instant instant writes\nexport type ScrollPhase =\n | 'queued'\n | 'smooth'\n | 'smoothLanded'\n | 'smoothFinishing'\n | 'instant'\n\n// The smooth animation may still be reporting positions, so a scroll event\n// that isn't the read-back of our own write may not be the user.\nconst isAnimating = (phase: ScrollPhase) =>\n phase === 'smooth' || phase === 'smoothFinishing'\n\nexport interface RetireInput {\n kind: ScrollEventKind\n // The reported position in layout coordinates, as classified.\n offset: number\n // The in-flight scroll command, or null.\n command: {\n index: number | null\n phase: ScrollPhase\n lastTargetOffset: number\n } | null\n // The command's target recomputed against the current layout. Called\n // only once every cheaper clause has passed, and at most once.\n target: () => number\n}\n\n// Whether a scroll event that is not the read-back of our own write retires\n// the in-flight index command: an external scroll moved away from its\n// target. Our own measurement updates (including clamped writes) still need\n// to reconcile.\nexport function externalRetiresCommand({\n kind,\n offset,\n command,\n target,\n}: RetireInput): boolean {\n return (\n kind !== 'spurious' &&\n kind !== 'readBack' &&\n // Our own write, clamped (`clampedReadBack`, #1310): the command is not\n // retired by it, and reconcile retargets below the new max.\n kind !== 'clampedReadBack' &&\n command?.index != null &&\n // Nothing has been written for a command waiting for the tail; these\n // are the fling's events.\n command.phase !== 'queued' &&\n // A non-smooth write lands at once, so any other scroll event is the\n // user's, even before reconcile saw the target reached (the target may\n // keep moving, or iOS ignored the write under a finger). A write the\n // browser clamped because the sizer hasn't grown yet reads back at the\n // max, which is also where the fresh target clamps, so the check below\n // keeps it reconciling (#1294). While a smooth scroll's animation may\n // still report, its events look like a user's, so it yields only once it\n // has landed.\n !isAnimating(command.phase) &&\n !approxEqual(offset, command.lastTargetOffset) &&\n !approxEqual(offset, target()) &&\n // A compensation write's clamped echo (#1258) keeps the command. The\n // order is deliberate, not tidiness: this clause stays after `target()`\n // (and `clampedReadBack` before it) because `target()` can rebuild the\n // measurements, so it must run for exactly the events it always did.\n kind !== 'clampedEcho'\n )\n}\n\n// A scroll position reported at a time, in the element's own coordinates.\nexport interface ScrollSample {\n t: number\n offset: number\n}\n\nconst FLING_WINDOW_MS = 40\n\n// Whether the scroll sped up: a finger flicked again or dragged on during a\n// fling. iOS fires no touch events for a touch that lands on a fling\n// (#1307), so this, and a turn against the fling, is all that shows it.\n// Momentum only slows down. Scroll events arrive 7–26ms apart and some\n// carry two frames, so one event can read 2–3× faster than its neighbours;\n// the speed is taken over the last 40ms and compared with the 40ms before.\n// On an iPhone (27.0.1) the flicks read 2.4–6× faster than the window before\n// and at least 1.5px/ms more, momentum alone at most 1.7× (#1307).\nexport function flingSpedUp(samples: ReadonlyArray<ScrollSample>): boolean {\n const last = samples[samples.length - 1]\n if (last === undefined) return false\n let j = samples.length - 2\n while (j >= 0 && last.t - samples[j]!.t < FLING_WINDOW_MS) j--\n if (j < 0) return false\n const mid = samples[j]!\n let k = j - 1\n while (k >= 0 && mid.t - samples[k]!.t < FLING_WINDOW_MS) k--\n if (k < 0) return false\n const first = samples[k]!\n const recent = (last.offset - mid.offset) / (last.t - mid.t)\n const before = (mid.offset - first.offset) / (mid.t - first.t)\n return (\n recent * before >= 0 &&\n Math.abs(recent) >= 2 * Math.abs(before) &&\n Math.abs(recent) - Math.abs(before) >= 0.8\n )\n}\n\n// Whether a fling that sent its last scroll event was still going at\n// 0.05px/ms or more over its last 40ms: a finger caught it. A fling that\n// ends on its own fades to at most 0.015px/ms first; taps stopped one at\n// 0.21–1.43px/ms (iPhones on 27.0.1, #1307). The finger stays down with no\n// touch events, and iOS undoes a write made under it. A tap in the last\n// pixels of a fling reads as its end.\nexport function flingCaught(samples: ReadonlyArray<ScrollSample>): boolean {\n const last = samples[samples.length - 1]\n if (last === undefined) return false\n let j = samples.length - 2\n while (j >= 0 && last.t - samples[j]!.t < FLING_WINDOW_MS) j--\n if (j < 0) return false\n const from = samples[j]!\n return Math.abs(last.offset - from.offset) / (last.t - from.t) >= 0.05\n}\n"],"names":[],"mappings":";AAgDO,SAAS,oBAAoB;AAAA,EAClC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAAsC;;AAMpC,MAAI,eAAe,iBAAiB,QAAQ,WAAW,cAAc;AACnE,WAAO;AAAA,EACT;AACA,MAAI,iBAAiB,QAAQ,KAAK,IAAI,SAAS,aAAa,MAAM,IAAI,KAAK;AACzE,WAAO;AAAA,EACT;AAOA,MACE,iBAAiB,QACjB,CAAC,aAAa,UACd,eAAe,QACf,aAAa,SAAS,aAAa,aAAa,OAChD,KAAK,IAAI,UAAU,aAAa,WAAW,IAAI,KAC/C;AACA,WAAO;AAAA,EACT;AAMA,MACE,iBAAiB,QACjB,CAAC,aAAa,YACd,kBAAa,aAAb,mBAAuB,KAAK,CAAC,QAAQ,KAAK,IAAI,SAAS,GAAG,IAAI,OAC9D;AACA,WAAO;AAAA,EACT;AACA,MACE,sBAAsB,QACtB,KAAK,IAAI,SAAS,iBAAiB,IAAI,KACvC;AACA,WAAO;AAAA,EACT;AACA,SAAO;AACT;AAsBA,MAAM,cAAc,CAAC,UACnB,UAAU,YAAY,UAAU;AAqB3B,SAAS,uBAAuB;AAAA,EACrC;AAAA,EACA;AAAA,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