@opencode/ui 2.2.5 → 2.2.6
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/actions/button/button.d.ts +2 -2
- package/dist/actions/icon-button/icon-button.d.ts +1 -1
- package/dist/components/animated-number-machine.d.ts +95 -0
- package/dist/components/file-icon-machine.d.ts +40 -0
- package/dist/components/file-icon.d.ts +0 -2
- package/dist/components/file-icons/choose-icon-name.d.ts +2 -0
- package/dist/components/image-flight-machine.d.ts +135 -0
- package/dist/components/image-zoom-machine.d.ts +95 -0
- package/dist/components/image-zoom.d.ts +6 -3
- package/dist/components/list.d.ts +2 -6
- package/dist/components/menu-scroll.d.ts +9 -3
- package/dist/components/popover-machine.d.ts +96 -0
- package/dist/components/popover.d.ts +2 -2
- package/dist/components/resize-handle-machine.d.ts +71 -0
- package/dist/components/resize-handle.d.ts +2 -0
- package/dist/components/scroll-view-machine.d.ts +68 -0
- package/dist/components/scroll-view.d.ts +2 -2
- package/dist/components/spinner.d.ts +11 -3
- package/dist/components/text-field-machine.d.ts +62 -0
- package/dist/components/text-reveal-machine.d.ts +90 -0
- package/dist/components/typewriter-machine.d.ts +62 -0
- package/dist/context/dialog-machine.d.ts +125 -0
- package/dist/context/dialog.d.ts +17 -16
- package/dist/data-display/line-comment/machine.d.ts +157 -0
- package/dist/data-display/project-avatar/animation-machine.d.ts +62 -0
- package/dist/data-display/project-avatar/animation.d.ts +1 -1
- package/dist/feedback/activity-ring/activity-ring.d.ts +2 -1
- package/dist/feedback/toast/machine.d.ts +57 -0
- package/dist/feedback/toast/toast.d.ts +1 -1
- package/dist/forms/checkbox/checkbox.d.ts +6 -1
- package/dist/forms/select/machine.d.ts +45 -0
- package/dist/forms/select/select.d.ts +4 -0
- package/dist/forms/typeahead/machine.d.ts +85 -0
- package/dist/hooks/auto-scroll-machine.d.ts +89 -0
- package/dist/hooks/create-auto-scroll.d.ts +1 -1
- package/dist/hooks/list-machine.d.ts +150 -0
- package/dist/hooks/use-filtered-list.d.ts +21 -35
- package/dist/i18n/en.d.ts +5 -0
- package/dist/icons/catalog.d.ts +8 -0
- package/dist/navigation/menu/machine.d.ts +49 -0
- package/dist/navigation/menu/menu.d.ts +5 -3
- package/dist/navigation/segmented-control/machine.d.ts +46 -0
- package/dist/overlays/mcp-sign-in/machine.d.ts +184 -0
- package/dist/overlays/mcp-sign-in/mcp-sign-in.d.ts +40 -0
- package/dist/overlays/tooltip/machine.d.ts +146 -0
- package/dist/theme/context.d.ts +38 -14
- package/dist/theme/machine.d.ts +174 -0
- package/dist/typography/text-shimmer/machine.d.ts +48 -0
- package/package.json +19 -3
- package/src/actions/button/button.tsx +11 -6
- package/src/actions/icon-button/icon-button.tsx +17 -4
- package/src/components/animated-number-machine.ts +214 -0
- package/src/components/animated-number.tsx +134 -83
- package/src/components/file-icon-machine.ts +78 -0
- package/src/components/file-icon.tsx +96 -579
- package/src/components/file-icons/choose-icon-name.ts +574 -0
- package/src/components/image-flight-machine.ts +247 -0
- package/src/components/image-preview.css +5 -2
- package/src/components/image-preview.tsx +390 -252
- package/src/components/image-zoom-machine.ts +191 -0
- package/src/components/image-zoom.ts +22 -14
- package/src/components/list.tsx +46 -137
- package/src/components/menu-scroll.css +19 -8
- package/src/components/menu-scroll.tsx +111 -34
- package/src/components/popover-machine.ts +198 -0
- package/src/components/popover.tsx +157 -89
- package/src/components/resize-handle-machine.ts +174 -0
- package/src/components/resize-handle.tsx +116 -67
- package/src/components/scroll-view-machine.ts +244 -0
- package/src/components/scroll-view.tsx +163 -116
- package/src/components/spinner.css +18 -5
- package/src/components/spinner.tsx +28 -14
- package/src/components/text-field-machine.ts +102 -0
- package/src/components/text-field.tsx +78 -24
- package/src/components/text-reveal-machine.ts +184 -0
- package/src/components/text-reveal.tsx +96 -93
- package/src/components/typewriter-machine.ts +134 -0
- package/src/components/typewriter.tsx +50 -39
- package/src/context/dialog-machine.ts +280 -0
- package/src/context/dialog.tsx +168 -177
- package/src/data-display/line-comment/line-comment.tsx +157 -178
- package/src/data-display/line-comment/machine.ts +391 -0
- package/src/data-display/project-avatar/animation-machine.ts +182 -0
- package/src/data-display/project-avatar/animation.ts +67 -38
- package/src/feedback/activity-ring/activity-ring.tsx +1 -1
- package/src/feedback/toast/machine.ts +115 -0
- package/src/feedback/toast/toast.tsx +55 -59
- package/src/forms/checkbox/checkbox-compact.css +169 -0
- package/src/forms/checkbox/checkbox.tsx +93 -31
- package/src/forms/search-list/search-list.css +5 -1
- package/src/forms/search-list/search-list.tsx +4 -4
- package/src/forms/select/machine.ts +81 -0
- package/src/forms/select/select.css +59 -17
- package/src/forms/select/select.tsx +67 -31
- package/src/forms/typeahead/machine.ts +253 -0
- package/src/forms/typeahead/typeahead.tsx +70 -62
- package/src/hooks/auto-scroll-machine.ts +261 -0
- package/src/hooks/create-auto-scroll.tsx +110 -205
- package/src/hooks/list-machine.ts +359 -0
- package/src/hooks/use-filtered-list.tsx +101 -112
- package/src/i18n/en.ts +6 -1
- package/src/icons/catalog.ts +8 -0
- package/src/navigation/menu/machine.ts +109 -0
- package/src/navigation/menu/menu.tsx +130 -22
- package/src/navigation/segmented-control/machine.ts +72 -0
- package/src/navigation/segmented-control/segmented-control.tsx +46 -46
- package/src/overlays/dialog/dialog.tsx +7 -1
- package/src/overlays/mcp-sign-in/machine.ts +343 -0
- package/src/overlays/mcp-sign-in/mcp-sign-in.ts +159 -0
- package/src/overlays/tooltip/machine.ts +303 -0
- package/src/overlays/tooltip/tooltip.tsx +157 -92
- package/src/styles/theme.css +10 -2
- package/src/theme/context.tsx +192 -269
- package/src/theme/machine.ts +508 -0
- package/src/typography/text-shimmer/machine.ts +90 -0
- package/src/typography/text-shimmer/text-shimmer.css +71 -39
- package/src/typography/text-shimmer/text-shimmer.tsx +41 -24
|
@@ -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
|
+
}
|
|
@@ -1,7 +1,17 @@
|
|
|
1
|
-
import { createEffect, on, onCleanup } from "solid-js"
|
|
1
|
+
import { createEffect, createMemo, createSignal, on, onCleanup, untrack } from "solid-js"
|
|
2
2
|
import { createStore } from "solid-js/store"
|
|
3
3
|
import { createEventListener } from "@solid-primitives/event-listener"
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
createAutoScrollState,
|
|
6
|
+
END_EPSILON,
|
|
7
|
+
SETTLE_MS,
|
|
8
|
+
selectOverflowAnchor,
|
|
9
|
+
selectPausesOnSelection,
|
|
10
|
+
selectUserScrolled,
|
|
11
|
+
transitionAutoScroll,
|
|
12
|
+
type AutoScrollEffect,
|
|
13
|
+
type AutoScrollEvent,
|
|
14
|
+
} from "./auto-scroll-machine"
|
|
5
15
|
|
|
6
16
|
export interface AutoScrollOptions {
|
|
7
17
|
working: () => boolean
|
|
@@ -10,116 +20,66 @@ export interface AutoScrollOptions {
|
|
|
10
20
|
bottomThreshold?: number
|
|
11
21
|
}
|
|
12
22
|
|
|
13
|
-
|
|
14
|
-
let settling = false
|
|
15
|
-
let settleTimer: ReturnType<typeof setTimeout> | undefined
|
|
16
|
-
let autoTimer: ReturnType<typeof setTimeout> | undefined
|
|
17
|
-
let auto: { top: number; time: number } | undefined
|
|
23
|
+
const DETACHED = { type: "scroller.detached" } as const
|
|
18
24
|
|
|
19
|
-
|
|
25
|
+
const BOTTOM = { type: "scroll.bottom", force: false } as const
|
|
20
26
|
|
|
21
|
-
|
|
22
|
-
contentRef: undefined as HTMLElement | undefined,
|
|
23
|
-
scrollRef: undefined as HTMLElement | undefined,
|
|
24
|
-
userScrolled: false,
|
|
25
|
-
})
|
|
27
|
+
const FORCE_BOTTOM = { type: "scroll.bottom", force: true } as const
|
|
26
28
|
|
|
27
|
-
|
|
29
|
+
const DISPOSE = { type: "disposed" } as const
|
|
28
30
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
31
|
+
export function createAutoScroll(options: AutoScrollOptions) {
|
|
32
|
+
const [state, setState] = createSignal(createAutoScrollState(untrack(options.working)))
|
|
33
|
+
const [refs, setRefs] = createStore<{ scroll?: HTMLElement; content?: HTMLElement }>({})
|
|
34
|
+
let attempts = 0
|
|
35
|
+
let settleTimer: ReturnType<typeof setTimeout> | undefined
|
|
32
36
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
37
|
+
// Scroll, resize and pin run every frame while content streams; reuse their events instead of allocating.
|
|
38
|
+
const resizeEvent = { type: "resize" as const, overflow: false }
|
|
39
|
+
const scrollEvent = { type: "scroll" as const, overflow: false, scrollTop: 0, maxScroll: 0, threshold: 0 }
|
|
40
|
+
const pauseEvent = { type: "pause" as const, overflow: false }
|
|
41
|
+
const flightEvent = { type: "flight.started" as const, top: 0 }
|
|
36
42
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const markAuto = (el: HTMLElement) => {
|
|
42
|
-
auto = {
|
|
43
|
-
top: Math.max(0, el.scrollHeight - el.clientHeight),
|
|
44
|
-
time: Date.now(),
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
if (autoTimer) clearTimeout(autoTimer)
|
|
48
|
-
autoTimer = setTimeout(() => {
|
|
49
|
-
auto = undefined
|
|
50
|
-
autoTimer = undefined
|
|
51
|
-
}, 1500)
|
|
43
|
+
const dispatch = (event: AutoScrollEvent) => {
|
|
44
|
+
const result = transitionAutoScroll(untrack(state), event)
|
|
45
|
+
setState(result.state)
|
|
46
|
+
result.effects.forEach(run)
|
|
52
47
|
}
|
|
53
48
|
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
if (!a) return false
|
|
49
|
+
const run = (effect: AutoScrollEffect) => {
|
|
50
|
+
if (effect.type === "pin.bottom") return pin()
|
|
58
51
|
|
|
59
|
-
if (
|
|
60
|
-
|
|
52
|
+
if (effect.type === "notify.interacted") return options.onUserInteracted?.()
|
|
53
|
+
clearTimeout(settleTimer)
|
|
61
54
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
return Math.abs(el.scrollTop - a.top) < 2
|
|
55
|
+
if (effect.type === "settle.stop") return
|
|
56
|
+
const attempt = effect.attempt
|
|
57
|
+
settleTimer = setTimeout(() => dispatch({ type: "settle.elapsed", attempt }), SETTLE_MS)
|
|
66
58
|
}
|
|
67
59
|
|
|
68
|
-
const
|
|
69
|
-
const el =
|
|
70
|
-
|
|
71
|
-
if (!el) return
|
|
72
|
-
markAuto(el)
|
|
73
|
-
|
|
74
|
-
if (behavior === "smooth") {
|
|
75
|
-
el.scrollTo({ top: el.scrollHeight, behavior })
|
|
60
|
+
const pin = () => {
|
|
61
|
+
const el = refs.scroll
|
|
76
62
|
|
|
77
|
-
|
|
78
|
-
|
|
63
|
+
if (!el || !el.isConnected) return
|
|
64
|
+
const before = el.scrollTop
|
|
79
65
|
|
|
66
|
+
if (el.scrollHeight - el.clientHeight - before <= END_EPSILON) return
|
|
80
67
|
// `scrollTop` assignment bypasses any CSS `scroll-behavior: smooth`.
|
|
81
68
|
el.scrollTop = el.scrollHeight
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
const scrollToBottom = (force: boolean) => {
|
|
85
|
-
if (!force && !active()) return
|
|
86
|
-
|
|
87
|
-
if (force && store.userScrolled) setStore("userScrolled", false)
|
|
88
|
-
|
|
89
|
-
const el = store.scrollRef
|
|
69
|
+
const after = el.scrollTop
|
|
90
70
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
const distance = distanceFromBottom(el)
|
|
96
|
-
|
|
97
|
-
if (distance < 2) {
|
|
98
|
-
markAuto(el)
|
|
99
|
-
|
|
100
|
-
return
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// For auto-following content we prefer immediate updates to avoid
|
|
104
|
-
// visible "catch up" animations while content is still settling.
|
|
105
|
-
scrollToBottomNow("auto")
|
|
71
|
+
// The browser queues a `scroll` event only when the position actually moved.
|
|
72
|
+
if (Math.abs(after - before) <= END_EPSILON) return
|
|
73
|
+
flightEvent.top = after
|
|
74
|
+
dispatch(flightEvent)
|
|
106
75
|
}
|
|
107
76
|
|
|
108
|
-
const
|
|
109
|
-
const el =
|
|
110
|
-
|
|
111
|
-
if (!el) return
|
|
77
|
+
const pause = () => {
|
|
78
|
+
const el = refs.scroll
|
|
112
79
|
|
|
113
|
-
if (!
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
return
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
if (store.userScrolled) return
|
|
120
|
-
|
|
121
|
-
setStore("userScrolled", true)
|
|
122
|
-
options.onUserInteracted?.()
|
|
80
|
+
if (!el || !el.isConnected) return
|
|
81
|
+
pauseEvent.overflow = canScroll(el)
|
|
82
|
+
dispatch(pauseEvent)
|
|
123
83
|
}
|
|
124
84
|
|
|
125
85
|
const handleWheel = (e: WheelEvent) => {
|
|
@@ -127,108 +87,65 @@ export function createAutoScroll(options: AutoScrollOptions) {
|
|
|
127
87
|
// If the user is scrolling within a nested scrollable region (tool output,
|
|
128
88
|
// code block, etc), don't treat it as leaving the "follow bottom" mode.
|
|
129
89
|
// Those regions opt in via `data-scrollable`.
|
|
130
|
-
const
|
|
131
|
-
const target = e.target instanceof Element ? e.target : undefined
|
|
132
|
-
const nested = target?.closest("[data-scrollable]")
|
|
90
|
+
const nested = e.target instanceof Element ? e.target.closest("[data-scrollable]") : null
|
|
133
91
|
|
|
134
|
-
if (
|
|
135
|
-
|
|
92
|
+
if (nested && nested !== refs.scroll) return
|
|
93
|
+
pause()
|
|
136
94
|
}
|
|
137
95
|
|
|
138
96
|
const handleScroll = () => {
|
|
139
|
-
const el =
|
|
140
|
-
|
|
141
|
-
if (!el) return
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
if (distanceFromBottom(el) < threshold()) {
|
|
150
|
-
if (store.userScrolled) setStore("userScrolled", false)
|
|
151
|
-
|
|
152
|
-
return
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
// Ignore scroll events triggered by our own scrollToBottom calls.
|
|
156
|
-
if (!store.userScrolled && isAuto(el)) {
|
|
157
|
-
scrollToBottom(false)
|
|
158
|
-
|
|
159
|
-
return
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
stop()
|
|
97
|
+
const el = refs.scroll
|
|
98
|
+
|
|
99
|
+
if (!el || !el.isConnected) return
|
|
100
|
+
scrollEvent.maxScroll = el.scrollHeight - el.clientHeight
|
|
101
|
+
scrollEvent.overflow = canScroll(el)
|
|
102
|
+
scrollEvent.scrollTop = el.scrollTop
|
|
103
|
+
scrollEvent.threshold = options.bottomThreshold ?? 10
|
|
104
|
+
dispatch(scrollEvent)
|
|
163
105
|
}
|
|
164
106
|
|
|
165
107
|
const handleInteraction = () => {
|
|
166
|
-
if (!
|
|
108
|
+
if (!selectPausesOnSelection(untrack(state))) return
|
|
167
109
|
const selection = window.getSelection()
|
|
168
110
|
|
|
169
|
-
if (selection
|
|
170
|
-
|
|
171
|
-
}
|
|
111
|
+
if (!selection || selection.isCollapsed || selection.toString().length === 0) return
|
|
112
|
+
pause()
|
|
172
113
|
}
|
|
173
114
|
|
|
174
|
-
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
if (mode === "none") {
|
|
178
|
-
el.style.overflowAnchor = "none"
|
|
179
|
-
|
|
180
|
-
return
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
if (mode === "auto") {
|
|
184
|
-
el.style.overflowAnchor = "auto"
|
|
185
|
-
|
|
186
|
-
return
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
el.style.overflowAnchor = store.userScrolled ? "auto" : "none"
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
createResizeObserver(
|
|
193
|
-
() => store.contentRef,
|
|
194
|
-
() => {
|
|
195
|
-
const el = store.scrollRef
|
|
115
|
+
createEffect(() => {
|
|
116
|
+
const el = refs.scroll
|
|
117
|
+
const content = refs.content
|
|
196
118
|
|
|
197
|
-
|
|
198
|
-
if (store.userScrolled) setStore("userScrolled", false)
|
|
119
|
+
if (!el || typeof ResizeObserver === "undefined") return
|
|
199
120
|
|
|
200
|
-
|
|
201
|
-
|
|
121
|
+
// ResizeObserver fires after layout, before paint. Keeping the bottom locked in
|
|
122
|
+
// the same frame avoids visible "jump up then catch up" artifacts while streaming.
|
|
123
|
+
const observer = new ResizeObserver(() => {
|
|
124
|
+
if (!el.isConnected) return
|
|
125
|
+
resizeEvent.overflow = canScroll(el)
|
|
126
|
+
dispatch(resizeEvent)
|
|
127
|
+
})
|
|
202
128
|
|
|
203
|
-
|
|
129
|
+
observer.observe(el)
|
|
204
130
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
// "jump up then catch up" artifacts while streaming content.
|
|
209
|
-
scrollToBottom(false)
|
|
210
|
-
},
|
|
211
|
-
)
|
|
131
|
+
if (content) observer.observe(content)
|
|
132
|
+
onCleanup(() => observer.disconnect())
|
|
133
|
+
})
|
|
212
134
|
|
|
213
135
|
createEffect(() => {
|
|
214
|
-
const el =
|
|
136
|
+
const el = refs.scroll
|
|
215
137
|
|
|
216
138
|
if (!el) return
|
|
217
139
|
const root = el.closest("main")
|
|
218
140
|
|
|
219
141
|
if (!root) return
|
|
220
|
-
|
|
221
|
-
const contains = (node: Node) => node === el || (node instanceof Element && node.contains(el))
|
|
142
|
+
const contains = (node: Node) => node.contains(el)
|
|
222
143
|
|
|
223
144
|
const observer = new MutationObserver((records) => {
|
|
224
145
|
records.forEach((record) => {
|
|
225
|
-
if ([...record.removedNodes].some(contains))
|
|
226
|
-
|
|
227
|
-
if (!record.addedNodes.length || ![...record.addedNodes].some(contains)) return
|
|
228
|
-
const reattached = !connected
|
|
229
|
-
connected = true
|
|
146
|
+
if ([...record.removedNodes].some(contains)) dispatch(DETACHED)
|
|
230
147
|
|
|
231
|
-
if (
|
|
148
|
+
if ([...record.addedNodes].some(contains)) dispatch({ type: "scroller.reattached", overflow: canScroll(el) })
|
|
232
149
|
})
|
|
233
150
|
})
|
|
234
151
|
|
|
@@ -237,55 +154,43 @@ export function createAutoScroll(options: AutoScrollOptions) {
|
|
|
237
154
|
})
|
|
238
155
|
|
|
239
156
|
createEffect(
|
|
240
|
-
on(options.working, (working
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
if (settleTimer) clearTimeout(settleTimer)
|
|
244
|
-
settleTimer = undefined
|
|
245
|
-
|
|
246
|
-
if (working) {
|
|
247
|
-
if (!store.userScrolled) scrollToBottom(true)
|
|
248
|
-
|
|
249
|
-
return
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
settling = true
|
|
253
|
-
settleTimer = setTimeout(() => {
|
|
254
|
-
settling = false
|
|
255
|
-
}, 300)
|
|
157
|
+
on(options.working, (working) => {
|
|
158
|
+
if (!working) attempts += 1
|
|
159
|
+
dispatch({ type: "working.changed", working, attempt: attempts })
|
|
256
160
|
}),
|
|
257
161
|
)
|
|
258
162
|
|
|
163
|
+
const userScrolled = createMemo(() => selectUserScrolled(state()))
|
|
164
|
+
const overflowAnchor = createMemo(() => selectOverflowAnchor(state(), options.overflowAnchor))
|
|
165
|
+
|
|
259
166
|
createEffect(() => {
|
|
260
|
-
|
|
261
|
-
// update overflow anchoring once the element exists.
|
|
262
|
-
store.userScrolled
|
|
263
|
-
const el = store.scrollRef
|
|
167
|
+
const el = refs.scroll
|
|
264
168
|
|
|
265
169
|
if (!el) return
|
|
266
|
-
|
|
170
|
+
el.style.overflowAnchor = overflowAnchor()
|
|
267
171
|
})
|
|
268
172
|
|
|
269
|
-
createEventListener(() =>
|
|
270
|
-
|
|
271
|
-
onCleanup(() => {
|
|
272
|
-
if (settleTimer) clearTimeout(settleTimer)
|
|
173
|
+
createEventListener(() => refs.scroll, "wheel", handleWheel, { passive: true })
|
|
273
174
|
|
|
274
|
-
|
|
275
|
-
})
|
|
175
|
+
onCleanup(() => dispatch(DISPOSE))
|
|
276
176
|
|
|
277
177
|
return {
|
|
278
|
-
scrollRef: (el: HTMLElement | undefined) =>
|
|
279
|
-
|
|
178
|
+
scrollRef: (el: HTMLElement | undefined) => {
|
|
179
|
+
if (el === untrack(() => refs.scroll)) return
|
|
180
|
+
setRefs("scroll", el)
|
|
181
|
+
dispatch({ type: "scroller.changed", overflow: !!el && el.isConnected && canScroll(el) })
|
|
182
|
+
},
|
|
183
|
+
contentRef: (el: HTMLElement | undefined) => setRefs("content", el),
|
|
280
184
|
handleScroll,
|
|
281
185
|
handleInteraction,
|
|
282
|
-
pause
|
|
283
|
-
resume: () =>
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
scrollToBottom: () => scrollToBottom(false),
|
|
288
|
-
forceScrollToBottom: () => scrollToBottom(true),
|
|
289
|
-
userScrolled: () => store.userScrolled,
|
|
186
|
+
pause,
|
|
187
|
+
resume: () => dispatch(FORCE_BOTTOM),
|
|
188
|
+
scrollToBottom: () => dispatch(BOTTOM),
|
|
189
|
+
forceScrollToBottom: () => dispatch(FORCE_BOTTOM),
|
|
190
|
+
userScrolled,
|
|
290
191
|
}
|
|
291
192
|
}
|
|
193
|
+
|
|
194
|
+
function canScroll(el: HTMLElement) {
|
|
195
|
+
return el.scrollHeight - el.clientHeight > 1
|
|
196
|
+
}
|