@opencode/ui 0.0.0-dev-750 → 0.0.0-dev-784

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 (48) hide show
  1. package/dist/components/animated-number-machine.d.ts +95 -0
  2. package/dist/components/popover-machine.d.ts +96 -0
  3. package/dist/components/popover.d.ts +2 -2
  4. package/dist/components/resize-handle-machine.d.ts +71 -0
  5. package/dist/components/scroll-view-machine.d.ts +66 -0
  6. package/dist/components/scroll-view.d.ts +2 -2
  7. package/dist/components/text-field-machine.d.ts +62 -0
  8. package/dist/components/text-reveal-machine.d.ts +90 -0
  9. package/dist/data-display/line-comment/machine.d.ts +157 -0
  10. package/dist/data-display/project-avatar/animation-machine.d.ts +62 -0
  11. package/dist/data-display/project-avatar/animation.d.ts +1 -1
  12. package/dist/feedback/toast/machine.d.ts +57 -0
  13. package/dist/feedback/toast/toast.d.ts +1 -1
  14. package/dist/forms/select/machine.d.ts +45 -0
  15. package/dist/forms/typeahead/machine.d.ts +85 -0
  16. package/dist/navigation/menu/machine.d.ts +49 -0
  17. package/dist/navigation/segmented-control/machine.d.ts +46 -0
  18. package/dist/theme/context.d.ts +20 -9
  19. package/dist/theme/machine.d.ts +174 -0
  20. package/package.json +2 -2
  21. package/src/components/animated-number-machine.ts +214 -0
  22. package/src/components/animated-number.tsx +134 -83
  23. package/src/components/popover-machine.ts +198 -0
  24. package/src/components/popover.tsx +157 -89
  25. package/src/components/resize-handle-machine.ts +174 -0
  26. package/src/components/resize-handle.tsx +113 -83
  27. package/src/components/scroll-view-machine.ts +231 -0
  28. package/src/components/scroll-view.tsx +163 -116
  29. package/src/components/text-field-machine.ts +102 -0
  30. package/src/components/text-field.tsx +78 -24
  31. package/src/components/text-reveal-machine.ts +184 -0
  32. package/src/components/text-reveal.tsx +96 -93
  33. package/src/data-display/line-comment/line-comment.tsx +154 -178
  34. package/src/data-display/line-comment/machine.ts +392 -0
  35. package/src/data-display/project-avatar/animation-machine.ts +182 -0
  36. package/src/data-display/project-avatar/animation.ts +67 -38
  37. package/src/feedback/toast/machine.ts +115 -0
  38. package/src/feedback/toast/toast.tsx +55 -59
  39. package/src/forms/select/machine.ts +81 -0
  40. package/src/forms/select/select.tsx +49 -27
  41. package/src/forms/typeahead/machine.ts +253 -0
  42. package/src/forms/typeahead/typeahead.tsx +70 -62
  43. package/src/navigation/menu/machine.ts +109 -0
  44. package/src/navigation/menu/menu.tsx +118 -20
  45. package/src/navigation/segmented-control/machine.ts +72 -0
  46. package/src/navigation/segmented-control/segmented-control.tsx +46 -46
  47. package/src/theme/context.tsx +134 -240
  48. package/src/theme/machine.ts +508 -0
@@ -1,27 +1,29 @@
1
1
  // @refresh reload
2
2
 
3
- import { createEffect, createMemo, onMount } from "solid-js"
4
- import { createStore, produce, unwrap } from "solid-js/store"
3
+ import { createMemo, createSignal, onCleanup, onMount, untrack } from "solid-js"
5
4
  import { makeEventListener } from "@solid-primitives/event-listener"
6
5
  import { createSimpleContext } from "../context/helper"
7
6
  import oc2ThemeJson from "./themes/oc-2.json"
8
7
  import { resolveThemeVariant, themeToCss } from "./resolve"
9
8
  import { resolveThemeVariantV2, themeV2ToCss } from "./v2/resolve"
