@link-foundation/ui-tokens 0.0.0 → 0.2.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.
package/README.md CHANGED
@@ -253,3 +253,64 @@ usage.
253
253
  Material 3 theme tokens may include `m3 md.sys.color.primary` metadata. The
254
254
  loader preserves this original source name as `token.m3` for reverse exporters;
255
255
  it does not participate in value typing or alias resolution.
256
+
257
+ ### Combining a theme with a host palette
258
+
259
+ The default theme selector gets its specificity from `[data-theme]`. Its
260
+ scheme, contrast and motion conditions are inside `:where()`. A host dark-mode
261
+ rule such as `:root:not([data-scheme='light'])` has higher specificity and can
262
+ override theme custom properties even when the theme stylesheet loads later.
263
+
264
+ Scope the host palette to the unthemed root and the host's own theme ID. This
265
+ keeps the fallback available when `data-theme` is absent or set to `app`.
266
+
267
+ ```css
268
+ :root:not([data-theme]),
269
+ :root[data-theme='app'] {
270
+ --color-surface: #f6f7f4;
271
+ }
272
+
273
+ @media (prefers-color-scheme: dark) {
274
+ :root:not([data-theme]):not([data-scheme='light']),
275
+ :root[data-theme='app']:not([data-scheme='light']) {
276
+ --color-surface: #141a18;
277
+ }
278
+ }
279
+ ```
280
+
281
+ If both stylesheets use unlayered declarations, you can keep the host rules
282
+ matching the themed root by wrapping the entire host selectors in `:where()`,
283
+ for example `:where(:root)` and `:where(:root:not([data-scheme='light']))`.
284
+ The default compiled selector then wins on specificity regardless of load order.
285
+
286
+ You can also put host defaults in a lower cascade layer. Unlayered theme rules
287
+ already outrank normal declarations in named layers. To give both stylesheets
288
+ an explicit order, declare the order before either layer is first used.
289
+
290
+ ```css
291
+ @layer app-defaults, theme;
292
+
293
+ @layer app-defaults {
294
+ :root {
295
+ --color-surface: #f6f7f4;
296
+ }
297
+
298
+ @media (prefers-color-scheme: dark) {
299
+ :root:not([data-scheme='light']) {
300
+ --color-surface: #141a18;
301
+ }
302
+ }
303
+ }
304
+ ```
305
+
306
+ ```js
307
+ const css = compileThemeCss(theme, { layer: 'theme' });
308
+ ```
309
+
310
+ `layer` wraps the entire stylesheet in one named `@layer` block, including all
311
+ palettes when compiling an array. It accepts an ASCII CSS identifier or a
312
+ dot-separated path such as `app.theme`. Invalid names throw `TypeError`.
313
+ When `layer` is omitted, the compiler emits unlayered CSS. Keep competing
314
+ host palette declarations inside `app-defaults` when you opt in because normal
315
+ unlayered declarations outrank both named layers. See the
316
+ [CSS cascade layer rules](https://www.w3.org/TR/css-cascade-5/#layer-ordering).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@link-foundation/ui-tokens",
3
- "version": "0.0.0",
3
+ "version": "0.2.0",
4
4
  "description": "Design tokens of the associative UI framework written in Links Notation, with inheritance, aliases and modes, exported to CSS custom properties, DTCG 2025.10 JSON, Swift and Kotlin",
5
5
  "type": "module",
6
6
  "exports": {
@@ -50,8 +50,8 @@
50
50
  "node": ">=20"
51
51
  },
52
52
  "dependencies": {
53
- "@link-foundation/ui-core": "^0.0.0",
54
- "@link-foundation/ui-style": "^0.0.0"
53
+ "@link-foundation/ui-core": "^0.1.0",
54
+ "@link-foundation/ui-style": "^0.1.1"
55
55
  },
56
56
  "peerDependencies": {
57
57
  "style-dictionary": ">=4.0.0"
@@ -115,21 +115,46 @@ const axis = (name, normal, active, feature, queryValue) => [
115
115
  // Each query nests separately: conjunctions of `not all` are not valid CSS media lists.
116
116
  const media = (css, queries) =>
117
117
  queries.reduceRight((body, query) => `@media ${query} {\n${body}\n}`, css);
118
+ const layerIdentifier = /^(?:-?[a-zA-Z_]|--)[\w-]*$/u;
119
+ const reservedLayerIdentifier =
120
+ /^(?:inherit|initial|revert|revert-layer|unset)$/iu;
118
121
 
119
- /** Scoped tokens for every explicit/system combination. No inherited dark residue. */
122
+ /** Compile a theme or palette pack, optionally inside a named cascade layer. */
120
123
  export function compileThemeCss(theme, options = {}) {
121
- if (Array.isArray(theme)) {
122
- return theme
123
- .map((entry) =>
124
- compileThemeCss(entry, {
125
- ...options,
126
- palettePack: true,
127
- selector:
128
- options.paletteSelector?.(entry.id) ?? attr('palette', entry.id),
129
- })
130
- )
131
- .join('\n');
124
+ const { layer } = options;
125
+ if (
126
+ layer !== undefined &&
127
+ (typeof layer !== 'string' ||
128
+ layer
129
+ .split('.')
130
+ .some(
131
+ (part) =>
132
+ !layerIdentifier.test(part) || reservedLayerIdentifier.test(part)
133
+ ))
134
+ ) {
135
+ throw new TypeError(
136
+ 'layer must be a CSS layer name with ASCII identifiers'
137
+ );
132
138
  }
139
+ const css = Array.isArray(theme)
140
+ ? theme
141
+ .map((entry) =>
142
+ compileTheme(entry, {
143
+ ...options,
144
+ palettePack: true,
145
+ selector:
146
+ options.paletteSelector?.(entry.id) ?? attr('palette', entry.id),
147
+ })
148
+ )
149
+ .join('\n')
150
+ : compileTheme(theme, options);
151
+ return layer === undefined || css === ''
152
+ ? css
153
+ : `@layer ${layer} {\n${css}\n}`;
154
+ }
155
+
156
+ /** Scoped tokens for every explicit/system combination. No inherited dark residue. */
157
+ function compileTheme(theme, options) {
133
158
  const root = options.selector ?? attr('theme', theme.id);
134
159
  const axes = [
135
160
  axis('scheme', 'light', 'dark', 'prefers-color-scheme', 'dark'),
@@ -53,6 +53,8 @@ export function ensureContrast(
53
53
  ): { color: string; adjusted: boolean; ratio: number };
54
54
  export interface ThemeCssOptions {
55
55
  channels?: boolean;
56
+ /** Wrap the stylesheet in a named CSS cascade layer, e.g. `theme` or `app.theme`. */
57
+ layer?: string;
56
58
  selector?: string;
57
59
  paletteSelector?: (palette: string) => string;
58
60
  transparency?: 'on' | 'off';