@asafarim/design-tokens 0.1.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/CHANGELOG.md +13 -0
- package/LICENSE +21 -0
- package/README.md +139 -0
- package/dist/build/index.cjs +166 -0
- package/dist/build/index.cjs.map +1 -0
- package/dist/build/index.d.cts +30 -0
- package/dist/build/index.d.ts +30 -0
- package/dist/build/index.js +135 -0
- package/dist/build/index.js.map +1 -0
- package/dist/css/index.css +8 -0
- package/dist/css/tokens.base.css +121 -0
- package/dist/css/tokens.dark.css +50 -0
- package/dist/css/tokens.density.comfortable.css +6 -0
- package/dist/css/tokens.density.compact.css +6 -0
- package/dist/css/tokens.high-contrast.css +50 -0
- package/dist/css/tokens.light.css +50 -0
- package/dist/css/tokens.rtl.css +3 -0
- package/dist/css/tokens.utilities.css +16 -0
- package/dist/index.cjs +1042 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1118 -0
- package/dist/index.d.ts +1118 -0
- package/dist/index.js +1015 -0
- package/dist/index.js.map +1 -0
- package/package.json +75 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `@asafarim/design-tokens` will be documented in this file.
|
|
4
|
+
|
|
5
|
+
This project follows [Semantic Versioning](https://semver.org/).
|
|
6
|
+
|
|
7
|
+
## 0.1.0
|
|
8
|
+
|
|
9
|
+
- Initial release.
|
|
10
|
+
- Added token model with strongly typed token tree.
|
|
11
|
+
- Added CSS variable output (`--asm-*`) for base + light/dark/high-contrast + density + RTL.
|
|
12
|
+
- Added theme helpers (`applyThemeToElement`, `prefersColorScheme`).
|
|
13
|
+
- Added build helpers (`toCssVars`, `toJson`, `validateTokens`).
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 ASafariM
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
# @asafarim/design-tokens
|
|
2
|
+
|
|
3
|
+
A production-grade design-tokens package for the ASafariM ecosystem.
|
|
4
|
+
|
|
5
|
+
This package provides:
|
|
6
|
+
|
|
7
|
+
- CSS variables (runtime theming) for:
|
|
8
|
+
- light / dark / high-contrast
|
|
9
|
+
- density (compact / comfortable)
|
|
10
|
+
- RTL support (logical property guidance)
|
|
11
|
+
- TypeScript exports for strongly-typed token consumption
|
|
12
|
+
- Programmatic theming helpers (SSR-safe)
|
|
13
|
+
- Validation utilities for safe token evolution
|
|
14
|
+
|
|
15
|
+
## Why design tokens
|
|
16
|
+
|
|
17
|
+
Design tokens are the single source of truth for design decisions (color, typography, spacing, motion…).
|
|
18
|
+
|
|
19
|
+
- **Consistency** across apps (React/Angular/Vue/vanilla)
|
|
20
|
+
- **Themeability** at runtime via CSS variables
|
|
21
|
+
- **Governance**: controlled evolution with semantic versioning
|
|
22
|
+
|
|
23
|
+
## Install
|
|
24
|
+
|
|
25
|
+
From the monorepo root (pnpm workspaces):
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pnpm -C packages/design-tokens build
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
In a consuming package/app:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pnpm add @asafarim/design-tokens
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## CSS usage (any framework)
|
|
38
|
+
|
|
39
|
+
Import the CSS entrypoint once at app startup:
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import "@asafarim/design-tokens/css";
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
This ships a layered CSS setup:
|
|
46
|
+
|
|
47
|
+
1. `:root` base tokens (spacing, typography, motion, radii, z-index…)
|
|
48
|
+
2. `[data-theme]` theme tokens (colors, surfaces)
|
|
49
|
+
3. `[data-density]` density tokens
|
|
50
|
+
4. `[dir="rtl"]` RTL helpers
|
|
51
|
+
|
|
52
|
+
### Playground screenshots
|
|
53
|
+
|
|
54
|
+

|
|
55
|
+
|
|
56
|
+

|
|
57
|
+
|
|
58
|
+
### Theme switching
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
document.documentElement.dataset.theme = "light";
|
|
62
|
+
// or
|
|
63
|
+
document.documentElement.dataset.theme = "dark";
|
|
64
|
+
// or
|
|
65
|
+
document.documentElement.dataset.contrast = "high";
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Density switching
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
document.documentElement.dataset.density = "compact";
|
|
72
|
+
// or
|
|
73
|
+
document.documentElement.dataset.density = "comfortable";
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## TypeScript usage
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
import { tokens, themes, applyThemeToElement } from "@asafarim/design-tokens";
|
|
80
|
+
|
|
81
|
+
console.log(tokens.color.brand.primary500.value); // "#3A5AFE"
|
|
82
|
+
|
|
83
|
+
applyThemeToElement(document.documentElement, themes.light);
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Accessing CSS variable names
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
import { cssVarNames } from "@asafarim/design-tokens";
|
|
90
|
+
|
|
91
|
+
// dot-path -> CSS var
|
|
92
|
+
console.log(cssVarNames["color.brand.primary500"]); // "--asm-color-brand-primary-500"
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## SSR safety
|
|
96
|
+
|
|
97
|
+
- `prefersColorScheme()` will not crash when `window` is unavailable.
|
|
98
|
+
- `applyThemeToElement()` can be used in tests or SSR hydration flows.
|
|
99
|
+
|
|
100
|
+
## Client overrides (branding)
|
|
101
|
+
|
|
102
|
+
You can apply partial overrides without forking the package:
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
import { applyThemeToElement, themes } from "@asafarim/design-tokens";
|
|
106
|
+
|
|
107
|
+
applyThemeToElement(document.documentElement, themes.light, {
|
|
108
|
+
overrides: {
|
|
109
|
+
"--asm-color-brand-primary-500": "#00A3FF"
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## High-contrast + reduced motion guidance
|
|
115
|
+
|
|
116
|
+
- High contrast is enabled using `[data-contrast="high"]`.
|
|
117
|
+
- Reduced motion is handled via `@media (prefers-reduced-motion: reduce)` and the motion preset tokens.
|
|
118
|
+
|
|
119
|
+
## Contribution rules (token governance)
|
|
120
|
+
|
|
121
|
+
- **Never rename or delete** existing semantic token keys in a minor/patch release.
|
|
122
|
+
- **Prefer adding** new semantic tokens, deprecating old ones with `meta.deprecated`.
|
|
123
|
+
- When changing a value of an existing token:
|
|
124
|
+
- Patch: only if it’s a clear bug fix (e.g., invalid color)
|
|
125
|
+
- Minor: safe visual adjustments
|
|
126
|
+
- Major: visual/systemic shifts
|
|
127
|
+
|
|
128
|
+
Run validation before publishing:
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
pnpm -C packages/design-tokens build
|
|
132
|
+
node -e "import('@asafarim/design-tokens').then(m => console.log(m.validateTokens()))"
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Versioning rules
|
|
136
|
+
|
|
137
|
+
- **PATCH**: bug fixes, documentation, token metadata
|
|
138
|
+
- **MINOR**: additive tokens, new themes, non-breaking build changes
|
|
139
|
+
- **MAJOR**: renames/removals, behavior changes in theming helpers, breaking token semantics
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/build/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
toCssVarNames: () => toCssVarNames,
|
|
24
|
+
toCssVars: () => toCssVars,
|
|
25
|
+
toJson: () => toJson,
|
|
26
|
+
tokenPathToCssVarName: () => tokenPathToCssVarName,
|
|
27
|
+
validateTokens: () => validateTokens
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(index_exports);
|
|
30
|
+
|
|
31
|
+
// src/core/utils.ts
|
|
32
|
+
function isTokenLeaf(value) {
|
|
33
|
+
return typeof value === "object" && value !== null && "value" in value && "description" in value && (typeof value.value === "string" || typeof value.value === "number") && typeof value.description === "string";
|
|
34
|
+
}
|
|
35
|
+
function toKebabCase(input) {
|
|
36
|
+
return input.replace(/([a-z0-9])([A-Z])/g, "$1-$2").replace(/[_\s]+/g, "-").toLowerCase();
|
|
37
|
+
}
|
|
38
|
+
function walkTokenTree(tree, visitor, path = []) {
|
|
39
|
+
for (const [key, value] of Object.entries(tree)) {
|
|
40
|
+
const nextPath = [...path, key];
|
|
41
|
+
if (isTokenLeaf(value)) {
|
|
42
|
+
visitor({ path: nextPath, leaf: value });
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (typeof value === "object" && value !== null) {
|
|
46
|
+
walkTokenTree(value, visitor, nextPath);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// src/build/toCssVars.ts
|
|
52
|
+
function tokenPathToCssVarName(path) {
|
|
53
|
+
const kebabParts = path.map(toKebabCase);
|
|
54
|
+
return `--asm-${kebabParts.join("-")}`;
|
|
55
|
+
}
|
|
56
|
+
function toCssVars(tree) {
|
|
57
|
+
const out = {};
|
|
58
|
+
walkTokenTree(tree, ({ path, leaf }) => {
|
|
59
|
+
const cssVar = tokenPathToCssVarName(path);
|
|
60
|
+
out[cssVar] = String(leaf.value);
|
|
61
|
+
});
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
function toCssVarNames(tree) {
|
|
65
|
+
const out = {};
|
|
66
|
+
walkTokenTree(tree, ({ path }) => {
|
|
67
|
+
const dotPath = path.join(".");
|
|
68
|
+
out[dotPath] = tokenPathToCssVarName(path);
|
|
69
|
+
});
|
|
70
|
+
return out;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// src/build/toJson.ts
|
|
74
|
+
function toJson(tree) {
|
|
75
|
+
const out = {};
|
|
76
|
+
walkTokenTree(tree, ({ path, leaf }) => {
|
|
77
|
+
const key = path.join(".");
|
|
78
|
+
if (leaf.meta) {
|
|
79
|
+
out[key] = { value: leaf.value, description: leaf.description, meta: leaf.meta };
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
out[key] = { value: leaf.value, description: leaf.description };
|
|
83
|
+
});
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// src/build/validate.ts
|
|
88
|
+
var HEX_COLOR = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/;
|
|
89
|
+
function validateTokens(args) {
|
|
90
|
+
const errors = [];
|
|
91
|
+
const warnings = [];
|
|
92
|
+
const seenDotKeys = /* @__PURE__ */ new Set();
|
|
93
|
+
const seenCssVars = /* @__PURE__ */ new Set();
|
|
94
|
+
walkTokenTree(args.tokens, ({ path, leaf }) => {
|
|
95
|
+
const dotKey = path.join(".");
|
|
96
|
+
if (seenDotKeys.has(dotKey)) {
|
|
97
|
+
errors.push(`Duplicate token key: ${dotKey}`);
|
|
98
|
+
} else {
|
|
99
|
+
seenDotKeys.add(dotKey);
|
|
100
|
+
}
|
|
101
|
+
if (!leaf.description || leaf.description.trim().length === 0) {
|
|
102
|
+
errors.push(`Missing description for token: ${dotKey}`);
|
|
103
|
+
}
|
|
104
|
+
const cssVar = tokenPathToCssVarName(path);
|
|
105
|
+
if (!cssVar.startsWith("--asm-")) {
|
|
106
|
+
errors.push(`Invalid CSS var prefix (must be --asm-): ${cssVar}`);
|
|
107
|
+
}
|
|
108
|
+
if (seenCssVars.has(cssVar)) {
|
|
109
|
+
errors.push(`Duplicate CSS var name generated: ${cssVar} (from ${dotKey})`);
|
|
110
|
+
} else {
|
|
111
|
+
seenCssVars.add(cssVar);
|
|
112
|
+
}
|
|
113
|
+
const v = leaf.value;
|
|
114
|
+
if (typeof v === "string" && v.startsWith("#") && !HEX_COLOR.test(v)) {
|
|
115
|
+
errors.push(`Invalid hex color for ${dotKey}: ${v}`);
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
const requiredThemeVars = [
|
|
119
|
+
"--asm-color-bg",
|
|
120
|
+
"--asm-color-surface",
|
|
121
|
+
"--asm-color-text",
|
|
122
|
+
"--asm-color-border",
|
|
123
|
+
"--asm-color-button-primary-bg",
|
|
124
|
+
"--asm-color-input-border-focus",
|
|
125
|
+
"--asm-color-focus-ring",
|
|
126
|
+
"--asm-color-overlay-scrim"
|
|
127
|
+
];
|
|
128
|
+
const themeNames = new Set(args.themes.map((t) => t.name));
|
|
129
|
+
for (const name of ["light", "dark", "high-contrast"]) {
|
|
130
|
+
if (!themeNames.has(name)) {
|
|
131
|
+
errors.push(`Missing required theme: ${name}`);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
for (const theme of args.themes) {
|
|
135
|
+
if (theme.name !== "light" && theme.name !== "dark" && theme.name !== "high-contrast") {
|
|
136
|
+
warnings.push(`Theme has non-standard name: ${theme.name}`);
|
|
137
|
+
}
|
|
138
|
+
for (const varName of requiredThemeVars) {
|
|
139
|
+
if (!(varName in theme.tokens)) {
|
|
140
|
+
errors.push(`Theme '${theme.name}' missing required token override: ${varName}`);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
for (const [key, value] of Object.entries(theme.tokens)) {
|
|
144
|
+
if (!key.startsWith("--asm-")) {
|
|
145
|
+
errors.push(`Theme '${theme.name}' contains non --asm- var: ${key}`);
|
|
146
|
+
}
|
|
147
|
+
if (typeof value === "string" && value.startsWith("#") && !HEX_COLOR.test(value)) {
|
|
148
|
+
errors.push(`Theme '${theme.name}' invalid hex for ${key}: ${value}`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return {
|
|
153
|
+
ok: errors.length === 0,
|
|
154
|
+
errors,
|
|
155
|
+
warnings
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
159
|
+
0 && (module.exports = {
|
|
160
|
+
toCssVarNames,
|
|
161
|
+
toCssVars,
|
|
162
|
+
toJson,
|
|
163
|
+
tokenPathToCssVarName,
|
|
164
|
+
validateTokens
|
|
165
|
+
});
|
|
166
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/build/index.ts","../../src/core/utils.ts","../../src/build/toCssVars.ts","../../src/build/toJson.ts","../../src/build/validate.ts"],"sourcesContent":["export { toCssVars, toCssVarNames, tokenPathToCssVarName } from \"./toCssVars\";\r\nexport { toJson } from \"./toJson\";\r\nexport { validateTokens } from \"./validate\";\r\nexport type { CssVarDict } from \"./toCssVars\";\r\nexport type { FlatTokenJson } from \"./toJson\";\r\n","import type { TokenLeaf, TokenTree } from \"./types\";\r\n\r\nexport function isTokenLeaf(value: unknown): value is TokenLeaf {\r\n return (\r\n typeof value === \"object\" &&\r\n value !== null &&\r\n \"value\" in value &&\r\n \"description\" in value &&\r\n (typeof (value as any).value === \"string\" || typeof (value as any).value === \"number\") &&\r\n typeof (value as any).description === \"string\"\r\n );\r\n}\r\n\r\nexport function toKebabCase(input: string): string {\r\n return input\r\n .replace(/([a-z0-9])([A-Z])/g, \"$1-$2\")\r\n .replace(/[_\\s]+/g, \"-\")\r\n .toLowerCase();\r\n}\r\n\r\nexport function joinPath(path: string[], sep = \".\"): string {\r\n return path.filter(Boolean).join(sep);\r\n}\r\n\r\nexport function walkTokenTree(\r\n tree: TokenTree,\r\n visitor: (args: { path: string[]; leaf: TokenLeaf }) => void,\r\n path: string[] = []\r\n) {\r\n for (const [key, value] of Object.entries(tree)) {\r\n const nextPath = [...path, key];\r\n\r\n if (isTokenLeaf(value)) {\r\n visitor({ path: nextPath, leaf: value });\r\n continue;\r\n }\r\n\r\n if (typeof value === \"object\" && value !== null) {\r\n walkTokenTree(value as TokenTree, visitor, nextPath);\r\n }\r\n }\r\n}\r\n","import type { TokenTree } from \"../core/types\";\r\nimport { toKebabCase, walkTokenTree } from \"../core/utils\";\r\n\r\nexport type CssVarDict = Record<string, string>;\r\n\r\nexport function tokenPathToCssVarName(path: string[]): string {\r\n const kebabParts = path.map(toKebabCase);\r\n return `--asm-${kebabParts.join(\"-\")}`;\r\n}\r\n\r\nexport function toCssVars(tree: TokenTree): CssVarDict {\r\n const out: CssVarDict = {};\r\n\r\n walkTokenTree(tree, ({ path, leaf }) => {\r\n const cssVar = tokenPathToCssVarName(path);\r\n out[cssVar] = String(leaf.value);\r\n });\r\n\r\n return out;\r\n}\r\n\r\nexport function toCssVarNames(tree: TokenTree): Record<string, string> {\r\n const out: Record<string, string> = {};\r\n\r\n walkTokenTree(tree, ({ path }) => {\r\n const dotPath = path.join(\".\");\r\n out[dotPath] = tokenPathToCssVarName(path);\r\n });\r\n\r\n return out;\r\n}\r\n","import type { TokenTree } from \"../core/types\";\r\nimport { walkTokenTree } from \"../core/utils\";\r\n\r\nexport type FlatTokenJson = Record<string, { value: string | number; description: string; meta?: Record<string, unknown> }>;\r\n\r\nexport function toJson(tree: TokenTree): FlatTokenJson {\r\n const out: FlatTokenJson = {};\r\n\r\n walkTokenTree(tree, ({ path, leaf }) => {\r\n const key = path.join(\".\");\r\n if (leaf.meta) {\r\n out[key] = { value: leaf.value, description: leaf.description, meta: leaf.meta };\r\n return;\r\n }\r\n\r\n out[key] = { value: leaf.value, description: leaf.description };\r\n });\r\n\r\n return out;\r\n}\r\n","import type { ThemeDefinition, TokenTree, ValidateResult } from \"../core/types\";\r\nimport { walkTokenTree } from \"../core/utils\";\r\nimport { tokenPathToCssVarName } from \"./toCssVars\";\r\n\r\nconst HEX_COLOR = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/;\r\n\r\nexport function validateTokens(args: {\r\n tokens: TokenTree;\r\n themes: ThemeDefinition[];\r\n}): ValidateResult {\r\n const errors: string[] = [];\r\n const warnings: string[] = [];\r\n\r\n const seenDotKeys = new Set<string>();\r\n const seenCssVars = new Set<string>();\r\n\r\n walkTokenTree(args.tokens, ({ path, leaf }) => {\r\n const dotKey = path.join(\".\");\r\n if (seenDotKeys.has(dotKey)) {\r\n errors.push(`Duplicate token key: ${dotKey}`);\r\n } else {\r\n seenDotKeys.add(dotKey);\r\n }\r\n\r\n if (!leaf.description || leaf.description.trim().length === 0) {\r\n errors.push(`Missing description for token: ${dotKey}`);\r\n }\r\n\r\n const cssVar = tokenPathToCssVarName(path);\r\n if (!cssVar.startsWith(\"--asm-\")) {\r\n errors.push(`Invalid CSS var prefix (must be --asm-): ${cssVar}`);\r\n }\r\n\r\n if (seenCssVars.has(cssVar)) {\r\n errors.push(`Duplicate CSS var name generated: ${cssVar} (from ${dotKey})`);\r\n } else {\r\n seenCssVars.add(cssVar);\r\n }\r\n\r\n const v = leaf.value;\r\n if (typeof v === \"string\" && v.startsWith(\"#\") && !HEX_COLOR.test(v)) {\r\n errors.push(`Invalid hex color for ${dotKey}: ${v}`);\r\n }\r\n });\r\n\r\n const requiredThemeVars = [\r\n \"--asm-color-bg\",\r\n \"--asm-color-surface\",\r\n \"--asm-color-text\",\r\n \"--asm-color-border\",\r\n \"--asm-color-button-primary-bg\",\r\n \"--asm-color-input-border-focus\",\r\n \"--asm-color-focus-ring\",\r\n \"--asm-color-overlay-scrim\"\r\n ];\r\n\r\n const themeNames = new Set(args.themes.map(t => t.name));\r\n for (const name of [\"light\", \"dark\", \"high-contrast\"] as const) {\r\n if (!themeNames.has(name)) {\r\n errors.push(`Missing required theme: ${name}`);\r\n }\r\n }\r\n\r\n for (const theme of args.themes) {\r\n if (theme.name !== \"light\" && theme.name !== \"dark\" && theme.name !== \"high-contrast\") {\r\n warnings.push(`Theme has non-standard name: ${theme.name}`);\r\n }\r\n\r\n for (const varName of requiredThemeVars) {\r\n if (!(varName in theme.tokens)) {\r\n errors.push(`Theme '${theme.name}' missing required token override: ${varName}`);\r\n }\r\n }\r\n\r\n for (const [key, value] of Object.entries(theme.tokens)) {\r\n if (!key.startsWith(\"--asm-\")) {\r\n errors.push(`Theme '${theme.name}' contains non --asm- var: ${key}`);\r\n }\r\n if (typeof value === \"string\" && value.startsWith(\"#\") && !HEX_COLOR.test(value)) {\r\n errors.push(`Theme '${theme.name}' invalid hex for ${key}: ${value}`);\r\n }\r\n }\r\n }\r\n\r\n return {\r\n ok: errors.length === 0,\r\n errors,\r\n warnings\r\n };\r\n}\r\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEO,SAAS,YAAY,OAAoC;AAC9D,SACE,OAAO,UAAU,YACjB,UAAU,QACV,WAAW,SACX,iBAAiB,UAChB,OAAQ,MAAc,UAAU,YAAY,OAAQ,MAAc,UAAU,aAC7E,OAAQ,MAAc,gBAAgB;AAE1C;AAEO,SAAS,YAAY,OAAuB;AACjD,SAAO,MACJ,QAAQ,sBAAsB,OAAO,EACrC,QAAQ,WAAW,GAAG,EACtB,YAAY;AACjB;AAMO,SAAS,cACd,MACA,SACA,OAAiB,CAAC,GAClB;AACA,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,IAAI,GAAG;AAC/C,UAAM,WAAW,CAAC,GAAG,MAAM,GAAG;AAE9B,QAAI,YAAY,KAAK,GAAG;AACtB,cAAQ,EAAE,MAAM,UAAU,MAAM,MAAM,CAAC;AACvC;AAAA,IACF;AAEA,QAAI,OAAO,UAAU,YAAY,UAAU,MAAM;AAC/C,oBAAc,OAAoB,SAAS,QAAQ;AAAA,IACrD;AAAA,EACF;AACF;;;ACpCO,SAAS,sBAAsB,MAAwB;AAC5D,QAAM,aAAa,KAA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,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,MAAM,MAAM,GAAG;AACvD,UAAI,CAAC,IAAI,WAAW,QAAQ,GAAG;AAC7B,eAAO,KAAK,UAAU,MAAM,IAAI,8BAA8B,GAAG,EAAE;AAAA,MACrE;AACA,UAAI,OAAO,UAAU,YAAY,MAAM,WAAW,GAAG,KAAK,CAAC,UAAU,KAAK,KAAK,GAAG;AAChF,eAAO,KAAK,UAAU,MAAM,IAAI,qBAAqB,GAAG,KAAK,KAAK,EAAE;AAAA,MACtE;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AAAA,IACL,IAAI,OAAO,WAAW;AAAA,IACtB;AAAA,IACA;AAAA,EACF;AACF;","names":[]}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
type ThemeName = "light" | "dark" | "high-contrast";
|
|
2
|
+
type ThemeDefinition = {
|
|
3
|
+
name: ThemeName;
|
|
4
|
+
tokens: Record<string, string | number>;
|
|
5
|
+
};
|
|
6
|
+
type TokenTree = Record<string, unknown>;
|
|
7
|
+
type ValidateResult = {
|
|
8
|
+
ok: boolean;
|
|
9
|
+
errors: string[];
|
|
10
|
+
warnings: string[];
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
type CssVarDict = Record<string, string>;
|
|
14
|
+
declare function tokenPathToCssVarName(path: string[]): string;
|
|
15
|
+
declare function toCssVars(tree: TokenTree): CssVarDict;
|
|
16
|
+
declare function toCssVarNames(tree: TokenTree): Record<string, string>;
|
|
17
|
+
|
|
18
|
+
type FlatTokenJson = Record<string, {
|
|
19
|
+
value: string | number;
|
|
20
|
+
description: string;
|
|
21
|
+
meta?: Record<string, unknown>;
|
|
22
|
+
}>;
|
|
23
|
+
declare function toJson(tree: TokenTree): FlatTokenJson;
|
|
24
|
+
|
|
25
|
+
declare function validateTokens(args: {
|
|
26
|
+
tokens: TokenTree;
|
|
27
|
+
themes: ThemeDefinition[];
|
|
28
|
+
}): ValidateResult;
|
|
29
|
+
|
|
30
|
+
export { type CssVarDict, type FlatTokenJson, toCssVarNames, toCssVars, toJson, tokenPathToCssVarName, validateTokens };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
type ThemeName = "light" | "dark" | "high-contrast";
|
|
2
|
+
type ThemeDefinition = {
|
|
3
|
+
name: ThemeName;
|
|
4
|
+
tokens: Record<string, string | number>;
|
|
5
|
+
};
|
|
6
|
+
type TokenTree = Record<string, unknown>;
|
|
7
|
+
type ValidateResult = {
|
|
8
|
+
ok: boolean;
|
|
9
|
+
errors: string[];
|
|
10
|
+
warnings: string[];
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
type CssVarDict = Record<string, string>;
|
|
14
|
+
declare function tokenPathToCssVarName(path: string[]): string;
|
|
15
|
+
declare function toCssVars(tree: TokenTree): CssVarDict;
|
|
16
|
+
declare function toCssVarNames(tree: TokenTree): Record<string, string>;
|
|
17
|
+
|
|
18
|
+
type FlatTokenJson = Record<string, {
|
|
19
|
+
value: string | number;
|
|
20
|
+
description: string;
|
|
21
|
+
meta?: Record<string, unknown>;
|
|
22
|
+
}>;
|
|
23
|
+
declare function toJson(tree: TokenTree): FlatTokenJson;
|
|
24
|
+
|
|
25
|
+
declare function validateTokens(args: {
|
|
26
|
+
tokens: TokenTree;
|
|
27
|
+
themes: ThemeDefinition[];
|
|
28
|
+
}): ValidateResult;
|
|
29
|
+
|
|
30
|
+
export { type CssVarDict, type FlatTokenJson, toCssVarNames, toCssVars, toJson, tokenPathToCssVarName, validateTokens };
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// src/core/utils.ts
|
|
2
|
+
function isTokenLeaf(value) {
|
|
3
|
+
return typeof value === "object" && value !== null && "value" in value && "description" in value && (typeof value.value === "string" || typeof value.value === "number") && typeof value.description === "string";
|
|
4
|
+
}
|
|
5
|
+
function toKebabCase(input) {
|
|
6
|
+
return input.replace(/([a-z0-9])([A-Z])/g, "$1-$2").replace(/[_\s]+/g, "-").toLowerCase();
|
|
7
|
+
}
|
|
8
|
+
function walkTokenTree(tree, visitor, path = []) {
|
|
9
|
+
for (const [key, value] of Object.entries(tree)) {
|
|
10
|
+
const nextPath = [...path, key];
|
|
11
|
+
if (isTokenLeaf(value)) {
|
|
12
|
+
visitor({ path: nextPath, leaf: value });
|
|
13
|
+
continue;
|
|
14
|
+
}
|
|
15
|
+
if (typeof value === "object" && value !== null) {
|
|
16
|
+
walkTokenTree(value, visitor, nextPath);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// src/build/toCssVars.ts
|
|
22
|
+
function tokenPathToCssVarName(path) {
|
|
23
|
+
const kebabParts = path.map(toKebabCase);
|
|
24
|
+
return `--asm-${kebabParts.join("-")}`;
|
|
25
|
+
}
|
|
26
|
+
function toCssVars(tree) {
|
|
27
|
+
const out = {};
|
|
28
|
+
walkTokenTree(tree, ({ path, leaf }) => {
|
|
29
|
+
const cssVar = tokenPathToCssVarName(path);
|
|
30
|
+
out[cssVar] = String(leaf.value);
|
|
31
|
+
});
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
function toCssVarNames(tree) {
|
|
35
|
+
const out = {};
|
|
36
|
+
walkTokenTree(tree, ({ path }) => {
|
|
37
|
+
const dotPath = path.join(".");
|
|
38
|
+
out[dotPath] = tokenPathToCssVarName(path);
|
|
39
|
+
});
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// src/build/toJson.ts
|
|
44
|
+
function toJson(tree) {
|
|
45
|
+
const out = {};
|
|
46
|
+
walkTokenTree(tree, ({ path, leaf }) => {
|
|
47
|
+
const key = path.join(".");
|
|
48
|
+
if (leaf.meta) {
|
|
49
|
+
out[key] = { value: leaf.value, description: leaf.description, meta: leaf.meta };
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
out[key] = { value: leaf.value, description: leaf.description };
|
|
53
|
+
});
|
|
54
|
+
return out;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// src/build/validate.ts
|
|
58
|
+
var HEX_COLOR = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/;
|
|
59
|
+
function validateTokens(args) {
|
|
60
|
+
const errors = [];
|
|
61
|
+
const warnings = [];
|
|
62
|
+
const seenDotKeys = /* @__PURE__ */ new Set();
|
|
63
|
+
const seenCssVars = /* @__PURE__ */ new Set();
|
|
64
|
+
walkTokenTree(args.tokens, ({ path, leaf }) => {
|
|
65
|
+
const dotKey = path.join(".");
|
|
66
|
+
if (seenDotKeys.has(dotKey)) {
|
|
67
|
+
errors.push(`Duplicate token key: ${dotKey}`);
|
|
68
|
+
} else {
|
|
69
|
+
seenDotKeys.add(dotKey);
|
|
70
|
+
}
|
|
71
|
+
if (!leaf.description || leaf.description.trim().length === 0) {
|
|
72
|
+
errors.push(`Missing description for token: ${dotKey}`);
|
|
73
|
+
}
|
|
74
|
+
const cssVar = tokenPathToCssVarName(path);
|
|
75
|
+
if (!cssVar.startsWith("--asm-")) {
|
|
76
|
+
errors.push(`Invalid CSS var prefix (must be --asm-): ${cssVar}`);
|
|
77
|
+
}
|
|
78
|
+
if (seenCssVars.has(cssVar)) {
|
|
79
|
+
errors.push(`Duplicate CSS var name generated: ${cssVar} (from ${dotKey})`);
|
|
80
|
+
} else {
|
|
81
|
+
seenCssVars.add(cssVar);
|
|
82
|
+
}
|
|
83
|
+
const v = leaf.value;
|
|
84
|
+
if (typeof v === "string" && v.startsWith("#") && !HEX_COLOR.test(v)) {
|
|
85
|
+
errors.push(`Invalid hex color for ${dotKey}: ${v}`);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
const requiredThemeVars = [
|
|
89
|
+
"--asm-color-bg",
|
|
90
|
+
"--asm-color-surface",
|
|
91
|
+
"--asm-color-text",
|
|
92
|
+
"--asm-color-border",
|
|
93
|
+
"--asm-color-button-primary-bg",
|
|
94
|
+
"--asm-color-input-border-focus",
|
|
95
|
+
"--asm-color-focus-ring",
|
|
96
|
+
"--asm-color-overlay-scrim"
|
|
97
|
+
];
|
|
98
|
+
const themeNames = new Set(args.themes.map((t) => t.name));
|
|
99
|
+
for (const name of ["light", "dark", "high-contrast"]) {
|
|
100
|
+
if (!themeNames.has(name)) {
|
|
101
|
+
errors.push(`Missing required theme: ${name}`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
for (const theme of args.themes) {
|
|
105
|
+
if (theme.name !== "light" && theme.name !== "dark" && theme.name !== "high-contrast") {
|
|
106
|
+
warnings.push(`Theme has non-standard name: ${theme.name}`);
|
|
107
|
+
}
|
|
108
|
+
for (const varName of requiredThemeVars) {
|
|
109
|
+
if (!(varName in theme.tokens)) {
|
|
110
|
+
errors.push(`Theme '${theme.name}' missing required token override: ${varName}`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
for (const [key, value] of Object.entries(theme.tokens)) {
|
|
114
|
+
if (!key.startsWith("--asm-")) {
|
|
115
|
+
errors.push(`Theme '${theme.name}' contains non --asm- var: ${key}`);
|
|
116
|
+
}
|
|
117
|
+
if (typeof value === "string" && value.startsWith("#") && !HEX_COLOR.test(value)) {
|
|
118
|
+
errors.push(`Theme '${theme.name}' invalid hex for ${key}: ${value}`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
ok: errors.length === 0,
|
|
124
|
+
errors,
|
|
125
|
+
warnings
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
export {
|
|
129
|
+
toCssVarNames,
|
|
130
|
+
toCssVars,
|
|
131
|
+
toJson,
|
|
132
|
+
tokenPathToCssVarName,
|
|
133
|
+
validateTokens
|
|
134
|
+
};
|
|
135
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/core/utils.ts","../../src/build/toCssVars.ts","../../src/build/toJson.ts","../../src/build/validate.ts"],"sourcesContent":["import type { TokenLeaf, TokenTree } from \"./types\";\r\n\r\nexport function isTokenLeaf(value: unknown): value is TokenLeaf {\r\n return (\r\n typeof value === \"object\" &&\r\n value !== null &&\r\n \"value\" in value &&\r\n \"description\" in value &&\r\n (typeof (value as any).value === \"string\" || typeof (value as any).value === \"number\") &&\r\n typeof (value as any).description === \"string\"\r\n );\r\n}\r\n\r\nexport function toKebabCase(input: string): string {\r\n return input\r\n .replace(/([a-z0-9])([A-Z])/g, \"$1-$2\")\r\n .replace(/[_\\s]+/g, \"-\")\r\n .toLowerCase();\r\n}\r\n\r\nexport function joinPath(path: string[], sep = \".\"): string {\r\n return path.filter(Boolean).join(sep);\r\n}\r\n\r\nexport function walkTokenTree(\r\n tree: TokenTree,\r\n visitor: (args: { path: string[]; leaf: TokenLeaf }) => void,\r\n path: string[] = []\r\n) {\r\n for (const [key, value] of Object.entries(tree)) {\r\n const nextPath = [...path, key];\r\n\r\n if (isTokenLeaf(value)) {\r\n visitor({ path: nextPath, leaf: value });\r\n continue;\r\n }\r\n\r\n if (typeof value === \"object\" && value !== null) {\r\n walkTokenTree(value as TokenTree, visitor, nextPath);\r\n }\r\n }\r\n}\r\n","import type { TokenTree } from \"../core/types\";\r\nimport { toKebabCase, walkTokenTree } from \"../core/utils\";\r\n\r\nexport type CssVarDict = Record<string, string>;\r\n\r\nexport function tokenPathToCssVarName(path: string[]): string {\r\n const kebabParts = path.map(toKebabCase);\r\n return `--asm-${kebabParts.join(\"-\")}`;\r\n}\r\n\r\nexport function toCssVars(tree: TokenTree): CssVarDict {\r\n const out: CssVarDict = {};\r\n\r\n walkTokenTree(tree, ({ path, leaf }) => {\r\n const cssVar = tokenPathToCssVarName(path);\r\n out[cssVar] = String(leaf.value);\r\n });\r\n\r\n return out;\r\n}\r\n\r\nexport function toCssVarNames(tree: TokenTree): Record<string, string> {\r\n const out: Record<string, string> = {};\r\n\r\n walkTokenTree(tree, ({ path }) => {\r\n const dotPath = path.join(\".\");\r\n out[dotPath] = tokenPathToCssVarName(path);\r\n });\r\n\r\n return out;\r\n}\r\n","import type { TokenTree } from \"../core/types\";\r\nimport { walkTokenTree } from \"../core/utils\";\r\n\r\nexport type FlatTokenJson = Record<string, { value: string | number; description: string; meta?: Record<string, unknown> }>;\r\n\r\nexport function toJson(tree: TokenTree): FlatTokenJson {\r\n const out: FlatTokenJson = {};\r\n\r\n walkTokenTree(tree, ({ path, leaf }) => {\r\n const key = path.join(\".\");\r\n if (leaf.meta) {\r\n out[key] = { value: leaf.value, description: leaf.description, meta: leaf.meta };\r\n return;\r\n }\r\n\r\n out[key] = { value: leaf.value, description: leaf.description };\r\n });\r\n\r\n return out;\r\n}\r\n","import type { ThemeDefinition, TokenTree, ValidateResult } from \"../core/types\";\r\nimport { walkTokenTree } from \"../core/utils\";\r\nimport { tokenPathToCssVarName } from \"./toCssVars\";\r\n\r\nconst HEX_COLOR = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/;\r\n\r\nexport function validateTokens(args: {\r\n tokens: TokenTree;\r\n themes: ThemeDefinition[];\r\n}): ValidateResult {\r\n const errors: string[] = [];\r\n const warnings: string[] = [];\r\n\r\n const seenDotKeys = new Set<string>();\r\n const seenCssVars = new Set<string>();\r\n\r\n walkTokenTree(args.tokens, ({ path, leaf }) => {\r\n const dotKey = path.join(\".\");\r\n if (seenDotKeys.has(dotKey)) {\r\n errors.push(`Duplicate token key: ${dotKey}`);\r\n } else {\r\n seenDotKeys.add(dotKey);\r\n }\r\n\r\n if (!leaf.description || leaf.description.trim().length === 0) {\r\n errors.push(`Missing description for token: ${dotKey}`);\r\n }\r\n\r\n const cssVar = tokenPathToCssVarName(path);\r\n if (!cssVar.startsWith(\"--asm-\")) {\r\n errors.push(`Invalid CSS var prefix (must be --asm-): ${cssVar}`);\r\n }\r\n\r\n if (seenCssVars.has(cssVar)) {\r\n errors.push(`Duplicate CSS var name generated: ${cssVar} (from ${dotKey})`);\r\n } else {\r\n seenCssVars.add(cssVar);\r\n }\r\n\r\n const v = leaf.value;\r\n if (typeof v === \"string\" && v.startsWith(\"#\") && !HEX_COLOR.test(v)) {\r\n errors.push(`Invalid hex color for ${dotKey}: ${v}`);\r\n }\r\n });\r\n\r\n const requiredThemeVars = [\r\n \"--asm-color-bg\",\r\n \"--asm-color-surface\",\r\n \"--asm-color-text\",\r\n \"--asm-color-border\",\r\n \"--asm-color-button-primary-bg\",\r\n \"--asm-color-input-border-focus\",\r\n \"--asm-color-focus-ring\",\r\n \"--asm-color-overlay-scrim\"\r\n ];\r\n\r\n const themeNames = new Set(args.themes.map(t => t.name));\r\n for (const name of [\"light\", \"dark\", \"high-contrast\"] as const) {\r\n if (!themeNames.has(name)) {\r\n errors.push(`Missing required theme: ${name}`);\r\n }\r\n }\r\n\r\n for (const theme of args.themes) {\r\n if (theme.name !== \"light\" && theme.name !== \"dark\" && theme.name !== \"high-contrast\") {\r\n warnings.push(`Theme has non-standard name: ${theme.name}`);\r\n }\r\n\r\n for (const varName of requiredThemeVars) {\r\n if (!(varName in theme.tokens)) {\r\n errors.push(`Theme '${theme.name}' missing required token override: ${varName}`);\r\n }\r\n }\r\n\r\n for (const [key, value] of Object.entries(theme.tokens)) {\r\n if (!key.startsWith(\"--asm-\")) {\r\n errors.push(`Theme '${theme.name}' contains non --asm- var: ${key}`);\r\n }\r\n if (typeof value === \"string\" && value.startsWith(\"#\") && !HEX_COLOR.test(value)) {\r\n errors.push(`Theme '${theme.name}' invalid hex for ${key}: ${value}`);\r\n }\r\n }\r\n }\r\n\r\n return {\r\n ok: errors.length === 0,\r\n errors,\r\n warnings\r\n };\r\n}\r\n"],"mappings":";AAEO,SAAS,YAAY,OAAoC;AAC9D,SACE,OAAO,UAAU,YACjB,UAAU,QACV,WAAW,SACX,iBAAiB,UAChB,OAAQ,MAAc,UAAU,YAAY,OAAQ,MAAc,UAAU,aAC7E,OAAQ,MAAc,gBAAgB;AAE1C;AAEO,SAAS,YAAY,OAAuB;AACjD,SAAO,MACJ,QAAQ,sBAAsB,OAAO,EACrC,QAAQ,W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Q,eAAe,GAAY;AAC9D,QAAI,CAAC,WAAW,IAAI,IAAI,GAAG;AACzB,aAAO,KAAK,2BAA2B,IAAI,EAAE;AAAA,IAC/C;AAAA,EACF;AAEA,aAAW,SAAS,KAAK,QAAQ;AAC/B,QAAI,MAAM,SAAS,WAAW,MAAM,SAAS,UAAU,MAAM,SAAS,iBAAiB;AACrF,eAAS,KAAK,gCAAgC,MAAM,IAAI,EAAE;AAAA,IAC5D;AAEA,eAAW,WAAW,mBAAmB;AACvC,UAAI,EAAE,WAAW,MAAM,SAAS;AAC9B,eAAO,KAAK,UAAU,MAAM,IAAI,sCAAsC,OAAO,EAAE;AAAA,MACjF;AAAA,IACF;AAEA,eAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,MAAM,MAAM,GAAG;AACvD,UAAI,CAAC,IAAI,WAAW,QAAQ,GAAG;AAC7B,eAAO,KAAK,UAAU,MAAM,IAAI,8BAA8B,GAAG,EAAE;AAAA,MACrE;AACA,UAAI,OAAO,UAAU,YAAY,MAAM,WAAW,GAAG,KAAK,CAAC,UAAU,KAAK,KAAK,GAAG;AAChF,eAAO,KAAK,UAAU,MAAM,IAAI,qBAAqB,GAAG,KAAK,KAAK,EAAE;AAAA,MACtE;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AAAA,IACL,IAAI,OAAO,WAAW;AAAA,IACtB;AAAA,IACA;AAAA,EACF;AACF;","names":[]}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
@import "./tokens.base.css";
|
|
2
|
+
@import "./tokens.light.css";
|
|
3
|
+
@import "./tokens.dark.css";
|
|
4
|
+
@import "./tokens.high-contrast.css";
|
|
5
|
+
@import "./tokens.density.compact.css";
|
|
6
|
+
@import "./tokens.density.comfortable.css";
|
|
7
|
+
@import "./tokens.rtl.css";
|
|
8
|
+
@import "./tokens.utilities.css";
|