10
9
  import type { DesktopTheme } from "./types"
11
-
12
- export type ColorScheme = "light" | "dark" | "system"
13
-
14
- type ThemeState = {
15
- /** Loaded bundled themes and registered themes, by id. */
16
- themes: Record<string, DesktopTheme>
17
- themeId: string
18
- colorScheme: ColorScheme
19
- mode: "light" | "dark"
20
- previewThemeId: string | null
21
- previewScheme: ColorScheme | null
22
- /** Registered themes that have not arrived are not coming. */
23
- settled: boolean
24
- }
10
+ import {
11
+ createThemeState,
12
+ DEFAULT_THEME_ID,
13
+ parseColorScheme,
14
+ selectColorScheme,
15
+ selectMode,
16
+ selectThemeId,
17
+ selectThemesLoading,
18
+ transitionTheme,
19
+ type ColorScheme,
20
+ type ThemeEffect,
21
+ type ThemeEvent,
22
+ type ThemeMachineState,
23
+ type ThemeTransition,
24
+ } from "./machine"
25
+
26
+ export type { ColorScheme } from "./machine"
25
27
 
26
28
  const STORAGE_KEYS = {
27
29
  THEME_ID: "opencode-theme-id",
@@ -127,12 +129,6 @@ function drop(key: string) {
127
129
  } catch {}
128
130
  }
129
131
 
