@adea-ai/ui 0.124.0 → 0.125.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 (44) hide show
  1. package/dist/NOTICE +18 -0
  2. package/dist/components/theme/theme-document.d.ts +6 -0
  3. package/dist/components/theme/theme-document.d.ts.map +1 -1
  4. package/dist/components/theme/theme-document.js +7 -2
  5. package/dist/components/theme/theme-document.js.map +1 -1
  6. package/dist/components/theme/theme-persistence.d.ts.map +1 -1
  7. package/dist/components/theme/theme-persistence.js +2 -1
  8. package/dist/components/theme/theme-persistence.js.map +1 -1
  9. package/dist/components/theme/theme-provider.d.ts +9 -0
  10. package/dist/components/theme/theme-provider.d.ts.map +1 -1
  11. package/dist/components/theme/theme-provider.js +13 -3
  12. package/dist/components/theme/theme-provider.js.map +1 -1
  13. package/dist/components/theme/theme-state.d.ts +27 -2
  14. package/dist/components/theme/theme-state.d.ts.map +1 -1
  15. package/dist/components/theme/theme-state.js +20 -2
  16. package/dist/components/theme/theme-state.js.map +1 -1
  17. package/dist/index.d.ts +1 -0
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +2 -1
  20. package/dist/lib/accent-roles.d.ts +124 -0
  21. package/dist/lib/accent-roles.d.ts.map +1 -0
  22. package/dist/lib/accent-roles.js +163 -0
  23. package/dist/lib/accent-roles.js.map +1 -0
  24. package/dist/lib/themes.d.ts +23 -1
  25. package/dist/lib/themes.d.ts.map +1 -1
  26. package/dist/lib/themes.js +23 -13
  27. package/dist/lib/themes.js.map +1 -1
  28. package/dist/r/lib.json +5 -0
  29. package/dist/r/registry.json +5 -0
  30. package/dist/r/src/components/theme/theme-document.ts +18 -2
  31. package/dist/r/src/components/theme/theme-persistence.ts +5 -2
  32. package/dist/r/src/components/theme/theme-provider.tsx +22 -1
  33. package/dist/r/src/components/theme/theme-state.ts +52 -3
  34. package/dist/r/src/lib/accent-roles.ts +338 -0
  35. package/dist/r/src/lib/themes.ts +44 -19
  36. package/package.json +1 -1
  37. package/registry.json +5 -0
  38. package/src/components/theme/theme-document.ts +18 -2
  39. package/src/components/theme/theme-persistence.ts +5 -2
  40. package/src/components/theme/theme-provider.tsx +22 -1
  41. package/src/components/theme/theme-state.ts +52 -3
  42. package/src/index.ts +8 -0
  43. package/src/lib/accent-roles.ts +338 -0
  44. package/src/lib/themes.ts +44 -19
