@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
|
@@ -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
|
+
}
|