130
- function readScheme(): ColorScheme {
131
- const value = read(STORAGE_KEYS.COLOR_SCHEME)
132
-
133
- return value === "light" || value === "dark" ? value : "system"
134
- }
135
-
136
132
  function clear() {
137
133
  drop(STORAGE_KEYS.THEME_CSS_LIGHT)
138
134
  drop(STORAGE_KEYS.THEME_CSS_DARK)
@@ -160,10 +156,6 @@ function applyThemeCss(theme: DesktopTheme, themeId: string, mode: "light" | "da
160
156
  const css = themeToCss(tokens)
161
157
  const v2 = themeV2ToCss(resolveThemeVariantV2(variant, isDark))
162
158
 
163
- if (themeId !== "oc-2") {
164
- write(isDark ? STORAGE_KEYS.THEME_CSS_DARK : STORAGE_KEYS.THEME_CSS_LIGHT, `${css}\n ${v2}`)
165
- }
166
-
167
159
  const fullCss = `:root {
168
160
  color-scheme: ${mode};
169
161
  --text-mix-blend-mode: ${isDark ? "plus-lighter" : "multiply"};
@@ -183,9 +175,7 @@ function applyThemeCss(theme: DesktopTheme, themeId: string, mode: "light" | "da
183
175
  if (meta) meta.setAttribute("content", isDark ? "#080808" : "#fafafa")
184
176
  }
185
177
 
186
- function cacheThemeVariants(theme: DesktopTheme, themeId: string) {
187
- if (themeId === "oc-2") return
188
-
178
+ function cacheThemeVariants(theme: DesktopTheme) {
189
179
  for (const mode of ["light", "dark"] as const) {
190
180
  const isDark = mode === "dark"
191
181
  const variant = isDark ? theme.dark : theme.light
@@ -204,264 +194,168 @@ export const { use: useTheme, provider: ThemeProvider } = createSimpleContext({
204
194
  onThemeApplied?: (mode: "light" | "dark", scheme: ColorScheme) => void
205
195
  }) => {
206
196
  // An id no theme has yet stays selected until `settle`: a registered theme can arrive after startup.
207
- const themeId = read(STORAGE_KEYS.THEME_ID) || props.defaultTheme || "oc-2"
208
- const colorScheme = readScheme()
209
- const mode = colorScheme === "system" ? getSystemMode() : colorScheme
210
-
211
- const [store, setStore] = createStore<ThemeState>({
212
- themes: {
213
- "oc-2": oc2Theme,
214
- },
215
- themeId,
216
- colorScheme,
217
- mode,
218
- previewThemeId: null,
219
- previewScheme: null,
220
- settled: false,
197
+ const initial = createThemeState({
198
+ themeId: read(STORAGE_KEYS.THEME_ID) || props.defaultTheme || DEFAULT_THEME_ID,
199
+ scheme: parseColorScheme(read(STORAGE_KEYS.COLOR_SCHEME)),
200
+ systemMode: getSystemMode(),
201
+ defaultTheme: oc2Theme,
202
+ bundled: knownThemes(),
221
203
  })
222
204
 
223
- // The selected theme, or the default while it is not loaded, registered or coming.
224
- const active = createMemo(() => {
225
- const id = store.themeId
226
-
227
- return knownThemes().has(id) || store.themes[id] || !store.settled ? id : "oc-2"
228
- })
229
-
230
- const loads = new Map<string, Promise<DesktopTheme | undefined>>()
231
-
232
- const load = (id: string) => {
233
- const next = id
234
-
235
- if (!next) return Promise.resolve(undefined)
236
- const hit = store.themes[next]
237
-
238
- if (hit) return Promise.resolve(hit)
239
- const pending = loads.get(next)
240
-
241
- if (pending) return pending
242
- const file = getFiles()[`./themes/${next}.json`]
243
-
244
- if (!file) return Promise.resolve(undefined)
205
+ const [state, setState] = createSignal(initial.state)
206
+ // Import timeouts by attempt.
207
+ const timers = new Map<number, ReturnType<typeof setTimeout>>()
208
+ // `loadThemes` calls waiting for every bundled import to settle.
209
+ const waiters = new Set<() => void>()
210
+ // Transitions an effect or `onThemeApplied` raises wait behind the one running.
211
+ const queue: ((current: ThemeMachineState) => ThemeTransition)[] = []
245
212
 
246
- const task = file()
247
- .then((mod) => {
248
- const theme = mod.default
249
- setStore("themes", next, theme)
213
+ const enqueue = (next: (current: ThemeMachineState) => ThemeTransition) => {
214
+ queue.push(next)
250
215
 
251
- return theme
252
- })
253
- .finally(() => {
254
- loads.delete(next)
255
- })
216
+ if (queue.length > 1) return
256
217
 
257
- loads.set(next, task)
258
-
259
- return task
260
- }
261
-
262
- const applyTheme = (theme: DesktopTheme, themeId: string, mode: "light" | "dark", scheme: ColorScheme) => {
263
- applyThemeCss(theme, themeId, mode)
264
- props.onThemeApplied?.(mode, scheme)
218
+ untrack(() => {
219
+ while (queue[0]) {
220
+ const head = queue[0]
221
+ void Promise.try(() => step(head(state()))).catch((cause: unknown) => console.error(cause))
222
+ queue.shift()
223
+ }
224
+ })
265
225
  }
266
226
 
267
- const name = (id: string) => store.themes[id]?.name ?? names.get(id) ?? id
227
+ const dispatch = (event: ThemeEvent) => enqueue((current) => transitionTheme(current, event, knownThemes()))
268
228
 
269
- // Bundled and registered themes in one list, alphabetical by name.
270
- const ids = () =>
271
- [...themeIDs(), ...Object.keys(store.themes).filter((id) => !knownThemes().has(id))].toSorted((a, b) =>
272
- name(a).localeCompare(name(b), undefined, { sensitivity: "base" }),
229
+ // The CSS and storage change before the state, so effects that read computed styles see the new theme.
230
+ const step = (next: ThemeTransition) => {
231
+ next.effects.forEach(
232
+ (effect) => void Promise.try(() => run(effect)).catch((cause: unknown) => console.error(cause)),
273
233
  )
274
234
 
275
- const loadThemes = () => Promise.all(themeIDs().map(load)).then(() => store.themes)
276
-
277
- const onStorage = (e: StorageEvent) => {
278
- if (e.key === STORAGE_KEYS.THEME_ID && e.newValue) {
279
- const next = e.newValue
280
- setStore("themeId", next)
281
-
282
- if (next === "oc-2") {
283
- clear()
284
-
285
- return
286
- }
235
+ if (next.state !== state()) setState(next.state)
287
236
 
288
- void load(next).then((theme) => {
289
- if (!theme || store.themeId !== next) return
290
- cacheThemeVariants(theme, next)
291
- })
237
+ if (!selectThemesLoading(next.state)) {
238
+ waiters.forEach((resolve) => resolve())
239
+ waiters.clear()
292
240
  }
293
241
 
294
- if (e.key === STORAGE_KEYS.COLOR_SCHEME && e.newValue) {
295
- const scheme = readScheme()
296
- setStore("colorScheme", scheme)
297
- setStore("mode", scheme === "system" ? getSystemMode() : scheme)
298
- }
242
+ const shown = next.effects.findLast(
243
+ (effect): effect is Extract<ThemeEffect, { type: "apply-theme" | "notify-applied" }> =>
244
+ effect.type === "apply-theme" || effect.type === "notify-applied",
245
+ )
246
+
247
+ if (shown) props.onThemeApplied?.(shown.mode, shown.scheme)
299
248
  }
300
249
 
301
- onMount(() => {
302
- makeEventListener(window, "storage", onStorage)
250
+ const run = (effect: ThemeEffect) => {
251
+ if (effect.type === "persist-theme") return write(STORAGE_KEYS.THEME_ID, effect.id)
303
252
 
304
- const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)")
253
+ if (effect.type === "persist-scheme") return write(STORAGE_KEYS.COLOR_SCHEME, effect.scheme)
305
254
 
306
- const onMedia = () => {
307
- if (store.colorScheme !== "system") return
308
- setStore("mode", getSystemMode())
309
- }
310
-
311
- makeEventListener(mediaQuery, "change", onMedia)
255
+ if (effect.type === "cache-theme") return cacheThemeVariants(effect.theme)
312
256
 
313
- const savedTheme = read(STORAGE_KEYS.THEME_ID) || props.defaultTheme || "oc-2"
314
- const savedScheme = readScheme()
257
+ if (effect.type === "clear-cache") return clear()
315
258
 
316
- if (savedTheme !== store.themeId) setStore("themeId", savedTheme)
259
+ if (effect.type === "apply-theme") return applyThemeCss(effect.theme, effect.id, effect.mode)
317
260
 
318
- if (savedScheme !== store.colorScheme) setStore("colorScheme", savedScheme)
319
- setStore("mode", savedScheme === "system" ? getSystemMode() : savedScheme)
320
- void load(savedTheme).then((theme) => {
321
- if (!theme || store.themeId !== savedTheme) return
322
- cacheThemeVariants(theme, savedTheme)
323
- })
324
- })
261
+ if (effect.type === "warn-missing-theme") return console.warn(`Theme "${effect.id}" not found`)
325
262
 
326
- createEffect(() => {
327
- const id = active()
328
- const theme = store.themes[id]
263
+ if (effect.type === "notify-applied") return
329
264
 
330
- if (theme) {
331
- // The selected theme is gone, so the next start paints the default theme instead of its cached colors. The
332
- // choice stays stored and returns with the theme.
333
- if (id !== store.themeId) clear()
334
- applyTheme(theme, id, store.mode, store.colorScheme)
265
+ if (effect.type === "cancel-load-timer") {
266
+ clearTimeout(timers.get(effect.attempt))
267
+ timers.delete(effect.attempt)
335
268
 
336
269
  return
337
270
  }
338
271
 
339
- // A bundled theme is loading: keep what is on screen until it arrives.
340
- if (knownThemes().has(id)) return
341
- // A registered theme may still arrive: the colors cached from the last run stay on screen meanwhile.
342
- props.onThemeApplied?.(store.mode, store.colorScheme)
343
- })
344
-
345
- const setTheme = (id: string) => {
346
- const next = id
347
-
348
- if (!next) {
349
- console.warn(`Theme "${id}" not found`)
272
+ if (effect.type === "cancel-all-loads") {
273
+ timers.forEach(clearTimeout)
274
+ timers.clear()
350
275
 
351
276
  return
352
277
  }
353
278
 
354
- if (next !== "oc-2" && !knownThemes().has(next) && !store.themes[next]) {
355
- console.warn(`Theme "${id}" not found`)
279
+ const file = getFiles()[`./themes/${effect.id}.json`]
356
280
 
357
- return
358
- }
281
+ if (!file) return dispatch({ type: "theme.load.failed", id: effect.id, attempt: effect.attempt })
359
282
 
360
- setStore("themeId", next)
283
+ timers.set(
284
+ effect.attempt,
285
+ setTimeout(() => {
286
+ timers.delete(effect.attempt)
287
+ dispatch({ type: "theme.load.timed-out", id: effect.id, attempt: effect.attempt })
288
+ }, effect.timeoutMs),
289
+ )
290
+ void Promise.try(file).then(
291
+ (mod) => dispatch({ type: "theme.loaded", id: effect.id, attempt: effect.attempt, theme: mod.default }),
292
+ () => dispatch({ type: "theme.load.failed", id: effect.id, attempt: effect.attempt }),
293
+ )
294
+ }
361
295
 
362
- if (next === "oc-2") {
363
- write(STORAGE_KEYS.THEME_ID, next)
364
- clear()
296
+ enqueue(() => initial)
365
297
 
366
- return
367
- }
298
+ onMount(() => {
299
+ makeEventListener(window, "storage", (e: StorageEvent) => {
300
+ if (e.key === STORAGE_KEYS.THEME_ID && e.newValue) dispatch({ type: "storage.theme.changed", id: e.newValue })
368
301
 
369
- void load(next).then((theme) => {
370
- if (!theme || store.themeId !== next) return
371
- cacheThemeVariants(theme, next)
372
- write(STORAGE_KEYS.THEME_ID, next)
302
+ if (e.key === STORAGE_KEYS.COLOR_SCHEME && e.newValue)
303
+ dispatch({ type: "storage.scheme.changed", scheme: parseColorScheme(e.newValue) })
373
304
  })
374
- }
375
305
 
376
- const setColorScheme = (scheme: ColorScheme) => {
377
- setStore("colorScheme", scheme)
378
- write(STORAGE_KEYS.COLOR_SCHEME, scheme)
379
- setStore("mode", scheme === "system" ? getSystemMode() : scheme)
380
- }
306
+ const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)")
307
+
308
+ makeEventListener(mediaQuery, "change", () =>
309
+ dispatch({ type: "system.mode.changed", mode: mediaQuery.matches ? "dark" : "light" }),
310
+ )
311
+ })
312
+
313
+ onCleanup(() => dispatch({ type: "disposed" }))
314
+
315
+ const name = (id: string) => state().themes[id]?.name ?? names.get(id) ?? id
381
316
 
382
317
  return {
383
- themeId: active,
384
- colorScheme: () => store.colorScheme,
385
- mode: () => store.mode,
386
- ids,
318
+ /** The theme the window shows: a previewed one, the selected one, or the default while that one is not coming. */
319
+ themeId: createMemo(() => selectThemeId(state(), knownThemes())),
320
+ colorScheme: createMemo(() => selectColorScheme(state())),
321
+ /** The mode the window shows, a previewed scheme included. */
322
+ mode: createMemo(() => selectMode(state())),
323
+ // Bundled and registered themes in one list, alphabetical by name.
324
+ ids: () =>
325
+ [...themeIDs(), ...Object.keys(state().themes).filter((id) => !knownThemes().has(id))].toSorted((a, b) =>
326
+ name(a).localeCompare(name(b), undefined, { sensitivity: "base" }),
327
+ ),
387
328
  name,
388
- loadThemes,
389
- themes: () => store.themes,
390
- setTheme,
391
- setColorScheme,
329
+ /** Loads every bundled theme; resolves once each import has loaded, failed or timed out. */
330
+ loadThemes: () => {
331
+ dispatch({ type: "themes.preload" })
332
+
333
+ if (!selectThemesLoading(untrack(state))) return Promise.resolve(untrack(state).themes)
334
+
335
+ return new Promise<Readonly<Record<string, DesktopTheme>>>((resolve) =>
336
+ waiters.add(() => resolve(untrack(state).themes)),
337
+ )
338
+ },
339
+ themes: () => state().themes,
340
+ setTheme: (id: string) => dispatch({ type: "theme.selected", id }),
341
+ setColorScheme: (scheme: ColorScheme) => dispatch({ type: "scheme.selected", scheme }),
392
342
  /** Lists a theme beside the bundled ones until the returned function withdraws it. */
393
343
  registerTheme: (theme: DesktopTheme) => {
394
- setStore("themes", theme.id, theme)
395
-
396
- return () => {
397
- // A theme registered again under the same id replaces this one; leave it.
398
- if (unwrap(store.themes)[theme.id] !== theme) return
399
- setStore(
400
- "themes",
401
- produce((themes) => {
402
- delete themes[theme.id]
403
- }),
404
- )
405
- }
344
+ dispatch({ type: "theme.registered", theme })
345
+
346
+ return () => dispatch({ type: "theme.unregistered", theme })
406
347
  },
407
348
  /**
408
349
  * Every theme source has registered what it will: a selected theme that is still missing is not coming, so the
409
350
  * window shows the default theme and keeps the choice for when it returns.
410
351
  */
411
- settle: () => setStore("settled", true),
412
- previewTheme: (id: string) => {
413
- const next = id
414
-
415
- if (!next) return
416
-
417
- if (next !== "oc-2" && !knownThemes().has(next) && !store.themes[next]) return
418
- setStore("previewThemeId", next)
419
- void load(next).then((theme) => {
420
- if (!theme || store.previewThemeId !== next) return
421
-
422
- const mode = store.previewScheme
423
- ? store.previewScheme === "system"
424
- ? getSystemMode()
425
- : store.previewScheme
426
- : store.mode
427
-
428
- applyTheme(theme, next, mode, store.previewScheme ?? store.colorScheme)
429
- })
430
- },
431
- previewColorScheme: (scheme: ColorScheme) => {
432
- setStore("previewScheme", scheme)
433
- const mode = scheme === "system" ? getSystemMode() : scheme
434
- const id = store.previewThemeId ?? active()
435
- void load(id).then((theme) => {
436
- if (!theme) return
437
-
438
- if ((store.previewThemeId ?? active()) !== id) return
439
-
440
- if (store.previewScheme !== scheme) return
441
- applyTheme(theme, id, mode, scheme)
442
- })
443
- },
444
- commitPreview: () => {
445
- if (store.previewThemeId) {
446
- setTheme(store.previewThemeId)
447
- }
448
-
449
- if (store.previewScheme) {
450
- setColorScheme(store.previewScheme)
451
- }
452
-
453
- setStore("previewThemeId", null)
454
- setStore("previewScheme", null)
455
- },
456
- cancelPreview: () => {
457
- setStore("previewThemeId", null)
458
- setStore("previewScheme", null)
459
- const id = active()
460
- void load(id).then((theme) => {
461
- if (!theme || active() !== id) return
462
- applyTheme(theme, id, store.mode, store.colorScheme)
463
- })
464
- },
352
+ settle: () => dispatch({ type: "settled" }),
353
+ /** Shows a theme without saving it, until `commitPreview` or `cancelPreview`. */
354
+ previewTheme: (id: string) => dispatch({ type: "preview.theme", id }),
355
+ /** Shows a color scheme without saving it, until `commitPreview` or `cancelPreview`. */
356
+ previewColorScheme: (scheme: ColorScheme) => dispatch({ type: "preview.scheme", scheme }),
357
+ commitPreview: () => dispatch({ type: "preview.committed" }),
358
+ cancelPreview: () => dispatch({ type: "preview.cancelled" }),
465
359
  }
466
360
  },
467
361
  })