@agent-native/design-system 0.0.0-stage → 0.229.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/LICENSE +21 -0
- package/README.md +78 -2
- package/THIRD_PARTY_NOTICES.md +58 -0
- package/css/appearances.css +91 -0
- package/css/main.css +174 -0
- package/css/parts/accordion.css +104 -0
- package/css/parts/alert-dialog.css +159 -0
- package/css/parts/app-shell.css +45 -0
- package/css/parts/aspect-ratio.css +20 -0
- package/css/parts/autocomplete.css +366 -0
- package/css/parts/avatar.css +108 -0
- package/css/parts/badge.css +100 -0
- package/css/parts/blockquote.css +23 -0
- package/css/parts/button.css +238 -0
- package/css/parts/callout.css +108 -0
- package/css/parts/card.css +144 -0
- package/css/parts/checkbox-cards.css +59 -0
- package/css/parts/checkbox-group.css +20 -0
- package/css/parts/checkbox.css +127 -0
- package/css/parts/code.css +194 -0
- package/css/parts/collapsible.css +40 -0
- package/css/parts/combobox.css +171 -0
- package/css/parts/context-menu.css +18 -0
- package/css/parts/data-list.css +130 -0
- package/css/parts/dialog.css +251 -0
- package/css/parts/drawer.css +289 -0
- package/css/parts/em.css +16 -0
- package/css/parts/field.css +55 -0
- package/css/parts/fieldset.css +34 -0
- package/css/parts/form.css +18 -0
- package/css/parts/heading.css +25 -0
- package/css/parts/icon-button.css +30 -0
- package/css/parts/input.css +206 -0
- package/css/parts/inset.css +90 -0
- package/css/parts/item.css +208 -0
- package/css/parts/kbd.css +126 -0
- package/css/parts/link.css +99 -0
- package/css/parts/menu.css +342 -0
- package/css/parts/menubar.css +71 -0
- package/css/parts/meter.css +112 -0
- package/css/parts/navigation-menu.css +199 -0
- package/css/parts/number-field.css +124 -0
- package/css/parts/otp-field.css +137 -0
- package/css/parts/page-header.css +45 -0
- package/css/parts/popover.css +149 -0
- package/css/parts/preview-card.css +116 -0
- package/css/parts/progress.css +159 -0
- package/css/parts/quote.css +18 -0
- package/css/parts/radio-cards.css +188 -0
- package/css/parts/radio-group.css +124 -0
- package/css/parts/scroll-area.css +95 -0
- package/css/parts/segmented-control.css +191 -0
- package/css/parts/select.css +378 -0
- package/css/parts/separator.css +48 -0
- package/css/parts/sidebar.css +211 -0
- package/css/parts/skeleton.css +65 -0
- package/css/parts/slider.css +156 -0
- package/css/parts/spinner.css +52 -0
- package/css/parts/strong.css +15 -0
- package/css/parts/switch.css +138 -0
- package/css/parts/tab-nav.css +48 -0
- package/css/parts/table.css +177 -0
- package/css/parts/tabs.css +214 -0
- package/css/parts/text-area.css +140 -0
- package/css/parts/text.css +245 -0
- package/css/parts/toast.css +302 -0
- package/css/parts/toggle-group.css +25 -0
- package/css/parts/toggle.css +181 -0
- package/css/parts/toolbar.css +133 -0
- package/css/parts/tooltip.css +61 -0
- package/css/parts/visually-hidden.css +21 -0
- package/css/styles.css +73 -0
- package/css/themes/agent-native.css +2321 -0
- package/css/themes/analytics.css +1159 -0
- package/css/themes/assets.css +1159 -0
- package/css/themes/baseline.css +1107 -0
- package/css/themes/brain.css +1159 -0
- package/css/themes/calendar.css +1159 -0
- package/css/themes/chat.css +1159 -0
- package/css/themes/clips.css +1159 -0
- package/css/themes/content.css +1159 -0
- package/css/themes/crm.css +1159 -0
- package/css/themes/design.css +1159 -0
- package/css/themes/desktop.css +1159 -0
- package/css/themes/dispatch.css +1159 -0
- package/css/themes/docs.css +1159 -0
- package/css/themes/factory.css +1159 -0
- package/css/themes/forms.css +1159 -0
- package/css/themes/mail.css +1159 -0
- package/css/themes/plan.css +1159 -0
- package/css/themes/slides.css +1159 -0
- package/css/themes/tasks.css +1159 -0
- package/css/tokens.css +1249 -0
- package/package.json +67 -4
- package/src/catalog/components/alert-dialog.ts +9 -0
- package/src/catalog/components/aspect-ratio.ts +6 -0
- package/src/catalog/components/autocomplete.ts +23 -0
- package/src/catalog/components/avatar.ts +16 -0
- package/src/catalog/components/badge.ts +19 -0
- package/src/catalog/components/blockquote.ts +10 -0
- package/src/catalog/components/button.ts +24 -0
- package/src/catalog/components/callout.ts +17 -0
- package/src/catalog/components/card.ts +10 -0
- package/src/catalog/components/checkbox-cards.ts +14 -0
- package/src/catalog/components/checkbox-group.ts +13 -0
- package/src/catalog/components/checkbox.ts +13 -0
- package/src/catalog/components/code.ts +22 -0
- package/src/catalog/components/combobox.ts +23 -0
- package/src/catalog/components/context-menu.ts +20 -0
- package/src/catalog/components/data-list.ts +14 -0
- package/src/catalog/components/dialog.ts +9 -0
- package/src/catalog/components/drawer.ts +9 -0
- package/src/catalog/components/em.ts +7 -0
- package/src/catalog/components/heading.ts +11 -0
- package/src/catalog/components/index.ts +54 -0
- package/src/catalog/components/input.ts +23 -0
- package/src/catalog/components/inset.ts +15 -0
- package/src/catalog/components/item.ts +21 -0
- package/src/catalog/components/kbd.ts +9 -0
- package/src/catalog/components/link.ts +19 -0
- package/src/catalog/components/menu.ts +20 -0
- package/src/catalog/components/menubar.ts +20 -0
- package/src/catalog/components/meter.ts +19 -0
- package/src/catalog/components/number-field.ts +10 -0
- package/src/catalog/components/otp-field.ts +9 -0
- package/src/catalog/components/popover.ts +9 -0
- package/src/catalog/components/preview-card.ts +9 -0
- package/src/catalog/components/progress.ts +24 -0
- package/src/catalog/components/quote.ts +7 -0
- package/src/catalog/components/radio-cards.ts +14 -0
- package/src/catalog/components/radio-group.ts +13 -0
- package/src/catalog/components/scroll-area.ts +15 -0
- package/src/catalog/components/segmented-control.ts +8 -0
- package/src/catalog/components/select.ts +26 -0
- package/src/catalog/components/separator.ts +14 -0
- package/src/catalog/components/skeleton.ts +8 -0
- package/src/catalog/components/slider.ts +19 -0
- package/src/catalog/components/spinner.ts +15 -0
- package/src/catalog/components/strong.ts +7 -0
- package/src/catalog/components/switch.ts +19 -0
- package/src/catalog/components/tab-nav.ts +18 -0
- package/src/catalog/components/table.ts +34 -0
- package/src/catalog/components/tabs.ts +18 -0
- package/src/catalog/components/text-area.ts +24 -0
- package/src/catalog/components/text.ts +11 -0
- package/src/catalog/components/toast.ts +12 -0
- package/src/catalog/components/toggle-group.ts +17 -0
- package/src/catalog/components/toggle.ts +17 -0
- package/src/catalog/components/toolbar.ts +6 -0
- package/src/catalog/index.ts +4 -0
- package/src/catalog/parts.ts +915 -0
- package/src/catalog/prop-types-source.ts +93 -0
- package/src/catalog/prop-types.generated.ts +1402 -0
- package/src/catalog/props.ts +225 -0
- package/src/css.ts +318 -0
- package/src/generated-css.ts +21 -0
- package/src/index.ts +8 -0
- package/src/model/compare.ts +127 -0
- package/src/model/dom.ts +253 -0
- package/src/model/ids.ts +25 -0
- package/src/model/index.ts +6 -0
- package/src/model/styles.ts +254 -0
- package/src/model/types.ts +97 -0
- package/src/model/validate.ts +133 -0
- package/src/themes/appearances.ts +105 -0
- package/src/themes/baseline.ts +27 -0
- package/src/themes/index.ts +85 -0
- package/src/themes/reference.ts +9 -0
- package/src/themes/shadcn.ts +239 -0
- package/src/themes/types.ts +53 -0
- package/src/tokens/at.ts +13 -0
- package/src/tokens/base.ts +48 -0
- package/src/tokens/color/bezier.ts +111 -0
- package/src/tokens/color/index.ts +2 -0
- package/src/tokens/color/radix-generator.ts +516 -0
- package/src/tokens/dtcg.ts +298 -0
- package/src/tokens/files/foundations.tokens.json +318 -0
- package/src/tokens/files/palette.tokens.json +58 -0
- package/src/tokens/files/themes/agent-native.dark.tokens.json +405 -0
- package/src/tokens/files/themes/agent-native.light.tokens.json +405 -0
- package/src/tokens/files/themes/analytics.dark.tokens.json +391 -0
- package/src/tokens/files/themes/analytics.light.tokens.json +391 -0
- package/src/tokens/files/themes/assets.dark.tokens.json +391 -0
- package/src/tokens/files/themes/assets.light.tokens.json +391 -0
- package/src/tokens/files/themes/brain.dark.tokens.json +391 -0
- package/src/tokens/files/themes/brain.light.tokens.json +391 -0
- package/src/tokens/files/themes/calendar.dark.tokens.json +391 -0
- package/src/tokens/files/themes/calendar.light.tokens.json +391 -0
- package/src/tokens/files/themes/chat.dark.tokens.json +391 -0
- package/src/tokens/files/themes/chat.light.tokens.json +391 -0
- package/src/tokens/files/themes/clips.dark.tokens.json +391 -0
- package/src/tokens/files/themes/clips.light.tokens.json +391 -0
- package/src/tokens/files/themes/content.dark.tokens.json +391 -0
- package/src/tokens/files/themes/content.light.tokens.json +391 -0
- package/src/tokens/files/themes/crm.dark.tokens.json +391 -0
- package/src/tokens/files/themes/crm.light.tokens.json +391 -0
- package/src/tokens/files/themes/design.dark.tokens.json +391 -0
- package/src/tokens/files/themes/design.light.tokens.json +391 -0
- package/src/tokens/files/themes/desktop.dark.tokens.json +391 -0
- package/src/tokens/files/themes/desktop.light.tokens.json +391 -0
- package/src/tokens/files/themes/dispatch.dark.tokens.json +391 -0
- package/src/tokens/files/themes/dispatch.light.tokens.json +391 -0
- package/src/tokens/files/themes/docs.dark.tokens.json +391 -0
- package/src/tokens/files/themes/docs.light.tokens.json +391 -0
- package/src/tokens/files/themes/factory.dark.tokens.json +391 -0
- package/src/tokens/files/themes/factory.light.tokens.json +391 -0
- package/src/tokens/files/themes/forms.dark.tokens.json +391 -0
- package/src/tokens/files/themes/forms.light.tokens.json +391 -0
- package/src/tokens/files/themes/mail.dark.tokens.json +391 -0
- package/src/tokens/files/themes/mail.light.tokens.json +391 -0
- package/src/tokens/files/themes/plan.dark.tokens.json +391 -0
- package/src/tokens/files/themes/plan.light.tokens.json +391 -0
- package/src/tokens/files/themes/slides.dark.tokens.json +391 -0
- package/src/tokens/files/themes/slides.light.tokens.json +391 -0
- package/src/tokens/files/themes/tasks.dark.tokens.json +391 -0
- package/src/tokens/files/themes/tasks.light.tokens.json +391 -0
- package/src/tokens/palette.ts +354 -0
- package/src/tokens/primitives.ts +84 -0
- package/src/tokens/roles.ts +269 -0
- package/src/tokens/round-trip.ts +277 -0
- package/src/tokens/source.ts +302 -0
- package/tsconfig.json +26 -0
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import {
|
|
2
|
+
colorComponents,
|
|
3
|
+
colorToken,
|
|
4
|
+
componentsToHex,
|
|
5
|
+
componentsToHslTriplet,
|
|
6
|
+
type DtcgToken,
|
|
7
|
+
EXTENSION,
|
|
8
|
+
flattenTokens,
|
|
9
|
+
TokenError,
|
|
10
|
+
unflattenTokens,
|
|
11
|
+
} from "./dtcg.js";
|
|
12
|
+
import { createPalette } from "./palette.js";
|
|
13
|
+
import {
|
|
14
|
+
FOUNDATIONS_FILE,
|
|
15
|
+
PALETTE_FILE,
|
|
16
|
+
readTokenFiles,
|
|
17
|
+
THEME_MODES,
|
|
18
|
+
THEME_NAMES,
|
|
19
|
+
themeFile,
|
|
20
|
+
type TokenFiles,
|
|
21
|
+
} from "./source.js";
|
|
22
|
+
|
|
23
|
+
/** A Figma collection: its folder, and the source file behind each of its modes. */
|
|
24
|
+
interface Collection {
|
|
25
|
+
folder: string;
|
|
26
|
+
modes: ReadonlyArray<{ mode: string; source: string }>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const MODE_LABELS = { light: "Light", dark: "Dark" } as const;
|
|
30
|
+
|
|
31
|
+
export const COLLECTIONS: readonly Collection[] = [
|
|
32
|
+
{
|
|
33
|
+
folder: "Foundations",
|
|
34
|
+
modes: [{ mode: "Value", source: FOUNDATIONS_FILE }],
|
|
35
|
+
},
|
|
36
|
+
{ folder: "Palette", modes: [{ mode: "Value", source: PALETTE_FILE }] },
|
|
37
|
+
...THEME_NAMES.map((name) => ({
|
|
38
|
+
folder: `Theme ${name}`,
|
|
39
|
+
modes: THEME_MODES.map((mode) => ({
|
|
40
|
+
mode: MODE_LABELS[mode],
|
|
41
|
+
source: themeFile(name, mode),
|
|
42
|
+
})),
|
|
43
|
+
})),
|
|
44
|
+
];
|
|
45
|
+
|
|
46
|
+
const SUFFIX = ".tokens.json";
|
|
47
|
+
|
|
48
|
+
/** The files to import into Figma, by path: `<collection>/<mode>.tokens.json`. */
|
|
49
|
+
export function exportForFigma(sources: TokenFiles): Map<string, unknown> {
|
|
50
|
+
const files = new Map<string, unknown>();
|
|
51
|
+
for (const collection of COLLECTIONS) {
|
|
52
|
+
for (const { mode, source } of collection.modes) {
|
|
53
|
+
if (!(source in sources)) {
|
|
54
|
+
throw new TokenError(source, "(file)", "is missing");
|
|
55
|
+
}
|
|
56
|
+
files.set(`${collection.folder}/${mode}${SUFFIX}`, sources[source]);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return files;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Token files as the formatter writes them: two-space JSON, number arrays on one line. */
|
|
63
|
+
export function formatTokenFile(group: unknown): string {
|
|
64
|
+
return `${JSON.stringify(group, null, 2).replace(
|
|
65
|
+
/\[\s+([^[\]{}"]*?)\s+\]/g,
|
|
66
|
+
(_match, items: string) => `[${items.split(/,\s*/).join(", ")}]`,
|
|
67
|
+
)}\n`;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const ALIAS = /^\{(.+)\}$/;
|
|
71
|
+
|
|
72
|
+
function importToken(
|
|
73
|
+
file: string,
|
|
74
|
+
tokenPath: string,
|
|
75
|
+
current: DtcgToken,
|
|
76
|
+
incoming: unknown,
|
|
77
|
+
): DtcgToken {
|
|
78
|
+
if (typeof incoming !== "object" || incoming === null) {
|
|
79
|
+
throw new TokenError(file, tokenPath, "is not a token");
|
|
80
|
+
}
|
|
81
|
+
const token = incoming as Partial<DtcgToken>;
|
|
82
|
+
if (token.$type !== current.$type) {
|
|
83
|
+
throw new TokenError(
|
|
84
|
+
file,
|
|
85
|
+
tokenPath,
|
|
86
|
+
`is a ${String(token.$type)}, expected a ${current.$type}`,
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
const value = token.$value;
|
|
90
|
+
if (typeof value === "string" && ALIAS.test(value)) {
|
|
91
|
+
throw new TokenError(
|
|
92
|
+
file,
|
|
93
|
+
tokenPath,
|
|
94
|
+
`is an alias of ${value}; give it its own value`,
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
const extensions: Record<string, unknown> = { ...current.$extensions };
|
|
98
|
+
for (const [key, extension] of Object.entries(token.$extensions ?? {})) {
|
|
99
|
+
if (key !== EXTENSION) extensions[key] = extension;
|
|
100
|
+
}
|
|
101
|
+
const description =
|
|
102
|
+
typeof token.$description === "string" && token.$description !== ""
|
|
103
|
+
? { $description: token.$description }
|
|
104
|
+
: {};
|
|
105
|
+
|
|
106
|
+
if (current.$type === "color") {
|
|
107
|
+
const components = colorComponents(value, file, tokenPath);
|
|
108
|
+
const before = colorComponents(current.$value, file, tokenPath);
|
|
109
|
+
if (componentsToHex(components) === componentsToHex(before)) {
|
|
110
|
+
return { ...current, ...description, $extensions: extensions };
|
|
111
|
+
}
|
|
112
|
+
const changed = colorToken(components, componentsToHslTriplet(components));
|
|
113
|
+
return {
|
|
114
|
+
...changed,
|
|
115
|
+
...description,
|
|
116
|
+
$extensions: { ...extensions, ...changed.$extensions },
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
if (current.$type === "number") {
|
|
120
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
121
|
+
throw new TokenError(file, tokenPath, "is not a finite number");
|
|
122
|
+
}
|
|
123
|
+
} else if (typeof value !== "string" || value.trim() === "") {
|
|
124
|
+
throw new TokenError(file, tokenPath, "is not a non-empty string");
|
|
125
|
+
}
|
|
126
|
+
return { ...current, ...description, $value: value, $extensions: extensions };
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function withoutEmptyExtensions(token: DtcgToken): DtcgToken {
|
|
130
|
+
if (!token.$extensions || Object.keys(token.$extensions).length > 0) {
|
|
131
|
+
return token;
|
|
132
|
+
}
|
|
133
|
+
const trimmed: DtcgToken = { $type: token.$type, $value: token.$value };
|
|
134
|
+
if (token.$description !== undefined) {
|
|
135
|
+
trimmed.$description = token.$description;
|
|
136
|
+
}
|
|
137
|
+
return trimmed;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export interface ImportResult {
|
|
141
|
+
/** Every source file the export touched, by path under `files/`, as it should now read. */
|
|
142
|
+
files: Map<string, unknown>;
|
|
143
|
+
/** Token paths whose value changed, by source file. */
|
|
144
|
+
changed: Map<string, string[]>;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* The source files for a Figma export, keyed by path under `files/`. Takes
|
|
149
|
+
* any subset of collections, but every mode of one it takes, and refuses an
|
|
150
|
+
* unknown file, a missing or extra token, a changed type, or a value code
|
|
151
|
+
* cannot read. Nothing is returned unless every file passes.
|
|
152
|
+
*/
|
|
153
|
+
export function importFromFigma(
|
|
154
|
+
exported: ReadonlyMap<string, unknown>,
|
|
155
|
+
sources: TokenFiles,
|
|
156
|
+
): ImportResult {
|
|
157
|
+
const problems: string[] = [];
|
|
158
|
+
const byFolder = new Map<string, Map<string, unknown>>();
|
|
159
|
+
for (const [path, group] of exported) {
|
|
160
|
+
const [folder, fileName, ...nested] = path.split("/");
|
|
161
|
+
const collection =
|
|
162
|
+
folder === undefined
|
|
163
|
+
? undefined
|
|
164
|
+
: COLLECTIONS.find(
|
|
165
|
+
(candidate) =>
|
|
166
|
+
candidate.folder.toLowerCase() === folder.toLowerCase(),
|
|
167
|
+
);
|
|
168
|
+
if (
|
|
169
|
+
!collection ||
|
|
170
|
+
fileName === undefined ||
|
|
171
|
+
nested.length > 0 ||
|
|
172
|
+
!fileName.endsWith(SUFFIX)
|
|
173
|
+
) {
|
|
174
|
+
problems.push(
|
|
175
|
+
`${path}: not a known collection file (<collection>/<mode>${SUFFIX})`,
|
|
176
|
+
);
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
const modes = byFolder.get(collection.folder) ?? new Map<string, unknown>();
|
|
180
|
+
modes.set(fileName.slice(0, -SUFFIX.length).toLowerCase(), group);
|
|
181
|
+
byFolder.set(collection.folder, modes);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const files = new Map<string, unknown>();
|
|
185
|
+
const changed = new Map<string, string[]>();
|
|
186
|
+
for (const collection of COLLECTIONS) {
|
|
187
|
+
const modes = byFolder.get(collection.folder);
|
|
188
|
+
if (!modes) continue;
|
|
189
|
+
const single = collection.modes.length === 1;
|
|
190
|
+
if (single && modes.size !== 1) {
|
|
191
|
+
problems.push(
|
|
192
|
+
`${collection.folder}: has ${modes.size} modes, expected 1`,
|
|
193
|
+
);
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
for (const name of modes.keys()) {
|
|
197
|
+
if (
|
|
198
|
+
!single &&
|
|
199
|
+
!collection.modes.some(({ mode }) => mode.toLowerCase() === name)
|
|
200
|
+
) {
|
|
201
|
+
problems.push(
|
|
202
|
+
`${collection.folder}/${name}${SUFFIX}: not a mode of this collection (${collection.modes.map(({ mode }) => mode).join(", ")})`,
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
for (const { mode, source } of collection.modes) {
|
|
207
|
+
const group = single
|
|
208
|
+
? [...modes.values()][0]
|
|
209
|
+
: modes.get(mode.toLowerCase());
|
|
210
|
+
const label = `${collection.folder}/${mode}${SUFFIX}`;
|
|
211
|
+
if (group === undefined) {
|
|
212
|
+
problems.push(
|
|
213
|
+
`${label}: is missing; export every mode of the collection`,
|
|
214
|
+
);
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
try {
|
|
218
|
+
const current = flattenTokens(sources[source], source);
|
|
219
|
+
const incoming = flattenTokens(group, label);
|
|
220
|
+
const next = new Map<string, DtcgToken>();
|
|
221
|
+
const changes: string[] = [];
|
|
222
|
+
for (const tokenPath of incoming.keys()) {
|
|
223
|
+
if (!current.has(tokenPath)) {
|
|
224
|
+
problems.push(`${label}: ${tokenPath} is not a token code reads`);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
for (const [tokenPath, token] of current) {
|
|
228
|
+
if (!incoming.has(tokenPath)) {
|
|
229
|
+
problems.push(`${label}: ${tokenPath} is missing`);
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
try {
|
|
233
|
+
const imported = withoutEmptyExtensions(
|
|
234
|
+
importToken(label, tokenPath, token, incoming.get(tokenPath)),
|
|
235
|
+
);
|
|
236
|
+
if (
|
|
237
|
+
JSON.stringify(imported.$value) !== JSON.stringify(token.$value)
|
|
238
|
+
) {
|
|
239
|
+
changes.push(tokenPath);
|
|
240
|
+
}
|
|
241
|
+
next.set(tokenPath, imported);
|
|
242
|
+
} catch (error) {
|
|
243
|
+
if (!(error instanceof TokenError)) throw error;
|
|
244
|
+
problems.push(error.message);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
files.set(source, unflattenTokens(next));
|
|
248
|
+
if (changes.length > 0) changed.set(source, changes);
|
|
249
|
+
} catch (error) {
|
|
250
|
+
if (!(error instanceof TokenError)) throw error;
|
|
251
|
+
problems.push(error.message);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if (problems.length === 0) {
|
|
257
|
+
const merged: Record<string, unknown> = { ...sources };
|
|
258
|
+
for (const [path, group] of files) merged[path] = group;
|
|
259
|
+
try {
|
|
260
|
+
const read = readTokenFiles(merged);
|
|
261
|
+
createPalette(read.palette);
|
|
262
|
+
} catch (error) {
|
|
263
|
+
problems.push(error instanceof Error ? error.message : String(error));
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
if (problems.length > 0) throw new TokenImportError(problems);
|
|
267
|
+
return { files, changed };
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export class TokenImportError extends Error {
|
|
271
|
+
constructor(readonly problems: readonly string[]) {
|
|
272
|
+
super(
|
|
273
|
+
`The export was not imported; fix these and export again:\n${problems.map((problem) => ` ${problem}`).join("\n")}`,
|
|
274
|
+
);
|
|
275
|
+
this.name = "TokenImportError";
|
|
276
|
+
}
|
|
277
|
+
}
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import { at } from "./at.js";
|
|
2
|
+
import { TokenError, TokenReader } from "./dtcg.js";
|
|
3
|
+
import foundationsFile from "./files/foundations.tokens.json" with { type: "json" };
|
|
4
|
+
import paletteFile from "./files/palette.tokens.json" with { type: "json" };
|
|
5
|
+
import agentNativeDark from "./files/themes/agent-native.dark.tokens.json" with { type: "json" };
|
|
6
|
+
import agentNativeLight from "./files/themes/agent-native.light.tokens.json" with { type: "json" };
|
|
7
|
+
import analyticsDark from "./files/themes/analytics.dark.tokens.json" with { type: "json" };
|
|
8
|
+
import analyticsLight from "./files/themes/analytics.light.tokens.json" with { type: "json" };
|
|
9
|
+
import assetsDark from "./files/themes/assets.dark.tokens.json" with { type: "json" };
|
|
10
|
+
import assetsLight from "./files/themes/assets.light.tokens.json" with { type: "json" };
|
|
11
|
+
import brainDark from "./files/themes/brain.dark.tokens.json" with { type: "json" };
|
|
12
|
+
import brainLight from "./files/themes/brain.light.tokens.json" with { type: "json" };
|
|
13
|
+
import calendarDark from "./files/themes/calendar.dark.tokens.json" with { type: "json" };
|
|
14
|
+
import calendarLight from "./files/themes/calendar.light.tokens.json" with { type: "json" };
|
|
15
|
+
import chatDark from "./files/themes/chat.dark.tokens.json" with { type: "json" };
|
|
16
|
+
import chatLight from "./files/themes/chat.light.tokens.json" with { type: "json" };
|
|
17
|
+
import clipsDark from "./files/themes/clips.dark.tokens.json" with { type: "json" };
|
|
18
|
+
import clipsLight from "./files/themes/clips.light.tokens.json" with { type: "json" };
|
|
19
|
+
import contentDark from "./files/themes/content.dark.tokens.json" with { type: "json" };
|
|
20
|
+
import contentLight from "./files/themes/content.light.tokens.json" with { type: "json" };
|
|
21
|
+
import crmDark from "./files/themes/crm.dark.tokens.json" with { type: "json" };
|
|
22
|
+
import crmLight from "./files/themes/crm.light.tokens.json" with { type: "json" };
|
|
23
|
+
import designDark from "./files/themes/design.dark.tokens.json" with { type: "json" };
|
|
24
|
+
import designLight from "./files/themes/design.light.tokens.json" with { type: "json" };
|
|
25
|
+
import desktopDark from "./files/themes/desktop.dark.tokens.json" with { type: "json" };
|
|
26
|
+
import desktopLight from "./files/themes/desktop.light.tokens.json" with { type: "json" };
|
|
27
|
+
import dispatchDark from "./files/themes/dispatch.dark.tokens.json" with { type: "json" };
|
|
28
|
+
import dispatchLight from "./files/themes/dispatch.light.tokens.json" with { type: "json" };
|
|
29
|
+
import docsDark from "./files/themes/docs.dark.tokens.json" with { type: "json" };
|
|
30
|
+
import docsLight from "./files/themes/docs.light.tokens.json" with { type: "json" };
|
|
31
|
+
import factoryDark from "./files/themes/factory.dark.tokens.json" with { type: "json" };
|
|
32
|
+
import factoryLight from "./files/themes/factory.light.tokens.json" with { type: "json" };
|
|
33
|
+
import formsDark from "./files/themes/forms.dark.tokens.json" with { type: "json" };
|
|
34
|
+
import formsLight from "./files/themes/forms.light.tokens.json" with { type: "json" };
|
|
35
|
+
import mailDark from "./files/themes/mail.dark.tokens.json" with { type: "json" };
|
|
36
|
+
import mailLight from "./files/themes/mail.light.tokens.json" with { type: "json" };
|
|
37
|
+
import planDark from "./files/themes/plan.dark.tokens.json" with { type: "json" };
|
|
38
|
+
import planLight from "./files/themes/plan.light.tokens.json" with { type: "json" };
|
|
39
|
+
import slidesDark from "./files/themes/slides.dark.tokens.json" with { type: "json" };
|
|
40
|
+
import slidesLight from "./files/themes/slides.light.tokens.json" with { type: "json" };
|
|
41
|
+
import tasksDark from "./files/themes/tasks.dark.tokens.json" with { type: "json" };
|
|
42
|
+
import tasksLight from "./files/themes/tasks.light.tokens.json" with { type: "json" };
|
|
43
|
+
|
|
44
|
+
/** Every token file by its path under `files/`, as parsed JSON. */
|
|
45
|
+
export type TokenFiles = Readonly<Record<string, unknown>>;
|
|
46
|
+
|
|
47
|
+
export const TOKEN_FILES: TokenFiles = {
|
|
48
|
+
"foundations.tokens.json": foundationsFile,
|
|
49
|
+
"palette.tokens.json": paletteFile,
|
|
50
|
+
"themes/agent-native.light.tokens.json": agentNativeLight,
|
|
51
|
+
"themes/agent-native.dark.tokens.json": agentNativeDark,
|
|
52
|
+
"themes/analytics.light.tokens.json": analyticsLight,
|
|
53
|
+
"themes/analytics.dark.tokens.json": analyticsDark,
|
|
54
|
+
"themes/assets.light.tokens.json": assetsLight,
|
|
55
|
+
"themes/assets.dark.tokens.json": assetsDark,
|
|
56
|
+
"themes/brain.light.tokens.json": brainLight,
|
|
57
|
+
"themes/brain.dark.tokens.json": brainDark,
|
|
58
|
+
"themes/calendar.light.tokens.json": calendarLight,
|
|
59
|
+
"themes/calendar.dark.tokens.json": calendarDark,
|
|
60
|
+
"themes/chat.light.tokens.json": chatLight,
|
|
61
|
+
"themes/chat.dark.tokens.json": chatDark,
|
|
62
|
+
"themes/clips.light.tokens.json": clipsLight,
|
|
63
|
+
"themes/clips.dark.tokens.json": clipsDark,
|
|
64
|
+
"themes/content.light.tokens.json": contentLight,
|
|
65
|
+
"themes/content.dark.tokens.json": contentDark,
|
|
66
|
+
"themes/crm.light.tokens.json": crmLight,
|
|
67
|
+
"themes/crm.dark.tokens.json": crmDark,
|
|
68
|
+
"themes/design.light.tokens.json": designLight,
|
|
69
|
+
"themes/design.dark.tokens.json": designDark,
|
|
70
|
+
"themes/desktop.light.tokens.json": desktopLight,
|
|
71
|
+
"themes/desktop.dark.tokens.json": desktopDark,
|
|
72
|
+
"themes/dispatch.light.tokens.json": dispatchLight,
|
|
73
|
+
"themes/dispatch.dark.tokens.json": dispatchDark,
|
|
74
|
+
"themes/docs.light.tokens.json": docsLight,
|
|
75
|
+
"themes/docs.dark.tokens.json": docsDark,
|
|
76
|
+
"themes/factory.light.tokens.json": factoryLight,
|
|
77
|
+
"themes/factory.dark.tokens.json": factoryDark,
|
|
78
|
+
"themes/forms.light.tokens.json": formsLight,
|
|
79
|
+
"themes/forms.dark.tokens.json": formsDark,
|
|
80
|
+
"themes/mail.light.tokens.json": mailLight,
|
|
81
|
+
"themes/mail.dark.tokens.json": mailDark,
|
|
82
|
+
"themes/plan.light.tokens.json": planLight,
|
|
83
|
+
"themes/plan.dark.tokens.json": planDark,
|
|
84
|
+
"themes/slides.light.tokens.json": slidesLight,
|
|
85
|
+
"themes/slides.dark.tokens.json": slidesDark,
|
|
86
|
+
"themes/tasks.light.tokens.json": tasksLight,
|
|
87
|
+
"themes/tasks.dark.tokens.json": tasksDark,
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
export const FOUNDATIONS_FILE = "foundations.tokens.json";
|
|
91
|
+
export const PALETTE_FILE = "palette.tokens.json";
|
|
92
|
+
export const THEME_NAMES: readonly string[] = [
|
|
93
|
+
"agent-native",
|
|
94
|
+
"analytics",
|
|
95
|
+
"assets",
|
|
96
|
+
"brain",
|
|
97
|
+
"calendar",
|
|
98
|
+
"chat",
|
|
99
|
+
"clips",
|
|
100
|
+
"content",
|
|
101
|
+
"crm",
|
|
102
|
+
"design",
|
|
103
|
+
"desktop",
|
|
104
|
+
"dispatch",
|
|
105
|
+
"docs",
|
|
106
|
+
"factory",
|
|
107
|
+
"forms",
|
|
108
|
+
"mail",
|
|
109
|
+
"plan",
|
|
110
|
+
"slides",
|
|
111
|
+
"tasks",
|
|
112
|
+
];
|
|
113
|
+
|
|
114
|
+
export type ThemeMode = "light" | "dark";
|
|
115
|
+
export const THEME_MODES: readonly ThemeMode[] = ["light", "dark"];
|
|
116
|
+
|
|
117
|
+
export const themeFile = (name: string, mode: ThemeMode) =>
|
|
118
|
+
`themes/${name}.${mode}.tokens.json`;
|
|
119
|
+
|
|
120
|
+
const FONT_WEIGHT_NAMES = [
|
|
121
|
+
"light",
|
|
122
|
+
"regular",
|
|
123
|
+
"medium",
|
|
124
|
+
"semibold",
|
|
125
|
+
"bold",
|
|
126
|
+
] as const;
|
|
127
|
+
const FONT_FAMILY_NAMES = ["sans", "heading", "mono", "wordmark"] as const;
|
|
128
|
+
|
|
129
|
+
/** The type scale starts at step 0, the 10px step below the 12px UI base. */
|
|
130
|
+
export const TYPE_FIRST_STEP = 0;
|
|
131
|
+
|
|
132
|
+
/** One step of the type scale, as `--font-size-N` and its pairs set it. */
|
|
133
|
+
export interface TypeStep {
|
|
134
|
+
readonly step: number;
|
|
135
|
+
readonly size: number;
|
|
136
|
+
readonly lineHeight: number;
|
|
137
|
+
readonly letterSpacing: string;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function steps(reader: TokenReader, group: string, first = 1): number[] {
|
|
141
|
+
const numbers = reader
|
|
142
|
+
.paths()
|
|
143
|
+
.filter((tokenPath) => tokenPath.startsWith(`${group}.`))
|
|
144
|
+
.map((tokenPath) => Number(tokenPath.slice(group.length + 1)))
|
|
145
|
+
.sort((a, b) => a - b);
|
|
146
|
+
numbers.forEach((step, index) => {
|
|
147
|
+
if (step !== index + first) {
|
|
148
|
+
throw new TokenError(
|
|
149
|
+
reader.file,
|
|
150
|
+
`${group}.${index + first}`,
|
|
151
|
+
"is missing",
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
return numbers.map((step) => reader.number(`${group}.${step}`));
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function file(files: TokenFiles, path: string): unknown {
|
|
159
|
+
if (!(path in files)) throw new TokenError(path, "(file)", "is missing");
|
|
160
|
+
return files[path];
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function readFoundations(files: TokenFiles) {
|
|
164
|
+
const foundations = new TokenReader(
|
|
165
|
+
FOUNDATIONS_FILE,
|
|
166
|
+
file(files, FOUNDATIONS_FILE),
|
|
167
|
+
);
|
|
168
|
+
const typeSizes = steps(foundations, "font.size", TYPE_FIRST_STEP);
|
|
169
|
+
const lineHeights = steps(foundations, "font.line-height", TYPE_FIRST_STEP);
|
|
170
|
+
const letterSpacings = steps(
|
|
171
|
+
foundations,
|
|
172
|
+
"font.letter-spacing",
|
|
173
|
+
TYPE_FIRST_STEP,
|
|
174
|
+
);
|
|
175
|
+
if (
|
|
176
|
+
lineHeights.length !== typeSizes.length ||
|
|
177
|
+
letterSpacings.length !== typeSizes.length
|
|
178
|
+
) {
|
|
179
|
+
throw new TokenError(
|
|
180
|
+
FOUNDATIONS_FILE,
|
|
181
|
+
"font",
|
|
182
|
+
"has steps of different lengths",
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
return {
|
|
186
|
+
controlHeights: steps(foundations, "size.control"),
|
|
187
|
+
controlIconSizes: steps(foundations, "size.control-icon"),
|
|
188
|
+
sizeRamp: steps(foundations, "size.box"),
|
|
189
|
+
radiusSteps: steps(foundations, "radius"),
|
|
190
|
+
typeRamp: typeSizes.map(
|
|
191
|
+
(size, index): TypeStep => ({
|
|
192
|
+
step: index + TYPE_FIRST_STEP,
|
|
193
|
+
size,
|
|
194
|
+
lineHeight: at(lineHeights, index),
|
|
195
|
+
letterSpacing: `${at(letterSpacings, index)}em`,
|
|
196
|
+
}),
|
|
197
|
+
),
|
|
198
|
+
fontWeights: Object.fromEntries(
|
|
199
|
+
FONT_WEIGHT_NAMES.map((name) => [
|
|
200
|
+
name,
|
|
201
|
+
foundations.number(`font.weight.${name}`),
|
|
202
|
+
]),
|
|
203
|
+
) as Record<(typeof FONT_WEIGHT_NAMES)[number], number>,
|
|
204
|
+
fontFamilies: Object.fromEntries(
|
|
205
|
+
FONT_FAMILY_NAMES.map((name) => [
|
|
206
|
+
name,
|
|
207
|
+
foundations.string(`font.family.${name}`),
|
|
208
|
+
]),
|
|
209
|
+
) as Record<(typeof FONT_FAMILY_NAMES)[number], string>,
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function readPalette(files: TokenFiles) {
|
|
214
|
+
const palette = new TokenReader(PALETTE_FILE, file(files, PALETTE_FILE));
|
|
215
|
+
return {
|
|
216
|
+
brand: palette.hex("brand"),
|
|
217
|
+
gray: palette.hex("gray"),
|
|
218
|
+
background: {
|
|
219
|
+
light: palette.hex("background.light"),
|
|
220
|
+
dark: palette.hex("background.dark"),
|
|
221
|
+
},
|
|
222
|
+
status: {
|
|
223
|
+
danger: palette.string("status.danger"),
|
|
224
|
+
warning: palette.string("status.warning"),
|
|
225
|
+
success: palette.string("status.success"),
|
|
226
|
+
info: palette.string("status.info"),
|
|
227
|
+
},
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function readThemeMode(
|
|
232
|
+
files: TokenFiles,
|
|
233
|
+
name: string,
|
|
234
|
+
mode: ThemeMode,
|
|
235
|
+
): Record<string, string> {
|
|
236
|
+
const path = themeFile(name, mode);
|
|
237
|
+
const reader = new TokenReader(path, file(files, path));
|
|
238
|
+
const values: Record<string, string> = {};
|
|
239
|
+
for (const tokenPath of reader.paths()) {
|
|
240
|
+
if (tokenPath.startsWith("color.")) {
|
|
241
|
+
values[tokenPath.slice("color.".length)] = reader.hslTriplet(tokenPath);
|
|
242
|
+
} else if (tokenPath === "radius") {
|
|
243
|
+
values.radius = `${reader.number("radius") / 16}rem`;
|
|
244
|
+
} else {
|
|
245
|
+
throw new TokenError(
|
|
246
|
+
path,
|
|
247
|
+
tokenPath,
|
|
248
|
+
"is not a theme value (color.* or radius)",
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return values;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Everything code reads from a set of token files. Throws a `TokenError` on
|
|
257
|
+
* the first file that is missing a token or holds one of the wrong shape.
|
|
258
|
+
*/
|
|
259
|
+
export function readTokenFiles(files: TokenFiles) {
|
|
260
|
+
return {
|
|
261
|
+
foundations: readFoundations(files),
|
|
262
|
+
palette: readPalette(files),
|
|
263
|
+
themes: Object.fromEntries(
|
|
264
|
+
THEME_NAMES.map((name) => [
|
|
265
|
+
name,
|
|
266
|
+
{
|
|
267
|
+
light: readThemeMode(files, name, "light"),
|
|
268
|
+
dark: readThemeMode(files, name, "dark"),
|
|
269
|
+
},
|
|
270
|
+
]),
|
|
271
|
+
) as Record<string, Record<ThemeMode, Record<string, string>>>,
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const tokens = readTokenFiles(TOKEN_FILES);
|
|
276
|
+
|
|
277
|
+
export const FOUNDATIONS = tokens.foundations;
|
|
278
|
+
export const PALETTE_DEFAULTS = tokens.palette;
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* A theme's values from its light and dark token files. The appearance it
|
|
282
|
+
* renders with no class is `base`; the other carries only what differs.
|
|
283
|
+
*/
|
|
284
|
+
export function tokenThemeValues(
|
|
285
|
+
name: string,
|
|
286
|
+
defaultAppearance: ThemeMode,
|
|
287
|
+
): {
|
|
288
|
+
base: Record<string, string>;
|
|
289
|
+
dark?: Record<string, string>;
|
|
290
|
+
light?: Record<string, string>;
|
|
291
|
+
} {
|
|
292
|
+
const modes = tokens.themes[name];
|
|
293
|
+
if (!modes) throw new Error(`No token files for theme ${name}`);
|
|
294
|
+
const base = modes[defaultAppearance];
|
|
295
|
+
const other = modes[defaultAppearance === "light" ? "dark" : "light"];
|
|
296
|
+
const differs = Object.fromEntries(
|
|
297
|
+
Object.entries(other).filter(([key, value]) => base[key] !== value),
|
|
298
|
+
);
|
|
299
|
+
return defaultAppearance === "light"
|
|
300
|
+
? { base, dark: differs }
|
|
301
|
+
: { base, light: differs };
|
|
302
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"noImplicitOverride": true,
|
|
4
|
+
"noFallthroughCasesInSwitch": true,
|
|
5
|
+
"target": "ES2022",
|
|
6
|
+
"module": "ESNext",
|
|
7
|
+
"moduleResolution": "bundler",
|
|
8
|
+
"declaration": true,
|
|
9
|
+
"declarationMap": true,
|
|
10
|
+
"sourceMap": true,
|
|
11
|
+
"outDir": "dist",
|
|
12
|
+
"rootDir": "src",
|
|
13
|
+
"strict": true,
|
|
14
|
+
"noUncheckedIndexedAccess": true,
|
|
15
|
+
"esModuleInterop": true,
|
|
16
|
+
"skipLibCheck": true,
|
|
17
|
+
"isolatedModules": true,
|
|
18
|
+
"resolveJsonModule": true,
|
|
19
|
+
"lib": ["ES2022", "DOM", "DOM.Iterable"],
|
|
20
|
+
"types": ["node"],
|
|
21
|
+
"incremental": true,
|
|
22
|
+
"tsBuildInfoFile": "node_modules/.cache/tsbuildinfo/design-system.tsbuildinfo"
|
|
23
|
+
},
|
|
24
|
+
"include": ["src/**/*"],
|
|
25
|
+
"exclude": ["node_modules", "dist", "src/**/*.spec.ts", "src/**/__fixtures__"]
|
|
26
|
+
}
|