@handamade/psi-tokens 0.19.0 → 0.20.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 (64) hide show
  1. package/README.md +19 -0
  2. package/dist/cli/psi-theme.d.ts +2 -0
  3. package/dist/cli/psi-theme.js +56 -0
  4. package/dist/components/button.d.ts +7 -0
  5. package/dist/components/button.js +76 -0
  6. package/dist/components/card.d.ts +2 -0
  7. package/dist/components/card.js +7 -0
  8. package/dist/components/checkbox.d.ts +2 -0
  9. package/dist/components/checkbox.js +13 -0
  10. package/dist/components/control.d.ts +21 -0
  11. package/dist/components/control.js +59 -0
  12. package/dist/components/description-list.d.ts +8 -0
  13. package/dist/components/description-list.js +13 -0
  14. package/dist/components/dialog.d.ts +6 -0
  15. package/dist/components/dialog.js +13 -0
  16. package/dist/components/field.d.ts +3 -0
  17. package/dist/components/field.js +9 -0
  18. package/dist/components/input.d.ts +2 -0
  19. package/dist/components/input.js +25 -0
  20. package/dist/components/media.d.ts +3 -0
  21. package/dist/components/media.js +5 -0
  22. package/dist/components/menu.d.ts +7 -0
  23. package/dist/components/menu.js +19 -0
  24. package/dist/components/navbar.d.ts +3 -0
  25. package/dist/components/navbar.js +11 -0
  26. package/dist/components/panel.d.ts +5 -0
  27. package/dist/components/panel.js +10 -0
  28. package/dist/components/registry.d.ts +1 -0
  29. package/dist/components/registry.js +46 -0
  30. package/dist/components/select.d.ts +2 -0
  31. package/dist/components/select.js +38 -0
  32. package/dist/components/surface.d.ts +5 -0
  33. package/dist/components/surface.js +10 -0
  34. package/dist/components/switch.d.ts +2 -0
  35. package/dist/components/switch.js +8 -0
  36. package/dist/components/table.d.ts +12 -0
  37. package/dist/components/table.js +30 -0
  38. package/dist/components/tabs.d.ts +14 -0
  39. package/dist/components/tabs.js +26 -0
  40. package/dist/components/tag.d.ts +4 -0
  41. package/dist/components/tag.js +26 -0
  42. package/dist/components/toast.d.ts +19 -0
  43. package/dist/components/toast.js +28 -0
  44. package/dist/components/toolbar.d.ts +13 -0
  45. package/dist/components/toolbar.js +15 -0
  46. package/dist/components/tooltip.d.ts +2 -0
  47. package/dist/components/tooltip.js +7 -0
  48. package/dist/dsl/validator.d.ts +12 -0
  49. package/dist/dsl/validator.js +97 -0
  50. package/dist/emit/css.d.ts +30 -0
  51. package/dist/emit/css.js +120 -0
  52. package/dist/gamut.d.ts +4 -0
  53. package/dist/gamut.js +44 -0
  54. package/dist/scope-gate.d.ts +15 -0
  55. package/dist/scope-gate.js +87 -0
  56. package/dist/scope-map.json +2 -1
  57. package/dist/scopes.d.ts +6 -0
  58. package/dist/scopes.js +88 -0
  59. package/dist/theme/gate.d.ts +19 -0
  60. package/dist/theme/gate.js +44 -0
  61. package/dist/theme/index.d.ts +16 -0
  62. package/dist/theme/index.js +23 -0
  63. package/llms.txt +7 -0
  64. package/package.json +9 -1
