@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.
Files changed (117) 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 +9 -3
  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/machine.d.ts +157 -0
  25. package/dist/data-display/project-avatar/animation-machine.d.ts +62 -0
  26. package/dist/data-display/project-avatar/animation.d.ts +1 -1
  27. package/dist/feedback/activity-ring/activity-ring.d.ts +2 -1
  28. package/dist/feedback/toast/machine.d.ts +57 -0
  29. package/dist/feedback/toast/toast.d.ts +1 -1
  30. package/dist/forms/checkbox/checkbox.d.ts +6 -1
  31. package/dist/forms/select/machine.d.ts +45 -0
  32. package/dist/forms/select/select.d.ts +4 -0
  33. package/dist/forms/typeahead/machine.d.ts +85 -0
  34. package/dist/hooks/auto-scroll-machine.d.ts +89 -0
  35. package/dist/hooks/create-auto-scroll.d.ts +1 -1
  36. package/dist/hooks/list-machine.d.ts +150 -0
  37. package/dist/hooks/use-filtered-list.d.ts +21 -35
  38. package/dist/i18n/en.d.ts +5 -0
  39. package/dist/icons/catalog.d.ts +8 -0
  40. package/dist/navigation/menu/machine.d.ts +49 -0
  41. package/dist/navigation/menu/menu.d.ts +5 -3
  42. package/dist/navigation/segmented-control/machine.d.ts +46 -0
  43. package/dist/overlays/mcp-sign-in/machine.d.ts +184 -0
  44. package/dist/overlays/mcp-sign-in/mcp-sign-in.d.ts +40 -0
  45. package/dist/overlays/tooltip/machine.d.ts +146 -0
  46. package/dist/theme/context.d.ts +38 -14
  47. package/dist/theme/machine.d.ts +174 -0
  48. package/dist/typography/text-shimmer/machine.d.ts +48 -0
  49. package/package.json +19 -3
  50. package/src/actions/button/button.tsx +11 -6
  51. package/src/actions/icon-button/icon-button.tsx +17 -4
  52. package/src/components/animated-number-machine.ts +214 -0
  53. package/src/components/animated-number.tsx +134 -83
  54. package/src/components/file-icon-machine.ts +78 -0
  55. package/src/components/file-icon.tsx +96 -579
  56. package/src/components/file-icons/choose-icon-name.ts +574 -0
  57. package/src/components/image-flight-machine.ts +247 -0
  58. package/src/components/image-preview.css +5 -2
  59. package/src/components/image-preview.tsx +390 -252
  60. package/src/components/image-zoom-machine.ts +191 -0
  61. package/src/components/image-zoom.ts +22 -14
  62. package/src/components/list.tsx +46 -137
  63. package/src/components/menu-scroll.css +19 -8
  64. package/src/components/menu-scroll.tsx +111 -34
  65. package/src/components/popover-machine.ts +198 -0
  66. package/src/components/popover.tsx +157 -89
  67. package/src/components/resize-handle-machine.ts +174 -0
  68. package/src/components/resize-handle.tsx +116 -67
  69. package/src/components/scroll-view-machine.ts +244 -0
  70. package/src/components/scroll-view.tsx +163 -116
  71. package/src/components/spinner.css +18 -5
  72. package/src/components/spinner.tsx +28 -14
  73. package/src/components/text-field-machine.ts +102 -0
  74. package/src/components/text-field.tsx +78 -24
  75. package/src/components/text-reveal-machine.ts +184 -0
  76. package/src/components/text-reveal.tsx +96 -93
  77. package/src/components/typewriter-machine.ts +134 -0
  78. package/src/components/typewriter.tsx +50 -39
  79. package/src/context/dialog-machine.ts +280 -0
  80. package/src/context/dialog.tsx +168 -177
  81. package/src/data-display/line-comment/line-comment.tsx +157 -178
  82. package/src/data-display/line-comment/machine.ts +391 -0
  83. package/src/data-display/project-avatar/animation-machine.ts +182 -0
  84. package/src/data-display/project-avatar/animation.ts +67 -38
  85. package/src/feedback/activity-ring/activity-ring.tsx +1 -1
  86. package/src/feedback/toast/machine.ts +115 -0
  87. package/src/feedback/toast/toast.tsx +55 -59
  88. package/src/forms/checkbox/checkbox-compact.css +169 -0
  89. package/src/forms/checkbox/checkbox.tsx +93 -31
  90. package/src/forms/search-list/search-list.css +5 -1
  91. package/src/forms/search-list/search-list.tsx +4 -4
  92. package/src/forms/select/machine.ts +81 -0
  93. package/src/forms/select/select.css +59 -17
  94. package/src/forms/select/select.tsx +67 -31
  95. package/src/forms/typeahead/machine.ts +253 -0
  96. package/src/forms/typeahead/typeahead.tsx +70 -62
  97. package/src/hooks/auto-scroll-machine.ts +261 -0
  98. package/src/hooks/create-auto-scroll.tsx +110 -205
  99. package/src/hooks/list-machine.ts +359 -0
  100. package/src/hooks/use-filtered-list.tsx +101 -112
  101. package/src/i18n/en.ts +6 -1
  102. package/src/icons/catalog.ts +8 -0
  103. package/src/navigation/menu/machine.ts +109 -0
  104. package/src/navigation/menu/menu.tsx +130 -22
  105. package/src/navigation/segmented-control/machine.ts +72 -0
  106. package/src/navigation/segmented-control/segmented-control.tsx +46 -46
  107. package/src/overlays/dialog/dialog.tsx +7 -1
  108. package/src/overlays/mcp-sign-in/machine.ts +343 -0
  109. package/src/overlays/mcp-sign-in/mcp-sign-in.ts +159 -0
  110. package/src/overlays/tooltip/machine.ts +303 -0
  111. package/src/overlays/tooltip/tooltip.tsx +157 -92
  112. package/src/styles/theme.css +10 -2
  113. package/src/theme/context.tsx +192 -269
  114. package/src/theme/machine.ts +508 -0
  115. package/src/typography/text-shimmer/machine.ts +90 -0
  116. package/src/typography/text-shimmer/text-shimmer.css +71 -39
  117. package/src/typography/text-shimmer/text-shimmer.tsx +41 -24
