@opencode/ui 0.0.0-dev-703 → 0.0.0-dev-735
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/hooks/auto-scroll-machine.d.ts +89 -0
- package/dist/hooks/create-auto-scroll.d.ts +1 -1
- package/dist/overlays/tooltip/machine.d.ts +146 -0
- package/package.json +2 -2
- package/src/hooks/auto-scroll-machine.ts +261 -0
- package/src/hooks/create-auto-scroll.tsx +110 -205
- package/src/overlays/tooltip/machine.ts +303 -0
- package/src/overlays/tooltip/tooltip.tsx +160 -133
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
export declare const SETTLE_MS = 300;
|
|
2
|
+
export declare const END_EPSILON = 0.5;
|
|
3
|
+
export type OverflowAnchorMode = "none" | "auto" | "dynamic";
|
|
4
|
+
/**
|
|
5
|
+
* `idle`, `following` and `settling` stay pinned to the bottom; `settling` is the tail after work ends, while a text
|
|
6
|
+
* selection still pauses. `detached` is set while `<Suspense>` holds the viewport outside the document. `flight` is the
|
|
7
|
+
* `scrollTop` our own write produced; the next `scroll` event consumes it.
|
|
8
|
+
*/
|
|
9
|
+
export type AutoScrollState = {
|
|
10
|
+
readonly status: "idle" | "following";
|
|
11
|
+
readonly detached: boolean;
|
|
12
|
+
readonly flight: number | undefined;
|
|
13
|
+
} | {
|
|
14
|
+
readonly status: "settling";
|
|
15
|
+
readonly attempt: number;
|
|
16
|
+
readonly detached: boolean;
|
|
17
|
+
readonly flight: number | undefined;
|
|
18
|
+
} | {
|
|
19
|
+
readonly status: "paused";
|
|
20
|
+
readonly working: boolean;
|
|
21
|
+
readonly detached: boolean;
|
|
22
|
+
} | {
|
|
23
|
+
readonly status: "disposed";
|
|
24
|
+
};
|
|
25
|
+
export type AutoScrollEvent = {
|
|
26
|
+
readonly type: "working.changed";
|
|
27
|
+
readonly working: boolean;
|
|
28
|
+
readonly attempt: number;
|
|
29
|
+
} | {
|
|
30
|
+
readonly type: "settle.elapsed";
|
|
31
|
+
readonly attempt: number;
|
|
32
|
+
} | {
|
|
33
|
+
readonly type: "scroller.changed";
|
|
34
|
+
readonly overflow: boolean;
|
|
35
|
+
} | {
|
|
36
|
+
readonly type: "scroller.detached";
|
|
37
|
+
} | {
|
|
38
|
+
readonly type: "scroller.reattached";
|
|
39
|
+
readonly overflow: boolean;
|
|
40
|
+
} | {
|
|
41
|
+
readonly type: "resize";
|
|
42
|
+
readonly overflow: boolean;
|
|
43
|
+
} | {
|
|
44
|
+
readonly type: "scroll";
|
|
45
|
+
readonly overflow: boolean;
|
|
46
|
+
readonly scrollTop: number;
|
|
47
|
+
readonly maxScroll: number;
|
|
48
|
+
readonly threshold: number;
|
|
49
|
+
} | {
|
|
50
|
+
readonly type: "pause";
|
|
51
|
+
readonly overflow: boolean;
|
|
52
|
+
} | {
|
|
53
|
+
readonly type: "scroll.bottom";
|
|
54
|
+
readonly force: boolean;
|
|
55
|
+
} | {
|
|
56
|
+
readonly type: "flight.started";
|
|
57
|
+
readonly top: number;
|
|
58
|
+
} | {
|
|
59
|
+
readonly type: "disposed";
|
|
60
|
+
};
|
|
61
|
+
export type AutoScrollEffect = {
|
|
62
|
+
readonly type: "pin.bottom";
|
|
63
|
+
} | {
|
|
64
|
+
readonly type: "notify.interacted";
|
|
65
|
+
} | {
|
|
66
|
+
readonly type: "settle.start";
|
|
67
|
+
readonly attempt: number;
|
|
68
|
+
} | {
|
|
69
|
+
readonly type: "settle.stop";
|
|
70
|
+
};
|
|
71
|
+
export type AutoScrollTransition = {
|
|
72
|
+
readonly state: AutoScrollState;
|
|
73
|
+
readonly effects: readonly AutoScrollEffect[];
|
|
74
|
+
};
|
|
75
|
+
export declare const IDLE: AutoScrollState;
|
|
76
|
+
export declare const FOLLOWING: AutoScrollState;
|
|
77
|
+
export declare const PAUSED_IDLE: AutoScrollState;
|
|
78
|
+
export declare const PAUSED_WORKING: AutoScrollState;
|
|
79
|
+
export declare const DISPOSED: AutoScrollState;
|
|
80
|
+
export declare function createAutoScrollState(working: boolean): AutoScrollState;
|
|
81
|
+
export declare function transitionAutoScroll(state: AutoScrollState, event: AutoScrollEvent): AutoScrollTransition;
|
|
82
|
+
export declare function selectUserScrolled(state: AutoScrollState): state is {
|
|
83
|
+
readonly status: "paused";
|
|
84
|
+
readonly working: boolean;
|
|
85
|
+
readonly detached: boolean;
|
|
86
|
+
};
|
|
87
|
+
export declare function selectOverflowAnchor(state: AutoScrollState, mode?: OverflowAnchorMode): "auto" | "none";
|
|
88
|
+
/** Selecting text pauses only while content still moves under the selection. */
|
|
89
|
+
export declare function selectPausesOnSelection(state: AutoScrollState): boolean;
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every tooltip trigger runs this machine, so it never allocates: each reachable state, event and transition is a
|
|
3
|
+
* frozen module singleton, and an event that changes nothing returns the state it was given.
|
|
4
|
+
*/
|
|
5
|
+
export type TooltipState =
|
|
6
|
+
/** Kobalte is not mounted yet. Most triggers are never hovered, so it mounts on the first hover or focus. */
|
|
7
|
+
{
|
|
8
|
+
readonly status: "unmounted";
|
|
9
|
+
readonly disabled: boolean;
|
|
10
|
+
} | {
|
|
11
|
+
readonly status: "idle";
|
|
12
|
+
readonly hovered: boolean;
|
|
13
|
+
readonly disabled: boolean;
|
|
14
|
+
}
|
|
15
|
+
/** `pressing` lasts from a press on the trigger until that press ends, so its click keeps the tooltip open. */
|
|
16
|
+
| {
|
|
17
|
+
readonly status: "open";
|
|
18
|
+
readonly hovered: boolean;
|
|
19
|
+
readonly pressing: boolean;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* A popup the trigger opened (a menu, a popover) hides the tooltip. While it is open, a modal layer turns pointer
|
|
23
|
+
* events off for the page, so the trigger's own enter and leave events lie and `hovered` follows the pointer's
|
|
24
|
+
* position instead. Once it closes over a hovered trigger, the tooltip stays hidden until the pointer moves off the
|
|
25
|
+
* trigger or the trigger itself takes focus.
|
|
26
|
+
*/
|
|
27
|
+
| {
|
|
28
|
+
readonly status: "suppressed";
|
|
29
|
+
readonly popup: "open";
|
|
30
|
+
readonly hovered: boolean;
|
|
31
|
+
readonly disabled: boolean;
|
|
32
|
+
} | {
|
|
33
|
+
readonly status: "suppressed";
|
|
34
|
+
readonly popup: "closed";
|
|
35
|
+
readonly disabled: boolean;
|
|
36
|
+
}
|
|
37
|
+
/** The caller sets `forceOpen`. `expanded` tracks the popup so releasing control can suppress the tooltip. */
|
|
38
|
+
| {
|
|
39
|
+
readonly status: "controlled";
|
|
40
|
+
readonly open: boolean;
|
|
41
|
+
readonly hovered: boolean;
|
|
42
|
+
readonly expanded: boolean;
|
|
43
|
+
readonly disabled: boolean;
|
|
44
|
+
} | {
|
|
45
|
+
readonly status: "disposed";
|
|
46
|
+
};
|
|
47
|
+
export type TooltipEvent = {
|
|
48
|
+
readonly type: "props.synced";
|
|
49
|
+
readonly forceOpen: boolean | undefined;
|
|
50
|
+
readonly disabled: boolean;
|
|
51
|
+
}
|
|
52
|
+
/** The pointer entered or left the trigger. Touch never hovers. */
|
|
53
|
+
| {
|
|
54
|
+
readonly type: "pointer.entered";
|
|
55
|
+
} | {
|
|
56
|
+
readonly type: "pointer.left";
|
|
57
|
+
}
|
|
58
|
+
/** Where a pointer move landed while a popup suppresses the tooltip, measured against the trigger's box. */
|
|
59
|
+
| {
|
|
60
|
+
readonly type: "pointer.moved";
|
|
61
|
+
readonly inside: boolean;
|
|
62
|
+
}
|
|
63
|
+
/** The trigger element itself took focus. */
|
|
64
|
+
| {
|
|
65
|
+
readonly type: "focused";
|
|
66
|
+
} | {
|
|
67
|
+
readonly type: "trigger.pressed";
|
|
68
|
+
}
|
|
69
|
+
/** The press ended, after the click it caused, if any. */
|
|
70
|
+
| {
|
|
71
|
+
readonly type: "press.ended";
|
|
72
|
+
}
|
|
73
|
+
/** The microtask after the pointer left, once Kobalte has seen the tooltip open and started its skip-delay window. */
|
|
74
|
+
| {
|
|
75
|
+
readonly type: "leave.settled";
|
|
76
|
+
} | {
|
|
77
|
+
readonly type: "popup.changed";
|
|
78
|
+
readonly expanded: boolean;
|
|
79
|
+
} | {
|
|
80
|
+
readonly type: "kobalte.changed";
|
|
81
|
+
readonly open: boolean;
|
|
82
|
+
} | {
|
|
83
|
+
readonly type: "disposed";
|
|
84
|
+
};
|
|
85
|
+
/** `replay` forwards the mounting hover or focus to Kobalte, which attached its listeners too late to receive it. */
|
|
86
|
+
export type TooltipEffect = "replay" | "schedule-leave";
|
|
87
|
+
export type TooltipTransition = {
|
|
88
|
+
readonly state: TooltipState;
|
|
89
|
+
readonly effects: readonly TooltipEffect[];
|
|
90
|
+
};
|
|
91
|
+
export declare const POINTER_ENTERED: TooltipEvent;
|
|
92
|
+
export declare const POINTER_LEFT: TooltipEvent;
|
|
93
|
+
export declare const POINTER_INSIDE: TooltipEvent;
|
|
94
|
+
export declare const POINTER_OUTSIDE: TooltipEvent;
|
|
95
|
+
export declare const FOCUSED: TooltipEvent;
|
|
96
|
+
export declare const TRIGGER_PRESSED: TooltipEvent;
|
|
97
|
+
export declare const PRESS_ENDED: TooltipEvent;
|
|
98
|
+
export declare const LEAVE_SETTLED: TooltipEvent;
|
|
99
|
+
export declare const POPUP_EXPANDED: TooltipEvent;
|
|
100
|
+
export declare const POPUP_COLLAPSED: TooltipEvent;
|
|
101
|
+
export declare const KOBALTE_OPENED: TooltipEvent;
|
|
102
|
+
export declare const KOBALTE_CLOSED: TooltipEvent;
|
|
103
|
+
export declare const DISPOSED_EVENT: TooltipEvent;
|
|
104
|
+
export declare function syncPropsEvent(forceOpen: boolean | undefined, disabled: boolean): TooltipEvent;
|
|
105
|
+
export declare function createTooltipState(forceOpen: boolean | undefined, disabled: boolean): TooltipState;
|
|
106
|
+
export declare function transitionTooltip(state: TooltipState, event: TooltipEvent): TooltipTransition;
|
|
107
|
+
/** Kobalte is mounted: the trigger owns its listeners and the popup observer runs. */
|
|
108
|
+
export declare function selectTooltipMounted(state: TooltipState): state is {
|
|
109
|
+
readonly status: "idle";
|
|
110
|
+
readonly hovered: boolean;
|
|
111
|
+
readonly disabled: boolean;
|
|
112
|
+
} | {
|
|
113
|
+
readonly status: "open";
|
|
114
|
+
readonly hovered: boolean;
|
|
115
|
+
readonly pressing: boolean;
|
|
116
|
+
} | {
|
|
117
|
+
readonly status: "suppressed";
|
|
118
|
+
readonly popup: "open";
|
|
119
|
+
readonly hovered: boolean;
|
|
120
|
+
readonly disabled: boolean;
|
|
121
|
+
} | {
|
|
122
|
+
readonly status: "suppressed";
|
|
123
|
+
readonly popup: "closed";
|
|
124
|
+
readonly disabled: boolean;
|
|
125
|
+
} | {
|
|
126
|
+
readonly status: "controlled";
|
|
127
|
+
readonly open: boolean;
|
|
128
|
+
readonly hovered: boolean;
|
|
129
|
+
readonly expanded: boolean;
|
|
130
|
+
readonly disabled: boolean;
|
|
131
|
+
};
|
|
132
|
+
export declare function selectTooltipOpen(state: TooltipState): boolean;
|
|
133
|
+
export declare function selectTooltipForceOpen(state: TooltipState): boolean | undefined;
|
|
134
|
+
/** A press is in progress, so the driver listens for its end. */
|
|
135
|
+
export declare function selectTooltipPressing(state: TooltipState): boolean;
|
|
136
|
+
/** A popup suppresses the tooltip, so the driver reports where the pointer moves. */
|
|
137
|
+
export declare function selectTooltipTracking(state: TooltipState): state is {
|
|
138
|
+
readonly status: "suppressed";
|
|
139
|
+
readonly popup: "open";
|
|
140
|
+
readonly hovered: boolean;
|
|
141
|
+
readonly disabled: boolean;
|
|
142
|
+
} | {
|
|
143
|
+
readonly status: "suppressed";
|
|
144
|
+
readonly popup: "closed";
|
|
145
|
+
readonly disabled: boolean;
|
|
146
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opencode/ui",
|
|
3
|
-
"version": "0.0.0-dev-
|
|
3
|
+
"version": "0.0.0-dev-735",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -288,7 +288,7 @@
|
|
|
288
288
|
"dependencies": {
|
|
289
289
|
"@kitlangton/solid-motion": "0.2.2",
|
|
290
290
|
"@kobalte/core": "0.13.13",
|
|
291
|
-
"@opencode/util": "0.0.0-dev-
|
|
291
|
+
"@opencode/util": "0.0.0-dev-735",
|
|
292
292
|
"@pierre/diffs": "1.6.1",
|
|
293
293
|
"@pierre/highlights": "0.2.0",
|
|
294
294
|
"@solid-primitives/event-listener": "2.4.6",
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
export const SETTLE_MS = 300
|
|
2
|
+
|
|
3
|
+
export const END_EPSILON = 0.5
|
|
4
|
+
|
|
5
|
+
export type OverflowAnchorMode = "none" | "auto" | "dynamic"
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* `idle`, `following` and `settling` stay pinned to the bottom; `settling` is the tail after work ends, while a text
|
|
9
|
+
* selection still pauses. `detached` is set while `<Suspense>` holds the viewport outside the document. `flight` is the
|
|
10
|
+
* `scrollTop` our own write produced; the next `scroll` event consumes it.
|
|
11
|
+
*/
|
|
12
|
+
export type AutoScrollState =
|
|
13
|
+
| {
|
|
14
|
+
readonly status: "idle" | "following"
|
|
15
|
+
readonly detached: boolean
|
|
16
|
+
readonly flight: number | undefined
|
|
17
|
+
}
|
|
18
|
+
| {
|
|
19
|
+
readonly status: "settling"
|
|
20
|
+
readonly attempt: number
|
|
21
|
+
readonly detached: boolean
|
|
22
|
+
readonly flight: number | undefined
|
|
23
|
+
}
|
|
24
|
+
| { readonly status: "paused"; readonly working: boolean; readonly detached: boolean }
|
|
25
|
+
| { readonly status: "disposed" }
|
|
26
|
+
|
|
27
|
+
export type AutoScrollEvent =
|
|
28
|
+
| { readonly type: "working.changed"; readonly working: boolean; readonly attempt: number }
|
|
29
|
+
| { readonly type: "settle.elapsed"; readonly attempt: number }
|
|
30
|
+
| { readonly type: "scroller.changed"; readonly overflow: boolean }
|
|
31
|
+
| { readonly type: "scroller.detached" }
|
|
32
|
+
| { readonly type: "scroller.reattached"; readonly overflow: boolean }
|
|
33
|
+
| { readonly type: "resize"; readonly overflow: boolean }
|
|
34
|
+
| {
|
|
35
|
+
readonly type: "scroll"
|
|
36
|
+
readonly overflow: boolean
|
|
37
|
+
readonly scrollTop: number
|
|
38
|
+
readonly maxScroll: number
|
|
39
|
+
readonly threshold: number
|
|
40
|
+
}
|
|
41
|
+
| { readonly type: "pause"; readonly overflow: boolean }
|
|
42
|
+
| { readonly type: "scroll.bottom"; readonly force: boolean }
|
|
43
|
+
| { readonly type: "flight.started"; readonly top: number }
|
|
44
|
+
| { readonly type: "disposed" }
|
|
45
|
+
|
|
46
|
+
export type AutoScrollEffect =
|
|
47
|
+
| { readonly type: "pin.bottom" }
|
|
48
|
+
| { readonly type: "notify.interacted" }
|
|
49
|
+
| { readonly type: "settle.start"; readonly attempt: number }
|
|
50
|
+
| { readonly type: "settle.stop" }
|
|
51
|
+
|
|
52
|
+
export type AutoScrollTransition = {
|
|
53
|
+
readonly state: AutoScrollState
|
|
54
|
+
readonly effects: readonly AutoScrollEffect[]
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
type Unpaused = Extract<AutoScrollState, { readonly detached: boolean; readonly flight: number | undefined }>
|
|
58
|
+
|
|
59
|
+
type Paused = Extract<AutoScrollState, { readonly status: "paused" }>
|
|
60
|
+
|
|
61
|
+
export const IDLE: AutoScrollState = Object.freeze({ status: "idle", detached: false, flight: undefined })
|
|
62
|
+
|
|
63
|
+
export const FOLLOWING: AutoScrollState = Object.freeze({ status: "following", detached: false, flight: undefined })
|
|
64
|
+
|
|
65
|
+
export const PAUSED_IDLE: AutoScrollState = Object.freeze({ status: "paused", working: false, detached: false })
|
|
66
|
+
|
|
67
|
+
export const PAUSED_WORKING: AutoScrollState = Object.freeze({ status: "paused", working: true, detached: false })
|
|
68
|
+
|
|
69
|
+
export const DISPOSED: AutoScrollState = Object.freeze({ status: "disposed" })
|
|
70
|
+
|
|
71
|
+
const NONE: readonly AutoScrollEffect[] = Object.freeze([])
|
|
72
|
+
|
|
73
|
+
const PIN: readonly AutoScrollEffect[] = Object.freeze([Object.freeze({ type: "pin.bottom" })])
|
|
74
|
+
|
|
75
|
+
const NOTIFY = Object.freeze({ type: "notify.interacted" })
|
|
76
|
+
|
|
77
|
+
const SETTLE_STOP = Object.freeze({ type: "settle.stop" })
|
|
78
|
+
|
|
79
|
+
const PAUSE: readonly AutoScrollEffect[] = Object.freeze([NOTIFY])
|
|
80
|
+
|
|
81
|
+
const STOP: readonly AutoScrollEffect[] = Object.freeze([SETTLE_STOP])
|
|
82
|
+
|
|
83
|
+
const STOP_AND_PIN: readonly AutoScrollEffect[] = Object.freeze([SETTLE_STOP, ...PIN])
|
|
84
|
+
|
|
85
|
+
const STOP_AND_PAUSE: readonly AutoScrollEffect[] = Object.freeze([SETTLE_STOP, NOTIFY])
|
|
86
|
+
|
|
87
|
+
// Scroll and resize fire every frame; the common results are prebuilt so they allocate nothing.
|
|
88
|
+
const SINGLETONS = [IDLE, FOLLOWING, PAUSED_IDLE, PAUSED_WORKING, DISPOSED]
|
|
89
|
+
|
|
90
|
+
const KEEP = new Map<AutoScrollState, AutoScrollTransition>(
|
|
91
|
+
SINGLETONS.map((state) => [state, Object.freeze({ state, effects: NONE })]),
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
const PINNED = new Map<AutoScrollState, AutoScrollTransition>(
|
|
95
|
+
SINGLETONS.map((state) => [state, Object.freeze({ state, effects: PIN })]),
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
export function createAutoScrollState(working: boolean) {
|
|
99
|
+
return working ? FOLLOWING : IDLE
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function transitionAutoScroll(state: AutoScrollState, event: AutoScrollEvent): AutoScrollTransition {
|
|
103
|
+
if (state.status === "disposed") return to(state, NONE)
|
|
104
|
+
|
|
105
|
+
if (event.type === "disposed") return to(DISPOSED, state.status === "settling" ? STOP : NONE)
|
|
106
|
+
|
|
107
|
+
if (state.status === "paused") return transitionPaused(state, event)
|
|
108
|
+
|
|
109
|
+
return transitionUnpaused(state, event)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function selectUserScrolled(state: AutoScrollState) {
|
|
113
|
+
return state.status === "paused"
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function selectOverflowAnchor(state: AutoScrollState, mode: OverflowAnchorMode = "dynamic") {
|
|
117
|
+
if (mode !== "dynamic") return mode
|
|
118
|
+
|
|
119
|
+
return state.status === "paused" ? "auto" : "none"
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Selecting text pauses only while content still moves under the selection. */
|
|
123
|
+
export function selectPausesOnSelection(state: AutoScrollState) {
|
|
124
|
+
return state.status === "following" || state.status === "settling"
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function transitionPaused(state: Paused, event: Exclude<AutoScrollEvent, { type: "disposed" }>) {
|
|
128
|
+
switch (event.type) {
|
|
129
|
+
case "working.changed":
|
|
130
|
+
return to(event.working === state.working ? state : paused(event.working, state.detached), NONE)
|
|
131
|
+
// A new viewport is measured only once it renders; the first `resize` ends the pause if it cannot scroll.
|
|
132
|
+
case "scroller.changed":
|
|
133
|
+
return to(paused(state.working, false), NONE)
|
|
134
|
+
case "scroller.detached":
|
|
135
|
+
return to(state.detached ? state : paused(state.working, true), NONE)
|
|
136
|
+
case "scroller.reattached":
|
|
137
|
+
if (!state.detached) return to(state, NONE)
|
|
138
|
+
|
|
139
|
+
return to(event.overflow ? paused(state.working, false) : resumed(state, false), NONE)
|
|
140
|
+
case "resize":
|
|
141
|
+
case "pause":
|
|
142
|
+
if (state.detached || event.overflow) return to(state, NONE)
|
|
143
|
+
|
|
144
|
+
return to(resumed(state, false), NONE)
|
|
145
|
+
case "scroll":
|
|
146
|
+
if (state.detached || !atBottom(event)) return to(state, NONE)
|
|
147
|
+
|
|
148
|
+
return to(resumed(state, false), NONE)
|
|
149
|
+
case "scroll.bottom":
|
|
150
|
+
if (!event.force) return to(state, NONE)
|
|
151
|
+
|
|
152
|
+
return to(resumed(state, state.detached), state.detached ? NONE : PIN)
|
|
153
|
+
case "settle.elapsed":
|
|
154
|
+
case "flight.started":
|
|
155
|
+
return to(state, NONE)
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function transitionUnpaused(
|
|
160
|
+
state: Unpaused,
|
|
161
|
+
event: Exclude<AutoScrollEvent, { type: "disposed" }>,
|
|
162
|
+
): AutoScrollTransition {
|
|
163
|
+
switch (event.type) {
|
|
164
|
+
case "working.changed":
|
|
165
|
+
if (event.working && state.status === "following") return to(state, NONE)
|
|
166
|
+
|
|
167
|
+
if (event.working) return to(unpaused("following", 0, state.detached, state.flight), follow(state))
|
|
168
|
+
|
|
169
|
+
if (state.status !== "following") return to(state, NONE)
|
|
170
|
+
|
|
171
|
+
return {
|
|
172
|
+
state: unpaused("settling", event.attempt, state.detached, state.flight),
|
|
173
|
+
effects: [{ type: "settle.start", attempt: event.attempt }],
|
|
174
|
+
}
|
|
175
|
+
case "settle.elapsed":
|
|
176
|
+
if (state.status !== "settling" || state.attempt !== event.attempt) return to(state, NONE)
|
|
177
|
+
|
|
178
|
+
return to(unpaused("idle", 0, state.detached, state.flight), NONE)
|
|
179
|
+
case "scroller.changed":
|
|
180
|
+
return to(patch(state, false, undefined), event.overflow ? PIN : NONE)
|
|
181
|
+
case "scroller.detached":
|
|
182
|
+
return to(patch(state, true, undefined), NONE)
|
|
183
|
+
case "scroller.reattached":
|
|
184
|
+
if (!state.detached) return to(state, NONE)
|
|
185
|
+
|
|
186
|
+
return to(patch(state, false, state.flight), event.overflow ? PIN : NONE)
|
|
187
|
+
case "resize":
|
|
188
|
+
if (state.detached || !event.overflow) return to(state, NONE)
|
|
189
|
+
|
|
190
|
+
return to(state, PIN)
|
|
191
|
+
case "scroll":
|
|
192
|
+
if (state.detached) return to(state, NONE)
|
|
193
|
+
|
|
194
|
+
if (atBottom(event)) return to(patch(state, false, undefined), NONE)
|
|
195
|
+
|
|
196
|
+
// Content grew between our write and its `scroll` event: catch up instead of reading it as the user.
|
|
197
|
+
if (state.flight !== undefined && Math.abs(event.scrollTop - state.flight) <= END_EPSILON)
|
|
198
|
+
return to(patch(state, false, undefined), PIN)
|
|
199
|
+
|
|
200
|
+
return pause(state)
|
|
201
|
+
case "pause":
|
|
202
|
+
if (state.detached || !event.overflow) return to(state, NONE)
|
|
203
|
+
|
|
204
|
+
return pause(state)
|
|
205
|
+
case "scroll.bottom":
|
|
206
|
+
return to(state, state.detached ? NONE : PIN)
|
|
207
|
+
case "flight.started":
|
|
208
|
+
if (state.detached) return to(state, NONE)
|
|
209
|
+
|
|
210
|
+
return to(patch(state, false, event.top), NONE)
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function atBottom(event: Extract<AutoScrollEvent, { type: "scroll" }>) {
|
|
215
|
+
return !event.overflow || event.maxScroll - event.scrollTop < event.threshold
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function pause(state: Unpaused) {
|
|
219
|
+
return to(paused(state.status === "following", false), state.status === "settling" ? STOP_AND_PAUSE : PAUSE)
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function follow(state: Unpaused) {
|
|
223
|
+
if (state.status === "settling") return state.detached ? STOP : STOP_AND_PIN
|
|
224
|
+
|
|
225
|
+
return state.detached ? NONE : PIN
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function to(state: AutoScrollState, effects: readonly AutoScrollEffect[]): AutoScrollTransition {
|
|
229
|
+
const cached = effects === NONE ? KEEP.get(state) : effects === PIN ? PINNED.get(state) : undefined
|
|
230
|
+
|
|
231
|
+
return cached ?? { state, effects }
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function patch(state: Unpaused, detached: boolean, flight: number | undefined) {
|
|
235
|
+
if (state.detached === detached && state.flight === flight) return state
|
|
236
|
+
|
|
237
|
+
return unpaused(state.status, state.status === "settling" ? state.attempt : 0, detached, flight)
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function unpaused(
|
|
241
|
+
status: Unpaused["status"],
|
|
242
|
+
attempt: number,
|
|
243
|
+
detached: boolean,
|
|
244
|
+
flight: number | undefined,
|
|
245
|
+
): AutoScrollState {
|
|
246
|
+
if (status === "settling") return { status, attempt, detached, flight }
|
|
247
|
+
|
|
248
|
+
if (!detached && flight === undefined) return status === "idle" ? IDLE : FOLLOWING
|
|
249
|
+
|
|
250
|
+
return { status, detached, flight }
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function paused(working: boolean, detached: boolean): AutoScrollState {
|
|
254
|
+
if (!detached) return working ? PAUSED_WORKING : PAUSED_IDLE
|
|
255
|
+
|
|
256
|
+
return { status: "paused", working, detached }
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function resumed(state: Paused, detached: boolean) {
|
|
260
|
+
return unpaused(state.working ? "following" : "idle", 0, detached, undefined)
|
|
261
|
+
}
|