basalt-ui 1.1.1 → 1.3.0

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 (191) hide show
  1. package/AGENTS.md +2 -2
  2. package/CHANGELOG.md +46 -0
  3. package/README.md +64 -8
  4. package/agent/rules/basalt-app.md +126 -0
  5. package/agent/rules/basalt-content.md +3 -1
  6. package/agent/rules/basalt-tokens.md +14 -5
  7. package/agent/skills/basalt-app/SKILL.md +28 -7
  8. package/dist/agent/part-list.d.ts.map +1 -1
  9. package/dist/agent/part-list.js +17 -7
  10. package/dist/agent/stick-to-bottom.js +2 -2
  11. package/dist/agent-chat/thread-detail-panel.d.ts.map +1 -1
  12. package/dist/agent-chat/thread-detail-panel.js +37 -33
  13. package/dist/agent-chat/thread-message.d.ts.map +1 -1
  14. package/dist/agent-chat/thread-message.js +8 -5
  15. package/dist/agent-chat/thread-outcome-card.js +3 -3
  16. package/dist/agent-chat/thread-workspace.js +1 -1
  17. package/dist/charts/primitives/ChartLegend.js +2 -0
  18. package/dist/charts/primitives/ChartTooltip.js +2 -1
  19. package/dist/cli/index.d.ts +29 -0
  20. package/dist/cli/index.d.ts.map +1 -1
  21. package/dist/cli/index.js +80 -9
  22. package/dist/connectivity/connectivity-indicator.d.ts.map +1 -1
  23. package/dist/connectivity/connectivity-indicator.js +3 -3
  24. package/dist/content/article-card.module.css +7 -5
  25. package/dist/content/article-layout.module.css +16 -12
  26. package/dist/content/callout.module.css +4 -4
  27. package/dist/content/code-block.module.css +8 -7
  28. package/dist/content/guide.module.css +3 -3
  29. package/dist/content/mermaid.module.css +2 -2
  30. package/dist/content/prose.module.css +59 -29
  31. package/dist/content/reading-progress.module.css +2 -0
  32. package/dist/content/toc.module.css +6 -3
  33. package/dist/dashboard/delta-badge.d.ts.map +1 -1
  34. package/dist/dashboard/delta-badge.js +10 -2
  35. package/dist/dashboard/empty-state.d.ts.map +1 -1
  36. package/dist/dashboard/empty-state.js +3 -16
  37. package/dist/dashboard/settings-section.js +2 -2
  38. package/dist/dashboard/settings-section.module.css +2 -2
  39. package/dist/dashboard/stat-card.js +2 -1
  40. package/dist/data/virtual-list.d.ts.map +1 -1
  41. package/dist/data/virtual-list.js +12 -2
  42. package/dist/forms/FormErrorSummary.js +2 -2
  43. package/dist/guard/index.d.ts +20 -4
  44. package/dist/guard/index.d.ts.map +1 -1
  45. package/dist/guard/index.js +266 -38
  46. package/dist/guard/types.d.ts +24 -3
  47. package/dist/guard/types.d.ts.map +1 -1
  48. package/dist/index.d.ts +1 -0
  49. package/dist/index.d.ts.map +1 -1
  50. package/dist/provider/index.d.ts +30 -1
  51. package/dist/provider/index.d.ts.map +1 -1
  52. package/dist/provider/index.js +32 -7
  53. package/dist/provider/lab-theme.d.ts +39 -0
  54. package/dist/provider/lab-theme.d.ts.map +1 -0
  55. package/dist/provider/lab-theme.js +52 -0
  56. package/dist/shell/app-header.module.css +2 -2
  57. package/dist/shell/app-mobile-nav.module.css +1 -1
  58. package/dist/shell/app-sidebar-account.d.ts.map +1 -1
  59. package/dist/shell/app-sidebar-account.js +4 -2
  60. package/dist/shell/app-sidebar.d.ts.map +1 -1
  61. package/dist/shell/app-sidebar.js +4 -2
  62. package/dist/shell/app-sidebar.module.css +36 -26
  63. package/dist/shell/index.d.ts.map +1 -1
  64. package/dist/shell/index.js +11 -4
  65. package/dist/shell/sidebar-search.module.css +16 -4
  66. package/dist/styles.css +22 -11
  67. package/dist/surfaces.d.ts +21 -11
  68. package/dist/surfaces.d.ts.map +1 -1
  69. package/dist/surfaces.js +10 -3
  70. package/dist/theme/controls.module.css +1 -1
  71. package/dist/theme/floating.module.css +1 -1
  72. package/dist/theme/index.d.ts +116 -11
  73. package/dist/theme/index.d.ts.map +1 -1
  74. package/dist/theme/index.js +703 -566
  75. package/dist/theme/nav-link.module.css +1 -1
  76. package/dist/theme/use-basalt-spacing.d.ts +3 -0
  77. package/dist/theme/use-basalt-spacing.d.ts.map +1 -0
  78. package/dist/theme/use-basalt-spacing.js +8 -0
  79. package/dist/theme-lab/derive-controls.d.ts +13 -0
  80. package/dist/theme-lab/derive-controls.d.ts.map +1 -0
  81. package/dist/theme-lab/derive-controls.js +137 -0
  82. package/dist/theme-lab/derive-state.d.ts +55 -0
  83. package/dist/theme-lab/derive-state.d.ts.map +1 -0
  84. package/dist/theme-lab/derive-state.js +81 -0
  85. package/dist/theme-lab/index.d.ts +12 -2
  86. package/dist/theme-lab/index.d.ts.map +1 -1
  87. package/dist/theme-lab/index.js +13 -62
  88. package/dist/theme-toggle/index.d.ts.map +1 -1
  89. package/dist/theme-toggle/index.js +3 -2
  90. package/dist/tokens/derive.d.ts +89 -0
  91. package/dist/tokens/derive.d.ts.map +1 -0
  92. package/dist/tokens/derive.js +250 -0
  93. package/dist/tokens/hct.d.ts +34 -0
  94. package/dist/tokens/hct.d.ts.map +1 -0
  95. package/dist/tokens/hct.js +159 -0
  96. package/dist/tokens/index.d.ts +143 -7
  97. package/dist/tokens/index.d.ts.map +1 -1
  98. package/dist/tokens/index.js +322 -48
  99. package/dist/tokens/palette.d.ts +984 -167
  100. package/dist/tokens/palette.d.ts.map +1 -1
  101. package/dist/tokens/palette.js +617 -119
  102. package/dist/tokens.css +248 -0
  103. package/dist/vite-color-mix.d.ts +30 -0
  104. package/dist/vite-color-mix.d.ts.map +1 -0
  105. package/dist/vite-color-mix.js +80 -0
  106. package/dist/vite.d.ts +82 -1
  107. package/dist/vite.d.ts.map +1 -1
  108. package/dist/vite.js +312 -0
  109. package/llms.txt +14 -4
  110. package/package.json +41 -10
  111. package/src/agent/part-list.tsx +13 -7
  112. package/src/agent/stick-to-bottom.tsx +2 -2
  113. package/src/agent-chat/thread-detail-panel.tsx +13 -10
  114. package/src/agent-chat/thread-message.tsx +8 -5
  115. package/src/agent-chat/thread-outcome-card.tsx +2 -2
  116. package/src/agent-chat/thread-workspace.tsx +2 -2
  117. package/src/charts/primitives/ChartLegend.tsx +2 -2
  118. package/src/charts/primitives/ChartTooltip.tsx +2 -2
  119. package/src/cli/check-theme.test.ts +18 -0
  120. package/src/cli/index.ts +129 -6
  121. package/src/cli/tokens-css.test.ts +90 -0
  122. package/src/connectivity/connectivity-indicator.tsx +8 -2
  123. package/src/content/article-card.module.css +7 -5
  124. package/src/content/article-layout.module.css +16 -12
  125. package/src/content/callout.module.css +4 -4
  126. package/src/content/code-block.module.css +8 -7
  127. package/src/content/guide.module.css +3 -3
  128. package/src/content/mermaid.module.css +2 -2
  129. package/src/content/prose.module.css +59 -29
  130. package/src/content/prose.module.css.test.ts +55 -0
  131. package/src/content/reading-progress.module.css +2 -0
  132. package/src/content/toc.module.css +6 -3
  133. package/src/dashboard/delta-badge.tsx +10 -4
  134. package/src/dashboard/empty-state.tsx +3 -16
  135. package/src/dashboard/settings-section.module.css +2 -2
  136. package/src/dashboard/settings-section.tsx +2 -2
  137. package/src/dashboard/stat-card.tsx +2 -2
  138. package/src/data/virtual-list.tsx +12 -2
  139. package/src/forms/FormErrorSummary.tsx +1 -1
  140. package/src/guard/check-source.test.ts +483 -15
  141. package/src/guard/index.ts +426 -47
  142. package/src/guard/types.ts +24 -2
  143. package/src/index.ts +1 -0
  144. package/src/provider/index.test.tsx +100 -0
  145. package/src/provider/index.tsx +79 -7
  146. package/src/provider/lab-theme.test.ts +147 -0
  147. package/src/provider/lab-theme.ts +166 -0
  148. package/src/shell/app-header.module.css +2 -2
  149. package/src/shell/app-mobile-nav.module.css +1 -1
  150. package/src/shell/app-sidebar-account.tsx +9 -2
  151. package/src/shell/app-sidebar.module.css +36 -26
  152. package/src/shell/app-sidebar.tsx +12 -2
  153. package/src/shell/index.tsx +14 -3
  154. package/src/shell/sidebar-search.module.css +16 -4
  155. package/src/styles.css +22 -11
  156. package/src/surfaces.ts +24 -10
  157. package/src/theme/contrast.test.ts +19 -3
  158. package/src/theme/controls.module.css +1 -1
  159. package/src/theme/density-option.test.ts +96 -0
  160. package/src/theme/density-relations.test.ts +428 -0
  161. package/src/theme/derive-theme.test.ts +101 -0
  162. package/src/theme/floating.module.css +1 -1
  163. package/src/theme/fonts-option.test.ts +69 -0
  164. package/src/theme/index.ts +930 -563
  165. package/src/theme/nav-link.module.css +1 -1
  166. package/src/theme/radius-option.test.ts +86 -0
  167. package/src/theme/radius.test.ts +132 -0
  168. package/src/theme/spacing.test.ts +432 -0
  169. package/src/theme/use-basalt-spacing.test.tsx +51 -0
  170. package/src/theme/use-basalt-spacing.ts +38 -0
  171. package/src/theme-lab/derive-controls.test.ts +94 -0
  172. package/src/theme-lab/derive-controls.tsx +227 -0
  173. package/src/theme-lab/derive-state.test.ts +71 -0
  174. package/src/theme-lab/derive-state.ts +163 -0
  175. package/src/theme-lab/index.tsx +29 -68
  176. package/src/theme-lab/theme-lab.test.ts +4 -18
  177. package/src/theme-toggle/index.tsx +9 -4
  178. package/src/tokens/build-fonts-css.test.ts +51 -0
  179. package/src/tokens/build-radius-css.test.ts +25 -0
  180. package/src/tokens/density.test.ts +274 -0
  181. package/src/tokens/derive-radius.test.ts +62 -0
  182. package/src/tokens/derive.test.ts +30 -0
  183. package/src/tokens/derive.ts +481 -0
  184. package/src/tokens/hct.ts +231 -0
  185. package/src/tokens/index.ts +534 -48
  186. package/src/tokens/palette.test.ts +114 -0
  187. package/src/tokens/palette.ts +1118 -180
  188. package/src/vite-color-mix.test.ts +46 -0
  189. package/src/vite-color-mix.ts +137 -0
  190. package/src/vite.test.ts +192 -0
  191. package/src/vite.ts +502 -1