@@ -0,0 +1,508 @@
1
+ import type { DesktopTheme } from "./types"
2
+
3
+ export type ColorScheme = "light" | "dark" | "system"
4
+
5
+ export type ResolvedMode = "light" | "dark"
6
+
7
+ export const DEFAULT_THEME_ID = "oc-2"
8
+
9
+ /** A bundled theme import that has not settled by then shows the default theme until it arrives. */
10
+ export const THEME_LOAD_TIMEOUT_MS = 5_000
11
+
12
+ export type ThemeSelection = { readonly themeId: string; readonly scheme: ColorScheme }
13
+
14
+ /** Overrides one or both axes of the committed selection; a preview with neither is `committed`. */
15
+ export type ThemePreview = { readonly themeId?: string; readonly scheme?: ColorScheme }
16
+
17
+ /** What the window shows. `preload` is a registered theme that may still arrive: the cached colors stay on screen. */
18
+ export type AppliedTheme =
19
+ | {
20
+ readonly kind: "theme"
21
+ readonly id: string
22
+ readonly theme: DesktopTheme
23
+ readonly mode: ResolvedMode
24
+ readonly scheme: ColorScheme
25
+ }
26
+ | { readonly kind: "preload"; readonly id: string; readonly mode: ResolvedMode; readonly scheme: ColorScheme }
27
+
28
+ /** The colors in localStorage that the next start paints before the app loads. `unknown` keeps the last run's. */
29
+ export type CachedTheme =
30
+ | { readonly status: "unknown" }
31
+ | { readonly status: "cleared" }
32
+ | { readonly status: "cached"; readonly id: string; readonly theme: DesktopTheme }
33
+
34
+ type ThemeData = {
35
+ readonly committed: ThemeSelection
36
+ readonly systemMode: ResolvedMode
37
+ /** `settled` once every theme source has registered what it will: a missing registered theme is not coming. */
38
+ readonly registration: "pending" | "settled"
39
+ /** Loaded bundled themes and registered themes, by id. */
40
+ readonly themes: Readonly<Record<string, DesktopTheme>>
41
+ /** The attempt of each bundled theme import in flight, including one that timed out and may still arrive. */
42
+ readonly loads: Readonly<Record<string, number>>
43
+ /**
44
+ * Bundled themes the window shows the default theme for: the import failed, or timed out and may still arrive.
45
+ * Selecting, previewing or preloading one tries again.
46
+ */
47
+ readonly failed: Readonly<Record<string, true>>
48
+ readonly issued: number
49
+ readonly applied: AppliedTheme | undefined
50
+ readonly cached: CachedTheme
51
+ }
52
+
53
+ export type ThemeMachineState =
54
+ | ({ readonly status: "committed" } & ThemeData)
55
+ | ({ readonly status: "previewing"; readonly preview: ThemePreview } & ThemeData)
56
+ | ({ readonly status: "disposed" } & ThemeData)
57
+
58
+ type ActiveState = Exclude<ThemeMachineState, { status: "disposed" }>
59
+
60
+ export type ThemeEvent =
61
+ | { readonly type: "theme.selected"; readonly id: string }
62
+ | { readonly type: "scheme.selected"; readonly scheme: ColorScheme }
63
+ | { readonly type: "preview.theme"; readonly id: string }
64
+ | { readonly type: "preview.scheme"; readonly scheme: ColorScheme }
65
+ | { readonly type: "preview.committed" }
66
+ | { readonly type: "preview.cancelled" }
67
+ | { readonly type: "theme.registered"; readonly theme: DesktopTheme }
68
+ | { readonly type: "theme.unregistered"; readonly theme: DesktopTheme }
69
+ | { readonly type: "settled" }
70
+ | { readonly type: "themes.preload" }
71
+ | { readonly type: "theme.loaded"; readonly id: string; readonly attempt: number; readonly theme: DesktopTheme }
72
+ | { readonly type: "theme.load.failed"; readonly id: string; readonly attempt: number }
73
+ | { readonly type: "theme.load.timed-out"; readonly id: string; readonly attempt: number }
74
+ | { readonly type: "system.mode.changed"; readonly mode: ResolvedMode }
75
+ | { readonly type: "storage.theme.changed"; readonly id: string }
76
+ | { readonly type: "storage.scheme.changed"; readonly scheme: ColorScheme }
77
+ | { readonly type: "disposed" }
78
+
79
+ export type ThemeEffect =
80
+ | { readonly type: "load-theme"; readonly id: string; readonly attempt: number; readonly timeoutMs: number }
81
+ | { readonly type: "cancel-load-timer"; readonly attempt: number }
82
+ | { readonly type: "cancel-all-loads" }
83
+ | { readonly type: "persist-theme"; readonly id: string }
84
+ | { readonly type: "persist-scheme"; readonly scheme: ColorScheme }
85
+ | { readonly type: "cache-theme"; readonly id: string; readonly theme: DesktopTheme }
86
+ | { readonly type: "clear-cache" }
87
+ | {
88
+ readonly type: "apply-theme"
89
+ readonly id: string
90
+ readonly theme: DesktopTheme
91
+ readonly mode: ResolvedMode
92
+ readonly scheme: ColorScheme
93
+ }
94
+ | { readonly type: "notify-applied"; readonly mode: ResolvedMode; readonly scheme: ColorScheme }
95
+ | { readonly type: "warn-missing-theme"; readonly id: string }
96
+
97
+ export type ThemeTransition = { readonly state: ThemeMachineState; readonly effects: readonly ThemeEffect[] }
98
+
99
+ type Step = { readonly state: ActiveState; readonly effects: readonly ThemeEffect[] }
100
+
101
+ const NO_EFFECTS: readonly ThemeEffect[] = []
102
+
103
+ export function parseColorScheme(value: string | null | undefined): ColorScheme {
104
+ return value === "light" || value === "dark" ? value : "system"
105
+ }
106
+
107
+ export function createThemeState(input: {
108
+ readonly themeId: string
109
+ readonly scheme: ColorScheme
110
+ readonly systemMode: ResolvedMode
111
+ readonly defaultTheme: DesktopTheme
112
+ readonly bundled: ReadonlySet<string>
113
+ }): ThemeTransition {
114
+ return reconcile(
115
+ {
116
+ status: "committed",
117
+ committed: { themeId: input.themeId, scheme: input.scheme },
118
+ systemMode: input.systemMode,
119
+ registration: "pending",
120
+ themes: { [DEFAULT_THEME_ID]: input.defaultTheme },
121
+ loads: {},
122
+ failed: {},
123
+ issued: 0,
124
+ applied: undefined,
125
+ cached: { status: "unknown" },
126
+ },
127
+ input.bundled,
128
+ NO_EFFECTS,
129
+ )
130
+ }
131
+
132
+ export function transitionTheme(
133
+ state: ThemeMachineState,
134
+ event: ThemeEvent,
135
+ bundled: ReadonlySet<string>,
136
+ ): ThemeTransition {
137
+ if (state.status === "disposed") return { state, effects: NO_EFFECTS }
138
+
139
+ switch (event.type) {
140
+ case "theme.selected": {
141
+ if (!known(state, event.id, bundled)) return { state, effects: [{ type: "warn-missing-theme", id: event.id }] }
142
+
143
+ if (state.committed.themeId === event.id && !state.failed[event.id] && preview(state).themeId === undefined)
144
+ return { state, effects: NO_EFFECTS }
145
+
146
+ return reconcile(
147
+ {
148
+ ...withPreview(state, { ...preview(state), themeId: undefined }),
149
+ ...retry(state, [event.id]),
150
+ committed: { ...state.committed, themeId: event.id },
151
+ },
152
+ bundled,
153
+ [{ type: "persist-theme", id: event.id }],
154
+ )
155
+ }
156
+
157
+ case "scheme.selected": {
158
+ if (state.committed.scheme === event.scheme && preview(state).scheme === undefined)
159
+ return { state, effects: NO_EFFECTS }
160
+
161
+ return reconcile(
162
+ {
163
+ ...withPreview(state, { ...preview(state), scheme: undefined }),
164
+ committed: { ...state.committed, scheme: event.scheme },
165
+ },
166
+ bundled,
167
+ [{ type: "persist-scheme", scheme: event.scheme }],
168
+ )
169
+ }
170
+
171
+ case "preview.theme": {
172
+ if (!event.id || !known(state, event.id, bundled)) return { state, effects: NO_EFFECTS }
173
+
174
+ if (preview(state).themeId === event.id && !state.failed[event.id]) return { state, effects: NO_EFFECTS }
175
+
176
+ return reconcile(
177
+ { ...withPreview(state, { ...preview(state), themeId: event.id }), ...retry(state, [event.id]) },
178
+ bundled,
179
+ NO_EFFECTS,
180
+ )
181
+ }
182
+
183
+ case "preview.scheme": {
184
+ if (preview(state).scheme === event.scheme) return { state, effects: NO_EFFECTS }
185
+
186
+ return reconcile(withPreview(state, { ...preview(state), scheme: event.scheme }), bundled, NO_EFFECTS)
187
+ }
188
+
189
+ case "preview.committed": {
190
+ if (state.status !== "previewing") return { state, effects: NO_EFFECTS }
191
+
192
+ return reconcile(
193
+ {
194
+ ...withPreview(state, {}),
195
+ committed: {
196
+ themeId: state.preview.themeId ?? state.committed.themeId,
197
+ scheme: state.preview.scheme ?? state.committed.scheme,
198
+ },
199
+ },
200
+ bundled,
201
+ [
202
+ ...(state.preview.themeId === undefined
203
+ ? []
204
+ : [{ type: "persist-theme", id: state.preview.themeId } as const]),
205
+ ...(state.preview.scheme === undefined
206
+ ? []
207
+ : [{ type: "persist-scheme", scheme: state.preview.scheme } as const]),
208
+ ],
209
+ )
210
+ }
211
+
212
+ case "preview.cancelled": {
213
+ if (state.status !== "previewing") return { state, effects: NO_EFFECTS }
214
+
215
+ return reconcile(withPreview(state, {}), bundled, NO_EFFECTS)
216
+ }
217
+
218
+ case "theme.registered": {
219
+ if (state.themes[event.theme.id] === event.theme) return { state, effects: NO_EFFECTS }
220
+
221
+ return reconcile(
222
+ {
223
+ ...state,
224
+ themes: { ...state.themes, [event.theme.id]: event.theme },
225
+ failed: omit(state.failed, [event.theme.id]),
226
+ },
227
+ bundled,
228
+ NO_EFFECTS,
229
+ )
230
+ }
231
+
232
+ case "theme.unregistered": {
233
+ // A theme registered again under the same id replaced this one; leave it.
234
+ if (event.theme.id === DEFAULT_THEME_ID || state.themes[event.theme.id] !== event.theme)
235
+ return { state, effects: NO_EFFECTS }
236
+
237
+ return reconcile({ ...state, themes: omit(state.themes, [event.theme.id]) }, bundled, NO_EFFECTS)
238
+ }
239
+
240
+ case "settled": {
241
+ if (state.registration === "settled") return { state, effects: NO_EFFECTS }
242
+
243
+ return reconcile({ ...state, registration: "settled" }, bundled, NO_EFFECTS)
244
+ }
245
+
246
+ case "themes.preload": {
247
+ const queued = [...bundled].filter(
248
+ (id) => id !== DEFAULT_THEME_ID && !state.themes[id] && (state.loads[id] === undefined || state.failed[id]),
249
+ )
250
+
251
+ if (queued.length === 0) return { state, effects: NO_EFFECTS }
252
+
253
+ const requested = request({ ...state, ...retry(state, queued) }, queued, bundled)
254
+
255
+ return reconcile(requested.state, bundled, requested.effects)
256
+ }
257
+
258
+ case "theme.loaded": {
259
+ if (state.loads[event.id] !== event.attempt) return { state, effects: NO_EFFECTS }
260
+
261
+ return reconcile(
262
+ {
263
+ ...state,
264
+ loads: omit(state.loads, [event.id]),
265
+ failed: omit(state.failed, [event.id]),
266
+ themes: { ...state.themes, [event.id]: event.theme },
267
+ },
268
+ bundled,
269
+ [{ type: "cancel-load-timer", attempt: event.attempt }],
270
+ )
271
+ }
272
+
273
+ case "theme.load.failed": {
274
+ if (state.loads[event.id] !== event.attempt) return { state, effects: NO_EFFECTS }
275
+
276
+ return reconcile(
277
+ { ...state, loads: omit(state.loads, [event.id]), failed: { ...state.failed, [event.id]: true } },
278
+ bundled,
279
+ [{ type: "cancel-load-timer", attempt: event.attempt }],
280
+ )
281
+ }
282
+
283
+ // The window shows the default theme meanwhile, but the attempt stays current: a late import still applies.
284
+ case "theme.load.timed-out": {
285
+ if (state.loads[event.id] !== event.attempt || state.failed[event.id]) return { state, effects: NO_EFFECTS }
286
+
287
+ return reconcile({ ...state, failed: { ...state.failed, [event.id]: true } }, bundled, NO_EFFECTS)
288
+ }
289
+
290
+ case "system.mode.changed": {
291
+ if (state.systemMode === event.mode) return { state, effects: NO_EFFECTS }
292
+
293
+ return reconcile({ ...state, systemMode: event.mode }, bundled, NO_EFFECTS)
294
+ }
295
+
296
+ case "storage.theme.changed": {
297
+ if (!event.id || (state.committed.themeId === event.id && !state.failed[event.id]))
298
+ return { state, effects: NO_EFFECTS }
299
+
300
+ return reconcile(
301
+ {
302
+ ...state,
303
+ ...retry(state, [event.id]),
304
+ committed: { ...state.committed, themeId: event.id },
305
+ },
306
+ bundled,
307
+ NO_EFFECTS,
308
+ )
309
+ }
310
+
311
+ case "storage.scheme.changed": {
312
+ if (state.committed.scheme === event.scheme) return { state, effects: NO_EFFECTS }
313
+
314
+ return reconcile({ ...state, committed: { ...state.committed, scheme: event.scheme } }, bundled, NO_EFFECTS)
315
+ }
316
+
317
+ case "disposed": {
318
+ const { preview: _, ...data } = { preview: undefined, ...state }
319
+
320
+ return {
321
+ state: { ...data, status: "disposed", loads: {} },
322
+ effects: Object.keys(state.loads).length > 0 ? [{ type: "cancel-all-loads" }] : NO_EFFECTS,
323
+ }
324
+ }
325
+ }
326
+ }
327
+
328
+ /** The theme the window should show when nothing is previewed. */
329
+ export function selectCommittedThemeId(state: ThemeMachineState, bundled: ReadonlySet<string>) {
330
+ const id = state.committed.themeId
331
+
332
+ if (id === DEFAULT_THEME_ID || state.themes[id]) return id
333
+
334
+ if (bundled.has(id)) return state.failed[id] ? DEFAULT_THEME_ID : id
335
+
336
+ return state.registration === "pending" ? id : DEFAULT_THEME_ID
337
+ }
338
+
339
+ export function selectThemeId(state: ThemeMachineState, bundled: ReadonlySet<string>) {
340
+ return (state.status === "previewing" ? state.preview.themeId : undefined) ?? selectCommittedThemeId(state, bundled)
341
+ }
342
+
343
+ /** The scheme the user chose; a preview does not change it. */
344
+ export function selectColorScheme(state: ThemeMachineState) {
345
+ return state.committed.scheme
346
+ }
347
+
348
+ /** The light or dark mode the window shows, preview included. */
349
+ export function selectMode(state: ThemeMachineState): ResolvedMode {
350
+ const scheme = previewedScheme(state)
351
+
352
+ return scheme === "system" ? state.systemMode : scheme
353
+ }
354
+
355
+ /** An import is still within its timeout. */
356
+ export function selectThemesLoading(state: ThemeMachineState) {
357
+ return Object.keys(state.loads).some((id) => !state.failed[id])
358
+ }
359
+
360
+ function reconcile(draft: ActiveState, bundled: ReadonlySet<string>, prefix: readonly ThemeEffect[]): ThemeTransition {
361
+ const valid = validatePreview(draft, bundled)
362
+
363
+ const requested = request(
364
+ valid,
365
+ [...new Set([valid.committed.themeId, preview(valid).themeId].filter((id) => id !== undefined))],
366
+ bundled,
367
+ )
368
+
369
+ const cached = reconcileCache(requested.state, bundled)
370
+ const applied = reconcileApplied(cached.state, bundled)
371
+
372
+ return { state: applied.state, effects: [...prefix, ...requested.effects, ...cached.effects, ...applied.effects] }
373
+ }
374
+
375
+ // A previewed theme that was withdrawn or failed to load leaves the preview; its scheme, if any, stays previewed.
376
+ function validatePreview(state: ActiveState, bundled: ReadonlySet<string>): ActiveState {
377
+ const id = preview(state).themeId
378
+
379
+ if (id === undefined || id === DEFAULT_THEME_ID || state.themes[id] || (bundled.has(id) && !state.failed[id]))
380
+ return state
381
+
382
+ return withPreview(state, { ...preview(state), themeId: undefined })
383
+ }
384
+
385
+ function request(state: ActiveState, ids: readonly string[], bundled: ReadonlySet<string>) {
386
+ return ids.reduce<Step>(
387
+ (result, id) => {
388
+ if (
389
+ id === DEFAULT_THEME_ID ||
390
+ !bundled.has(id) ||
391
+ result.state.themes[id] ||
392
+ result.state.failed[id] ||
393
+ result.state.loads[id] !== undefined
394
+ )
395
+ return result
396
+
397
+ const attempt = result.state.issued + 1
398
+
399
+ return {
400
+ state: { ...result.state, issued: attempt, loads: { ...result.state.loads, [id]: attempt } },
401
+ effects: [...result.effects, { type: "load-theme", id, attempt, timeoutMs: THEME_LOAD_TIMEOUT_MS }],
402
+ }
403
+ },
404
+ { state, effects: NO_EFFECTS },
405
+ )
406
+ }
407
+
408
+ // The cache follows the committed theme only: a preview never reaches the next start.
409
+ function reconcileCache(state: ActiveState, bundled: ReadonlySet<string>): Step {
410
+ const id = state.committed.themeId
411
+ const theme = state.themes[id]
412
+
413
+ if (theme && id !== DEFAULT_THEME_ID) {
414
+ if (state.cached.status === "cached" && state.cached.id === id && state.cached.theme === theme)
415
+ return { state, effects: NO_EFFECTS }
416
+
417
+ return {
418
+ state: { ...state, cached: { status: "cached", id, theme } },
419
+ effects: [{ type: "cache-theme", id, theme }],
420
+ }
421
+ }
422
+
423
+ // A bundled theme that has not loaded (loading, slow or failed) or a registered theme that may still arrive: the last
424
+ // run's colors for it stay, unless they are another theme's.
425
+ const waiting =
426
+ id !== DEFAULT_THEME_ID &&
427
+ (bundled.has(id) || state.registration === "pending") &&
428
+ !(state.cached.status === "cached" && state.cached.id !== id)
429
+
430
+ if (waiting || state.cached.status === "cleared") return { state, effects: NO_EFFECTS }
431
+
432
+ return {
433
+ state: { ...state, cached: { status: "cleared" } },
434
+ effects: [{ type: "clear-cache" }],
435
+ }
436
+ }
437
+
438
+ function reconcileApplied(state: ActiveState, bundled: ReadonlySet<string>): Step {
439
+ const id = selectThemeId(state, bundled)
440
+ const scheme = previewedScheme(state)
441
+ const mode = selectMode(state)
442
+ const theme = state.themes[id]
443
+ const applied = state.applied
444
+
445
+ if (theme) {
446
+ if (
447
+ applied?.kind === "theme" &&
448
+ applied.id === id &&
449
+ applied.theme === theme &&
450
+ applied.mode === mode &&
451
+ applied.scheme === scheme
452
+ )
453
+ return { state, effects: NO_EFFECTS }
454
+
455
+ return {
456
+ state: { ...state, applied: { kind: "theme", id, theme, mode, scheme } },
457
+ effects: [{ type: "apply-theme", id, theme, mode, scheme }],
458
+ }
459
+ }
460
+
461
+ // A bundled theme is loading: keep what is on screen until it arrives.
462
+ if (bundled.has(id)) return { state, effects: NO_EFFECTS }
463
+
464
+ if (applied?.kind === "preload" && applied.id === id && applied.mode === mode && applied.scheme === scheme)
465
+ return { state, effects: NO_EFFECTS }
466
+
467
+ return {
468
+ state: { ...state, applied: { kind: "preload", id, mode, scheme } },
469
+ effects: [{ type: "notify-applied", mode, scheme }],
470
+ }
471
+ }
472
+
473
+ function known(state: ThemeMachineState, id: string, bundled: ReadonlySet<string>) {
474
+ return id === DEFAULT_THEME_ID || bundled.has(id) || !!state.themes[id]
475
+ }
476
+
477
+ function preview(state: ThemeMachineState): ThemePreview {
478
+ return state.status === "previewing" ? state.preview : {}
479
+ }
480
+
481
+ function previewedScheme(state: ThemeMachineState) {
482
+ return preview(state).scheme ?? state.committed.scheme
483
+ }
484
+
485
+ function withPreview(state: ActiveState, next: ThemePreview): ActiveState {
486
+ const { preview: _, ...data } = { preview: undefined, ...state }
487
+
488
+ if (next.themeId === undefined && next.scheme === undefined) return { ...data, status: "committed" }
489
+
490
+ return { ...data, status: "previewing", preview: next }
491
+ }
492
+
493
+ // Clears a failure and drops an import that timed out, so the next request starts a fresh attempt with its own timeout.
494
+ function retry(state: ActiveState, ids: readonly string[]) {
495
+ return {
496
+ failed: omit(state.failed, ids),
497
+ loads: omit(
498
+ state.loads,
499
+ ids.filter((id) => state.failed[id]),
500
+ ),
501
+ }
502
+ }
503
+
504
+ function omit<T>(record: Readonly<Record<string, T>>, ids: readonly string[]) {
505
+ if (!ids.some((id) => id in record)) return record
506
+
507
+ return Object.fromEntries(Object.entries(record).filter(([id]) => !ids.includes(id)))
508
+ }
@@ -0,0 +1,90 @@
1
+ /** Matches the opacity crossfade in `text-shimmer.css`; the sweep keeps running until the shimmer layer has faded. */
2
+ export const TEXT_SHIMMER_SWAP_MS = 220
3
+
4
+ /**
5
+ * `active` shows the shimmer layer and runs its sweep. `fading` has already crossfaded back to the plain text and only
6
+ * keeps the sweep running until the fade ends, so the layer never freezes mid-fade. `inactive` runs nothing.
7
+ */
8
+ export type TextShimmerState =
9
+ | { readonly status: "inactive"; readonly attempts: number }
10
+ | { readonly status: "active"; readonly attempts: number }
11
+ | { readonly status: "fading"; readonly attempt: number }
12
+ | { readonly status: "disposed" }
13
+
14
+ export type TextShimmerEvent =
15
+ | { readonly type: "sync"; readonly active: boolean; readonly reducedMotion: boolean }
16
+ | { readonly type: "settled"; readonly attempt: number }
17
+ | { readonly type: "disposed" }
18
+
19
+ // Every timeline row mounts a shimmer, so mounting, syncing and disposing reuse these instead of allocating.
20
+ export const INITIAL_INACTIVE_TEXT_SHIMMER_STATE: TextShimmerState = Object.freeze({ status: "inactive", attempts: 0 })
21
+
22
+ export const INITIAL_ACTIVE_TEXT_SHIMMER_STATE: TextShimmerState = Object.freeze({ status: "active", attempts: 0 })
23
+
24
+ export const DISPOSED_TEXT_SHIMMER_STATE: TextShimmerState = Object.freeze({ status: "disposed" })
25
+
26
+ export const DISPOSED_TEXT_SHIMMER_EVENT: TextShimmerEvent = Object.freeze({ type: "disposed" })
27
+
28
+ const SYNC_ACTIVE: TextShimmerEvent = Object.freeze({ type: "sync", active: true, reducedMotion: false })
29
+
30
+ const SYNC_ACTIVE_REDUCED: TextShimmerEvent = Object.freeze({ type: "sync", active: true, reducedMotion: true })
31
+
32
+ const SYNC_INACTIVE: TextShimmerEvent = Object.freeze({ type: "sync", active: false, reducedMotion: false })
33
+
34
+ const SYNC_INACTIVE_REDUCED: TextShimmerEvent = Object.freeze({ type: "sync", active: false, reducedMotion: true })
35
+
36
+ export function syncTextShimmerEvent(active: boolean, reducedMotion: boolean): TextShimmerEvent {
37
+ if (active) return reducedMotion ? SYNC_ACTIVE_REDUCED : SYNC_ACTIVE
38
+
39
+ return reducedMotion ? SYNC_INACTIVE_REDUCED : SYNC_INACTIVE
40
+ }
41
+
42
+ export function createTextShimmerState(active: boolean): TextShimmerState {
43
+ return active ? INITIAL_ACTIVE_TEXT_SHIMMER_STATE : INITIAL_INACTIVE_TEXT_SHIMMER_STATE
44
+ }
45
+
46
+ export function transitionTextShimmer(state: TextShimmerState, event: TextShimmerEvent): TextShimmerState {
47
+ if (state.status === "disposed") return state
48
+
49
+ if (event.type === "disposed") return DISPOSED_TEXT_SHIMMER_STATE
50
+
51
+ if (event.type === "settled") {
52
+ if (state.status !== "fading" || event.attempt !== state.attempt) return state
53
+
54
+ return { status: "inactive", attempts: state.attempt }
55
+ }
56
+
57
+ if (state.status === "inactive") {
58
+ if (!event.active) return state
59
+
60
+ return state.attempts === 0 ? INITIAL_ACTIVE_TEXT_SHIMMER_STATE : { status: "active", attempts: state.attempts }
61
+ }
62
+
63
+ if (state.status === "active") {
64
+ if (event.active) return state
65
+
66
+ // Reduced motion turns the crossfade off, so there is nothing to wait for.
67
+ if (event.reducedMotion)
68
+ return state.attempts === 0
69
+ ? INITIAL_INACTIVE_TEXT_SHIMMER_STATE
70
+ : { status: "inactive", attempts: state.attempts }
71
+
72
+ return { status: "fading", attempt: state.attempts + 1 }
73
+ }
74
+
75
+ if (event.active) return { status: "active", attempts: state.attempt }
76
+
77
+ if (event.reducedMotion) return { status: "inactive", attempts: state.attempt }
78
+
79
+ return state
80
+ }
81
+
82
+ /** Whether the shimmer layer is shown; drives the opacity crossfade. */
83
+ export function selectTextShimmerActive(state: TextShimmerState) {
84
+ return state.status === "active"
85
+ }
86
+
87
+ /** Whether the sweep animation runs; it outlives `active` by the crossfade. */
88
+ export function selectTextShimmerRunning(state: TextShimmerState) {
89
+ return state.status === "active" || state.status === "fading"
90
+ }