package/dist/scopes.js ADDED
@@ -0,0 +1,88 @@
1
+ // D46 — token scope vocabulary. Scope entries on a token are either a
2
+ // property-group name (key of PROPERTY_GROUPS) or a concrete CSS property
3
+ // listed in one of the groups. The -bg/-fg/-border suffix convention on
4
+ // component-token keys is normative: SUFFIX_GROUPS is the shipped map.
5
+ // ── Property groups ──────────────────────────────────────────────────
6
+ export const PROPERTY_GROUPS = {
7
+ // background-image (D81): a glyph drawn with CSS gradients — the Select
8
+ // chevron, which a strict CSP forbids as a data: SVG — is foreground ink,
9
+ // the same role fill/stroke play on an SVG glyph.
10
+ text: ["color", "fill", "stroke", "caret-color", "text-decoration-color", "background-image"],
11
+ surface: ["background", "background-color", "accent-color"],
12
+ border: [
13
+ "border", "border-color", "border-top-color", "border-right-color",
14
+ "border-bottom-color", "border-left-color", "outline", "outline-color",
15
+ "box-shadow",
16
+ "border-top", "border-right", "border-bottom", "border-left",
17
+ "border-inline", "border-block",
18
+ // Logical longhands. The group already carried every physical longhand and
19
+ // both logical shorthands, so their absence was an omission rather than a
20
+ // rule: a vertical Tabs list binding --psi-tabs-list-border to
21
+ // border-inline-start is exactly the case the group exists to allow, and
22
+ // the logical form is the one that survives RTL. Found by the D46 gate
23
+ // itself during D67.
24
+ "border-inline-start", "border-inline-end",
25
+ "border-block-start", "border-block-end",
26
+ ],
27
+ gap: [
28
+ "gap", "row-gap", "column-gap",
29
+ "padding", "padding-top", "padding-right", "padding-bottom", "padding-left",
30
+ "padding-inline", "padding-block",
31
+ "padding-inline-start", "padding-inline-end", "padding-block-start", "padding-block-end",
32
+ "margin", "margin-top", "margin-right", "margin-bottom", "margin-left",
33
+ "margin-inline", "margin-block",
34
+ "margin-inline-start", "margin-inline-end", "margin-block-start", "margin-block-end",
35
+ "inset", "top", "right", "bottom", "left",
36
+ "scroll-margin", "scroll-padding", "translate",
37
+ // edge-distance uses: icon offsets (background-position) and viewport-inset sizing (max-width in calc) are gap-family intent
38
+ "background-position", "max-width",
39
+ ],
40
+ };
41
+ // ── Normative suffix groups ──────────────────────────────────────────
42
+ // Normative component-token suffix → property group (D46).
43
+ export const SUFFIX_GROUPS = {
44
+ fg: "text",
45
+ bg: "surface",
46
+ border: "border",
47
+ };
48
+ // ── Scale scopes ─────────────────────────────────────────────────────
49
+ // Scale-family scopes (D46 "gaps" family). Families not listed are unscoped.
50
+ export const SCALE_SCOPES = {
51
+ space: ["gap"],
52
+ };
53
+ // ── Internal lookup ──────────────────────────────────────────────────
54
+ const KNOWN_PROPERTIES = new Set(Object.values(PROPERTY_GROUPS).flat());
55
+ // ── Query functions ─────────────────────────────────────────────────
56
+ export function isKnownScopeEntry(entry) {
57
+ return entry in PROPERTY_GROUPS || KNOWN_PROPERTIES.has(entry);
58
+ }
59
+ // Expand scope entries (group names or property names) to a deduped
60
+ // property list. Throws on unknown entries (D48 posture).
61
+ export function expandScopes(scopes) {
62
+ const out = new Set();
63
+ for (const entry of scopes) {
64
+ if (entry in PROPERTY_GROUPS) {
65
+ for (const p of PROPERTY_GROUPS[entry])
66
+ out.add(p);
67
+ }
68
+ else if (KNOWN_PROPERTIES.has(entry)) {
69
+ out.add(entry);
70
+ }
71
+ else {
72
+ throw new Error(`unknown scope entry "${entry}" — not a known CSS property or declared group`);
73
+ }
74
+ }
75
+ return [...out];
76
+ }
77
+ // Property group a component-token key declares via its last bg/fg/border
78
+ // segment ("accent-bg-hover" → surface). Undefined for unsuffixed keys
79
+ // ("focus-ring", "radius", "backdrop") — those are not scope-checked.
80
+ export function keyGroup(key) {
81
+ const segments = key.split("-");
82
+ for (let i = segments.length - 1; i >= 0; i--) {
83
+ const group = SUFFIX_GROUPS[segments[i]];
84
+ if (group)
85
+ return group;
86
+ }
87
+ return undefined;
88
+ }
@@ -0,0 +1,19 @@
1
+ import type { Palette, SlotMap, ThemeDef } from "../dsl/types.js";
2
+ import { type ResolvedTheme } from "../dsl/resolver.js";
3
+ import { type ContrastResult } from "../contrast-matrix.js";
4
+ import { type ScopeViolation } from "../scope-gate.js";
5
+ /** Thrown when a theme fails the WCAG AA contrast gate or the D46 scope gate. */
6
+ export declare class ThemeGateError extends Error {
7
+ readonly theme: string;
8
+ readonly contrastFailures: readonly ContrastResult[];
9
+ readonly scopeViolations: readonly ScopeViolation[];
10
+ constructor(theme: string, contrastFailures: readonly ContrastResult[], scopeViolations: readonly ScopeViolation[]);
11
+ }
12
+ export interface GatedTheme {
13
+ resolved: ResolvedTheme;
14
+ /** Non-fatal gamut warnings. */
15
+ warnings: string[];
16
+ }
17
+ /** Validate, resolve and gate one theme. Throws ValidationError on a malformed
18
+ * definition and ThemeGateError on any contrast or scope failure. */
19
+ export declare function gateTheme(name: string, theme: ThemeDef, palette: Palette, slots: SlotMap, componentOverrides?: Record<string, string>): GatedTheme;
@@ -0,0 +1,44 @@
1
+ import { validate } from "../dsl/validator.js";
2
+ import { resolve } from "../dsl/resolver.js";
3
+ import { gamutWarnings } from "../gamut.js";
4
+ import { checkContrast, wcagAAPairs, componentLabelPairs } from "../contrast-matrix.js";
5
+ import { checkScopes, checkOverrideScopes } from "../scope-gate.js";
6
+ import { componentVars } from "../components/registry.js";
7
+ /** Thrown when a theme fails the WCAG AA contrast gate or the D46 scope gate. */
8
+ export class ThemeGateError extends Error {
9
+ theme;
10
+ contrastFailures;
11
+ scopeViolations;
12
+ constructor(theme, contrastFailures, scopeViolations) {
13
+ const parts = [];
14
+ if (contrastFailures.length > 0) {
15
+ parts.push(`${contrastFailures.length} contrast failures: ` +
16
+ contrastFailures.map((f) => `${f.fg} on ${f.bg} ${f.ratio} (need ${f.minRatio})`).join("; "));
17
+ }
18
+ if (scopeViolations.length > 0) {
19
+ parts.push(`${scopeViolations.length} scope violations: ` +
20
+ scopeViolations.map((v) => `--psi-${v.component}-${v.key} (${v.group}) binds ${v.token}`).join("; "));
21
+ }
22
+ super(`${theme} theme failed its gates — ${parts.join(" · ")}`);
23
+ this.theme = theme;
24
+ this.contrastFailures = contrastFailures;
25
+ this.scopeViolations = scopeViolations;
26
+ this.name = "ThemeGateError";
27
+ }
28
+ }
29
+ /** Validate, resolve and gate one theme. Throws ValidationError on a malformed
30
+ * definition and ThemeGateError on any contrast or scope failure. */
31
+ export function gateTheme(name, theme, palette, slots, componentOverrides = {}) {
32
+ validate(theme, slots);
33
+ const resolved = resolve(theme, palette, slots);
34
+ const warnings = gamutWarnings(resolved, theme, palette, slots);
35
+ const contrastFailures = checkContrast(resolved, [...wcagAAPairs, ...componentLabelPairs]).filter((r) => !r.pass);
36
+ const scopeViolations = [
37
+ ...checkScopes(componentVars, theme),
38
+ ...checkOverrideScopes(componentOverrides, componentVars, theme),
39
+ ];
40
+ if (contrastFailures.length > 0 || scopeViolations.length > 0) {
41
+ throw new ThemeGateError(name, contrastFailures, scopeViolations);
42
+ }
43
+ return { resolved, warnings };
44
+ }
@@ -0,0 +1,16 @@
1
+ import { type CustomerTheme } from "../themes/customers/index.js";
2
+ export { ThemeGateError } from "./gate.js";
3
+ export type { CustomerTheme, BrandFonts } from "../themes/customers/index.js";
4
+ export type { Palette, PaletteEntry, SlotMap, ThemeDef, TokenDef } from "../dsl/types.js";
5
+ export { token, set, delta, cap, slot, ref } from "../dsl/builders.js";
6
+ export interface ThemeBuild {
7
+ /** The theme's CSS, scoped to `[data-psi-theme="<name>"]`. Load it after
8
+ * `base.css`; apply it with the attribute on any element. */
9
+ css: string;
10
+ /** Non-fatal gamut warnings. */
11
+ warnings: string[];
12
+ }
13
+ /** Build one theme through the same gates as Psi's shipped themes: the
14
+ * definition is validated, then refused with a ThemeGateError if any WCAG AA
15
+ * contrast pair or D46 token scope fails. */
16
+ export declare function buildThemeCss(name: string, theme: CustomerTheme): ThemeBuild;
@@ -0,0 +1,23 @@
1
+ // D81 — @handamade/psi-tokens/theme: build a gated theme outside the Psi repo.
2
+ // A consumer keeps its brand's definition in its own repository; nothing
3
+ // client-named has to ship in this package.
4
+ import { assembleCustomerTheme } from "../themes/customers/index.js";
5
+ import { emitThemeCSS } from "../emit/css.js";
6
+ import { gateTheme } from "./gate.js";
7
+ export { ThemeGateError } from "./gate.js";
8
+ export { token, set, delta, cap, slot, ref } from "../dsl/builders.js";
9
+ /** Build one theme through the same gates as Psi's shipped themes: the
10
+ * definition is validated, then refused with a ThemeGateError if any WCAG AA
11
+ * contrast pair or D46 token scope fails. */
12
+ export function buildThemeCss(name, theme) {
13
+ if (name === "light" || name === "dark") {
14
+ throw new Error(`"${name}" is a built-in theme name; choose another`);
15
+ }
16
+ const def = assembleCustomerTheme(theme);
17
+ const { warnings } = gateTheme(name, def, theme.palette, theme.slots, theme.componentOverrides);
18
+ const css = emitThemeCSS(name, def, theme.palette, theme.slots, {
19
+ fonts: theme.fonts,
20
+ componentOverrides: theme.componentOverrides,
21
+ });
22
+ return { css, warnings };
23
+ }
package/llms.txt CHANGED
@@ -61,4 +61,11 @@ OKLCH formula-based design tokens. Code is the source of truth; all artifacts ge
61
61
  - Lower-level helpers are also exported for callers that need one step in isolation: `fnv1a`, `seededRandom`, `buildBrandPalette`, `hexFor`, `contrastOf`, `solveL`, `isBrandVector`, and the `BrandVector`/`ChromaWord`/`RadiusRung` types plus `CHROMA_WORDS`/`RADIUS_RUNGS` constants.