@@ -0,0 +1,338 @@
1
+ /*
2
+ * Copyright (c) 2026 Wing
3
+ * SPDX-License-Identifier: MIT
4
+ *
5
+ * The colour helpers, the contrast gate and the custom-accent grammar are
6
+ * substantially translated from Zeron's `Color` and `AccentRoles`
7
+ * (https://github.com/zeronsh/zeron), as Adea's accepted port of them in
8
+ * `packages/ui/src/components/appearance.ts` expresses it. Extracted here so the
9
+ * shared provider and Adea run one implementation; `@adea-ai/themes` owns the
10
+ * colour engine (parsing, contrast, repair, the preset values and their roles).
11
+ * See NOTICE.
12
+ */
13
+
14
+ import {
15
+ ACCENTS,
16
+ accentRoles,
17
+ accentValue,
18
+ primarySubtleCss,
19
+ type AccentPreset,
20
+ type AccentRoles as CatalogueAccentRoles,
21
+ } from '@adea-ai/themes'
22
+ import {
23
+ contrastRatio as canonicalContrastRatio,
24
+ oklchToHex,
25
+ parseColor as parseCanonicalColor,
26
+ repairContrast,
27
+ type Oklch,
28
+ } from '@adea-ai/themes/oklch'
29
+ import type { ThemeAppearance } from './themes'
30
+
31
+ /**
32
+ * Accent colour maths and the roles an accent paints.
33
+ *
34
+ * Three questions live here, and none of them touches the document or a signal:
35
+ *
36
+ * - **What colour is this?** `parseColor`, `colorToHex`, `contrastRatio` and
37
+ * `ensureContrast` are the hex helpers a host needs around an accent (an
38
+ * imported theme's colours, a colour field). Hex is the host preference format;
39
+ * the catalogue's colour engine does the maths.
40
+ * - **Is this accent readable on this surface?** The *contrast floor*. A preset is
41
+ * curated, so the catalogue already measured it against its own themes and the
42
+ * floor is the identity for it; a custom `#rrggbb` is anyone's choice, so it is
43
+ * raised until it clears the floor and can never ship unreadable. A host that
44
+ * imports themes (whose backgrounds the catalogue never saw) turns the floor on
45
+ * for presets too.
46
+ * - **Which custom properties does an accent own?** `AccentRoles`, written on
47
+ * `<html>` by the provider and onto one element by {@link paintAccentRoles}.
48
+ */
49
+
50
+ /* Hex is the host preference format; the shared Themes package owns colour maths. */
51
+
52
+ export type RgbColor = Readonly<{ r: number; g: number; b: number; a: number }>
53
+
54
+ const HEX = /^#([0-9a-fA-F]+)$/
55
+
56
+ /** Parse a hex colour (`#rgb`, `#rgba`, `#rrggbb`, `#rrggbbaa`). `undefined` for anything else. */
57
+ export function parseColor(value: string): RgbColor | undefined {
58
+ const match = HEX.exec(value.trim())
59
+ const digits = match?.[1]
60
+ if (!digits) return undefined
61
+ if (digits.length === 3 || digits.length === 4) {
62
+ // Each nibble is doubled: `#abc` is `#aabbcc`.
63
+ const [r, g, b, a] = [...digits].map((digit) => Number.parseInt(digit + digit, 16))
64
+ return { r: r!, g: g!, b: b!, a: a ?? 255 }
65
+ }
66
+ if (digits.length === 6 || digits.length === 8) {
67
+ const byte = (index: number) => Number.parseInt(digits.slice(index, index + 2), 16)
68
+ return { r: byte(0), g: byte(2), b: byte(4), a: digits.length === 8 ? byte(6) : 255 }
69
+ }
70
+ return undefined
71
+ }
72
+
73
+ function toHexByte(value: number): string {
74
+ return value.toString(16).padStart(2, '0')
75
+ }
76
+
77
+ /** `#rrggbb`, or `#rrggbbaa` when the colour is not opaque. */
78
+ export function colorToHex(color: RgbColor): string {
79
+ const opaque = `#${toHexByte(color.r)}${toHexByte(color.g)}${toHexByte(color.b)}`
80
+ return color.a === 255 ? opaque : `${opaque}${toHexByte(color.a)}`
81
+ }
82
+
83
+ function blendOver(foreground: RgbColor, background: RgbColor): RgbColor {
84
+ const alpha = foreground.a / 255
85
+ const blend = (front: number, back: number) => Math.round(front * alpha + back * (1 - alpha))
86
+ return {
87
+ r: blend(foreground.r, background.r),
88
+ g: blend(foreground.g, background.g),
89
+ b: blend(foreground.b, background.b),
90
+ a: 255,
91
+ }
92
+ }
93
+
94
+ function toCanonicalColor(color: RgbColor): Oklch | undefined {
95
+ return parseCanonicalColor(colorToHex({ ...color, a: 255 }))
96
+ }
97
+
98
+ /** WCAG contrast ratio between two colours. Alpha composites over the background first. */
99
+ export function contrastRatio(
100
+ foreground: RgbColor | string,
101
+ background: RgbColor | string
102
+ ): number {
103
+ const front = typeof foreground === 'string' ? parseColor(foreground) : foreground
104
+ const back = typeof background === 'string' ? parseColor(background) : background
105
+ if (!front || !back) return 0
106
+ const opaqueFront = front.a === 255 ? front : blendOver(front, back)
107
+ const foregroundColor = toCanonicalColor(opaqueFront)
108
+ const backgroundColor = toCanonicalColor(back)
109
+ if (!foregroundColor || !backgroundColor) return 0
110
+ return canonicalContrastRatio(foregroundColor, backgroundColor)
111
+ }
112
+
113
+ const WHITE: RgbColor = { r: 255, g: 255, b: 255, a: 255 }
114
+ const WHITE_OKLCH: Oklch = { l: 1, c: 0, h: 0 }
115
+
116
+ /**
117
+ * The nearest colour to `source` that reaches `minimum` against `canvas`, as hex.
118
+ *
119
+ * The catalogue's `repairContrast` is asked for a little more than the floor, in
120
+ * 0.001 steps up to 0.05, because the result is rounded to 8-bit hex and the
121
+ * rounded colour is what is measured: asking for exactly the floor can land a hair
122
+ * under it after rounding. If nothing in that range survives the rounding, white or
123
+ * black stands in, whichever clears the floor.
124
+ */
125
+ function repairedHex(source: Oklch, canvas: Oklch, minimum: number): string {
126
+ for (let margin = 0; margin <= 0.05; margin = Number((margin + 0.001).toFixed(3))) {
127
+ const repaired = repairContrast(source, canvas, minimum + margin)
128
+ if (!repaired.satisfied) continue
129
+ const hex = oklchToHex(repaired.color)
130
+ const rounded = parseCanonicalColor(hex)
131
+ if (rounded && canonicalContrastRatio(rounded, canvas) >= minimum) return hex
132
+ }
133
+ return canonicalContrastRatio(WHITE_OKLCH, canvas) >= minimum ? '#ffffff' : '#000000'
134
+ }
135
+
136
+ /** Preserve the host hex shape while delegating contrast repair to the catalogue. */
137
+ export function ensureContrast(color: RgbColor, background: RgbColor, minimum: number): RgbColor {
138
+ if (contrastRatio(color, background) >= minimum) return color
139
+
140
+ const renderedColor = color.a === 255 ? color : blendOver(color, background)
141
+ const source = toCanonicalColor(renderedColor)
142
+ const canvas = toCanonicalColor(background)
143
+ if (!source || !canvas) return color
144
+ return parseColor(repairedHex(source, canvas, minimum))!
145
+ }
146
+
147
+ /** The contrast an accent is held to against its surface unless the host sets another. */
148
+ export const DEFAULT_ACCENT_CONTRAST_FLOOR = 3
149
+
150
+ /**
151
+ * Normalise a custom accent against a surface: unparseable input is rejected
152
+ * (`undefined`), and any accepted colour is raised to `minimum` so an accent can
153
+ * never ship unreadable. Accepts every CSS hex length; for the stored grammar use
154
+ * {@link normalizeCustomAccent}.
155
+ */
156
+ export function normalizeAccentValue(
157
+ value: string,
158
+ background: string,
159
+ minimum: number = DEFAULT_ACCENT_CONTRAST_FLOOR
160
+ ): string | undefined {
161
+ const parsed = parseColor(value)
162
+ if (!parsed) return undefined
163
+ const backdrop = parseColor(background) ?? WHITE
164
+ return colorToHex(ensureContrast(parsed, backdrop, minimum))
165
+ }
166
+
167
+ /**
168
+ * The stored form of a custom accent: `#rrggbb`, six digits, opaque, lower case.
169
+ *
170
+ * Deliberately narrower than {@link parseColor}. A stored accent is compared,
171
+ * deduplicated and used as an attribute, so it has one spelling; a host that
172
+ * accepts `#abc` in a field expands it before storing. Anything else — a name, an
173
+ * `rgb()`, a short or alpha form, a non-string — is not an accent and returns
174
+ * `undefined`.
175
+ */
176
+ export function normalizeCustomAccent(value: unknown): string | undefined {
177
+ return typeof value === 'string' && /^#[0-9a-fA-F]{6}$/.test(value)
178
+ ? value.toLowerCase()
179
+ : undefined
180
+ }
181
+
182
+ /** Whether a selection's accent is the custom `#rrggbb` form (any letter case). */
183
+ export function isCustomAccent(value: unknown): boolean {
184
+ return normalizeCustomAccent(value) !== undefined
185
+ }
186
+
187
+ /**
188
+ * The roles an accent paints, as CSS values: the catalogue's `AccentRoles`, so the
189
+ * provider, the generated stylesheet and a host's own paint cannot disagree about
190
+ * what a role is. `primarySubtle` is an expression over `var(--primary)`; the
191
+ * document writer never sets it (see {@link accentOverrideVariables}), an element
192
+ * paint must.
193
+ */
194
+ export type AccentRoles = CatalogueAccentRoles
195
+
196
+ /** What an accent resolves against: the surface it sits on and the theme it sits in. */
197
+ export type AccentTarget = {
198
+ /** The theme, or only its appearance. A theme gives the tint its measured strength. */
199
+ target: Parameters<typeof accentRoles>[1]
200
+ /** The surface the accent must read on. Without it nothing can be measured or repaired. */
201
+ background: string | undefined
202
+ /**
203
+ * The contrast to hold the accent to against `background`. Absent, a preset is
204
+ * painted verbatim and a custom colour is held to {@link DEFAULT_ACCENT_CONTRAST_FLOOR}.
205
+ */
206
+ floor?: number
207
+ /**
208
+ * The accents the selected theme pair carries (`ansi-blue`…), for an id that is
209
+ * neither a preset nor a colour.
210
+ */
211
+ themeAccents?: readonly AccentPreset[]
212
+ }
213
+
214
+ function appearanceOf(target: AccentTarget['target']): ThemeAppearance {
215
+ return typeof target === 'string' ? target : target.appearance
216
+ }
217
+
218
+ /**
219
+ * `value` raised to `floor` against `background` as hex, or `value` itself when it
220
+ * already clears the floor, is not a colour, or there is no surface to measure it on.
221
+ *
222
+ * Returning the argument itself is what makes the floor the identity for a preset
223
+ * that already reads: the caller compares and keeps the catalogue's own roles.
224
+ */
225
+ function gateColor(value: string, background: string | undefined, floor: number): string {
226
+ const source = parseCanonicalColor(value)
227
+ const canvas = background ? parseCanonicalColor(background) : undefined
228
+ if (!source || !canvas) return value
229
+ if (canonicalContrastRatio(source, canvas) >= floor) return value
230
+ return repairedHex(source, canvas, floor)
231
+ }
232
+
233
+ /**
234
+ * The roles for a preset (or a custom colour made into one), held to the floor.
235
+ *
236
+ * A preset that clears the floor keeps the catalogue's roles untouched. One that
237
+ * does not is repaired to the nearest passing colour and the whole role set is
238
+ * rebuilt from the repaired value, so the label, hover and ring follow it instead
239
+ * of describing a colour that is no longer on screen.
240
+ */
241
+ export function accentRolesForPreset(
242
+ preset: AccentPreset,
243
+ context: AccentTarget
244
+ ): AccentRoles | undefined {
245
+ const appearance = appearanceOf(context.target)
246
+ const requested = accentValue(preset, appearance)
247
+ if (!parseCanonicalColor(requested)) return undefined
248
+
249
+ const floor = context.floor
250
+ if (floor === undefined) return accentRoles(preset, context.target)
251
+
252
+ const gated = gateColor(requested, context.background, floor)
253
+ if (gated === requested) return accentRoles(preset, context.target)
254
+
255
+ const roles = accentRoles({ ...preset, light: gated, dark: gated }, context.target)
256
+ // The tint's strength is measured against the primary it will sit over, and the
257
+ // repaired primary is not one the theme's own measurement saw.
258
+ return { ...roles, primarySubtle: primarySubtleCss(context.target, '--primary', [gated]) }
259
+ }
260
+
261
+ /**
262
+ * Resolve an accent selection to its roles, or `undefined` when it overrides nothing.
263
+ *
264
+ * `undefined` is "use the theme's own primary": the literal `theme`, an id that is
265
+ * neither a preset, an offered theme accent nor a colour, and a colour that does not
266
+ * parse. A host that wants one of those to mean something else decides before
267
+ * asking (Adea's codec maps a stored `theme` to its default preset when it reads).
268
+ */
269
+ export function resolveAccentRoles(accent: string, context: AccentTarget): AccentRoles | undefined {
270
+ if (accent === 'theme') return undefined
271
+
272
+ const custom = normalizeCustomAccent(accent)
273
+ if (custom) {
274
+ const floor = context.floor ?? DEFAULT_ACCENT_CONTRAST_FLOOR
275
+ return accentRolesForPreset(
276
+ { id: 'custom', label: 'Custom', description: '', light: custom, dark: custom },
277
+ { ...context, floor }
278
+ )
279
+ }
280
+
281
+ const preset =
282
+ ACCENTS.find((candidate) => candidate.id === accent) ??
283
+ context.themeAccents?.find((candidate) => candidate.id === accent)
284
+ return preset ? accentRolesForPreset(preset, context) : undefined
285
+ }
286
+
287
+ /**
288
+ * The custom properties an accent override owns on `<html>`.
289
+ *
290
+ * `--primary-subtle` is deliberately not among them. It is a `color-mix()` over
291
+ * `var(--primary)`, so on the same element it follows an accent by itself, and its
292
+ * strength is the theme's, measured to keep body text at 4.5:1 on the tint.
293
+ * Writing one here would replace that measurement with the argument's.
294
+ */
295
+ export function accentOverrideVariables(roles: AccentRoles): Record<string, string> {
296
+ return {
297
+ '--primary': roles.primary,
298
+ '--primary-foreground': roles.primaryForeground,
299
+ '--primary-hover': roles.primaryHover,
300
+ '--ring': roles.ring,
301
+ }
302
+ }
303
+
304
+ /** The custom properties {@link paintAccentRoles} writes and clears. */
305
+ export const ACCENT_PAINT_PROPERTIES = [
306
+ '--primary',
307
+ '--primary-foreground',
308
+ '--primary-hover',
309
+ '--primary-subtle',
310
+ '--ring',
311
+ ] as const
312
+
313
+ /**
314
+ * Scope an accent to one element and its descendants.
315
+ *
316
+ * Unlike the document, an element must write `--primary-subtle` too. A custom
317
+ * property that uses `var()` is resolved where it is declared, so the tint inherited
318
+ * from `<html>` is already a mix over *`<html>`'s* primary and an element that only
319
+ * overrides `--primary` would keep the old tint. Declared here, the same expression
320
+ * resolves against this element's primary.
321
+ *
322
+ * `undefined` clears every property, and the element inherits the document's accent
323
+ * again.
324
+ */
325
+ export function paintAccentRoles(
326
+ element: { style: Pick<CSSStyleDeclaration, 'setProperty' | 'removeProperty'> },
327
+ roles: AccentRoles | undefined
328
+ ): void {
329
+ if (!roles) {
330
+ for (const property of ACCENT_PAINT_PROPERTIES) element.style.removeProperty(property)
331
+ return
332
+ }
333
+ element.style.setProperty('--primary', roles.primary)
334
+ element.style.setProperty('--primary-foreground', roles.primaryForeground)
335
+ element.style.setProperty('--primary-hover', roles.primaryHover)
336
+ element.style.setProperty('--primary-subtle', roles.primarySubtle)
337
+ element.style.setProperty('--ring', roles.ring)
338
+ }
package/src/lib/themes.ts CHANGED
@@ -36,7 +36,6 @@
36
36
  import {
37
37
  ACCENTS,
38
38
  accentForeground,
39
- accentRoles,
40
39
  chartSeries,
41
40
  getAccent,
42
41
  getTheme,
@@ -58,6 +57,7 @@ import {
58
57
  } from '@adea-ai/themes'
59
58
  import { shadcnVariables } from '@adea-ai/themes/adapters/shadcn'
60
59
  import { oklchToHex, parseColor } from '@adea-ai/themes/oklch'
60
+ import { accentOverrideVariables, resolveAccentRoles, type AccentRoles } from './accent-roles'
61
61
  import { isThemeAccentId } from './theme-accents'
62
62
 
63
63
  export { isThemeAccentId } from './theme-accents'
@@ -566,26 +566,51 @@ export function accentVariables(
566
566
  pair: { lightThemeId: string; darkThemeId: string } = {
567
567
  lightThemeId: defaultLightThemeId,
568
568
  darkThemeId: defaultDarkThemeId,
569
- }
569
+ },
570
+ options: AccentVariableOptions = {}
570
571
  ): Record<string, string> | undefined {
572
+ const roles = accentRolesForSelection(accentId, appearance, pair, options)
573
+ return roles ? accentOverrideVariables(roles) : undefined
574
+ }
575
+
576
+ /** What an accent is held to when it is resolved against a surface. */
577
+ export type AccentVariableOptions = {
578
+ /** The surface the accent must read on: the variant's `colors.background`. */
579
+ background?: string
580
+ /**
581
+ * The contrast floor against `background`. Absent, a preset is painted verbatim
582
+ * and only a custom `#rrggbb` is held to the default floor.
583
+ */
584
+ floor?: number
585
+ /** The variant the accent is applied to, so the tint takes its measured strength. */
586
+ variantId?: string
587
+ }
588
+
589
+ /**
590
+ * An accent selection resolved to the full {@link AccentRoles}, against a theme
591
+ * pair. {@link accentVariables} is the document half of this; an element-scoped
592
+ * paint (`paintAccentRoles`) wants all of it, including the tint.
593
+ */
594
+ export function accentRolesForSelection(
595
+ accentId: string,
596
+ appearance: ThemeAppearance,
597
+ pair: { lightThemeId: string; darkThemeId: string } = {
598
+ lightThemeId: defaultLightThemeId,
599
+ darkThemeId: defaultDarkThemeId,
600
+ },
601
+ options: AccentVariableOptions = {}
602
+ ): AccentRoles | undefined {
571
603
  if (accentId === 'theme') return undefined
572
- const { light, dark } = recordPair(pair.lightThemeId, pair.darkThemeId)
573
- const preset = resolveAccentPreset(accentId, light, dark)
574
- if (!preset) return undefined
575
-
576
- // `--primary-subtle` is deliberately not here. It is a `color-mix()` over
577
- // `var(--primary)`, so the theme's own declaration (`themeCssVariables`, or the
578
- // default block in `theme.css`) already follows the accent; and its strength is the
579
- // *theme's*, measured by `primarySubtleAlpha` to hold body text at 4.5:1 for every
580
- // accent the theme offers. Writing one here would replace that measured strength
581
- // with whatever this function was given, which is how an accent used to undo it.
582
- const roles = accentRoles(preset, appearance)
583
- return {
584
- '--primary': roles.primary,
585
- '--primary-foreground': roles.primaryForeground,
586
- '--primary-hover': roles.primaryHover,
587
- '--ring': roles.ring,
588
- }
604
+ const record = options.variantId ? getTheme(options.variantId) : undefined
605
+ return resolveAccentRoles(accentId, {
606
+ target: record ?? appearance,
607
+ background: options.background,
608
+ floor: options.floor,
609
+ // Only an `ansi-*` id needs the pair's offering, and measuring it is not free.
610
+ themeAccents: isThemeAccentId(accentId)
611
+ ? themeAccentsFor(pair.lightThemeId, pair.darkThemeId)
612
+ : undefined,
613
+ })
589
614
  }
590
615
 
591
616
  /**