@opencode/ui 0.0.0-dev-552 → 0.0.0-dev-555

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.
@@ -2,7 +2,7 @@ import type { DesktopTheme } from "./types";
2
2
  export type ColorScheme = "light" | "dark" | "system";
3
3
  export declare const { use: useTheme, provider: ThemeProvider }: {
4
4
  context: import("solid-js").Context<{
5
- themeId: () => string;
5
+ themeId: import("solid-js").Accessor<string>;
6
6
  colorScheme: () => ColorScheme;
7
7
  mode: () => "dark" | "light";
8
8
  ids: () => string[];
@@ -11,7 +11,13 @@ export declare const { use: useTheme, provider: ThemeProvider }: {
11
11
  themes: () => Record<string, DesktopTheme>;
12
12
  setTheme: (id: string) => void;
13
13
  setColorScheme: (scheme: ColorScheme) => void;
14
- registerTheme: (theme: DesktopTheme) => void;
14
+ /** Lists a theme beside the bundled ones until the returned function withdraws it. */
15
+ registerTheme: (theme: DesktopTheme) => () => void;
16
+ /**
17
+ * Every theme source has registered what it will: a selected theme that is still missing is not coming, so the
18
+ * window shows the default theme and keeps the choice for when it returns.
19
+ */
20
+ settle: () => void;
15
21
  previewTheme: (id: string) => void;
16
22
  previewColorScheme: (scheme: ColorScheme) => void;
17
23
  commitPreview: () => void;
@@ -19,10 +25,11 @@ export declare const { use: useTheme, provider: ThemeProvider }: {
19
25
  } | undefined>;
20
26
  provider: (props: import("solid-js").ParentProps<{
21
27
  defaultTheme?: string;
22
- onThemeApplied?: (theme: DesktopTheme, mode: "light" | "dark", scheme: ColorScheme) => void;
28
+ /** The window shows the theme: its colors, or until a registered theme arrives, those cached from the last run. */
29
+ onThemeApplied?: (mode: "light" | "dark", scheme: ColorScheme) => void;
23
30
  }>) => import("solid-js").JSX.Element;
24
31
  use(): {
25
- themeId: () => string;
32
+ themeId: import("solid-js").Accessor<string>;
26
33
  colorScheme: () => ColorScheme;
27
34
  mode: () => "dark" | "light";
28
35
  ids: () => string[];
@@ -31,7 +38,13 @@ export declare const { use: useTheme, provider: ThemeProvider }: {
31
38
  themes: () => Record<string, DesktopTheme>;
32
39
  setTheme: (id: string) => void;
33
40
  setColorScheme: (scheme: ColorScheme) => void;
34
- registerTheme: (theme: DesktopTheme) => void;
41
+ /** Lists a theme beside the bundled ones until the returned function withdraws it. */
42
+ registerTheme: (theme: DesktopTheme) => () => void;
43
+ /**
44
+ * Every theme source has registered what it will: a selected theme that is still missing is not coming, so the
45
+ * window shows the default theme and keeps the choice for when it returns.
46
+ */
47
+ settle: () => void;
35
48
  previewTheme: (id: string) => void;
36
49
  previewColorScheme: (scheme: ColorScheme) => void;
37
50
  commitPreview: () => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opencode/ui",
3
- "version": "0.0.0-dev-552",
3
+ "version": "0.0.0-dev-555",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -288,7 +288,7 @@
288
288
  "dependencies": {
289
289
  "@kitlangton/solid-motion": "0.2.2",
290
290
  "@kobalte/core": "0.13.13",
291
- "@opencode/util": "0.0.0-dev-552",
291
+ "@opencode/util": "0.0.0-dev-555",
292
292
  "@pierre/diffs": "1.6.1",
293
293
  "@pierre/highlights": "0.2.0",
294
294
  "@solid-primitives/event-listener": "2.4.6",
@@ -1,7 +1,7 @@
1
1
  // @refresh reload
2
2
 
3
- import { createEffect, onMount } from "solid-js"
4
- import { createStore } from "solid-js/store"
3
+ import { createEffect, createMemo, onMount } from "solid-js"
4
+ import { createStore, produce, unwrap } from "solid-js/store"
5
5
  import { makeEventListener } from "@solid-primitives/event-listener"
6
6
  import { createSimpleContext } from "../context/helper"
7
7
  import oc2ThemeJson from "./themes/oc-2.json"
@@ -11,6 +11,18 @@ import type { DesktopTheme } from "./types"
11
11
 
12
12
  export type ColorScheme = "light" | "dark" | "system"
13
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
+ }
25
+
14
26
  const STORAGE_KEYS = {
15
27
  THEME_ID: "opencode-theme-id",
16
28
  COLOR_SCHEME: "opencode-color-scheme",
@@ -49,56 +61,53 @@ function knownThemes() {
49
61
  return known
50
62
  }
51
63
 
52
- const names: Record<string, string> = {
53
- "oc-2": "OpenCode",
54
- amoled: "AMOLED",
55
- aura: "Aura",
56
- ayu: "Ayu",
57
- carbonfox: "Carbonfox",
58
- catppuccin: "Catppuccin",
59
- "catppuccin-frappe": "Catppuccin Frappe",
60
- "catppuccin-macchiato": "Catppuccin Macchiato",
61
- cobalt2: "Cobalt2",
62
- cursor: "Cursor",
63
- dracula: "Dracula",
64
- everforest: "Everforest",
65
- flexoki: "Flexoki",
66
- github: "GitHub",
67
- gruvbox: "Gruvbox",
68
- kanagawa: "Kanagawa",
69
- "lucent-orng": "Lucent Orng",
70
- material: "Material",
71
- matrix: "Matrix",
72
- mercury: "Mercury",
73
- monokai: "Monokai",
74
- nightowl: "Night Owl",
75
- nord: "Nord",
76
- "one-dark": "One Dark",
77
- onedarkpro: "One Dark Pro",
78
- orng: "Orng",
79
- "osaka-jade": "Osaka Jade",
80
- palenight: "Palenight",
81
- rosepine: "Rose Pine",
82
- shadesofpurple: "Shades of Purple",
83
- solarized: "Solarized",
84
- synthwave84: "Synthwave '84",
85
- tokyonight: "Tokyonight",
86
- vercel: "Vercel",
87
- vesper: "Vesper",
88
- zenburn: "Zenburn",
89
- }
90
-
64
+ const names = new Map(
65
+ Object.entries({
66
+ "oc-2": "OpenCode",
67
+ amoled: "AMOLED",
68
+ aura: "Aura",
69
+ ayu: "Ayu",
70
+ carbonfox: "Carbonfox",
71
+ catppuccin: "Catppuccin",
72
+ "catppuccin-frappe": "Catppuccin Frappe",
73
+ "catppuccin-macchiato": "Catppuccin Macchiato",
74
+ cobalt2: "Cobalt2",
75
+ cursor: "Cursor",
76
+ dracula: "Dracula",
77
+ everforest: "Everforest",
78
+ flexoki: "Flexoki",
79
+ github: "GitHub",
80
+ gruvbox: "Gruvbox",
81
+ kanagawa: "Kanagawa",
82
+ "lucent-orng": "Lucent Orng",
83
+ material: "Material",
84
+ matrix: "Matrix",
85
+ mercury: "Mercury",
86
+ monokai: "Monokai",
87
+ nightowl: "Night Owl",
88
+ nord: "Nord",
89
+ "one-dark": "One Dark",
90
+ onedarkpro: "One Dark Pro",
91
+ orng: "Orng",
92
+ "osaka-jade": "Osaka Jade",
93
+ palenight: "Palenight",
94
+ rosepine: "Rose Pine",
95
+ shadesofpurple: "Shades of Purple",
96
+ solarized: "Solarized",
97
+ synthwave84: "Synthwave '84",
98
+ tokyonight: "Tokyonight",
99
+ vercel: "Vercel",
100
+ vesper: "Vesper",
101
+ zenburn: "Zenburn",
102
+ }),
103
+ )
104
+
105
+ // SAFETY: oc-2.json follows desktop-theme.schema.json; JSON modules only widen its colors to `string`, and
106
+ // v2/resolve.test.ts resolves the same file as a DesktopTheme.
91
107
  const oc2Theme = oc2ThemeJson as DesktopTheme
92
108
 
93
- function resolveStoredTheme(id: string | null | undefined, registered?: Record<string, DesktopTheme>) {
94
- if (id === "oc-2" || (id && (knownThemes().has(id) || registered?.[id]))) return id
95
-
96
- return "oc-2"
97
- }
98
-
109
+ // Storage access throws where localStorage is missing or blocked; every caller treats that as no stored value.
99
110
  function read(key: string) {
100
- if (typeof localStorage !== "object") return null
101
-
102
111
  try {
103
112
  return localStorage.getItem(key)
104
113
  } catch {
@@ -107,30 +116,32 @@ function read(key: string) {
107
116
  }
108
117
 
109
118
  function write(key: string, value: string) {
110
- if (typeof localStorage !== "object") return
111
-
112
119
  try {
113
120
  localStorage.setItem(key, value)
114
121
  } catch {}
115
122
  }
116
123
 
117
124
  function drop(key: string) {
118
- if (typeof localStorage !== "object") return
119
-
120
125
  try {
121
126
  localStorage.removeItem(key)
122
127
  } catch {}
123
128
  }
124
129
 
130
+ function readScheme(): ColorScheme {
131
+ const value = read(STORAGE_KEYS.COLOR_SCHEME)
132
+
133
+ return value === "light" || value === "dark" ? value : "system"
134
+ }
135
+
125
136
  function clear() {
126
137
  drop(STORAGE_KEYS.THEME_CSS_LIGHT)
127
138
  drop(STORAGE_KEYS.THEME_CSS_DARK)
128
139
  }
129
140
 
130
141
  function ensureThemeStyleElement(): HTMLStyleElement {
131
- const existing = document.getElementById(THEME_STYLE_ID) as HTMLStyleElement | null
142
+ const existing = document.getElementById(THEME_STYLE_ID)
132
143
 
133
- if (existing) return existing
144
+ if (existing instanceof HTMLStyleElement) return existing
134
145
  const element = document.createElement("style")
135
146
  element.id = THEME_STYLE_ID
136
147
  document.head.appendChild(element)
@@ -139,9 +150,7 @@ function ensureThemeStyleElement(): HTMLStyleElement {
139
150
  }
140
151
 
141
152
  function getSystemMode(): "light" | "dark" {
142
- if (typeof window !== "object") return "light"
143
-
144
- return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"
153
+ return globalThis.matchMedia?.("(prefers-color-scheme: dark)").matches ? "dark" : "light"
145
154
  }
146
155
 
147
156
  function applyThemeCss(theme: DesktopTheme, themeId: string, mode: "light" | "dark") {
@@ -191,28 +200,31 @@ export const { use: useTheme, provider: ThemeProvider } = createSimpleContext({
191
200
  name: "Theme",
192
201
  init: (props: {
193
202
  defaultTheme?: string
194
- onThemeApplied?: (theme: DesktopTheme, mode: "light" | "dark", scheme: ColorScheme) => void
203
+ /** The window shows the theme: its colors, or until a registered theme arrives, those cached from the last run. */
204
+ onThemeApplied?: (mode: "light" | "dark", scheme: ColorScheme) => void
195
205
  }) => {
196
- const rawTheme = read(STORAGE_KEYS.THEME_ID) ?? props.defaultTheme
197
- const themeId = resolveStoredTheme(rawTheme)
198
-
199
- if (rawTheme && rawTheme !== themeId) {
200
- write(STORAGE_KEYS.THEME_ID, themeId)
201
- clear()
202
- }
203
-
204
- const colorScheme = (read(STORAGE_KEYS.COLOR_SCHEME) as ColorScheme | null) ?? "system"
206
+ // 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()
205
209
  const mode = colorScheme === "system" ? getSystemMode() : colorScheme
206
210
 
207
- const [store, setStore] = createStore({
211
+ const [store, setStore] = createStore<ThemeState>({
208
212
  themes: {
209
213
  "oc-2": oc2Theme,
210
- } as Record<string, DesktopTheme>,
214
+ },
211
215
  themeId,
212
216
  colorScheme,
213
217
  mode,
214
- previewThemeId: null as string | null,
215
- previewScheme: null as ColorScheme | null,
218
+ previewThemeId: null,
219
+ previewScheme: null,
220
+ settled: false,
221
+ })
222
+
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"
216
228
  })
217
229
 
218
230
  const loads = new Map<string, Promise<DesktopTheme | undefined>>()
@@ -249,32 +261,22 @@ export const { use: useTheme, provider: ThemeProvider } = createSimpleContext({
249
261
 
250
262
  const applyTheme = (theme: DesktopTheme, themeId: string, mode: "light" | "dark", scheme: ColorScheme) => {
251
263
  applyThemeCss(theme, themeId, mode)
252
- props.onThemeApplied?.(theme, mode, scheme)
264
+ props.onThemeApplied?.(mode, scheme)
253
265
  }
254
266
 
255
- const ids = () => {
256
- const extra = Object.keys(store.themes)
257
- .filter((id) => !knownThemes().has(id))
258
- .sort()
259
-
260
- const all = themeIDs()
267
+ const name = (id: string) => store.themes[id]?.name ?? names.get(id) ?? id
261
268
 
262
- if (extra.length === 0) return all
263
-
264
- return [...all, ...extra]
265
- }
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" }),
273
+ )
266
274
 
267
275
  const loadThemes = () => Promise.all(themeIDs().map(load)).then(() => store.themes)
268
276
 
269
277
  const onStorage = (e: StorageEvent) => {
270
278
  if (e.key === STORAGE_KEYS.THEME_ID && e.newValue) {
271
- const next = resolveStoredTheme(e.newValue, store.themes)
272
-
273
- if (next !== e.newValue) {
274
- write(STORAGE_KEYS.THEME_ID, next)
275
- clear()
276
- }
277
-
279
+ const next = e.newValue
278
280
  setStore("themeId", next)
279
281
 
280
282
  if (next === "oc-2") {
@@ -290,8 +292,9 @@ export const { use: useTheme, provider: ThemeProvider } = createSimpleContext({
290
292
  }
291
293
 
292
294
  if (e.key === STORAGE_KEYS.COLOR_SCHEME && e.newValue) {
293
- setStore("colorScheme", e.newValue as ColorScheme)
294
- setStore("mode", e.newValue === "system" ? getSystemMode() : (e.newValue as "light" | "dark"))
295
+ const scheme = readScheme()
296
+ setStore("colorScheme", scheme)
297
+ setStore("mode", scheme === "system" ? getSystemMode() : scheme)
295
298
  }
296
299
  }
297
300
 
@@ -307,14 +310,8 @@ export const { use: useTheme, provider: ThemeProvider } = createSimpleContext({
307
310
 
308
311
  makeEventListener(mediaQuery, "change", onMedia)
309
312
 
310
- const rawTheme = read(STORAGE_KEYS.THEME_ID) ?? props.defaultTheme
311
- const savedTheme = resolveStoredTheme(rawTheme, store.themes)
312
- const savedScheme = (read(STORAGE_KEYS.COLOR_SCHEME) as ColorScheme | null) ?? "system"
313
-
314
- if (rawTheme && rawTheme !== savedTheme) {
315
- write(STORAGE_KEYS.THEME_ID, savedTheme)
316
- clear()
317
- }
313
+ const savedTheme = read(STORAGE_KEYS.THEME_ID) || props.defaultTheme || "oc-2"
314
+ const savedScheme = readScheme()
318
315
 
319
316
  if (savedTheme !== store.themeId) setStore("themeId", savedTheme)
320
317
 
@@ -327,10 +324,22 @@ export const { use: useTheme, provider: ThemeProvider } = createSimpleContext({
327
324
  })
328
325
 
329
326
  createEffect(() => {
330
- const theme = store.themes[store.themeId]
327
+ const id = active()
328
+ const theme = store.themes[id]
329
+
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)
335
+
336
+ return
337
+ }
331
338
 
332
- if (!theme) return
333
- applyTheme(theme, store.themeId, store.mode, store.colorScheme)
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)
334
343
  })
335
344
 
336
345
  const setTheme = (id: string) => {
@@ -371,16 +380,35 @@ export const { use: useTheme, provider: ThemeProvider } = createSimpleContext({
371
380
  }
372
381
 
373
382
  return {
374
- themeId: () => store.themeId,
383
+ themeId: active,
375
384
  colorScheme: () => store.colorScheme,
376
385
  mode: () => store.mode,
377
386
  ids,
378
- name: (id: string) => store.themes[id]?.name ?? names[id] ?? id,
387
+ name,
379
388
  loadThemes,
380
389
  themes: () => store.themes,
381
390
  setTheme,
382
391
  setColorScheme,
383
- registerTheme: (theme: DesktopTheme) => setStore("themes", theme.id, theme),
392
+ /** Lists a theme beside the bundled ones until the returned function withdraws it. */
393
+ 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
+ }
406
+ },
407
+ /**
408
+ * Every theme source has registered what it will: a selected theme that is still missing is not coming, so the
409
+ * window shows the default theme and keeps the choice for when it returns.
410
+ */
411
+ settle: () => setStore("settled", true),
384
412
  previewTheme: (id: string) => {
385
413
  const next = id
386
414
 
@@ -403,11 +431,11 @@ export const { use: useTheme, provider: ThemeProvider } = createSimpleContext({
403
431
  previewColorScheme: (scheme: ColorScheme) => {
404
432
  setStore("previewScheme", scheme)
405
433
  const mode = scheme === "system" ? getSystemMode() : scheme
406
- const id = store.previewThemeId ?? store.themeId
434
+ const id = store.previewThemeId ?? active()
407
435
  void load(id).then((theme) => {
408
436
  if (!theme) return
409
437
 
410
- if ((store.previewThemeId ?? store.themeId) !== id) return
438
+ if ((store.previewThemeId ?? active()) !== id) return
411
439
 
412
440
  if (store.previewScheme !== scheme) return
413
441
  applyTheme(theme, id, mode, scheme)
@@ -428,9 +456,10 @@ export const { use: useTheme, provider: ThemeProvider } = createSimpleContext({
428
456
  cancelPreview: () => {
429
457
  setStore("previewThemeId", null)
430
458
  setStore("previewScheme", null)
431
- void load(store.themeId).then((theme) => {
432
- if (!theme) return
433
- applyTheme(theme, store.themeId, store.mode, store.colorScheme)
459
+ const id = active()
460
+ void load(id).then((theme) => {
461
+ if (!theme || active() !== id) return
462
+ applyTheme(theme, id, store.mode, store.colorScheme)
434
463
  })
435
464
  },
436
465
  }