62
62
  - `isBrandVector` is the runtime guard for untrusted input (e.g. the `api/theme.ts` Vercel Function) — narrow before calling `deriveTheme`, never trust a client-supplied vector shape directly.
63
63
 
64
+ ## Building a theme outside this repo (`./theme`, D81)
65
+ - `import { buildThemeCss, ThemeGateError } from "@handamade/psi-tokens/theme"` — `buildThemeCss(name, definition)` returns `{ css, warnings }` for a definition shaped like `customers/<name>.ts` (`palette`, `slots`, optional `base: "light" | "dark"`, `overrides`, `fonts`, `componentOverrides`). It runs the same gates as Psi's own build, through the same code (`src/theme/gate.ts`), and throws `ThemeGateError` (`contrastFailures`, `scopeViolations`) instead of returning a failing theme. The names `light` and `dark` are refused.
66
+ - Use it to keep a client brand in the client's repository: nothing client-named has to ship in this public package.
67
+ - The CSS is scoped to `[data-psi-theme="<name>"]`, never `:root`, so a `base: "dark"` theme can style a sub-tree such as a dark sidebar.
68
+ - `psi-theme <definition-module> --name <name> [--out <file.css>]` is the same build as a bin, for a consumer's build script. The module default-exports the definition. Exit codes: 0 built · 1 refused by a gate · 2 usage error.
69
+ - The token builders (`token`, `set`, `delta`, `cap`, `slot`, `ref`) and the definition types are re-exported from `./theme` for writing `overrides`.
70
+
64
71
  Live MCP access to this data: see packages/mcp/llms.txt (hosted: https://psi.kurkin.de/mcp).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@handamade/psi-tokens",
3
- "version": "0.19.0",
3
+ "version": "0.20.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {
@@ -16,8 +16,15 @@
16
16
  "types": "./dist/generate/index.d.ts",
17
17
  "import": "./dist/generate/index.js"
18
18
  },
19
+ "./theme": {
20
+ "types": "./dist/theme/index.d.ts",
21
+ "import": "./dist/theme/index.js"
22
+ },
19
23
  "./guidance.json": "./dist/guidance.json"
20
24
  },
25
+ "bin": {
26
+ "psi-theme": "./dist/cli/psi-theme.js"
27
+ },
21
28
  "files": [
22
29
  "dist",
23
30
  "README.md",
@@ -27,6 +34,7 @@
27
34
  "culori": "^4.0.0"
28
35
  },
29
36
  "devDependencies": {
37
+ "@types/node": "^24.19.0",
30
38
  "tsx": "^4.19.0"
31
39
  },
32
40
  "scripts": {