@adea-ai/themes 0.11.7 → 0.11.9
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.
- package/README.md +46 -0
- package/dist/theme-token-projection-core.d.ts +35 -0
- package/dist/theme-token-projection-core.d.ts.map +1 -0
- package/dist/theme-token-projection-core.js +84 -0
- package/dist/theme-token-projection-core.js.map +1 -0
- package/dist/theme-token-projection-script.d.ts +2 -0
- package/dist/theme-token-projection-script.js +2 -0
- package/dist/theme-token-projection.d.ts +4 -0
- package/dist/theme-token-projection.d.ts.map +1 -0
- package/dist/theme-token-projection.js +3 -0
- package/dist/theme-token-projection.js.map +1 -0
- package/package.json +13 -3
- package/src/theme-token-projection-core.ts +131 -0
- package/src/theme-token-projection.ts +12 -0
package/README.md
CHANGED
|
@@ -79,6 +79,52 @@ palettes and catalogue tooling. Its 16 KiB uncompressed ceiling leaves headroom
|
|
|
79
79
|
for one record and the terminal adapter while detecting catalogue inclusion.
|
|
80
80
|
This package still has no runtime dependencies.
|
|
81
81
|
|
|
82
|
+
### Projecting an existing token map
|
|
83
|
+
|
|
84
|
+
The projection API is not present in published `@adea-ai/themes` 0.11.7; use a
|
|
85
|
+
later release that includes this change. It provides a declarative projector for
|
|
86
|
+
consumers that need to derive missing custom properties from an older stored token
|
|
87
|
+
map. The runtime API returns a new map and never mutates or persists the input:
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
import { projectThemeTokens } from '@adea-ai/themes/theme-token-projection'
|
|
91
|
+
import { themeTokenProjectionBundle } from '@adea-ai/themes/theme-token-projection/script'
|
|
92
|
+
|
|
93
|
+
const sidebarProjection = [
|
|
94
|
+
{ target: '--sidebar', source: '--card' },
|
|
95
|
+
{
|
|
96
|
+
target: '--sidebar-foreground',
|
|
97
|
+
source: '--card-foreground',
|
|
98
|
+
contrast: { against: '--sidebar', minimum: 4.5 },
|
|
99
|
+
},
|
|
100
|
+
] as const
|
|
101
|
+
|
|
102
|
+
const hydratedTokens = projectThemeTokens(storedTokens, sidebarProjection)
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Projection rules are plain data: each rule names a source and destination, with an
|
|
106
|
+
optional contrast floor for a generated value. Rules run in order, so a generated
|
|
107
|
+
surface can anchor a later foreground. Existing destination tokens are preserved
|
|
108
|
+
exactly, including explicit values that do not meet the requested floor.
|
|
109
|
+
|
|
110
|
+
The `/script` export is a self-contained JavaScript **expression string** built from
|
|
111
|
+
the same projector and colour routines as the runtime API. Use it in a pre-paint
|
|
112
|
+
script after parsing the token map; pass the map as a variable and serialize only
|
|
113
|
+
application-owned rule data. Escaping `<` keeps the serialized rules safe to embed in
|
|
114
|
+
an inline `<script>` element:
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
const serializedRules = JSON.stringify(sidebarProjection).replaceAll('<', '\\u003c')
|
|
118
|
+
const prepaintCall = `(${themeTokenProjectionBundle})(tokens, ${serializedRules})`
|
|
119
|
+
// Include `prepaintCall` in the script after `tokens` has been parsed.
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Do not serialize callbacks or interpolate stored token values into executable
|
|
123
|
+
source. The expression evaluates to `(tokens, rules) => projectedTokens`; it adds
|
|
124
|
+
only missing tokens and does not write to storage. A malformed or translucent color
|
|
125
|
+
is preserved when it cannot be measured safely; use
|
|
126
|
+
`projectThemeTokensDetailed` to inspect unsupported colors or unreachable floors.
|
|
127
|
+
|
|
82
128
|
```ts
|
|
83
129
|
import { getTheme, themeFamilies, resolveTheme } from '@adea-ai/themes'
|
|
84
130
|
import { themeCssVariables } from '@adea-ai/themes/adapters/css'
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export type ThemeTokenContrastRule = Readonly<{
|
|
2
|
+
/** The custom property the generated foreground must read against. */
|
|
3
|
+
against: string;
|
|
4
|
+
/** Minimum WCAG 2.1 contrast ratio for the generated value. */
|
|
5
|
+
minimum: number;
|
|
6
|
+
}>;
|
|
7
|
+
export type ThemeTokenProjectionRule = Readonly<{
|
|
8
|
+
/** Destination custom property; an explicit value is never replaced. */
|
|
9
|
+
target: string;
|
|
10
|
+
/** Existing custom property to project when `target` is absent. */
|
|
11
|
+
source: string;
|
|
12
|
+
/** Repair a generated destination when both source and surface are colours. */
|
|
13
|
+
contrast?: ThemeTokenContrastRule;
|
|
14
|
+
}>;
|
|
15
|
+
export type ThemeTokenProjection = readonly ThemeTokenProjectionRule[];
|
|
16
|
+
export type ThemeTokenProjectionFailure = Readonly<{
|
|
17
|
+
target: string;
|
|
18
|
+
source: string;
|
|
19
|
+
against: string;
|
|
20
|
+
minimum: number;
|
|
21
|
+
reason: 'unsupported_color' | 'contrast_unreachable';
|
|
22
|
+
}>;
|
|
23
|
+
export type ThemeTokenProjectionResult = Readonly<{
|
|
24
|
+
tokens: Record<string, string>;
|
|
25
|
+
failures: readonly ThemeTokenProjectionFailure[];
|
|
26
|
+
}>;
|
|
27
|
+
/**
|
|
28
|
+
* Add projected values for absent custom properties, leaving explicit values byte
|
|
29
|
+
* for byte intact. Rules run in order so one generated surface can anchor its text.
|
|
30
|
+
* Unknown or malformed rules and unparseable colors fail closed by skipping repair.
|
|
31
|
+
*/
|
|
32
|
+
export declare function projectThemeTokensDetailed(tokens: Readonly<Record<string, string>>, projection: ThemeTokenProjection): ThemeTokenProjectionResult;
|
|
33
|
+
/** Project tokens while discarding diagnostics for callers that only paint them. */
|
|
34
|
+
export declare function projectThemeTokensCore(tokens: Readonly<Record<string, string>>, projection: ThemeTokenProjection): Record<string, string>;
|
|
35
|
+
//# sourceMappingURL=theme-token-projection-core.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"theme-token-projection-core.d.ts","sourceRoot":"","sources":["../src/theme-token-projection-core.ts"],"names":[],"mappings":"AAUA,MAAM,MAAM,sBAAsB,GAAG,QAAQ,CAAC;IAC5C,sEAAsE;IACtE,OAAO,EAAE,MAAM,CAAA;IACf,+DAA+D;IAC/D,OAAO,EAAE,MAAM,CAAA;CAChB,CAAC,CAAA;AAEF,MAAM,MAAM,wBAAwB,GAAG,QAAQ,CAAC;IAC9C,wEAAwE;IACxE,MAAM,EAAE,MAAM,CAAA;IACd,mEAAmE;IACnE,MAAM,EAAE,MAAM,CAAA;IACd,+EAA+E;IAC/E,QAAQ,CAAC,EAAE,sBAAsB,CAAA;CAClC,CAAC,CAAA;AAEF,MAAM,MAAM,oBAAoB,GAAG,SAAS,wBAAwB,EAAE,CAAA;AAEtE,MAAM,MAAM,2BAA2B,GAAG,QAAQ,CAAC;IACjD,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,MAAM,CAAA;IACf,OAAO,EAAE,MAAM,CAAA;IACf,MAAM,EAAE,mBAAmB,GAAG,sBAAsB,CAAA;CACrD,CAAC,CAAA;AAEF,MAAM,MAAM,0BAA0B,GAAG,QAAQ,CAAC;IAChD,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC9B,QAAQ,EAAE,SAAS,2BAA2B,EAAE,CAAA;CACjD,CAAC,CAAA;AA+CF;;;;GAIG;AACH,wBAAgB,0BAA0B,CACxC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,EACxC,UAAU,EAAE,oBAAoB,GAC/B,0BAA0B,CA4B5B;AAED,oFAAoF;AACpF,wBAAgB,sBAAsB,CACpC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,EACxC,UAAU,EAAE,oBAAoB,GAC/B,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAExB"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Serializable projections for token maps that have stable roles but predate one
|
|
3
|
+
* of the design system's derived token families.
|
|
4
|
+
*
|
|
5
|
+
* The rules are data rather than a callback because the same projection can run
|
|
6
|
+
* against a provider's live variant and against a stored theme during the
|
|
7
|
+
* pre-paint script. Explicit destination tokens always win.
|
|
8
|
+
*/
|
|
9
|
+
import { contrastRatio, oklchToHex, parseColor, repairContrast } from './oklch.js';
|
|
10
|
+
const CUSTOM_PROPERTY = /^--[A-Za-z0-9_-]+$/;
|
|
11
|
+
function validRule(rule) {
|
|
12
|
+
if (!rule || typeof rule !== 'object' || Array.isArray(rule))
|
|
13
|
+
return false;
|
|
14
|
+
const contrast = rule.contrast;
|
|
15
|
+
return (CUSTOM_PROPERTY.test(rule.target) &&
|
|
16
|
+
CUSTOM_PROPERTY.test(rule.source) &&
|
|
17
|
+
(contrast === undefined ||
|
|
18
|
+
(!!contrast &&
|
|
19
|
+
typeof contrast === 'object' &&
|
|
20
|
+
CUSTOM_PROPERTY.test(contrast.against) &&
|
|
21
|
+
Number.isFinite(contrast.minimum) &&
|
|
22
|
+
contrast.minimum > 1 &&
|
|
23
|
+
contrast.minimum <= 21)));
|
|
24
|
+
}
|
|
25
|
+
function hasUnsupportedAlpha(value) {
|
|
26
|
+
return /^oklch\([^)]*\/[^)]*\)$/i.test(value.trim());
|
|
27
|
+
}
|
|
28
|
+
function repairGeneratedValue(value, against, minimum) {
|
|
29
|
+
if (hasUnsupportedAlpha(value) || hasUnsupportedAlpha(against)) {
|
|
30
|
+
return { value, failure: 'unsupported_color' };
|
|
31
|
+
}
|
|
32
|
+
const foreground = parseColor(value);
|
|
33
|
+
const background = parseColor(against);
|
|
34
|
+
if (!foreground || !background)
|
|
35
|
+
return { value, failure: 'unsupported_color' };
|
|
36
|
+
if (contrastRatio(foreground, background) >= minimum)
|
|
37
|
+
return { value };
|
|
38
|
+
const repaired = repairContrast(foreground, background, minimum, 0.6);
|
|
39
|
+
if (!repaired.satisfied)
|
|
40
|
+
return { value, failure: 'contrast_unreachable' };
|
|
41
|
+
const hex = oklchToHex(repaired.color);
|
|
42
|
+
const rounded = parseColor(hex);
|
|
43
|
+
return rounded && contrastRatio(rounded, background) >= minimum
|
|
44
|
+
? { value: hex }
|
|
45
|
+
: { value, failure: 'contrast_unreachable' };
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Add projected values for absent custom properties, leaving explicit values byte
|
|
49
|
+
* for byte intact. Rules run in order so one generated surface can anchor its text.
|
|
50
|
+
* Unknown or malformed rules and unparseable colors fail closed by skipping repair.
|
|
51
|
+
*/
|
|
52
|
+
export function projectThemeTokensDetailed(tokens, projection) {
|
|
53
|
+
const projected = { ...tokens };
|
|
54
|
+
const failures = [];
|
|
55
|
+
for (const rule of Array.isArray(projection) ? projection : []) {
|
|
56
|
+
if (!validRule(rule) || Object.hasOwn(projected, rule.target))
|
|
57
|
+
continue;
|
|
58
|
+
const value = projected[rule.source];
|
|
59
|
+
if (typeof value !== 'string')
|
|
60
|
+
continue;
|
|
61
|
+
if (!rule.contrast) {
|
|
62
|
+
projected[rule.target] = value;
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
const against = projected[rule.contrast.against] ?? '';
|
|
66
|
+
const result = repairGeneratedValue(value, against, rule.contrast.minimum);
|
|
67
|
+
projected[rule.target] = result.value;
|
|
68
|
+
if (result.failure) {
|
|
69
|
+
failures.push({
|
|
70
|
+
target: rule.target,
|
|
71
|
+
source: rule.source,
|
|
72
|
+
against: rule.contrast.against,
|
|
73
|
+
minimum: rule.contrast.minimum,
|
|
74
|
+
reason: result.failure,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return { tokens: projected, failures };
|
|
79
|
+
}
|
|
80
|
+
/** Project tokens while discarding diagnostics for callers that only paint them. */
|
|
81
|
+
export function projectThemeTokensCore(tokens, projection) {
|
|
82
|
+
return projectThemeTokensDetailed(tokens, projection).tokens;
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=theme-token-projection-core.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"theme-token-projection-core.js","sourceRoot":"","sources":["../src/theme-token-projection-core.ts"],"names":[],"mappings":"AAAA;;;;;;;GAO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vC,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC;YACnB,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,KAAK,CAAA;YAC9B,SAAQ;QACV,CAAC;QAED,MAAM,OAAO,GAAG,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,EAAE,CAAA;QACtD,MAAM,MAAM,GAAG,oBAAoB,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAA;QAC1E,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,KAAK,CAAA;QACrC,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;YACnB,QAAQ,CAAC,IAAI,CAAC;gBACZ,MAAM,EAAE,IAAI,CAAC,MAAM;gBACnB,MAAM,EAAE,IAAI,CAAC,MAAM;gBACnB,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC,OAAO;gBAC9B,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC,OAAO;gBAC9B,MAAM,EAAE,MAAM,CAAC,OAAO;aACvB,CAAC,CAAA;QACJ,CAAC;IACH,CAAC;IAED,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,QAAQ,EAAE,CAAA;AACxC,CAAC;AAED,oFAAoF;AACpF,MAAM,UAAU,sBAAsB,CACpC,MAAwC,EACxC,UAAgC;IAEhC,OAAO,0BAA0B,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC,MAAM,CAAA;AAC9D,CAAC"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
/** Generated by scripts/build-theme-token-projection.ts. Do not edit. */
|
|
2
|
+
export const themeTokenProjectionBundle = "(function(globalThis){(()=>{var R=(t)=>t<=0.04045?t/12.92:((t+0.055)/1.055)**2.4,y=(t)=>t<=0.0031308?t*12.92:1.055*t**0.4166666666666667-0.055;function m(t){return Math.round(Math.min(1,Math.max(0,y(t)))*255).toString(16).padStart(2,\"0\")}function F({r:t,g:e,b:n}){let r=Math.cbrt(0.4122214708*t+0.5363325363*e+0.0514459929*n),o=Math.cbrt(0.2119034982*t+0.6806995451*e+0.1073969566*n),c=Math.cbrt(0.0883024619*t+0.2817188376*e+0.6299787005*n);return{l:0.2104542553*r+0.793617785*o-0.0040720468*c,a:1.9779984951*r-2.428592205*o+0.4505937099*c,b:0.0259040371*r+0.7827717662*o-0.808675766*c}}function p({l:t,a:e,b:n}){let r=t+0.3963377774*e+0.2158037573*n,o=t-0.1055613458*e-0.0638541728*n,c=t-0.0894841775*e-1.291485548*n,i=r**3,s=o**3,a=c**3;return{r:4.0767416621*i-3.3077115913*s+0.2309699292*a,g:-1.2684380046*i+2.6097574011*s-0.3413193965*a,b:-0.0041960863*i-0.7034186147*s+1.707614701*a}}var b=({l:t,c:e,h:n})=>{let r=n*Math.PI/180;return{l:t,a:e*Math.cos(r),b:e*Math.sin(r)}},A=({l:t,a:e,b:n})=>{let r=Math.hypot(e,n),o=r<0.000001?0:(Math.atan2(n,e)*180/Math.PI+360)%360;return{l:t,c:r,h:o}};function g(t,e=0.0001){let{r:n,g:r,b:o}=p(b(t)),c=(i)=>i>=-e&&i<=1+e;return c(n)&&c(r)&&c(o)}function M(t){let e=Math.min(1,Math.max(0,t.l)),n={...t,l:e};if(g(n))return n;let r=0,o=n.c;for(let c=0;c<24;c+=1){let i=(r+o)/2;if(g({...n,c:i}))r=i;else o=i}return{...n,c:r}}var L=/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i;function E(t){let e=L.exec(t.trim());if(!e)return;let n=e[1]??\"\",r=n.length===3?n.split(\"\").map((s)=>s+s).join(\"\"):n,[o,c,i]=[0,2,4].map((s)=>R(Number.parseInt(r.slice(s,s+2),16)/255));return A(F({r:o,g:c,b:i}))}function j(t){let e=M(t),{r:n,g:r,b:o}=p(b(e));return`#${m(n)}${m(r)}${m(o)}`}function x(t,e){let n=t??\"0\",r=Number.parseFloat(n);if(Number.isNaN(r))return Number.NaN;return n.endsWith(\"%\")?r/100*e:r}var S=/^oklch\\(\\s*((?:\\d+(?:\\.\\d*)?|\\.\\d+)%?)\\s+((?:\\d+(?:\\.\\d*)?|\\.\\d+)%?)\\s+((?:\\d+(?:\\.\\d*)?|\\.\\d+))(?:deg)?\\s*(?:\\/\\s*(?:\\d+(?:\\.\\d*)?|\\.\\d+)%?\\s*)?\\)$/i;function I(t){let e=S.exec(t.trim());if(!e)return;let n=x(e[1],1),r=x(e[2],0.4),o=Number.parseFloat(e[3]??\"0\");if(![n,r,o].every(Number.isFinite))return;return{l:n,c:r,h:o}}function l(t){if(typeof t!==\"string\")return;let e=t.trim();return e.toLowerCase().startsWith(\"oklch\")?I(e):E(e)}function w(t){let e=Number(t.l.toFixed(4)),n=Number(t.c.toFixed(4)),r=t.c<0.000001?0:Number(t.h.toFixed(2));return`oklch(${e} ${n} ${r})`}function f(t){return R(Math.round(y(G(t))*255)/255)}function O(t){let{r:e,g:n,b:r}=p(b(M(t)));return 0.2126*f(e)+0.7152*f(n)+0.0722*f(r)}function u(t,e){let n=O(t),r=O(e),[o,c]=n>r?[n,r]:[r,n];return(o+0.05)/(c+0.05)}function G(t){return Math.min(1,Math.max(0,t))}function H(t,e){return t.l>=e.l?1:-1}function d(t){return l(w(t))??t}function P(t,e,n,r=0.22){let o=u(d(t),e);if(o>=n)return{color:t,ratio:o,delta:0,satisfied:!0};let c=H(t,e),i=0.002,s=Math.floor(r/i);for(let a=1;a<=s;a+=1)for(let _ of[c,-c]){let h={...t,l:t.l+_*i*a};if(h.l<=0||h.l>=1)continue;let T=u(d(h),e);if(T>=n)return{color:d(h),ratio:T,delta:Number((i*a).toFixed(4)),satisfied:!0}}return{color:t,ratio:o,delta:0,satisfied:!1}}var k=/^--[A-Za-z0-9_-]+$/;function v(t){if(!t||typeof t!==\"object\"||Array.isArray(t))return!1;let e=t.contrast;return k.test(t.target)&&k.test(t.source)&&(e===void 0||!!e&&typeof e===\"object\"&&k.test(e.against)&&Number.isFinite(e.minimum)&&e.minimum>1&&e.minimum<=21)}function C(t){return/^oklch\\([^)]*\\/[^)]*\\)$/i.test(t.trim())}function B(t,e,n){if(C(t)||C(e))return{value:t,failure:\"unsupported_color\"};let r=l(t),o=l(e);if(!r||!o)return{value:t,failure:\"unsupported_color\"};if(u(r,o)>=n)return{value:t};let c=P(r,o,n,0.6);if(!c.satisfied)return{value:t,failure:\"contrast_unreachable\"};let i=j(c.color),s=l(i);return s&&u(s,o)>=n?{value:i}:{value:t,failure:\"contrast_unreachable\"}}function D(t,e){let n={...t},r=[];for(let o of Array.isArray(e)?e:[]){if(!v(o)||Object.hasOwn(n,o.target))continue;let c=n[o.source];if(typeof c!==\"string\")continue;if(!o.contrast){n[o.target]=c;continue}let i=n[o.contrast.against]??\"\",s=B(c,i,o.contrast.minimum);if(n[o.target]=s.value,s.failure)r.push({target:o.target,source:o.source,against:o.contrast.against,minimum:o.contrast.minimum,reason:s.failure})}return{tokens:n,failures:r}}function N(t,e){return D(t,e).tokens}var U=globalThis;U.adeaThemeTokenProjection=N;})();;return globalThis.adeaThemeTokenProjection})(Object.create(null))"
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** Public runtime API for declarative custom-property projection. */
|
|
2
|
+
export { projectThemeTokensCore as projectThemeTokens, projectThemeTokensDetailed, } from './theme-token-projection-core.js';
|
|
3
|
+
export type { ThemeTokenContrastRule, ThemeTokenProjection, ThemeTokenProjectionFailure, ThemeTokenProjectionResult, ThemeTokenProjectionRule, } from './theme-token-projection-core.js';
|
|
4
|
+
//# sourceMappingURL=theme-token-projection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"theme-token-projection.d.ts","sourceRoot":"","sources":["../src/theme-token-projection.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,OAAO,EACL,sBAAsB,IAAI,kBAAkB,EAC5C,0BAA0B,GAC3B,MAAM,kCAAkC,CAAA;AACzC,YAAY,EACV,sBAAsB,EACtB,oBAAoB,EACpB,2BAA2B,EAC3B,0BAA0B,EAC1B,wBAAwB,GACzB,MAAM,kCAAkC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"theme-token-projection.js","sourceRoot":"","sources":["../src/theme-token-projection.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,OAAO,EACL,sBAAsB,IAAI,kBAAkB,EAC5C,0BAA0B,GAC3B,MAAM,kCAAkC,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adea-ai/themes",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.9",
|
|
4
4
|
"description": "The Adea theme catalogue: first-party and imported semantic OKLCH themes, with Base24, terminal, CSS and syntax adapters.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"adea",
|
|
@@ -48,6 +48,14 @@
|
|
|
48
48
|
"types": "./dist/oklch.d.ts",
|
|
49
49
|
"import": "./dist/oklch.js"
|
|
50
50
|
},
|
|
51
|
+
"./theme-token-projection": {
|
|
52
|
+
"types": "./dist/theme-token-projection.d.ts",
|
|
53
|
+
"import": "./dist/theme-token-projection.js"
|
|
54
|
+
},
|
|
55
|
+
"./theme-token-projection/script": {
|
|
56
|
+
"types": "./dist/theme-token-projection-script.d.ts",
|
|
57
|
+
"import": "./dist/theme-token-projection-script.js"
|
|
58
|
+
},
|
|
51
59
|
"./catalogue": {
|
|
52
60
|
"types": "./dist/catalogue.d.ts",
|
|
53
61
|
"import": "./dist/catalogue.js"
|
|
@@ -95,7 +103,7 @@
|
|
|
95
103
|
"access": "public"
|
|
96
104
|
},
|
|
97
105
|
"scripts": {
|
|
98
|
-
"build": "tsc -p tsconfig.build.json",
|
|
106
|
+
"build": "tsc -p tsconfig.build.json && bun run token-projection:build",
|
|
99
107
|
"prepublishOnly": "bun run build",
|
|
100
108
|
"typecheck": "bun check -p tsconfig.json",
|
|
101
109
|
"fmt": "oxfmt .",
|
|
@@ -106,7 +114,9 @@
|
|
|
106
114
|
"vendor": "bun scripts/vendor-palettes.ts",
|
|
107
115
|
"catalogue:build": "bun scripts/build-catalogue.ts",
|
|
108
116
|
"catalogue:check": "bun scripts/build-catalogue.ts --check",
|
|
109
|
-
"
|
|
117
|
+
"token-projection:build": "bun scripts/build-theme-token-projection.ts",
|
|
118
|
+
"token-projection:check": "bun scripts/build-theme-token-projection.ts --check",
|
|
119
|
+
"verify": "bun run format:check && bun run typecheck && bun run lint && bun run catalogue:check && bun run test && bun run build && bun run token-projection:check"
|
|
110
120
|
},
|
|
111
121
|
"devDependencies": {
|
|
112
122
|
"@types/node": "^26.6.6",
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Serializable projections for token maps that have stable roles but predate one
|
|
3
|
+
* of the design system's derived token families.
|
|
4
|
+
*
|
|
5
|
+
* The rules are data rather than a callback because the same projection can run
|
|
6
|
+
* against a provider's live variant and against a stored theme during the
|
|
7
|
+
* pre-paint script. Explicit destination tokens always win.
|
|
8
|
+
*/
|
|
9
|
+
import { contrastRatio, oklchToHex, parseColor, repairContrast } from './oklch.js'
|
|
10
|
+
|
|
11
|
+
export type ThemeTokenContrastRule = Readonly<{
|
|
12
|
+
/** The custom property the generated foreground must read against. */
|
|
13
|
+
against: string
|
|
14
|
+
/** Minimum WCAG 2.1 contrast ratio for the generated value. */
|
|
15
|
+
minimum: number
|
|
16
|
+
}>
|
|
17
|
+
|
|
18
|
+
export type ThemeTokenProjectionRule = Readonly<{
|
|
19
|
+
/** Destination custom property; an explicit value is never replaced. */
|
|
20
|
+
target: string
|
|
21
|
+
/** Existing custom property to project when `target` is absent. */
|
|
22
|
+
source: string
|
|
23
|
+
/** Repair a generated destination when both source and surface are colours. */
|
|
24
|
+
contrast?: ThemeTokenContrastRule
|
|
25
|
+
}>
|
|
26
|
+
|
|
27
|
+
export type ThemeTokenProjection = readonly ThemeTokenProjectionRule[]
|
|
28
|
+
|
|
29
|
+
export type ThemeTokenProjectionFailure = Readonly<{
|
|
30
|
+
target: string
|
|
31
|
+
source: string
|
|
32
|
+
against: string
|
|
33
|
+
minimum: number
|
|
34
|
+
reason: 'unsupported_color' | 'contrast_unreachable'
|
|
35
|
+
}>
|
|
36
|
+
|
|
37
|
+
export type ThemeTokenProjectionResult = Readonly<{
|
|
38
|
+
tokens: Record<string, string>
|
|
39
|
+
failures: readonly ThemeTokenProjectionFailure[]
|
|
40
|
+
}>
|
|
41
|
+
|
|
42
|
+
const CUSTOM_PROPERTY = /^--[A-Za-z0-9_-]+$/
|
|
43
|
+
|
|
44
|
+
function validRule(rule: ThemeTokenProjectionRule): boolean {
|
|
45
|
+
if (!rule || typeof rule !== 'object' || Array.isArray(rule)) return false
|
|
46
|
+
const contrast = rule.contrast
|
|
47
|
+
return (
|
|
48
|
+
CUSTOM_PROPERTY.test(rule.target) &&
|
|
49
|
+
CUSTOM_PROPERTY.test(rule.source) &&
|
|
50
|
+
(contrast === undefined ||
|
|
51
|
+
(!!contrast &&
|
|
52
|
+
typeof contrast === 'object' &&
|
|
53
|
+
CUSTOM_PROPERTY.test(contrast.against) &&
|
|
54
|
+
Number.isFinite(contrast.minimum) &&
|
|
55
|
+
contrast.minimum > 1 &&
|
|
56
|
+
contrast.minimum <= 21))
|
|
57
|
+
)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function hasUnsupportedAlpha(value: string): boolean {
|
|
61
|
+
return /^oklch\([^)]*\/[^)]*\)$/i.test(value.trim())
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function repairGeneratedValue(
|
|
65
|
+
value: string,
|
|
66
|
+
against: string,
|
|
67
|
+
minimum: number
|
|
68
|
+
): { value: string; failure?: ThemeTokenProjectionFailure['reason'] } {
|
|
69
|
+
if (hasUnsupportedAlpha(value) || hasUnsupportedAlpha(against)) {
|
|
70
|
+
return { value, failure: 'unsupported_color' }
|
|
71
|
+
}
|
|
72
|
+
const foreground = parseColor(value)
|
|
73
|
+
const background = parseColor(against)
|
|
74
|
+
if (!foreground || !background) return { value, failure: 'unsupported_color' }
|
|
75
|
+
if (contrastRatio(foreground, background) >= minimum) return { value }
|
|
76
|
+
|
|
77
|
+
const repaired = repairContrast(foreground, background, minimum, 0.6)
|
|
78
|
+
if (!repaired.satisfied) return { value, failure: 'contrast_unreachable' }
|
|
79
|
+
|
|
80
|
+
const hex = oklchToHex(repaired.color)
|
|
81
|
+
const rounded = parseColor(hex)
|
|
82
|
+
return rounded && contrastRatio(rounded, background) >= minimum
|
|
83
|
+
? { value: hex }
|
|
84
|
+
: { value, failure: 'contrast_unreachable' }
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Add projected values for absent custom properties, leaving explicit values byte
|
|
89
|
+
* for byte intact. Rules run in order so one generated surface can anchor its text.
|
|
90
|
+
* Unknown or malformed rules and unparseable colors fail closed by skipping repair.
|
|
91
|
+
*/
|
|
92
|
+
export function projectThemeTokensDetailed(
|
|
93
|
+
tokens: Readonly<Record<string, string>>,
|
|
94
|
+
projection: ThemeTokenProjection
|
|
95
|
+
): ThemeTokenProjectionResult {
|
|
96
|
+
const projected = { ...tokens }
|
|
97
|
+
const failures: ThemeTokenProjectionFailure[] = []
|
|
98
|
+
|
|
99
|
+
for (const rule of Array.isArray(projection) ? projection : []) {
|
|
100
|
+
if (!validRule(rule) || Object.hasOwn(projected, rule.target)) continue
|
|
101
|
+
const value = projected[rule.source]
|
|
102
|
+
if (typeof value !== 'string') continue
|
|
103
|
+
if (!rule.contrast) {
|
|
104
|
+
projected[rule.target] = value
|
|
105
|
+
continue
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const against = projected[rule.contrast.against] ?? ''
|
|
109
|
+
const result = repairGeneratedValue(value, against, rule.contrast.minimum)
|
|
110
|
+
projected[rule.target] = result.value
|
|
111
|
+
if (result.failure) {
|
|
112
|
+
failures.push({
|
|
113
|
+
target: rule.target,
|
|
114
|
+
source: rule.source,
|
|
115
|
+
against: rule.contrast.against,
|
|
116
|
+
minimum: rule.contrast.minimum,
|
|
117
|
+
reason: result.failure,
|
|
118
|
+
})
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return { tokens: projected, failures }
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Project tokens while discarding diagnostics for callers that only paint them. */
|
|
126
|
+
export function projectThemeTokensCore(
|
|
127
|
+
tokens: Readonly<Record<string, string>>,
|
|
128
|
+
projection: ThemeTokenProjection
|
|
129
|
+
): Record<string, string> {
|
|
130
|
+
return projectThemeTokensDetailed(tokens, projection).tokens
|
|
131
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** Public runtime API for declarative custom-property projection. */
|
|
2
|
+
export {
|
|
3
|
+
projectThemeTokensCore as projectThemeTokens,
|
|
4
|
+
projectThemeTokensDetailed,
|
|
5
|
+
} from './theme-token-projection-core.js'
|
|
6
|
+
export type {
|
|
7
|
+
ThemeTokenContrastRule,
|
|
8
|
+
ThemeTokenProjection,
|
|
9
|
+
ThemeTokenProjectionFailure,
|
|
10
|
+
ThemeTokenProjectionResult,
|
|
11
|
+
ThemeTokenProjectionRule,
|
|
12
|
+
} from './theme-token-projection-core.js'
|