@link-foundation/ui-tokens 0.0.0-stage → 0.0.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 +24 -0
- package/README.md +254 -2
- package/bin/cli.js +4 -0
- package/package.json +69 -4
- package/src/cli.js +185 -0
- package/src/diagnostics.js +47 -0
- package/src/export/css.js +195 -0
- package/src/export/dtcg.js +216 -0
- package/src/export/kotlin.js +208 -0
- package/src/export/native.js +153 -0
- package/src/export/swift.js +187 -0
- package/src/import/dtcg.js +258 -0
- package/src/index.d.ts +181 -0
- package/src/index.js +30 -0
- package/src/model.js +216 -0
- package/src/names.js +36 -0
- package/src/resolve.js +241 -0
- package/src/style-dictionary.d.ts +12 -0
- package/src/style-dictionary.js +64 -0
- package/src/themes/boot.js +12 -0
- package/src/themes/compile-css.js +210 -0
- package/src/themes/contrast.js +78 -0
- package/src/themes/dtcg-resolver.js +27 -0
- package/src/themes/extras-css.js +91 -0
- package/src/themes/index.d.ts +75 -0
- package/src/themes/index.js +6 -0
- package/src/themes/model.js +262 -0
- package/src/themes/springs.js +35 -0
- package/src/themes/templates.js +71 -0
- package/src/types.js +70 -0
- package/src/values.js +402 -0
package/src/values.js
ADDED
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Token values: typing the Links Notation value of a `(token PATH VALUE)`
|
|
3
|
+
* link and writing it back. Primitive values (colours, dimensions,
|
|
4
|
+
* durations, numbers, `(token ...)` aliases) are the typed values of
|
|
5
|
+
* `@link-foundation/ui-core` (#15); the DTCG-only types (`fontFamily`,
|
|
6
|
+
* `fontWeight`, `cubicBezier`) and the composites (`shadow`, `typography`)
|
|
7
|
+
* are added here.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { values as coreValues } from '@link-foundation/ui-core';
|
|
11
|
+
|
|
12
|
+
const { parseColor, parseDimension, parseDuration, parseNumber, toRaw } =
|
|
13
|
+
coreValues;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @typedef {import('@link-foundation/ui-core').RawValue} RawValue
|
|
17
|
+
* @typedef {import('./types.js').TokenValue} TokenValue
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** Every token type, in the order of the README. */
|
|
21
|
+
export const TOKEN_TYPES = Object.freeze([
|
|
22
|
+
'color',
|
|
23
|
+
'dimension',
|
|
24
|
+
'duration',
|
|
25
|
+
'number',
|
|
26
|
+
'fontFamily',
|
|
27
|
+
'fontWeight',
|
|
28
|
+
'cubicBezier',
|
|
29
|
+
'shadow',
|
|
30
|
+
'typography',
|
|
31
|
+
]);
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The named font weights of DTCG 2025.10, section 8.4 "Font weight":
|
|
35
|
+
* "The value must either be a number value in the range [1, 1000] or one of
|
|
36
|
+
* the pre-defined string values defined in the table below."
|
|
37
|
+
*/
|
|
38
|
+
export const FONT_WEIGHTS = Object.freeze({
|
|
39
|
+
thin: 100,
|
|
40
|
+
hairline: 100,
|
|
41
|
+
'extra-light': 200,
|
|
42
|
+
'ultra-light': 200,
|
|
43
|
+
light: 300,
|
|
44
|
+
normal: 400,
|
|
45
|
+
regular: 400,
|
|
46
|
+
book: 400,
|
|
47
|
+
medium: 500,
|
|
48
|
+
'semi-bold': 600,
|
|
49
|
+
'demi-bold': 600,
|
|
50
|
+
bold: 700,
|
|
51
|
+
'extra-bold': 800,
|
|
52
|
+
'ultra-bold': 800,
|
|
53
|
+
black: 900,
|
|
54
|
+
heavy: 900,
|
|
55
|
+
'extra-black': 950,
|
|
56
|
+
'ultra-black': 950,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Fields of the composites: the Links Notation name, the DTCG name and the
|
|
61
|
+
* type of the field value (DTCG 2025.10 sections 9.6 "Shadow" and 9.8
|
|
62
|
+
* "Typography").
|
|
63
|
+
*/
|
|
64
|
+
export const COMPOSITE_FIELDS = Object.freeze({
|
|
65
|
+
shadow: Object.freeze([
|
|
66
|
+
['color', 'color', 'color'],
|
|
67
|
+
['offset-x', 'offsetX', 'dimension'],
|
|
68
|
+
['offset-y', 'offsetY', 'dimension'],
|
|
69
|
+
['blur', 'blur', 'dimension'],
|
|
70
|
+
['spread', 'spread', 'dimension'],
|
|
71
|
+
]),
|
|
72
|
+
typography: Object.freeze([
|
|
73
|
+
['font-family', 'fontFamily', 'fontFamily'],
|
|
74
|
+
['font-size', 'fontSize', 'dimension'],
|
|
75
|
+
['font-weight', 'fontWeight', 'fontWeight'],
|
|
76
|
+
['letter-spacing', 'letterSpacing', 'dimension'],
|
|
77
|
+
['line-height', 'lineHeight', 'number'],
|
|
78
|
+
]),
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const isAtom = (raw) => typeof raw === 'string';
|
|
82
|
+
const isLink = (raw) => Array.isArray(raw);
|
|
83
|
+
const headOf = (raw) => (isLink(raw) && isAtom(raw[0]) ? raw[0] : null);
|
|
84
|
+
|
|
85
|
+
/** `(token a.b)`: an alias of another token. */
|
|
86
|
+
export function parseAlias(raw) {
|
|
87
|
+
return headOf(raw) === 'token' && raw.length === 2 && isAtom(raw[1])
|
|
88
|
+
? { type: 'token-ref', path: raw[1] }
|
|
89
|
+
: null;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const quantity = (type, parsed) =>
|
|
93
|
+
parsed === null ? null : { type, value: parsed.value, unit: parsed.unit };
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The units of a dimension token: DTCG 2025.10 section 8.2 "Dimension":
|
|
97
|
+
* "unit: ... MUST be one of the following: px, rem" (ui-core also knows
|
|
98
|
+
* `em`, `%`, `vw` and `vh`, which have no meaning on other platforms).
|
|
99
|
+
*/
|
|
100
|
+
export const DIMENSION_UNITS = Object.freeze(['px', 'rem']);
|
|
101
|
+
|
|
102
|
+
/** A dimension in px or rem; a unitless `0` is `0px`. */
|
|
103
|
+
function parseTokenDimension(raw) {
|
|
104
|
+
const parsed = isAtom(raw) ? parseDimension(raw) : null;
|
|
105
|
+
if (parsed === null) {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
const unit = parsed.unit === '' ? 'px' : parsed.unit;
|
|
109
|
+
return DIMENSION_UNITS.includes(unit)
|
|
110
|
+
? { type: 'dimension', value: parsed.value, unit }
|
|
111
|
+
: null;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Heads of the function-like values; a list with one is no font family. */
|
|
115
|
+
const VALUE_HEADS = new Set([
|
|
116
|
+
'token',
|
|
117
|
+
'cubic-bezier',
|
|
118
|
+
'shadow',
|
|
119
|
+
'typography',
|
|
120
|
+
'oklch',
|
|
121
|
+
'rgb',
|
|
122
|
+
'rgba',
|
|
123
|
+
'hsl',
|
|
124
|
+
'hsla',
|
|
125
|
+
]);
|
|
126
|
+
|
|
127
|
+
function parseFontFamily(raw) {
|
|
128
|
+
if (isAtom(raw)) {
|
|
129
|
+
return { type: 'fontFamily', value: raw };
|
|
130
|
+
}
|
|
131
|
+
return isLink(raw) &&
|
|
132
|
+
raw.length > 0 &&
|
|
133
|
+
raw.every(isAtom) &&
|
|
134
|
+
!VALUE_HEADS.has(raw[0])
|
|
135
|
+
? { type: 'fontFamily', value: [...raw] }
|
|
136
|
+
: null;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* An undeclared font family: no name may look like a quantity (`12em`,
|
|
141
|
+
* `50%`): such a name is a mistyped dimension, not a family.
|
|
142
|
+
*/
|
|
143
|
+
function inferFontFamily(raw) {
|
|
144
|
+
const value = parseFontFamily(raw);
|
|
145
|
+
const names = value === null ? [] : [value.value].flat();
|
|
146
|
+
return names.some((name) => parseDimension(name) !== null) ? null : value;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function parseFontWeight(raw) {
|
|
150
|
+
if (!isAtom(raw)) {
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
if (Object.hasOwn(FONT_WEIGHTS, raw)) {
|
|
154
|
+
return { type: 'fontWeight', value: raw };
|
|
155
|
+
}
|
|
156
|
+
const n = parseNumber(raw);
|
|
157
|
+
return n !== null && n >= 1 && n <= 1000
|
|
158
|
+
? { type: 'fontWeight', value: n }
|
|
159
|
+
: null;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function parseCubicBezier(raw) {
|
|
163
|
+
if (headOf(raw) !== 'cubic-bezier' || raw.length !== 5) {
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
const points = raw.slice(1).map((p) => (isAtom(p) ? parseNumber(p) : null));
|
|
167
|
+
const inRange = (x) => x !== null && x >= 0 && x <= 1;
|
|
168
|
+
return points.every((p) => p !== null) &&
|
|
169
|
+
inRange(points[0]) &&
|
|
170
|
+
inRange(points[2])
|
|
171
|
+
? { type: 'cubicBezier', value: points }
|
|
172
|
+
: null;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** The field links of a composite as `name -> raw value`. */
|
|
176
|
+
function fieldMap(links) {
|
|
177
|
+
const fields = new Map();
|
|
178
|
+
for (const link of links) {
|
|
179
|
+
if (!isLink(link) || !isAtom(link[0]) || link.length < 2) {
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
fields.set(link[0], link.length === 2 ? link[1] : link.slice(1));
|
|
183
|
+
}
|
|
184
|
+
return fields;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* A composite object: every field typed with its field type (or an alias).
|
|
189
|
+
* @returns {Record<string, TokenValue>|null}
|
|
190
|
+
*/
|
|
191
|
+
function parseFields(kind, links, extra = []) {
|
|
192
|
+
const fields = fieldMap(links);
|
|
193
|
+
if (fields === null) {
|
|
194
|
+
return null;
|
|
195
|
+
}
|
|
196
|
+
const out = {};
|
|
197
|
+
for (const [name, key, type] of COMPOSITE_FIELDS[kind]) {
|
|
198
|
+
const raw = fields.get(name);
|
|
199
|
+
const value = raw === undefined ? null : typeTokenValue(raw, type);
|
|
200
|
+
if (value === null) {
|
|
201
|
+
return null;
|
|
202
|
+
}
|
|
203
|
+
out[key] = value;
|
|
204
|
+
fields.delete(name);
|
|
205
|
+
}
|
|
206
|
+
for (const name of extra) {
|
|
207
|
+
fields.delete(name);
|
|
208
|
+
}
|
|
209
|
+
return fields.size === 0 ? out : null;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function parseShadowLayer(raw) {
|
|
213
|
+
if (headOf(raw) !== 'shadow') {
|
|
214
|
+
return parseAlias(raw);
|
|
215
|
+
}
|
|
216
|
+
const layer = parseFields('shadow', raw.slice(1), ['inset']);
|
|
217
|
+
if (layer === null) {
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
const inset = fieldMap(raw.slice(1)).get('inset');
|
|
221
|
+
if (inset !== undefined) {
|
|
222
|
+
if (inset !== 'true' && inset !== 'false') {
|
|
223
|
+
return null;
|
|
224
|
+
}
|
|
225
|
+
layer.inset = inset === 'true';
|
|
226
|
+
}
|
|
227
|
+
return layer;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function parseShadow(raw) {
|
|
231
|
+
if (headOf(raw) === 'shadow') {
|
|
232
|
+
const layer = parseShadowLayer(raw);
|
|
233
|
+
return layer === null ? null : { type: 'shadow', value: layer };
|
|
234
|
+
}
|
|
235
|
+
if (!isLink(raw) || raw.length === 0 || headOf(raw) !== null) {
|
|
236
|
+
return null;
|
|
237
|
+
}
|
|
238
|
+
const layers = raw.map(parseShadowLayer);
|
|
239
|
+
return layers.includes(null) ? null : { type: 'shadow', value: layers };
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function parseTypography(raw) {
|
|
243
|
+
if (headOf(raw) !== 'typography') {
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
const value = parseFields('typography', raw.slice(1));
|
|
247
|
+
return value === null ? null : { type: 'typography', value };
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** Readers of a declared type: raw value -> token value or `null`. */
|
|
251
|
+
const READERS = {
|
|
252
|
+
color: parseColor,
|
|
253
|
+
dimension: parseTokenDimension,
|
|
254
|
+
duration: (raw) =>
|
|
255
|
+
isAtom(raw) ? quantity('duration', parseDuration(raw)) : null,
|
|
256
|
+
number: (raw) => {
|
|
257
|
+
const value = isAtom(raw) ? parseNumber(raw) : null;
|
|
258
|
+
return value === null ? null : { type: 'number', value };
|
|
259
|
+
},
|
|
260
|
+
fontFamily: parseFontFamily,
|
|
261
|
+
fontWeight: parseFontWeight,
|
|
262
|
+
cubicBezier: parseCubicBezier,
|
|
263
|
+
shadow: parseShadow,
|
|
264
|
+
typography: parseTypography,
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
/** The order in which an undeclared value is tried; `fontFamily` is last. */
|
|
268
|
+
const INFER_ORDER = [
|
|
269
|
+
'number',
|
|
270
|
+
'dimension',
|
|
271
|
+
'duration',
|
|
272
|
+
'color',
|
|
273
|
+
'fontWeight',
|
|
274
|
+
'cubicBezier',
|
|
275
|
+
'shadow',
|
|
276
|
+
'typography',
|
|
277
|
+
'fontFamily',
|
|
278
|
+
];
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Type the value of a token.
|
|
282
|
+
*
|
|
283
|
+
* With a declared type the value must be of that type. Without one the type
|
|
284
|
+
* is read from the spelling: `16px` is a dimension, `150ms` a duration,
|
|
285
|
+
* `'#6750A4'` and `(oklch ...)` colours, `1.5` a number, `bold` a font
|
|
286
|
+
* weight, `(cubic-bezier ...)`, `(shadow ...)` and `(typography ...)` are
|
|
287
|
+
* what they say, and any other atom or a list of atoms is a font family.
|
|
288
|
+
* An alias `(token a.b)` is accepted with any declared type; the resolver
|
|
289
|
+
* checks it against the type of its target.
|
|
290
|
+
* @param {RawValue} raw
|
|
291
|
+
* @param {string} [declared] one of {@link TOKEN_TYPES}
|
|
292
|
+
* @returns {TokenValue|null} `null` when the value is invalid
|
|
293
|
+
*/
|
|
294
|
+
export function typeTokenValue(raw, declared) {
|
|
295
|
+
const alias = parseAlias(raw);
|
|
296
|
+
if (alias !== null) {
|
|
297
|
+
return alias;
|
|
298
|
+
}
|
|
299
|
+
if (declared !== undefined) {
|
|
300
|
+
return Object.hasOwn(READERS, declared) ? READERS[declared](raw) : null;
|
|
301
|
+
}
|
|
302
|
+
for (const type of INFER_ORDER) {
|
|
303
|
+
const value =
|
|
304
|
+
type === 'fontFamily' ? inferFontFamily(raw) : READERS[type](raw);
|
|
305
|
+
if (value !== null) {
|
|
306
|
+
return value;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
return null;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
const num = (n) => String(n);
|
|
313
|
+
|
|
314
|
+
function fieldsRaw(kind, object) {
|
|
315
|
+
return COMPOSITE_FIELDS[kind].map(([name, key]) => {
|
|
316
|
+
const raw = tokenValueRaw(object[key]);
|
|
317
|
+
return isLink(raw) && headOf(raw) === null && raw.every(isAtom)
|
|
318
|
+
? [name, ...raw]
|
|
319
|
+
: [name, raw];
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function shadowLayerRaw(layer) {
|
|
324
|
+
if (layer.type === 'token-ref') {
|
|
325
|
+
return toRaw(layer);
|
|
326
|
+
}
|
|
327
|
+
const inset =
|
|
328
|
+
layer.inset === undefined ? [] : [['inset', String(layer.inset)]];
|
|
329
|
+
return ['shadow', ...fieldsRaw('shadow', layer), ...inset];
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* A token value as the raw Links Notation value it is written as; the
|
|
334
|
+
* inverse of {@link typeTokenValue}.
|
|
335
|
+
* @param {TokenValue} value
|
|
336
|
+
* @returns {RawValue}
|
|
337
|
+
*/
|
|
338
|
+
export function tokenValueRaw(value) {
|
|
339
|
+
switch (value.type) {
|
|
340
|
+
case 'fontFamily':
|
|
341
|
+
return Array.isArray(value.value) ? [...value.value] : value.value;
|
|
342
|
+
case 'fontWeight':
|
|
343
|
+
return num(value.value);
|
|
344
|
+
case 'cubicBezier':
|
|
345
|
+
return ['cubic-bezier', ...value.value.map(num)];
|
|
346
|
+
case 'shadow':
|
|
347
|
+
return Array.isArray(value.value)
|
|
348
|
+
? value.value.map(shadowLayerRaw)
|
|
349
|
+
: shadowLayerRaw(value.value);
|
|
350
|
+
case 'typography':
|
|
351
|
+
return ['typography', ...fieldsRaw('typography', value.value)];
|
|
352
|
+
default:
|
|
353
|
+
return toRaw(value);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* The type a value would be inferred as without `(type ...)`, used to
|
|
359
|
+
* decide when the writers must spell the type out.
|
|
360
|
+
* @param {RawValue} raw
|
|
361
|
+
* @returns {string|null}
|
|
362
|
+
*/
|
|
363
|
+
export function inferredType(raw) {
|
|
364
|
+
return typeTokenValue(raw)?.type ?? null;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Replace every alias inside a composite (a field, or a layer of a shadow)
|
|
369
|
+
* with `replace(ref, type)`, where `type` is the type the alias must
|
|
370
|
+
* resolve to. A shadow layer that is an alias of a shadow token is replaced
|
|
371
|
+
* by the layers of that token. Other values are returned unchanged.
|
|
372
|
+
* @param {TokenValue} value
|
|
373
|
+
* @param {(ref: { type: 'token-ref', path: string }, type: string) => TokenValue} replace
|
|
374
|
+
* @returns {TokenValue}
|
|
375
|
+
*/
|
|
376
|
+
export function mapNestedAliases(value, replace) {
|
|
377
|
+
const fields = (kind, object) => {
|
|
378
|
+
const out = { ...object };
|
|
379
|
+
for (const [, key, type] of COMPOSITE_FIELDS[kind]) {
|
|
380
|
+
if (object[key].type === 'token-ref') {
|
|
381
|
+
out[key] = replace(object[key], type);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
return out;
|
|
385
|
+
};
|
|
386
|
+
if (value.type === 'typography') {
|
|
387
|
+
return { ...value, value: fields('typography', value.value) };
|
|
388
|
+
}
|
|
389
|
+
if (value.type !== 'shadow') {
|
|
390
|
+
return value;
|
|
391
|
+
}
|
|
392
|
+
const layer = (l) => {
|
|
393
|
+
if (l.type !== 'token-ref') {
|
|
394
|
+
return fields('shadow', l);
|
|
395
|
+
}
|
|
396
|
+
const target = replace(l, 'shadow');
|
|
397
|
+
return target.type === 'shadow' ? target.value : l;
|
|
398
|
+
};
|
|
399
|
+
return Array.isArray(value.value)
|
|
400
|
+
? { ...value, value: value.value.flatMap(layer) }
|
|
401
|
+
: { ...value, value: layer(value.value) };
|
|
402
|
+
}
|