package/src/vite.ts CHANGED
@@ -16,7 +16,9 @@
16
16
  */
17
17
  import { resolve } from 'node:path'
18
18
  import { searchForWorkspaceRoot } from 'vite'
19
- import type { UserConfig } from 'vite'
19
+ import type { HtmlTagDescriptor, Plugin, PluginOption, UserConfig } from 'vite'
20
+ import { SURFACE } from './tokens/palette'
21
+ import { resolveColorMix } from './vite-color-mix'
20
22
 
21
23
  export type BasaltViteOptions = {
22
24
  /** Dev server port. Passed with `strictPort: true` so the app fails fast on a busy port. */
@@ -120,3 +122,502 @@ export function basaltViteConfig(opts: BasaltViteOptions): UserConfig {
120
122
 
121
123
  return config
122
124
  }
125
+
126
+ // ──────────────────────────────────────────────────────────────────────────────
127
+ // basaltAppPlugin — PWA / favicon / head-metadata plugin, one config object
128
+ // ──────────────────────────────────────────────────────────────────────────────
129
+
130
+ /** The realfavicongenerator filenames argo already ships under its `public/` root. */
131
+ const ICON_FILES = {
132
+ favicon: 'favicon.ico',
133
+ svg: 'favicon.svg',
134
+ png96: 'favicon-96x96.png',
135
+ appleTouch: 'apple-touch-icon.png',
136
+ manifest192: 'web-app-manifest-192x192.png',
137
+ manifest512: 'web-app-manifest-512x512.png',
138
+ } as const
139
+
140
+ export type BasaltAppOptions = {
141
+ /** App display name — used verbatim as the manifest `name` and (unless overridden) OG title. */
142
+ name: string
143
+ /** Short name for home-screen labels. Default: `name`. */
144
+ shortName?: string
145
+ /** Site-wide description — feeds the `<meta name="description">` and OG/Twitter description. */
146
+ description?: string
147
+ /**
148
+ * `'auto'` (default) resolves the flat theme-color pair from `SURFACE.bg` (basalt's own page
149
+ * background token, a `color-mix()` expression) via the color-mix resolver — the reason this
150
+ * plugin exists. Pass an explicit `{ light, dark }` pair of already-flat CSS colors to override.
151
+ */
152
+ themeColor?: 'auto' | { light: string; dark: string }
153
+ /** Manifest `background_color` + anti-FOUC paint color. Default: the resolved dark theme color. */
154
+ backgroundColor?: string
155
+ /** Manifest `display` mode. Default: `'standalone'`. */
156
+ display?: 'standalone' | 'minimal-ui' | 'fullscreen' | 'browser'
157
+ /**
158
+ * Manifest `start_url`. Defaults to Vite's resolved `base` (so a non-root deploy — e.g. GitHub
159
+ * Pages' `/repo/` — gets a correct value with zero config). An explicit value is used verbatim:
160
+ * it is NOT re-prefixed with `base`, so pass the fully base-aware path yourself if you set it.
161
+ */
162
+ startUrl?: string
163
+ /**
164
+ * Manifest `scope`. Defaults to Vite's resolved `base`. An explicit value is used verbatim (not
165
+ * re-prefixed with `base`) — same rule as `startUrl`.
166
+ */
167
+ scope?: string
168
+ /** Manifest `id`. Default: the resolved `startUrl` (default- or explicitly-derived, per above). */
169
+ id?: string
170
+ /** Icon links + manifest icon paths. `false` skips the head `<link>` icons entirely. */
171
+ icons?: false | { dir?: string }
172
+ /** Emits `<meta name="darkreader-lock">` when `'lock'` (default). Pass `false` to omit it. */
173
+ darkreader?: 'lock' | false
174
+ /** Site-wide SEO metadata — only the keys provided are emitted, no per-page SEO. */
175
+ seo?: { url?: string; image?: string; twitterCard?: 'summary' | 'summary_large_image' }
176
+ /** `false` skips manifest generation/serving and the `<link rel="manifest">` tag entirely. */
177
+ manifest?: false
178
+ /**
179
+ * Opt-in service worker via the optional peer `vite-plugin-pwa`. `true` uses argo-tuned workbox
180
+ * defaults; an object is deep-merged over those defaults. Default `false` (no service worker).
181
+ * Degrades to a one-line console warning (no service worker, no throw) when the peer — plus its
182
+ * non-optional peers `workbox-build` / `workbox-window` — is not installed.
183
+ */
184
+ serviceWorker?: boolean | Record<string, unknown>
185
+ }
186
+
187
+ /** Hand-written peer type for `vite-plugin-pwa` (optional peer — never statically imported). */
188
+ type VitePwaModule = {
189
+ VitePWA: (options: Record<string, unknown>) => Plugin[]
190
+ }
191
+
192
+ /** Argo-derived workbox defaults, deep-merged under any user-supplied `serviceWorker` object. */
193
+ const DEFAULT_SERVICE_WORKER_OPTIONS: Record<string, unknown> = {
194
+ registerType: 'autoUpdate',
195
+ injectRegister: 'auto',
196
+ // We emit our own manifest (theme-color-resolved, site.webmanifest) — never let VitePWA emit one.
197
+ manifest: false,
198
+ workbox: {
199
+ globPatterns: ['**/*.{js,css,html,svg,png,ico,woff2}'],
200
+ navigateFallback: '/index.html',
201
+ navigateFallbackDenylist: [/^\/api/],
202
+ cleanupOutdatedCaches: true,
203
+ clientsClaim: true,
204
+ maximumFileSizeToCacheInBytes: 3 * 1024 * 1024,
205
+ },
206
+ devOptions: { enabled: false },
207
+ }
208
+
209
+ function isPlainObject(value: unknown): value is Record<string, unknown> {
210
+ return typeof value === 'object' && value !== null && !Array.isArray(value)
211
+ }
212
+
213
+ function deepMerge(
214
+ base: Record<string, unknown>,
215
+ overrides: Record<string, unknown>,
216
+ ): Record<string, unknown> {
217
+ const result: Record<string, unknown> = { ...base }
218
+ for (const [key, value] of Object.entries(overrides)) {
219
+ const baseValue = result[key]
220
+ result[key] =
221
+ isPlainObject(baseValue) && isPlainObject(value) ? deepMerge(baseValue, value) : value
222
+ }
223
+ return result
224
+ }
225
+
226
+ function resolveThemeColors(themeColor: BasaltAppOptions['themeColor']): {
227
+ light: string
228
+ dark: string
229
+ } {
230
+ if (themeColor === undefined || themeColor === 'auto') {
231
+ return { light: resolveColorMix(SURFACE.bg.light), dark: resolveColorMix(SURFACE.bg.dark) }
232
+ }
233
+ return themeColor
234
+ }
235
+
236
+ function withIconPath(dir: string | undefined, file: string): string {
237
+ if (!dir) return `/${file}`
238
+ const trimmed = dir.replace(/^\/+|\/+$/g, '')
239
+ return trimmed ? `/${trimmed}/${file}` : `/${file}`
240
+ }
241
+
242
+ /**
243
+ * Joins Vite's resolved `base` (always leading+trailing slash — `'/'` or e.g. `'/myapp/'`) with a
244
+ * root-relative path (leading slash, e.g. `/favicon.ico`) without doubling or dropping the slash
245
+ * between them. `base === '/'` is a no-op (`'/' + path.slice(1)` reconstructs `path` verbatim),
246
+ * which is what keeps default-base output byte-identical to the pre-base-aware behavior.
247
+ */
248
+ function withBase(base: string, path: string): string {
249
+ return base + path.slice(1)
250
+ }
251
+
252
+ function hasViewportMeta(html: string): boolean {
253
+ return /<meta\s+[^>]*name=["']viewport["']/i.test(html)
254
+ }
255
+
256
+ const VIEWPORT_TAG: HtmlTagDescriptor = {
257
+ tag: 'meta',
258
+ attrs: { name: 'viewport', content: 'width=device-width, initial-scale=1.0, viewport-fit=cover' },
259
+ }
260
+
261
+ function buildSeoTags(input: {
262
+ name: string
263
+ description: string | undefined
264
+ seo: BasaltAppOptions['seo']
265
+ }): HtmlTagDescriptor[] {
266
+ const { name, description, seo } = input
267
+ const tags: HtmlTagDescriptor[] = []
268
+
269
+ if (description) {
270
+ tags.push({ tag: 'meta', attrs: { name: 'description', content: description } })
271
+ }
272
+
273
+ if (description || seo?.url || seo?.image) {
274
+ if (seo?.url) {
275
+ tags.push(
276
+ { tag: 'link', attrs: { rel: 'canonical', href: seo.url } },
277
+ { tag: 'meta', attrs: { property: 'og:url', content: seo.url } },
278
+ )
279
+ }
280
+ tags.push(
281
+ { tag: 'meta', attrs: { property: 'og:type', content: 'website' } },
282
+ { tag: 'meta', attrs: { property: 'og:title', content: name } },
283
+ )
284
+ if (description) {
285
+ tags.push({ tag: 'meta', attrs: { property: 'og:description', content: description } })
286
+ }
287
+ if (seo?.image) {
288
+ tags.push({ tag: 'meta', attrs: { property: 'og:image', content: seo.image } })
289
+ }
290
+ }
291
+
292
+ if (seo?.twitterCard) {
293
+ tags.push(
294
+ { tag: 'meta', attrs: { name: 'twitter:card', content: seo.twitterCard } },
295
+ { tag: 'meta', attrs: { name: 'twitter:title', content: name } },
296
+ )
297
+ if (description) {
298
+ tags.push({ tag: 'meta', attrs: { name: 'twitter:description', content: description } })
299
+ }
300
+ if (seo?.image) {
301
+ tags.push({ tag: 'meta', attrs: { name: 'twitter:image', content: seo.image } })
302
+ }
303
+ }
304
+
305
+ return tags
306
+ }
307
+
308
+ /**
309
+ * Builds the plugin's static head tags (everything except the viewport meta, which is
310
+ * conditional on the consumer's own `index.html` — see `hasViewportMeta`).
311
+ */
312
+ function buildStaticTags(input: {
313
+ options: BasaltAppOptions
314
+ shortName: string
315
+ themeColor: { light: string; dark: string }
316
+ backgroundColor: string
317
+ iconsEnabled: boolean
318
+ iconsDir: string | undefined
319
+ darkreader: 'lock' | false
320
+ manifestEnabled: boolean
321
+ base: string
322
+ manifestHref: string
323
+ }): HtmlTagDescriptor[] {
324
+ const {
325
+ options,
326
+ shortName,
327
+ themeColor,
328
+ backgroundColor,
329
+ iconsEnabled,
330
+ iconsDir,
331
+ darkreader,
332
+ manifestEnabled,
333
+ base,
334
+ manifestHref,
335
+ } = input
336
+ const tags: HtmlTagDescriptor[] = []
337
+
338
+ // Dual theme-color — flat resolved hex, never the raw color-mix() expression.
339
+ tags.push(
340
+ {
341
+ tag: 'meta',
342
+ attrs: {
343
+ name: 'theme-color',
344
+ media: '(prefers-color-scheme: light)',
345
+ content: themeColor.light,
346
+ },
347
+ },
348
+ {
349
+ tag: 'meta',
350
+ attrs: {
351
+ name: 'theme-color',
352
+ media: '(prefers-color-scheme: dark)',
353
+ content: themeColor.dark,
354
+ },
355
+ },
356
+ )
357
+
358
+ // Anti-FOUC — BasaltProvider defaults to dark, so this paints the dark surface before any
359
+ // stylesheet loads. injectTo defaults to 'head-prepend', so this (and every tag below) lands
360
+ // immediately after <head>, ahead of the consumer's own <link rel="stylesheet"> tags.
361
+ tags.push({
362
+ tag: 'style',
363
+ children: `html{background-color:${backgroundColor};color-scheme:dark}`,
364
+ })
365
+
366
+ if (iconsEnabled) {
367
+ tags.push(
368
+ {
369
+ tag: 'link',
370
+ attrs: {
371
+ rel: 'shortcut icon',
372
+ href: withBase(base, withIconPath(iconsDir, ICON_FILES.favicon)),
373
+ },
374
+ },
375
+ {
376
+ tag: 'link',
377
+ attrs: {
378
+ rel: 'icon',
379
+ type: 'image/svg+xml',
380
+ href: withBase(base, withIconPath(iconsDir, ICON_FILES.svg)),
381
+ },
382
+ },
383
+ {
384
+ tag: 'link',
385
+ attrs: {
386
+ rel: 'icon',
387
+ sizes: '96x96',
388
+ href: withBase(base, withIconPath(iconsDir, ICON_FILES.png96)),
389
+ },
390
+ },
391
+ {
392
+ tag: 'link',
393
+ attrs: {
394
+ rel: 'apple-touch-icon',
395
+ sizes: '180x180',
396
+ href: withBase(base, withIconPath(iconsDir, ICON_FILES.appleTouch)),
397
+ },
398
+ },
399
+ )
400
+ }
401
+
402
+ tags.push(
403
+ { tag: 'meta', attrs: { name: 'apple-mobile-web-app-title', content: shortName } },
404
+ { tag: 'meta', attrs: { name: 'apple-mobile-web-app-capable', content: 'yes' } },
405
+ { tag: 'meta', attrs: { name: 'mobile-web-app-capable', content: 'yes' } },
406
+ { tag: 'meta', attrs: { name: 'apple-mobile-web-app-status-bar-style', content: 'default' } },
407
+ )
408
+
409
+ if (darkreader === 'lock') {
410
+ tags.push({ tag: 'meta', attrs: { name: 'darkreader-lock' } })
411
+ }
412
+
413
+ if (manifestEnabled) {
414
+ tags.push({ tag: 'link', attrs: { rel: 'manifest', href: manifestHref } })
415
+ }
416
+
417
+ tags.push(
418
+ ...buildSeoTags({ name: options.name, description: options.description, seo: options.seo }),
419
+ )
420
+
421
+ return tags
422
+ }
423
+
424
+ function buildManifestJson(input: {
425
+ options: BasaltAppOptions
426
+ shortName: string
427
+ startUrl: string
428
+ scope: string
429
+ id: string
430
+ display: NonNullable<BasaltAppOptions['display']>
431
+ themeColor: { light: string; dark: string }
432
+ backgroundColor: string
433
+ iconsDir: string | undefined
434
+ base: string
435
+ }): string {
436
+ const {
437
+ options,
438
+ shortName,
439
+ startUrl,
440
+ scope,
441
+ id,
442
+ display,
443
+ themeColor,
444
+ backgroundColor,
445
+ iconsDir,
446
+ base,
447
+ } = input
448
+ const manifest = {
449
+ id,
450
+ name: options.name,
451
+ short_name: shortName,
452
+ ...(options.description ? { description: options.description } : {}),
453
+ start_url: startUrl,
454
+ scope,
455
+ display,
456
+ theme_color: themeColor.dark,
457
+ background_color: backgroundColor,
458
+ icons: [
459
+ {
460
+ src: withBase(base, withIconPath(iconsDir, ICON_FILES.manifest192)),
461
+ sizes: '192x192',
462
+ type: 'image/png',
463
+ },
464
+ {
465
+ src: withBase(base, withIconPath(iconsDir, ICON_FILES.manifest512)),
466
+ sizes: '512x512',
467
+ type: 'image/png',
468
+ purpose: 'any maskable',
469
+ },
470
+ ],
471
+ }
472
+ return JSON.stringify(manifest, null, 2)
473
+ }
474
+
475
+ function createMainPlugin(options: BasaltAppOptions): Plugin {
476
+ const shortName = options.shortName ?? options.name
477
+ const display = options.display ?? 'standalone'
478
+ const darkreader = options.darkreader ?? 'lock'
479
+ const iconsEnabled = options.icons !== false
480
+ const iconsDir = options.icons === false ? undefined : options.icons?.dir
481
+ const manifestEnabled = options.manifest !== false
482
+
483
+ const themeColor = resolveThemeColors(options.themeColor)
484
+ const backgroundColor = options.backgroundColor ?? themeColor.dark
485
+
486
+ // Icon hrefs, the manifest link, and the manifest's start_url/scope/icon src all depend on
487
+ // Vite's resolved `base`, which is only known once `configResolved` fires — so manifestJson and
488
+ // staticTags are computed there instead of at plugin-construction time. The pre-configResolved
489
+ // values below are never actually served or rendered from.
490
+ let manifestJson = ''
491
+ let manifestHref = '/site.webmanifest'
492
+ let staticTags: HtmlTagDescriptor[] = []
493
+
494
+ return {
495
+ name: 'basalt:app',
496
+ configResolved(config) {
497
+ const base = config.base
498
+ const startUrl = options.startUrl ?? base
499
+ const scope = options.scope ?? base
500
+ const id = options.id ?? startUrl
501
+
502
+ manifestHref = withBase(base, '/site.webmanifest')
503
+
504
+ manifestJson = buildManifestJson({
505
+ options,
506
+ shortName,
507
+ startUrl,
508
+ scope,
509
+ id,
510
+ display,
511
+ themeColor,
512
+ backgroundColor,
513
+ iconsDir,
514
+ base,
515
+ })
516
+
517
+ staticTags = buildStaticTags({
518
+ options,
519
+ shortName,
520
+ themeColor,
521
+ backgroundColor,
522
+ iconsEnabled,
523
+ iconsDir,
524
+ darkreader,
525
+ manifestEnabled,
526
+ base,
527
+ manifestHref,
528
+ })
529
+ },
530
+ transformIndexHtml(html) {
531
+ const tags = hasViewportMeta(html) ? staticTags : [VIEWPORT_TAG, ...staticTags]
532
+ return { html, tags }
533
+ },
534
+ generateBundle() {
535
+ if (!manifestEnabled) return
536
+ this.emitFile({ type: 'asset', fileName: 'site.webmanifest', source: manifestJson })
537
+ },
538
+ configureServer(server) {
539
+ if (!manifestEnabled) return
540
+ server.middlewares.use((req, res, next) => {
541
+ // Vite adds its own base-stripping middleware (`viteBaseMiddleware`) AFTER plugin
542
+ // `configureServer` hooks push middleware directly onto the stack (confirmed against this
543
+ // repo's installed vite: configureServer hooks run first, then
544
+ // `if (config.base !== '/') middlewares.use(baseMiddleware(...))`), so `req.url` here still
545
+ // carries the raw, un-stripped base prefix rather than the stripped path. Match both the
546
+ // base-prefixed and bare path defensively so this keeps working regardless of that
547
+ // internal ordering.
548
+ if (req.url === manifestHref || req.url === '/site.webmanifest') {
549
+ res.setHeader('Content-Type', 'application/manifest+json')
550
+ res.end(manifestJson)
551
+ return
552
+ }
553
+ next()
554
+ })
555
+ },
556
+ }
557
+ }
558
+
559
+ /**
560
+ * Resolves the optional `vite-plugin-pwa` peer (plus its non-optional peers `workbox-build` /
561
+ * `workbox-window`) and returns its plugin array, or `false` when the peer is absent.
562
+ *
563
+ * A Vite plugin factory is synchronous, but `vite-plugin-pwa` can only be loaded via a dynamic
564
+ * `import()` (it must stay an OPTIONAL peer — `basaltAppPlugin` has to keep resolving cleanly when
565
+ * it isn't installed). Vite's own `PluginOption` type is a `Thenable` that recursively allows
566
+ * `Promise<Plugin | Plugin[] | false>`, so returning a promise for this half of the plugin array —
567
+ * rather than shoehorning the dynamic import into a synchronous hook — is the idiomatic way to
568
+ * resolve an optional async plugin dependency in Vite 7/8 (confirmed directly against this repo's
569
+ * installed `vite` package's `.d.ts`, not from memory).
570
+ */
571
+ function createServiceWorkerPlugin(
572
+ serviceWorker: boolean | Record<string, unknown>,
573
+ ): Promise<Plugin[] | false> {
574
+ return (async (): Promise<Plugin[] | false> => {
575
+ let mod: VitePwaModule
576
+ try {
577
+ mod = (await import('vite-plugin-pwa')) as unknown as VitePwaModule
578
+ } catch {
579
+ console.warn(
580
+ '[basalt-ui] serviceWorker was requested but the optional peer "vite-plugin-pwa" ' +
581
+ '(plus its own peers workbox-build, workbox-window) is not installed — skipping the ' +
582
+ 'service worker. Install with: bun add -D vite-plugin-pwa workbox-build workbox-window',
583
+ )
584
+ return false
585
+ }
586
+ const overrides = typeof serviceWorker === 'object' ? serviceWorker : {}
587
+ const pwaOptions = deepMerge(DEFAULT_SERVICE_WORKER_OPTIONS, overrides)
588
+ return mod.VitePWA(pwaOptions)
589
+ })()
590
+ }
591
+
592
+ /**
593
+ * Generates PWA / favicon / head metadata for a consumer app from one config object — a Vite
594
+ * plugin (well, plugin array — see the service-worker note below) placed in the consumer's own
595
+ * `plugins` array (never returned by `basaltViteConfig`, which stays plugin-free by contract).
596
+ *
597
+ * The core value over hand-authoring this yourself: `themeColor: 'auto'` (the default) derives
598
+ * the flat `<meta name="theme-color">` / manifest `theme_color` pair from basalt's own
599
+ * `SURFACE.bg` design token, so the value tracks the palette instead of being a copied hex that
600
+ * silently rots when the palette is retuned.
601
+ *
602
+ * `SURFACE.bg` is generated by `tokens/derive.ts` and currently resolves to plain hex, which the
603
+ * resolver passes through untouched. It is run through `resolveColorMix` anyway because the token
604
+ * layer has historically expressed surfaces as `color-mix()` and may again — a meta tag and a
605
+ * manifest `theme_color` both need a FLAT color, so the flattening step is the contract this
606
+ * plugin owes its output regardless of which shape the palette hands it.
607
+ *
608
+ * @example
609
+ * // vite.config.ts
610
+ * import { basaltAppPlugin, basaltViteConfig } from 'basalt-ui/vite'
611
+ *
612
+ * export default {
613
+ * ...basaltViteConfig({ port: 5173 }),
614
+ * plugins: [react(), ...basaltAppPlugin({ name: 'Argo' })],
615
+ * }
616
+ */
617
+ export function basaltAppPlugin(options: BasaltAppOptions): PluginOption[] {
618
+ const plugins: PluginOption[] = [createMainPlugin(options)]
619
+ if (options.serviceWorker) {
620
+ plugins.push(createServiceWorkerPlugin(options.serviceWorker))
621
+ }
622
+ return plugins
623
+ }