@ruckc/truss-code 1.52.0 → 1.54.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/dist/theme.d.ts +1 -6
- package/dist/theme.js +9 -4
- package/package.json +1 -1
package/dist/theme.d.ts
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Turns the design tokens into an editor palette. Everything here is pure except `readTheme`, which reads the live tokens.
|
|
3
|
-
* The syntax colours come from the chart categorical palette (--trc-s1..8) and are nudged until they reach WCAG AA (4.5:1)
|
|
4
|
-
* on every background the text can sit on (editor, current line, selection), so any brand's tokens stay readable.
|
|
5
|
-
*/
|
|
6
1
|
export type RGBA = [number, number, number, number];
|
|
7
2
|
export type Mode = 'light' | 'dark';
|
|
8
3
|
export type Role = 'keyword' | 'string' | 'number' | 'type' | 'function' | 'variable' | 'constant' | 'operator' | 'tag' | 'attribute' | 'regexp' | 'comment' | 'invalid' | 'delimiter' | 'meta';
|
|
@@ -76,7 +71,7 @@ export interface Theme {
|
|
|
76
71
|
palette: Palette;
|
|
77
72
|
metrics: Metrics;
|
|
78
73
|
}
|
|
79
|
-
/** The theme in effect at `el`: nearest `data-theme`, else the OS preference; tokens and density as computed there. */
|
|
74
|
+
/** The theme in effect at `el`: nearest `data-theme`, else the OS preference; tokens (any CSS colour syntax) and density as computed there. */
|
|
80
75
|
export declare function readTheme(el: Element): Theme;
|
|
81
76
|
/** Calls `cb` whenever the theme or density can have changed (`data-theme`, `data-truss-theme`, `data-density`, class or style on any ancestor, or the OS scheme). Returns a stop function. */
|
|
82
77
|
export declare function watchTheme(el: Element, cb: () => void): () => void;
|
package/dist/theme.js
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turns the design tokens into an editor palette. Everything here is pure except `readTheme`, which reads the live tokens.
|
|
3
|
+
* The syntax colours come from the chart categorical palette (--trc-s1..8) and are nudged until they reach WCAG AA (4.5:1)
|
|
4
|
+
* on every background the text can sit on (editor, current line, selection), so any brand's tokens stay readable.
|
|
5
|
+
*/
|
|
6
|
+
import { readColorTokens } from '@ruckc/truss-ui';
|
|
1
7
|
export const MIN_CONTRAST = 4.5;
|
|
2
8
|
const clamp = (n) => Math.max(0, Math.min(255, Math.round(n)));
|
|
3
9
|
/** Parses `#rgb`, `#rgba`, `#rrggbb`, `#rrggbbaa`, `rgb()` and `rgba()` (comma or space syntax). Anything else is `null`. */
|
|
@@ -194,14 +200,13 @@ export function fontMetrics(textMd, leading, family) {
|
|
|
194
200
|
const fontSize = size > 0 ? size : 14;
|
|
195
201
|
return { fontSize, lineHeight: Math.round(fontSize * (ratio > 0 ? ratio : 1.5)), fontFamily: family?.trim() || "ui-monospace, 'SF Mono', Menlo, monospace" };
|
|
196
202
|
}
|
|
197
|
-
/** The theme in effect at `el`: nearest `data-theme`, else the OS preference; tokens and density as computed there. */
|
|
203
|
+
/** The theme in effect at `el`: nearest `data-theme`, else the OS preference; tokens (any CSS colour syntax) and density as computed there. */
|
|
198
204
|
export function readTheme(el) {
|
|
199
205
|
const forced = el.closest('[data-theme]')?.getAttribute('data-theme');
|
|
200
206
|
const mode = forced === 'dark' || forced === 'light' ? forced : matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
|
201
207
|
const cs = getComputedStyle(el);
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
tokens[name] = cs.getPropertyValue(name).trim() || undefined;
|
|
208
|
+
// resolved by the browser at `el` to #hex / rgba(), so a theme may write its tokens in any colour syntax (oklch(), light-dark()…)
|
|
209
|
+
const tokens = readColorTokens(el, TOKEN_VARS);
|
|
205
210
|
return {
|
|
206
211
|
mode,
|
|
207
212
|
palette: buildPalette(tokens, mode),
|
package/package.json
CHANGED