@opencode/ui 2.2.4 → 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.
Files changed (127) hide show
  1. package/dist/actions/button/button.d.ts +2 -2
  2. package/dist/actions/icon-button/icon-button.d.ts +1 -1
  3. package/dist/components/animated-number-machine.d.ts +95 -0
  4. package/dist/components/file-icon-machine.d.ts +40 -0
  5. package/dist/components/file-icon.d.ts +0 -2
  6. package/dist/components/file-icons/choose-icon-name.d.ts +2 -0
  7. package/dist/components/image-flight-machine.d.ts +135 -0
  8. package/dist/components/image-zoom-machine.d.ts +95 -0
  9. package/dist/components/image-zoom.d.ts +6 -3
  10. package/dist/components/list.d.ts +2 -6
  11. package/dist/components/menu-scroll.d.ts +12 -0
  12. package/dist/components/popover-machine.d.ts +96 -0
  13. package/dist/components/popover.d.ts +2 -2
  14. package/dist/components/resize-handle-machine.d.ts +71 -0
  15. package/dist/components/resize-handle.d.ts +2 -0
  16. package/dist/components/scroll-view-machine.d.ts +68 -0
  17. package/dist/components/scroll-view.d.ts +2 -2
  18. package/dist/components/spinner.d.ts +11 -3
  19. package/dist/components/text-field-machine.d.ts +62 -0
  20. package/dist/components/text-reveal-machine.d.ts +90 -0
  21. package/dist/components/typewriter-machine.d.ts +62 -0
  22. package/dist/context/dialog-machine.d.ts +125 -0
  23. package/dist/context/dialog.d.ts +17 -16
  24. package/dist/data-display/line-comment/line-comment.d.ts +9 -0
  25. package/dist/data-display/line-comment/machine.d.ts +157 -0
  26. package/dist/data-display/project-avatar/animation-machine.d.ts +62 -0
  27. package/dist/data-display/project-avatar/animation.d.ts +1 -1
  28. package/dist/feedback/activity-ring/activity-ring.d.ts +2 -1
  29. package/dist/feedback/toast/machine.d.ts +57 -0
  30. package/dist/feedback/toast/toast.d.ts +1 -1
  31. package/dist/forms/checkbox/checkbox.d.ts +6 -1
  32. package/dist/forms/select/machine.d.ts +45 -0
  33. package/dist/forms/select/select.d.ts +4 -0
  34. package/dist/forms/text-input/text-input.d.ts +4 -2
  35. package/dist/forms/textarea/textarea.d.ts +1 -0
  36. package/dist/forms/typeahead/machine.d.ts +85 -0
  37. package/dist/hooks/auto-scroll-machine.d.ts +89 -0
  38. package/dist/hooks/create-auto-scroll.d.ts +1 -1
  39. package/dist/hooks/list-machine.d.ts +150 -0
  40. package/dist/hooks/use-filtered-list.d.ts +21 -35
  41. package/dist/i18n/en.d.ts +5 -0
  42. package/dist/icons/catalog.d.ts +8 -0
  43. package/dist/navigation/menu/machine.d.ts +49 -0
  44. package/dist/navigation/menu/menu.d.ts +5 -3
  45. package/dist/navigation/segmented-control/machine.d.ts +46 -0
  46. package/dist/overlays/mcp-sign-in/machine.d.ts +184 -0
  47. package/dist/overlays/mcp-sign-in/mcp-sign-in.d.ts +40 -0
  48. package/dist/overlays/tooltip/machine.d.ts +146 -0
  49. package/dist/theme/context.d.ts +38 -14
  50. package/dist/theme/machine.d.ts +174 -0
  51. package/dist/typography/text-shimmer/machine.d.ts +48 -0
  52. package/package.json +19 -3
  53. package/src/actions/button/button.tsx +11 -6
  54. package/src/actions/icon-button/icon-button.tsx +17 -4
  55. package/src/components/animated-number-machine.ts +214 -0
  56. package/src/components/animated-number.tsx +134 -83
  57. package/src/components/file-icon-machine.ts +78 -0
  58. package/src/components/file-icon.tsx +96 -579
  59. package/src/components/file-icons/choose-icon-name.ts +574 -0
  60. package/src/components/image-flight-machine.ts +247 -0
  61. package/src/components/image-preview.css +5 -2
  62. package/src/components/image-preview.tsx +390 -252
  63. package/src/components/image-zoom-machine.ts +191 -0
  64. package/src/components/image-zoom.ts +22 -14
  65. package/src/components/list.tsx +46 -137
  66. package/src/components/menu-scroll.css +70 -0
  67. package/src/components/menu-scroll.tsx +161 -0
  68. package/src/components/popover-machine.ts +198 -0
  69. package/src/components/popover.tsx +157 -89
  70. package/src/components/resize-handle-machine.ts +174 -0
  71. package/src/components/resize-handle.tsx +116 -67
  72. package/src/components/scroll-view-machine.ts +244 -0
  73. package/src/components/scroll-view.tsx +163 -116
  74. package/src/components/spinner.css +18 -5
  75. package/src/components/spinner.tsx +28 -14
  76. package/src/components/text-field-machine.ts +102 -0
  77. package/src/components/text-field.tsx +78 -24
  78. package/src/components/text-reveal-machine.ts +184 -0
  79. package/src/components/text-reveal.tsx +96 -93
  80. package/src/components/typewriter-machine.ts +134 -0
  81. package/src/components/typewriter.tsx +50 -39
  82. package/src/context/dialog-machine.ts +280 -0
  83. package/src/context/dialog.tsx +168 -177
  84. package/src/data-display/line-comment/line-comment.tsx +196 -132
  85. package/src/data-display/line-comment/machine.ts +391 -0
  86. package/src/data-display/project-avatar/animation-machine.ts +182 -0
  87. package/src/data-display/project-avatar/animation.ts +67 -38
  88. package/src/feedback/activity-ring/activity-ring.tsx +1 -1
  89. package/src/feedback/toast/machine.ts +115 -0
  90. package/src/feedback/toast/toast.tsx +55 -59
  91. package/src/forms/checkbox/checkbox-compact.css +169 -0
  92. package/src/forms/checkbox/checkbox.tsx +93 -31
  93. package/src/forms/search-list/search-list.css +5 -1
  94. package/src/forms/search-list/search-list.tsx +4 -4
  95. package/src/forms/select/machine.ts +81 -0
  96. package/src/forms/select/select.css +59 -17
  97. package/src/forms/select/select.tsx +67 -31
  98. package/src/forms/text-input/text-input.css +30 -16
  99. package/src/forms/text-input/text-input.tsx +6 -2
  100. package/src/forms/textarea/textarea.css +13 -8
  101. package/src/forms/textarea/textarea.tsx +3 -1
  102. package/src/forms/typeahead/machine.ts +253 -0
  103. package/src/forms/typeahead/typeahead.tsx +75 -66
  104. package/src/hooks/auto-scroll-machine.ts +261 -0
  105. package/src/hooks/create-auto-scroll.tsx +110 -205
  106. package/src/hooks/list-machine.ts +359 -0
  107. package/src/hooks/use-filtered-list.tsx +101 -112
  108. package/src/i18n/en.ts +6 -1
  109. package/src/icons/catalog.ts +8 -0
  110. package/src/navigation/menu/machine.ts +109 -0
  111. package/src/navigation/menu/menu.tsx +130 -22
  112. package/src/navigation/segmented-control/machine.ts +72 -0
  113. package/src/navigation/segmented-control/segmented-control.tsx +46 -46
  114. package/src/overlays/dialog/dialog.tsx +7 -1
  115. package/src/overlays/mcp-sign-in/machine.ts +343 -0
  116. package/src/overlays/mcp-sign-in/mcp-sign-in.ts +159 -0
  117. package/src/overlays/tooltip/machine.ts +303 -0
  118. package/src/overlays/tooltip/tooltip.tsx +157 -92
  119. package/src/styles/theme.css +10 -2
  120. package/src/styles/tokens/theme.css +13 -0
  121. package/src/theme/context.tsx +192 -269
  122. package/src/theme/machine.ts +508 -0
  123. package/src/theme/themes/oc-2.json +2 -0
  124. package/src/theme/v2/mapping.ts +12 -8
  125. package/src/typography/text-shimmer/machine.ts +90 -0
  126. package/src/typography/text-shimmer/text-shimmer.css +71 -39
  127. 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 { createResizeObserver } from "@solid-primitives/resize-observer"
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
- export function createAutoScroll(options: AutoScrollOptions) {
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
- const threshold = () => options.bottomThreshold ?? 10
25
+ const BOTTOM = { type: "scroll.bottom", force: false } as const
20
26
 
21
- const [store, setStore] = createStore({
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
- const active = () => options.working() || settling
29
+ const DISPOSE = { type: "disposed" } as const
28
30
 
29
- const distanceFromBottom = (el: HTMLElement) => {
30
- return el.scrollHeight - el.clientHeight - el.scrollTop
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
- const canScroll = (el: HTMLElement) => {
34
- return el.scrollHeight - el.clientHeight > 1
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
- // Browsers can dispatch scroll events asynchronously. If new content arrives
38
- // between us calling `scrollTo()` and the subsequent `scroll` event firing,
39
- // the handler can see a non-zero `distanceFromBottom` and incorrectly assume
40
- // the user scrolled.
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 isAuto = (el: HTMLElement) => {
55
- const a = auto
56
-
57
- if (!a) return false
49
+ const run = (effect: AutoScrollEffect) => {
50
+ if (effect.type === "pin.bottom") return pin()
58
51
 
59
- if (Date.now() - a.time > 1500) {
60
- auto = undefined
52
+ if (effect.type === "notify.interacted") return options.onUserInteracted?.()
53
+ clearTimeout(settleTimer)
61
54
 
62
- return false
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 scrollToBottomNow = (behavior: ScrollBehavior) => {
69
- const el = store.scrollRef
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
- return
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
- if (!el) return
92
-
93
- if (!force && store.userScrolled) return
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 stop = () => {
109
- const el = store.scrollRef
110
-
111
- if (!el) return
77
+ const pause = () => {
78
+ const el = refs.scroll
112
79
 
113
- if (!canScroll(el)) {
114
- if (store.userScrolled) setStore("userScrolled", false)
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 el = store.scrollRef
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 (el && nested && nested !== el) return
135
- stop()
92
+ if (nested && nested !== refs.scroll) return
93
+ pause()
136
94
  }
137
95
 
138
96
  const handleScroll = () => {
139
- const el = store.scrollRef
140
-
141
- if (!el) return
142
-
143
- if (!canScroll(el)) {
144
- if (store.userScrolled) setStore("userScrolled", false)
145
-
146
- return
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 (!active()) return
108
+ if (!selectPausesOnSelection(untrack(state))) return
167
109
  const selection = window.getSelection()
168
110
 
169
- if (selection && selection.toString().length > 0) {
170
- stop()
171
- }
111
+ if (!selection || selection.isCollapsed || selection.toString().length === 0) return
112
+ pause()
172
113
  }
173
114
 
174
- const updateOverflowAnchor = (el: HTMLElement) => {
175
- const mode = options.overflowAnchor ?? "dynamic"
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
- if (el && !canScroll(el)) {
198
- if (store.userScrolled) setStore("userScrolled", false)
119
+ if (!el || typeof ResizeObserver === "undefined") return
199
120
 
200
- return
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
- if (!active()) return
129
+ observer.observe(el)
204
130
 
205
- if (store.userScrolled) return
206
- // ResizeObserver fires after layout, before paint.
207
- // Keep the bottom locked in the same frame to avoid visible
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 = store.scrollRef
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
- let connected = el.isConnected
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)) connected = false
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 (reattached) scrollToBottom(false)
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: boolean) => {
241
- settling = false
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
- // Track `userScrolled` even before `scrollRef` is attached, so we can
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
- updateOverflowAnchor(el)
170
+ el.style.overflowAnchor = overflowAnchor()
267
171
  })
268
172
 
269
- createEventListener(() => store.scrollRef, "wheel", handleWheel, { passive: true })
270
-
271
- onCleanup(() => {
272
- if (settleTimer) clearTimeout(settleTimer)
173
+ createEventListener(() => refs.scroll, "wheel", handleWheel, { passive: true })
273
174
 
274
- if (autoTimer) clearTimeout(autoTimer)
275
- })
175
+ onCleanup(() => dispatch(DISPOSE))
276
176
 
277
177
  return {
278
- scrollRef: (el: HTMLElement | undefined) => setStore("scrollRef", el),
279
- contentRef: (el: HTMLElement | undefined) => setStore("contentRef", el),
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: stop,
283
- resume: () => {
284
- if (store.userScrolled) setStore("userScrolled", false)
285
- scrollToBottom(true)
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
+ }