@plexui/styles 0.16.6 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/es/components/Accordion/Accordion.module.css +74 -12
- package/dist/es/components/Alert/Alert.module.css +196 -0
- package/dist/es/components/Breadcrumb/Breadcrumb.module.css +32 -1
- package/dist/es/components/Button/Button.module.css +483 -5
- package/dist/es/components/Button/NavigationButton.module.css +195 -0
- package/dist/es/components/Checkbox/Checkbox.module.css +246 -0
- package/dist/es/components/CodeBlock/CodeBlock.module.css +15 -1
- package/dist/es/components/DatePicker/Calendar.module.css +216 -0
- package/dist/es/components/DatePicker/CalendarNavigationIcon.module.css +19 -0
- package/dist/es/components/DateRangePicker/Calendar.module.css +200 -0
- package/dist/es/components/DateRangePicker/Trigger.module.css +15 -1
- package/dist/es/components/Dialog/Dialog.module.css +456 -0
- package/dist/es/components/Field/Field.module.css +713 -0
- package/dist/es/components/FieldGroup/FieldGroup.module.css +272 -0
- package/dist/es/components/FloatingLabelInput/FloatingLabelInput.module.css +49 -0
- package/dist/es/components/FloatingLabelSelect/FloatingLabelSelect.module.css +81 -0
- package/dist/es/components/Input/Input.module.css +184 -0
- package/dist/es/components/InputGroup/InputGroup.module.css +17 -0
- package/dist/es/components/MarkdownEditor/MarkdownEditor.module.css +60 -0
- package/dist/es/components/Menu/Menu.module.css +244 -0
- package/dist/es/components/NavigationRow/NavigationRow.module.css +82 -0
- package/dist/es/components/Page/Page.module.css +278 -0
- package/dist/es/components/PageControl/PageControl.module.css +35 -0
- package/dist/es/components/Popover/Popover.module.css +20 -0
- package/dist/es/components/RadioGroup/RadioGroup.module.css +151 -0
- package/dist/es/components/Select/Select.module.css +315 -0
- package/dist/es/components/SelectControl/SelectControl.module.css +104 -0
- package/dist/es/components/Sidebar/Sidebar.module.css +169 -0
- package/dist/es/components/Skeleton/Skeleton.module.css +50 -0
- package/dist/es/components/Slider/Slider.module.css +12 -1
- package/dist/es/components/Switch/Switch.module.css +143 -0
- package/dist/es/components/TabBar/TabBar.module.css +354 -0
- package/dist/es/components/Tabs/Tabs.module.css +162 -0
- package/dist/es/components/Tip/Tip.module.css +225 -0
- package/dist/es/components/Toast/Toast.module.css +207 -0
- package/dist/es/components/Toolbar/Toolbar.module.css +1542 -0
- package/dist/es/components/Tooltip/Tooltip.module.css +44 -0
- package/dist/es/styles/android.css +882 -0
- package/dist/es/styles/touch.css +672 -0
- package/dist/es/styles/variables-components.css +4 -0
- package/dist/es/styles/variables-semantic.css +28 -0
- package/dist/es/tokens/index.js +5 -0
- package/dist/es/tokens/index.js.map +1 -0
- package/dist/es/tokens/resolve.js +746 -0
- package/dist/es/tokens/resolve.js.map +1 -0
- package/dist/es/tokens/tokens.generated.js +5872 -0
- package/dist/es/tokens/tokens.generated.js.map +1 -0
- package/dist/types/tokens/index.d.ts +19 -0
- package/dist/types/tokens/resolve.d.ts +145 -0
- package/dist/types/tokens/tokens.generated.d.ts +13 -0
- package/package.json +7 -2
|
@@ -0,0 +1,746 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A small evaluator of Plex UI token values, for platforms that do not read CSS (React Native).
|
|
3
|
+
*
|
|
4
|
+
* It resolves a custom property the way a browser does on `:root`: `var()` is substituted as text, then the value is
|
|
5
|
+
* evaluated for one color scheme. It knows the functions the token files use: `light-dark()`, `alpha()` and `spacing()`
|
|
6
|
+
* (the PostCSS functions of @plexui/styles), `color-mix()` in srgb and oklab, `rgb()`/`rgba()`, hex colors, `calc()`,
|
|
7
|
+
* `min()`, `max()`, `clamp()` and `env()`. Lengths come out in px (1rem = 16px; em only with a font size), times in ms.
|
|
8
|
+
* Anything else (a gradient, a curve, a font stack, a shadow list) comes back as text with its parts resolved.
|
|
9
|
+
*
|
|
10
|
+
* This file has no imports and only erasable TypeScript, so Node runs it as is (scripts/build-tokens.mjs).
|
|
11
|
+
*/
|
|
12
|
+
const isDigit = (c) => c >= "0" && c <= "9";
|
|
13
|
+
const isIdentStart = (c) => /[A-Za-z_\u0080-]/.test(c);
|
|
14
|
+
const isIdent = (c) => /[A-Za-z0-9_\-\u0080-]/.test(c);
|
|
15
|
+
/** Parses a CSS value into nodes. Whitespace separates nodes; commas and operators are nodes of their own. */
|
|
16
|
+
export function parse(text) {
|
|
17
|
+
let i = 0;
|
|
18
|
+
const src = text.replace(/\/\*[\s\S]*?\*\//g, " ");
|
|
19
|
+
const readNumber = () => {
|
|
20
|
+
const match = /^[+-]?(\d+\.?\d*|\.\d+)(e[+-]?\d+)?/i.exec(src.slice(i));
|
|
21
|
+
i += match[0].length;
|
|
22
|
+
const value = parseFloat(match[0]);
|
|
23
|
+
if (src[i] === "%") {
|
|
24
|
+
i++;
|
|
25
|
+
return { type: "dim", value, unit: "%" };
|
|
26
|
+
}
|
|
27
|
+
let unit = "";
|
|
28
|
+
while (i < src.length && /[A-Za-z]/.test(src[i]))
|
|
29
|
+
unit += src[i++];
|
|
30
|
+
return { type: "dim", value, unit: unit.toLowerCase() };
|
|
31
|
+
};
|
|
32
|
+
const readList = (close) => {
|
|
33
|
+
const nodes = [];
|
|
34
|
+
while (i < src.length) {
|
|
35
|
+
const c = src[i];
|
|
36
|
+
if (close && c === close) {
|
|
37
|
+
i++;
|
|
38
|
+
return nodes;
|
|
39
|
+
}
|
|
40
|
+
if (/\s/.test(c)) {
|
|
41
|
+
i++;
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (c === ",") {
|
|
45
|
+
nodes.push({ type: "comma" });
|
|
46
|
+
i++;
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (c === "(") {
|
|
50
|
+
i++;
|
|
51
|
+
nodes.push({ type: "paren", nodes: readList(")") });
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
if (c === '"' || c === "'") {
|
|
55
|
+
let j = i + 1;
|
|
56
|
+
while (j < src.length && src[j] !== c)
|
|
57
|
+
j += src[j] === "\\" ? 2 : 1;
|
|
58
|
+
nodes.push({ type: "string", value: src.slice(i + 1, j) });
|
|
59
|
+
i = j + 1;
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (c === "#") {
|
|
63
|
+
let j = i + 1;
|
|
64
|
+
while (j < src.length && isIdent(src[j]))
|
|
65
|
+
j++;
|
|
66
|
+
nodes.push({ type: "hash", value: src.slice(i + 1, j).toLowerCase() });
|
|
67
|
+
i = j;
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
const next = src[i + 1] ?? "";
|
|
71
|
+
// A sign right before a digit starts a number, as in the CSS tokenizer (calc() needs spaces around + and -)
|
|
72
|
+
if (isDigit(c) || (c === "." && isDigit(next)) || ((c === "+" || c === "-") && (isDigit(next) || (next === "." && isDigit(src[i + 2] ?? ""))))) {
|
|
73
|
+
nodes.push(readNumber());
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (isIdentStart(c) || (c === "-" && (isIdentStart(next) || next === "-"))) {
|
|
77
|
+
let j = i;
|
|
78
|
+
while (j < src.length && isIdent(src[j]))
|
|
79
|
+
j++;
|
|
80
|
+
const name = src.slice(i, j);
|
|
81
|
+
i = j;
|
|
82
|
+
if (src[i] === "(") {
|
|
83
|
+
i++;
|
|
84
|
+
nodes.push({ type: "fn", name: name.toLowerCase(), args: splitArgs(readList(")")) });
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
nodes.push({ type: "ident", value: name });
|
|
88
|
+
}
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
nodes.push({ type: "op", value: c });
|
|
92
|
+
i++;
|
|
93
|
+
}
|
|
94
|
+
return nodes;
|
|
95
|
+
};
|
|
96
|
+
return readList(null);
|
|
97
|
+
}
|
|
98
|
+
function splitArgs(nodes) {
|
|
99
|
+
const args = [[]];
|
|
100
|
+
for (const node of nodes) {
|
|
101
|
+
if (node.type === "comma")
|
|
102
|
+
args.push([]);
|
|
103
|
+
else
|
|
104
|
+
args[args.length - 1].push(node);
|
|
105
|
+
}
|
|
106
|
+
return args.length === 1 && args[0].length === 0 ? [] : args;
|
|
107
|
+
}
|
|
108
|
+
export function stringify(nodes) {
|
|
109
|
+
let out = "";
|
|
110
|
+
for (const node of nodes) {
|
|
111
|
+
const part = stringifyNode(node);
|
|
112
|
+
if (node.type === "comma")
|
|
113
|
+
out += ",";
|
|
114
|
+
else
|
|
115
|
+
out += (out && !out.endsWith(",") ? " " : out ? " " : "") + part;
|
|
116
|
+
}
|
|
117
|
+
return out;
|
|
118
|
+
}
|
|
119
|
+
function stringifyNode(node) {
|
|
120
|
+
switch (node.type) {
|
|
121
|
+
case "fn":
|
|
122
|
+
return `${node.name}(${node.args.map(stringify).join(", ")})`;
|
|
123
|
+
case "dim":
|
|
124
|
+
return `${formatNumber(node.value)}${node.unit}`;
|
|
125
|
+
case "hash":
|
|
126
|
+
return `#${node.value}`;
|
|
127
|
+
case "ident":
|
|
128
|
+
return node.value;
|
|
129
|
+
case "string":
|
|
130
|
+
return `"${node.value}"`;
|
|
131
|
+
case "op":
|
|
132
|
+
return node.value;
|
|
133
|
+
case "comma":
|
|
134
|
+
return ",";
|
|
135
|
+
case "paren":
|
|
136
|
+
return `(${stringify(node.nodes)})`;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/** A `var()` cycle: every property in it is invalid at computed-value time, whatever fallbacks it has. */
|
|
140
|
+
class Cycle {
|
|
141
|
+
members;
|
|
142
|
+
constructor(members) {
|
|
143
|
+
this.members = members;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Substitutes every `var()` in a value with the value of its property, recursively, as the browser does at computed
|
|
148
|
+
* value time. A missing or invalid property takes the fallback; a property in a cycle is invalid and so is every
|
|
149
|
+
* value of the cycle, fallbacks or not (CSS Variables 1, Resolving Dependency Cycles), while a value outside it that
|
|
150
|
+
* refers to it takes its fallback. `alpha()` and `spacing()` are expanded as the PostCSS functions of @plexui/styles
|
|
151
|
+
* expand them. `seen` is the chain of properties being resolved, the last one the property this value belongs to.
|
|
152
|
+
*/
|
|
153
|
+
export function substitute(text, lookup, seen = new Set()) {
|
|
154
|
+
try {
|
|
155
|
+
return substituteChain(text, lookup, [...seen]);
|
|
156
|
+
}
|
|
157
|
+
catch (error) {
|
|
158
|
+
if (error instanceof Cycle)
|
|
159
|
+
return undefined;
|
|
160
|
+
throw error;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
function substituteChain(text, lookup, chain) {
|
|
164
|
+
const self = chain[chain.length - 1];
|
|
165
|
+
const walk = (nodes) => {
|
|
166
|
+
const out = [];
|
|
167
|
+
for (const node of nodes) {
|
|
168
|
+
if (node.type === "fn" && node.name === "var") {
|
|
169
|
+
const nameNode = node.args[0]?.[0];
|
|
170
|
+
const name = nameNode?.type === "ident" ? nameNode.value : undefined;
|
|
171
|
+
const fallback = node.args.length > 1 ? node.args.slice(1) : undefined;
|
|
172
|
+
let replacement;
|
|
173
|
+
if (name && chain.includes(name))
|
|
174
|
+
throw new Cycle(new Set(chain.slice(chain.indexOf(name))));
|
|
175
|
+
const raw = name ? lookup(name) : undefined;
|
|
176
|
+
if (name && raw !== undefined) {
|
|
177
|
+
try {
|
|
178
|
+
replacement = substituteChain(raw, lookup, [...chain, name]);
|
|
179
|
+
}
|
|
180
|
+
catch (error) {
|
|
181
|
+
// This value is in the cycle too: invalid, fallback or not. Outside it, the property is merely invalid.
|
|
182
|
+
if (!(error instanceof Cycle) || (self !== undefined && error.members.has(self)))
|
|
183
|
+
throw error;
|
|
184
|
+
replacement = undefined;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (replacement === undefined && fallback) {
|
|
188
|
+
// The fallback is everything after the first comma, commas included
|
|
189
|
+
const joined = [];
|
|
190
|
+
fallback.forEach((arg, index) => {
|
|
191
|
+
if (index)
|
|
192
|
+
joined.push({ type: "comma" });
|
|
193
|
+
joined.push(...arg);
|
|
194
|
+
});
|
|
195
|
+
replacement = walk(joined);
|
|
196
|
+
}
|
|
197
|
+
if (replacement === undefined)
|
|
198
|
+
return undefined;
|
|
199
|
+
out.push(...replacement);
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
if (node.type === "fn" && node.name === "spacing" && node.args.length === 1) {
|
|
203
|
+
// postcss/functions.mjs: spacing(n) is calc(var(--spacing) * n), expanded before var() is
|
|
204
|
+
const expanded = walk(parse(`calc(var(--spacing) * ${stringify(node.args[0])})`));
|
|
205
|
+
if (!expanded)
|
|
206
|
+
return undefined;
|
|
207
|
+
out.push(...expanded);
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
if (node.type === "fn") {
|
|
211
|
+
const args = [];
|
|
212
|
+
for (const arg of node.args) {
|
|
213
|
+
const next = walk(arg);
|
|
214
|
+
if (!next)
|
|
215
|
+
return undefined;
|
|
216
|
+
args.push(next);
|
|
217
|
+
}
|
|
218
|
+
out.push(expandFunction({ type: "fn", name: node.name, args }));
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
if (node.type === "paren") {
|
|
222
|
+
const inner = walk(node.nodes);
|
|
223
|
+
if (!inner)
|
|
224
|
+
return undefined;
|
|
225
|
+
out.push({ type: "paren", nodes: inner });
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
out.push(node);
|
|
229
|
+
}
|
|
230
|
+
return out;
|
|
231
|
+
};
|
|
232
|
+
// The comma-separated fallback of a var() may hold commas: re-split function arguments after substitution
|
|
233
|
+
const result = walk(parse(text));
|
|
234
|
+
return result && resplit(result);
|
|
235
|
+
}
|
|
236
|
+
function resplit(nodes) {
|
|
237
|
+
return nodes.map((node) => {
|
|
238
|
+
if (node.type === "fn") {
|
|
239
|
+
const flat = [];
|
|
240
|
+
node.args.forEach((arg, index) => {
|
|
241
|
+
if (index)
|
|
242
|
+
flat.push({ type: "comma" });
|
|
243
|
+
flat.push(...resplit(arg));
|
|
244
|
+
});
|
|
245
|
+
return { type: "fn", name: node.name, args: splitArgs(flat) };
|
|
246
|
+
}
|
|
247
|
+
if (node.type === "paren")
|
|
248
|
+
return { type: "paren", nodes: resplit(node.nodes) };
|
|
249
|
+
return node;
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
// The PostCSS functions of @plexui/styles (postcss/functions.mjs), applied after substitution
|
|
253
|
+
function expandFunction(node) {
|
|
254
|
+
if (node.name === "alpha" && node.args.length === 2) {
|
|
255
|
+
const amount = node.args[1];
|
|
256
|
+
let percent = amount;
|
|
257
|
+
if (amount.length === 1 && amount[0].type === "dim" && amount[0].unit === "") {
|
|
258
|
+
const raw = amount[0].value;
|
|
259
|
+
// ".5" and "0.5" are fractions, "8" is 8%
|
|
260
|
+
percent = [{ type: "dim", value: raw < 1 ? raw * 100 : raw, unit: "%" }];
|
|
261
|
+
}
|
|
262
|
+
return {
|
|
263
|
+
type: "fn",
|
|
264
|
+
name: "color-mix",
|
|
265
|
+
args: [[{ type: "ident", value: "in" }, { type: "ident", value: "oklab" }], [...node.args[0], ...percent], [{ type: "ident", value: "transparent" }]],
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
return node;
|
|
269
|
+
}
|
|
270
|
+
/* ------------------------------------------------------------------ */
|
|
271
|
+
/* Colors */
|
|
272
|
+
/* ------------------------------------------------------------------ */
|
|
273
|
+
const NAMED = {
|
|
274
|
+
transparent: { r: 0, g: 0, b: 0, a: 0 },
|
|
275
|
+
white: { r: 255, g: 255, b: 255, a: 1 },
|
|
276
|
+
black: { r: 0, g: 0, b: 0, a: 1 },
|
|
277
|
+
red: { r: 255, g: 0, b: 0, a: 1 },
|
|
278
|
+
};
|
|
279
|
+
function parseHex(hex) {
|
|
280
|
+
if (!/^[0-9a-f]+$/.test(hex))
|
|
281
|
+
return undefined;
|
|
282
|
+
const expand = hex.length === 3 || hex.length === 4 ? hex.split("").map((c) => c + c).join("") : hex;
|
|
283
|
+
if (expand.length !== 6 && expand.length !== 8)
|
|
284
|
+
return undefined;
|
|
285
|
+
const n = (k) => parseInt(expand.slice(k, k + 2), 16);
|
|
286
|
+
return { r: n(0), g: n(2), b: n(4), a: expand.length === 8 ? n(6) / 255 : 1 };
|
|
287
|
+
}
|
|
288
|
+
const toLinear = (c) => {
|
|
289
|
+
const v = c / 255;
|
|
290
|
+
return v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4;
|
|
291
|
+
};
|
|
292
|
+
const fromLinear = (v) => {
|
|
293
|
+
const sign = v < 0 ? -1 : 1;
|
|
294
|
+
const abs = Math.abs(v);
|
|
295
|
+
return 255 * sign * (abs <= 0.0031308 ? 12.92 * abs : 1.055 * abs ** (1 / 2.4) - 0.055);
|
|
296
|
+
};
|
|
297
|
+
/** sRGB (0-255) to OKLab. */
|
|
298
|
+
export function toOklab({ r, g, b }) {
|
|
299
|
+
const lr = toLinear(r);
|
|
300
|
+
const lg = toLinear(g);
|
|
301
|
+
const lb = toLinear(b);
|
|
302
|
+
const l = Math.cbrt(0.4122214708 * lr + 0.5363325363 * lg + 0.0514459929 * lb);
|
|
303
|
+
const m = Math.cbrt(0.2119034982 * lr + 0.6806995451 * lg + 0.1073969566 * lb);
|
|
304
|
+
const s = Math.cbrt(0.0883024619 * lr + 0.2817188376 * lg + 0.6299787005 * lb);
|
|
305
|
+
return [
|
|
306
|
+
0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s,
|
|
307
|
+
1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s,
|
|
308
|
+
0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s,
|
|
309
|
+
];
|
|
310
|
+
}
|
|
311
|
+
/** OKLab to sRGB (0-255, unclamped). */
|
|
312
|
+
export function fromOklab([L, A, B]) {
|
|
313
|
+
const l = (L + 0.3963377774 * A + 0.2158037573 * B) ** 3;
|
|
314
|
+
const m = (L - 0.1055613458 * A - 0.0638541728 * B) ** 3;
|
|
315
|
+
const s = (L - 0.0894841775 * A - 1.291485548 * B) ** 3;
|
|
316
|
+
return [
|
|
317
|
+
fromLinear(4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s),
|
|
318
|
+
fromLinear(-1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s),
|
|
319
|
+
fromLinear(-0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s),
|
|
320
|
+
];
|
|
321
|
+
}
|
|
322
|
+
const clamp = (v, min, max) => Math.min(max, Math.max(min, v));
|
|
323
|
+
/** `color-mix()` after CSS Color 5: normalized percentages, premultiplied alpha, an alpha multiplier below 100%. */
|
|
324
|
+
export function mixColors(space, c1, p1, c2, p2) {
|
|
325
|
+
let w1 = p1;
|
|
326
|
+
let w2 = p2;
|
|
327
|
+
if (w1 === undefined && w2 === undefined)
|
|
328
|
+
w1 = w2 = 50;
|
|
329
|
+
else if (w1 === undefined)
|
|
330
|
+
w1 = 100 - w2;
|
|
331
|
+
else if (w2 === undefined)
|
|
332
|
+
w2 = 100 - w1;
|
|
333
|
+
const sum = w1 + w2;
|
|
334
|
+
if (sum <= 0)
|
|
335
|
+
return undefined;
|
|
336
|
+
const multiplier = sum < 100 ? sum / 100 : 1;
|
|
337
|
+
const t = w2 / sum;
|
|
338
|
+
const alpha = c1.a * (1 - t) + c2.a * t;
|
|
339
|
+
const channels = (c) => space === "srgb" ? [c.r / 255, c.g / 255, c.b / 255] : toOklab(c);
|
|
340
|
+
const a1 = channels(c1);
|
|
341
|
+
const a2 = channels(c2);
|
|
342
|
+
const mixed = [0, 1, 2].map((k) => (alpha === 0 ? 0 : (a1[k] * c1.a * (1 - t) + a2[k] * c2.a * t) / alpha));
|
|
343
|
+
const rgb = space === "srgb" ? mixed.map((v) => v * 255) : fromOklab(mixed);
|
|
344
|
+
return {
|
|
345
|
+
r: clamp(rgb[0], 0, 255),
|
|
346
|
+
g: clamp(rgb[1], 0, 255),
|
|
347
|
+
b: clamp(rgb[2], 0, 255),
|
|
348
|
+
a: clamp(alpha * multiplier, 0, 1),
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
export function formatColor({ r, g, b, a }) {
|
|
352
|
+
const hex = (v) => Math.round(v).toString(16).padStart(2, "0");
|
|
353
|
+
if (a >= 0.9995)
|
|
354
|
+
return `#${hex(r)}${hex(g)}${hex(b)}`;
|
|
355
|
+
return `rgba(${Math.round(r)}, ${Math.round(g)}, ${Math.round(b)}, ${formatNumber(Math.round(a * 1000) / 1000)})`;
|
|
356
|
+
}
|
|
357
|
+
function formatNumber(n) {
|
|
358
|
+
const rounded = Math.round(n * 10000) / 10000;
|
|
359
|
+
return Object.is(rounded, -0) ? "0" : String(rounded);
|
|
360
|
+
}
|
|
361
|
+
/* ------------------------------------------------------------------ */
|
|
362
|
+
/* Evaluation */
|
|
363
|
+
/* ------------------------------------------------------------------ */
|
|
364
|
+
const text = (t) => ({ kind: "text", text: t });
|
|
365
|
+
function dimension(node, options) {
|
|
366
|
+
const root = options.rootFontSize ?? 16;
|
|
367
|
+
switch (node.unit) {
|
|
368
|
+
case "":
|
|
369
|
+
return { kind: "number", value: node.value };
|
|
370
|
+
case "%":
|
|
371
|
+
return { kind: "percent", value: node.value };
|
|
372
|
+
case "px":
|
|
373
|
+
return { kind: "length", px: node.value };
|
|
374
|
+
case "rem":
|
|
375
|
+
return { kind: "length", px: node.value * root };
|
|
376
|
+
case "em":
|
|
377
|
+
return options.fontSize === undefined ? { kind: "em", em: node.value } : { kind: "length", px: node.value * options.fontSize };
|
|
378
|
+
case "ms":
|
|
379
|
+
return { kind: "time", ms: node.value };
|
|
380
|
+
case "s":
|
|
381
|
+
return { kind: "time", ms: node.value * 1000 };
|
|
382
|
+
case "pt":
|
|
383
|
+
return { kind: "length", px: (node.value * 4) / 3 };
|
|
384
|
+
default:
|
|
385
|
+
return text(stringifyNode(node));
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
function asColor(value) {
|
|
389
|
+
return value?.kind === "color" ? value.color : undefined;
|
|
390
|
+
}
|
|
391
|
+
function channel(value, scale) {
|
|
392
|
+
if (value?.kind === "number")
|
|
393
|
+
return value.value;
|
|
394
|
+
if (value?.kind === "percent")
|
|
395
|
+
return (value.value / 100) * scale;
|
|
396
|
+
return undefined;
|
|
397
|
+
}
|
|
398
|
+
function evalRgb(args, options) {
|
|
399
|
+
// Legacy comma syntax (rgb(0, 0, 0, 0.1)) or modern space syntax (rgb(0 0 0 / 10%))
|
|
400
|
+
let parts;
|
|
401
|
+
let alphaNodes;
|
|
402
|
+
if (args.length >= 3) {
|
|
403
|
+
parts = [args[0], args[1], args[2]].map((arg) => arg[0]);
|
|
404
|
+
alphaNodes = args[3];
|
|
405
|
+
}
|
|
406
|
+
else {
|
|
407
|
+
const all = args[0] ?? [];
|
|
408
|
+
const slash = all.findIndex((node) => node.type === "op" && node.value === "/");
|
|
409
|
+
parts = slash === -1 ? all : all.slice(0, slash);
|
|
410
|
+
alphaNodes = slash === -1 ? undefined : all.slice(slash + 1);
|
|
411
|
+
}
|
|
412
|
+
if (parts.length !== 3)
|
|
413
|
+
return undefined;
|
|
414
|
+
const rgb = parts.map((node) => channel(evaluateNodes([node], options), 255));
|
|
415
|
+
if (rgb.some((v) => v === undefined))
|
|
416
|
+
return undefined;
|
|
417
|
+
let a = 1;
|
|
418
|
+
if (alphaNodes && alphaNodes.length) {
|
|
419
|
+
const alpha = channel(evaluateNodes(alphaNodes, options), 1);
|
|
420
|
+
if (alpha === undefined)
|
|
421
|
+
return undefined;
|
|
422
|
+
a = alpha;
|
|
423
|
+
}
|
|
424
|
+
return { kind: "color", color: { r: clamp(rgb[0], 0, 255), g: clamp(rgb[1], 0, 255), b: clamp(rgb[2], 0, 255), a: clamp(a, 0, 1) } };
|
|
425
|
+
}
|
|
426
|
+
function evalColorMix(args, options) {
|
|
427
|
+
const [method, first, second] = args;
|
|
428
|
+
if (!method || !first || !second || method[0]?.type !== "ident" || method[0].value !== "in")
|
|
429
|
+
return undefined;
|
|
430
|
+
const space = method[1]?.type === "ident" ? method[1].value.toLowerCase() : "";
|
|
431
|
+
if (space !== "srgb" && space !== "oklab")
|
|
432
|
+
return undefined;
|
|
433
|
+
const part = (nodes) => {
|
|
434
|
+
const percentIndex = nodes.findIndex((node) => node.type === "dim" && node.unit === "%");
|
|
435
|
+
const colorNodes = percentIndex === -1 ? nodes : nodes.filter((_, index) => index !== percentIndex);
|
|
436
|
+
const percent = percentIndex === -1 ? undefined : nodes[percentIndex].value;
|
|
437
|
+
return [asColor(evaluateNodes(colorNodes, options)), percent];
|
|
438
|
+
};
|
|
439
|
+
const [c1, p1] = part(first);
|
|
440
|
+
const [c2, p2] = part(second);
|
|
441
|
+
if (!c1 || !c2)
|
|
442
|
+
return undefined;
|
|
443
|
+
const color = mixColors(space, c1, p1, c2, p2);
|
|
444
|
+
return color && { kind: "color", color };
|
|
445
|
+
}
|
|
446
|
+
/* calc(): a recursive descent over + - * / with lengths, numbers, percents and times */
|
|
447
|
+
function evalMath(nodes, options) {
|
|
448
|
+
let index = 0;
|
|
449
|
+
const peekOp = () => {
|
|
450
|
+
const node = nodes[index];
|
|
451
|
+
return node?.type === "op" ? node.value : undefined;
|
|
452
|
+
};
|
|
453
|
+
const primary = () => {
|
|
454
|
+
const node = nodes[index++];
|
|
455
|
+
if (!node)
|
|
456
|
+
return undefined;
|
|
457
|
+
if (node.type === "paren")
|
|
458
|
+
return evalMath(node.nodes, options);
|
|
459
|
+
if (node.type === "op" && node.value === "-") {
|
|
460
|
+
const inner = primary();
|
|
461
|
+
return inner && scale(inner, -1);
|
|
462
|
+
}
|
|
463
|
+
return evaluateNodes([node], options);
|
|
464
|
+
};
|
|
465
|
+
const term = () => {
|
|
466
|
+
let left = primary();
|
|
467
|
+
while (left && (peekOp() === "*" || peekOp() === "/")) {
|
|
468
|
+
const op = nodes[index++];
|
|
469
|
+
const right = primary();
|
|
470
|
+
if (!right)
|
|
471
|
+
return undefined;
|
|
472
|
+
if (op.value === "*") {
|
|
473
|
+
if (right.kind === "number")
|
|
474
|
+
left = scale(left, right.value);
|
|
475
|
+
else if (left.kind === "number")
|
|
476
|
+
left = scale(right, left.value);
|
|
477
|
+
else
|
|
478
|
+
return undefined;
|
|
479
|
+
}
|
|
480
|
+
else {
|
|
481
|
+
if (right.kind !== "number" || right.value === 0)
|
|
482
|
+
return undefined;
|
|
483
|
+
left = scale(left, 1 / right.value);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
return left;
|
|
487
|
+
};
|
|
488
|
+
let left = term();
|
|
489
|
+
while (left && (peekOp() === "+" || peekOp() === "-")) {
|
|
490
|
+
const op = nodes[index++].value;
|
|
491
|
+
const right = term();
|
|
492
|
+
if (!right)
|
|
493
|
+
return undefined;
|
|
494
|
+
left = add(left, op === "-" ? scale(right, -1) : right);
|
|
495
|
+
}
|
|
496
|
+
if (index < nodes.length)
|
|
497
|
+
return undefined;
|
|
498
|
+
return left;
|
|
499
|
+
}
|
|
500
|
+
function scale(value, factor) {
|
|
501
|
+
switch (value.kind) {
|
|
502
|
+
case "length":
|
|
503
|
+
return { kind: "length", px: value.px * factor };
|
|
504
|
+
case "em":
|
|
505
|
+
return { kind: "em", em: value.em * factor };
|
|
506
|
+
case "time":
|
|
507
|
+
return { kind: "time", ms: value.ms * factor };
|
|
508
|
+
case "number":
|
|
509
|
+
return { kind: "number", value: value.value * factor };
|
|
510
|
+
case "percent":
|
|
511
|
+
return { kind: "percent", value: value.value * factor };
|
|
512
|
+
default:
|
|
513
|
+
return undefined;
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
function add(a, b) {
|
|
517
|
+
if (!a || !b)
|
|
518
|
+
return undefined;
|
|
519
|
+
if (a.kind === "length" && b.kind === "length")
|
|
520
|
+
return { kind: "length", px: a.px + b.px };
|
|
521
|
+
if (a.kind === "em" && b.kind === "em")
|
|
522
|
+
return { kind: "em", em: a.em + b.em };
|
|
523
|
+
if (a.kind === "time" && b.kind === "time")
|
|
524
|
+
return { kind: "time", ms: a.ms + b.ms };
|
|
525
|
+
if (a.kind === "number" && b.kind === "number")
|
|
526
|
+
return { kind: "number", value: a.value + b.value };
|
|
527
|
+
if (a.kind === "percent" && b.kind === "percent")
|
|
528
|
+
return { kind: "percent", value: a.value + b.value };
|
|
529
|
+
// 0 written without a unit beside a length
|
|
530
|
+
if (a.kind === "length" && b.kind === "number" && b.value === 0)
|
|
531
|
+
return a;
|
|
532
|
+
if (b.kind === "length" && a.kind === "number" && a.value === 0)
|
|
533
|
+
return b;
|
|
534
|
+
return undefined;
|
|
535
|
+
}
|
|
536
|
+
const magnitude = (value) => {
|
|
537
|
+
if (value.kind === "length")
|
|
538
|
+
return ["length", value.px];
|
|
539
|
+
if (value.kind === "time")
|
|
540
|
+
return ["time", value.ms];
|
|
541
|
+
if (value.kind === "number")
|
|
542
|
+
return ["number", value.value];
|
|
543
|
+
if (value.kind === "percent")
|
|
544
|
+
return ["percent", value.value];
|
|
545
|
+
if (value.kind === "em")
|
|
546
|
+
return ["em", value.em];
|
|
547
|
+
return undefined;
|
|
548
|
+
};
|
|
549
|
+
function evalMinMax(name, args, options) {
|
|
550
|
+
const values = args.map((arg) => evalMath(arg, options));
|
|
551
|
+
if (values.some((v) => !v))
|
|
552
|
+
return undefined;
|
|
553
|
+
if (name === "clamp") {
|
|
554
|
+
if (values.length !== 3)
|
|
555
|
+
return undefined;
|
|
556
|
+
const [lo, preferred, hi] = values;
|
|
557
|
+
const upper = pickExtreme("min", [preferred, hi]);
|
|
558
|
+
return upper && pickExtreme("max", [lo, upper]);
|
|
559
|
+
}
|
|
560
|
+
return pickExtreme(name === "min" ? "min" : "max", values);
|
|
561
|
+
}
|
|
562
|
+
function pickExtreme(which, values) {
|
|
563
|
+
// A zero number may stand beside lengths
|
|
564
|
+
const kinds = new Set(values.map((v) => (v.kind === "number" && v.value === 0 && values.some((o) => o.kind === "length") ? "length" : v.kind)));
|
|
565
|
+
if (kinds.size !== 1)
|
|
566
|
+
return undefined;
|
|
567
|
+
// Only values with a magnitude compare: a unit the evaluator does not know (vw, cqi) leaves the expression invalid
|
|
568
|
+
if (values.some((value) => !magnitude(value)))
|
|
569
|
+
return undefined;
|
|
570
|
+
let best;
|
|
571
|
+
for (const value of values) {
|
|
572
|
+
const m = magnitude(value)[1];
|
|
573
|
+
if (!best || (which === "min" ? m < magnitude(best)[1] : m > magnitude(best)[1]))
|
|
574
|
+
best = value;
|
|
575
|
+
}
|
|
576
|
+
return best && best.kind === "number" && kinds.has("length") ? { kind: "length", px: best.value } : best;
|
|
577
|
+
}
|
|
578
|
+
/** Evaluates a list of nodes (one space-separated component, or a whole value). */
|
|
579
|
+
export function evaluateNodes(nodes, options) {
|
|
580
|
+
if (nodes.length === 0)
|
|
581
|
+
return undefined;
|
|
582
|
+
if (nodes.length > 1 || nodes[0].type === "comma") {
|
|
583
|
+
// A list (a shadow, a font stack, a transition): resolve its parts and write it out
|
|
584
|
+
const parts = nodes.map((node) => (node.type === "comma" ? "," : cssText(evaluateNodes([node], options)) ?? stringifyNode(node)));
|
|
585
|
+
let out = "";
|
|
586
|
+
for (const part of parts)
|
|
587
|
+
out += part === "," ? "," : (out ? " " : "") + part;
|
|
588
|
+
return text(out);
|
|
589
|
+
}
|
|
590
|
+
const node = nodes[0];
|
|
591
|
+
switch (node.type) {
|
|
592
|
+
case "dim":
|
|
593
|
+
return dimension(node, options);
|
|
594
|
+
case "hash": {
|
|
595
|
+
const color = parseHex(node.value);
|
|
596
|
+
return color ? { kind: "color", color } : undefined;
|
|
597
|
+
}
|
|
598
|
+
case "ident": {
|
|
599
|
+
const named = NAMED[node.value.toLowerCase()];
|
|
600
|
+
return named ? { kind: "color", color: named } : text(node.value);
|
|
601
|
+
}
|
|
602
|
+
case "string":
|
|
603
|
+
return text(stringifyNode(node));
|
|
604
|
+
case "paren":
|
|
605
|
+
return evalMath(node.nodes, options);
|
|
606
|
+
case "op":
|
|
607
|
+
return text(stringifyNode(node));
|
|
608
|
+
case "fn":
|
|
609
|
+
return evalFunction(node, options);
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
function evalFunction(node, options) {
|
|
613
|
+
switch (node.name) {
|
|
614
|
+
case "light-dark":
|
|
615
|
+
return node.args.length === 2 ? evaluateNodes(node.args[options.scheme === "light" ? 0 : 1], options) : undefined;
|
|
616
|
+
case "rgb":
|
|
617
|
+
case "rgba":
|
|
618
|
+
return evalRgb(node.args, options);
|
|
619
|
+
case "color-mix":
|
|
620
|
+
return evalColorMix(node.args, options);
|
|
621
|
+
case "calc":
|
|
622
|
+
return node.args.length === 1 ? evalMath(node.args[0], options) : undefined;
|
|
623
|
+
case "min":
|
|
624
|
+
case "max":
|
|
625
|
+
case "clamp":
|
|
626
|
+
return evalMinMax(node.name, node.args, options);
|
|
627
|
+
case "env":
|
|
628
|
+
return node.args.length > 1 ? evaluateNodes(node.args.slice(1).flat(), options) : { kind: "length", px: 0 };
|
|
629
|
+
default: {
|
|
630
|
+
// Another function (a gradient, a curve, a filter): its arguments resolved, the function kept
|
|
631
|
+
const args = node.args.map((arg) => cssText(evaluateNodes(arg, options)) ?? stringify(arg));
|
|
632
|
+
return text(`${node.name}(${args.join(", ")})`);
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
/** A value written back as CSS text, units included: a part of a list or of a function. */
|
|
637
|
+
function cssText(value) {
|
|
638
|
+
if (!value)
|
|
639
|
+
return undefined;
|
|
640
|
+
if (value.kind === "length")
|
|
641
|
+
return `${formatNumber(value.px)}px`;
|
|
642
|
+
if (value.kind === "time")
|
|
643
|
+
return `${formatNumber(value.ms)}ms`;
|
|
644
|
+
const out = serializeValue(value);
|
|
645
|
+
return out === undefined ? undefined : String(out);
|
|
646
|
+
}
|
|
647
|
+
export function serializeValue(value) {
|
|
648
|
+
if (!value)
|
|
649
|
+
return undefined;
|
|
650
|
+
switch (value.kind) {
|
|
651
|
+
case "color":
|
|
652
|
+
return formatColor(value.color);
|
|
653
|
+
case "length":
|
|
654
|
+
return Math.round(value.px * 10000) / 10000;
|
|
655
|
+
case "time":
|
|
656
|
+
return Math.round(value.ms * 10000) / 10000;
|
|
657
|
+
case "number":
|
|
658
|
+
return Math.round(value.value * 10000) / 10000;
|
|
659
|
+
case "percent":
|
|
660
|
+
return `${formatNumber(value.value)}%`;
|
|
661
|
+
case "em":
|
|
662
|
+
return `${formatNumber(value.em)}em`;
|
|
663
|
+
case "text":
|
|
664
|
+
return value.text;
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
/** Resolves one custom property for one scheme; `undefined` when it is invalid at computed value time. */
|
|
668
|
+
export function resolveValue(raw, lookup, options, self) {
|
|
669
|
+
const nodes = substitute(raw, lookup, self ? new Set([self]) : new Set());
|
|
670
|
+
if (!nodes)
|
|
671
|
+
return undefined;
|
|
672
|
+
const value = evaluateNodes(nodes, options);
|
|
673
|
+
return serializeValue(value) ?? stringify(nodes);
|
|
674
|
+
}
|
|
675
|
+
export function layerApplies(layer, context, scheme) {
|
|
676
|
+
const when = layer.when;
|
|
677
|
+
if (!when)
|
|
678
|
+
return true;
|
|
679
|
+
if (when.scheme && when.scheme !== scheme)
|
|
680
|
+
return false;
|
|
681
|
+
if (when.minWidth !== undefined && context.width < when.minWidth)
|
|
682
|
+
return false;
|
|
683
|
+
if (when.maxWidth !== undefined && context.width > when.maxWidth)
|
|
684
|
+
return false;
|
|
685
|
+
if (when.highDensity && !context.highDensity)
|
|
686
|
+
return false;
|
|
687
|
+
if (when.reducedMotion && !context.reducedMotion)
|
|
688
|
+
return false;
|
|
689
|
+
if (when.linearEasing)
|
|
690
|
+
return false;
|
|
691
|
+
return true;
|
|
692
|
+
}
|
|
693
|
+
const compareCascade = (a, b) => {
|
|
694
|
+
if (a.rank !== b.rank)
|
|
695
|
+
return a.rank - b.rank;
|
|
696
|
+
for (let k = 0; k < 3; k++)
|
|
697
|
+
if ((a.specificity[k] ?? 0) !== (b.specificity[k] ?? 0))
|
|
698
|
+
return (a.specificity[k] ?? 0) - (b.specificity[k] ?? 0);
|
|
699
|
+
return a.order - b.order;
|
|
700
|
+
};
|
|
701
|
+
/** The declarations that win for a context and a scheme, then the overrides (a brand theme: as if set on `:root`). */
|
|
702
|
+
export function declarationsFor(layers, context, scheme, overrides) {
|
|
703
|
+
const out = {};
|
|
704
|
+
for (const layer of [...layers].filter((l) => layerApplies(l, context, scheme)).sort(compareCascade))
|
|
705
|
+
Object.assign(out, layer.tokens);
|
|
706
|
+
if (overrides)
|
|
707
|
+
Object.assign(out, overrides);
|
|
708
|
+
return out;
|
|
709
|
+
}
|
|
710
|
+
/** A resolver over the layers for one context. Values are resolved on first use and kept. */
|
|
711
|
+
export function createTokenResolver(layers, context, overrides) {
|
|
712
|
+
const declarations = { light: declarationsFor(layers, context, "light", overrides), dark: declarationsFor(layers, context, "dark", overrides) };
|
|
713
|
+
const cache = { light: new Map(), dark: new Map() };
|
|
714
|
+
const key = (name) => (name.startsWith("--") ? name : `--${name}`);
|
|
715
|
+
const lookupFor = (scheme) => (name) => declarations[scheme][name];
|
|
716
|
+
const get = (name, scheme) => {
|
|
717
|
+
const prop = key(name);
|
|
718
|
+
const memo = cache[scheme];
|
|
719
|
+
if (memo.has(prop))
|
|
720
|
+
return memo.get(prop);
|
|
721
|
+
const raw = declarations[scheme][prop];
|
|
722
|
+
const value = raw === undefined ? undefined : resolveValue(raw, lookupFor(scheme), { scheme }, prop);
|
|
723
|
+
memo.set(prop, value);
|
|
724
|
+
return value;
|
|
725
|
+
};
|
|
726
|
+
return {
|
|
727
|
+
get,
|
|
728
|
+
at(name, scheme, fontSize) {
|
|
729
|
+
const raw = declarations[scheme][key(name)];
|
|
730
|
+
return raw === undefined ? undefined : resolveValue(raw, lookupFor(scheme), { scheme, fontSize }, key(name));
|
|
731
|
+
},
|
|
732
|
+
value(css, scheme, fontSize) {
|
|
733
|
+
return resolveValue(css, lookupFor(scheme), { scheme, fontSize });
|
|
734
|
+
},
|
|
735
|
+
raw(name, scheme) {
|
|
736
|
+
return declarations[scheme][key(name)];
|
|
737
|
+
},
|
|
738
|
+
all(scheme) {
|
|
739
|
+
const out = {};
|
|
740
|
+
for (const name of Object.keys(declarations[scheme]).sort())
|
|
741
|
+
out[name] = get(name, scheme) ?? null;
|
|
742
|
+
return out;
|
|
743
|
+
},
|
|
744
|
+
};
|
|
745
|
+
}
|
|
746
|
+
//# sourceMappingURL=resolve.js.map
|