@bamboocss/token-dictionary 1.11.1 → 1.11.3
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/index.cjs +1313 -0
- package/dist/index.d.cts +235 -0
- package/dist/index.d.mts +235 -0
- package/dist/index.mjs +1234 -1277
- package/package.json +11 -11
- package/dist/index.js +0 -1359
package/dist/index.mjs
CHANGED
|
@@ -1,1324 +1,1281 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
import { isMatching as isMatching2, match as match2 } from "ts-pattern";
|
|
14
|
-
|
|
15
|
-
// src/is-composite.ts
|
|
16
|
-
import { P, isMatching } from "ts-pattern";
|
|
17
|
-
var isCompositeShadow = isMatching({
|
|
18
|
-
inset: P.optional(P.boolean),
|
|
19
|
-
offsetX: P.union(P.number, P.string),
|
|
20
|
-
offsetY: P.union(P.number, P.string),
|
|
21
|
-
blur: P.union(P.number, P.string),
|
|
22
|
-
spread: P.union(P.number, P.string),
|
|
23
|
-
color: P.string
|
|
1
|
+
import { BambooError, calc, capitalize, compact, cssVar, esc, isBaseCondition, isCssUnit, isObject, isString, mapObject, memo, toHash, toPx, walkObject } from "@bamboocss/shared";
|
|
2
|
+
import { P, isMatching, match } from "ts-pattern";
|
|
3
|
+
import { logger } from "@bamboocss/logger";
|
|
4
|
+
import picomatch from "picomatch";
|
|
5
|
+
//#region src/is-composite.ts
|
|
6
|
+
const isCompositeShadow = isMatching({
|
|
7
|
+
inset: P.optional(P.boolean),
|
|
8
|
+
offsetX: P.union(P.number, P.string),
|
|
9
|
+
offsetY: P.union(P.number, P.string),
|
|
10
|
+
blur: P.union(P.number, P.string),
|
|
11
|
+
spread: P.union(P.number, P.string),
|
|
12
|
+
color: P.string
|
|
24
13
|
});
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
position: P.number
|
|
33
|
-
})
|
|
34
|
-
)
|
|
14
|
+
const isCompositeGradient = isMatching({
|
|
15
|
+
type: P.string,
|
|
16
|
+
placement: P.string,
|
|
17
|
+
stops: P.union(P.array(P.string), P.array({
|
|
18
|
+
color: P.string,
|
|
19
|
+
position: P.number
|
|
20
|
+
}))
|
|
35
21
|
});
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
22
|
+
const isCompositeBorder = isMatching({
|
|
23
|
+
color: P.string,
|
|
24
|
+
width: P.union(P.string, P.number),
|
|
25
|
+
style: P.string
|
|
40
26
|
});
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
27
|
+
const isCompositeAsset = isMatching({
|
|
28
|
+
type: P.union("url", "svg"),
|
|
29
|
+
value: P.string
|
|
44
30
|
});
|
|
45
|
-
|
|
46
|
-
|
|
31
|
+
const isCompositeTokenValue = (value) => {
|
|
32
|
+
return isCompositeGradient(value) || isCompositeShadow(value) || isCompositeBorder(value) || isCompositeAsset(value) || Array.isArray(value);
|
|
47
33
|
};
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/utils.ts
|
|
36
|
+
/**
|
|
37
|
+
* Regex for matching a tokenized reference.
|
|
38
|
+
*/
|
|
39
|
+
const REFERENCE_REGEX = /({([^}]*)})/g;
|
|
40
|
+
const curlyBracketRegex = /[{}]/g;
|
|
41
|
+
/**
|
|
42
|
+
* Returns all references in a string
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
*
|
|
46
|
+
* `{colors.red.300} {sizes.sm}` => ['colors.red.300', 'sizes.sm']
|
|
47
|
+
*/
|
|
60
48
|
function getReferences(value) {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
49
|
+
if (typeof value !== "string") return [];
|
|
50
|
+
const matches = value.match(REFERENCE_REGEX);
|
|
51
|
+
if (!matches) return [];
|
|
52
|
+
return matches.map((match) => match.replace(curlyBracketRegex, "")).map((value) => value.trim());
|
|
65
53
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
54
|
+
const hasReference = (value) => getReferences(value).length > 0;
|
|
55
|
+
const tokenFunctionRegex = /token\(([^)]+)\)/g;
|
|
56
|
+
const closingParenthesisRegex = /\)$/g;
|
|
57
|
+
const hasTokenReference = (str) => str.includes("token(");
|
|
58
|
+
const tokenReplacer = (a, b) => b ? a.endsWith(")") ? a.replace(closingParenthesisRegex, `, ${b})`) : `var(${a}, ${b})` : a;
|
|
59
|
+
const notFoundMessage = (key, value) => `Reference not found: \`${key}\` in "${value}"`;
|
|
60
|
+
const isTokenReference = (v) => hasReference(v) || hasTokenReference(v);
|
|
73
61
|
function expandReferences(value, fn) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
const [a, b] = result;
|
|
96
|
-
return tokenReplacer(a, b);
|
|
97
|
-
}
|
|
98
|
-
return tokenReplacer(result[0]);
|
|
99
|
-
});
|
|
62
|
+
if (!isTokenReference(value)) return value;
|
|
63
|
+
const expanded = getReferences(value).reduce((valueStr, key) => {
|
|
64
|
+
const resolved = fn(key);
|
|
65
|
+
if (!resolved) logger.warn("token", notFoundMessage(key, value));
|
|
66
|
+
const expandedValue = resolved ?? esc(key);
|
|
67
|
+
return valueStr.replaceAll(`{${key}}`, expandedValue);
|
|
68
|
+
}, value);
|
|
69
|
+
if (!expanded.includes(`token(`)) return expanded;
|
|
70
|
+
return expanded.replace(tokenFunctionRegex, (_, token) => {
|
|
71
|
+
const [tokenValue, tokenFallback] = token.split(",").map((s) => s.trim());
|
|
72
|
+
const result = [tokenValue, tokenFallback].filter(Boolean).map((key) => {
|
|
73
|
+
const resolved = fn(key);
|
|
74
|
+
if (!resolved && isTokenReference(key)) logger.warn("token", notFoundMessage(key, value));
|
|
75
|
+
return resolved ?? esc(key);
|
|
76
|
+
});
|
|
77
|
+
if (result.length > 1) {
|
|
78
|
+
const [a, b] = result;
|
|
79
|
+
return tokenReplacer(a, b);
|
|
80
|
+
}
|
|
81
|
+
return tokenReplacer(result[0]);
|
|
82
|
+
});
|
|
100
83
|
}
|
|
84
|
+
/**
|
|
85
|
+
* Converts a JS Map to an object
|
|
86
|
+
*/
|
|
101
87
|
function mapToJson(map) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
}
|
|
109
|
-
});
|
|
110
|
-
return obj;
|
|
88
|
+
const obj = {};
|
|
89
|
+
map.forEach((value, key) => {
|
|
90
|
+
if (value instanceof Map) obj[key] = Object.fromEntries(value);
|
|
91
|
+
else obj[key] = value;
|
|
92
|
+
});
|
|
93
|
+
return obj;
|
|
111
94
|
}
|
|
112
|
-
|
|
113
|
-
|
|
95
|
+
const isToken = (value) => {
|
|
96
|
+
return isObject(value) && "value" in value;
|
|
114
97
|
};
|
|
115
98
|
function assertTokenFormat(token) {
|
|
116
|
-
|
|
117
|
-
throw new BambooError("INVALID_TOKEN", `Invalid token format: ${JSON.stringify(token)}`);
|
|
118
|
-
}
|
|
99
|
+
if (!isToken(token)) throw new BambooError("INVALID_TOKEN", `Invalid token format: ${JSON.stringify(token)}`);
|
|
119
100
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
}
|
|
246
|
-
}
|
|
101
|
+
//#endregion
|
|
102
|
+
//#region src/token.ts
|
|
103
|
+
/**
|
|
104
|
+
* Represents a design token in the dictionary
|
|
105
|
+
*/
|
|
106
|
+
var Token = class Token {
|
|
107
|
+
name;
|
|
108
|
+
value;
|
|
109
|
+
originalValue;
|
|
110
|
+
path;
|
|
111
|
+
type;
|
|
112
|
+
description;
|
|
113
|
+
deprecated;
|
|
114
|
+
extensions;
|
|
115
|
+
constructor(input) {
|
|
116
|
+
this.name = input.name;
|
|
117
|
+
this.value = input.value;
|
|
118
|
+
this.originalValue = input.value;
|
|
119
|
+
this.path = input.path ?? [];
|
|
120
|
+
if (input.type) this.type = input.type;
|
|
121
|
+
if (input.description) this.description = input.description;
|
|
122
|
+
this.extensions = input.extensions ?? {};
|
|
123
|
+
this.extensions.condition = input.extensions?.condition ?? "base";
|
|
124
|
+
this.deprecated = input.deprecated;
|
|
125
|
+
this.setType();
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* The unique identifier of the token.
|
|
129
|
+
*/
|
|
130
|
+
get id() {
|
|
131
|
+
return toHash(`${this.name}-${this.extensions.condition}-${this.value}`);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Whether the token is a conditional token.
|
|
135
|
+
* Conditional tokens are tokens that have multiple values based on a condition.
|
|
136
|
+
*/
|
|
137
|
+
get isConditional() {
|
|
138
|
+
return !!this.extensions?.conditions;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Whether the token has a reference in its value.
|
|
142
|
+
* e.g. {color.gray.100}
|
|
143
|
+
*/
|
|
144
|
+
get hasReference() {
|
|
145
|
+
return !!this.extensions?.references;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Whether the token is a complex or composite token.
|
|
149
|
+
*/
|
|
150
|
+
get isComposite() {
|
|
151
|
+
return isCompositeTokenValue(this.originalValue);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Returns the token value with the references expanded.
|
|
155
|
+
* e.g. {color.gray.100} => var(--colors-gray-100)
|
|
156
|
+
*
|
|
157
|
+
*/
|
|
158
|
+
expandReferences() {
|
|
159
|
+
if (!this.hasReference) return this.extensions.varRef ?? this.value;
|
|
160
|
+
const references = this.extensions.references ?? {};
|
|
161
|
+
this.value = Object.keys(references).reduce((valueStr, key) => {
|
|
162
|
+
const referenceToken = references[key];
|
|
163
|
+
if (referenceToken.isConditional) return valueStr;
|
|
164
|
+
const value = referenceToken.expandReferences();
|
|
165
|
+
return valueStr.replaceAll(`{${key}}`, value);
|
|
166
|
+
}, this.value);
|
|
167
|
+
delete this.extensions.references;
|
|
168
|
+
return this.value;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Whether this token has a reference to another token
|
|
172
|
+
*/
|
|
173
|
+
get isReference() {
|
|
174
|
+
return hasReference(this.originalValue);
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Returns the list of references in the token value
|
|
178
|
+
*/
|
|
179
|
+
get references() {
|
|
180
|
+
return getReferences(this.originalValue);
|
|
181
|
+
}
|
|
182
|
+
clone() {
|
|
183
|
+
return new Token({
|
|
184
|
+
name: this.name,
|
|
185
|
+
value: this.value,
|
|
186
|
+
type: this.type,
|
|
187
|
+
path: [...this.path],
|
|
188
|
+
description: this.description,
|
|
189
|
+
extensions: cloneDeep(this.extensions)
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Returns an array of tokens per conditions.
|
|
194
|
+
* It is commonly used in semantic tokens, and can have multiple values based on a condition.
|
|
195
|
+
* e.g. primary: { light: '#000', dark: '#fff' }
|
|
196
|
+
*/
|
|
197
|
+
getConditionTokens() {
|
|
198
|
+
if (!this.isConditional) return;
|
|
199
|
+
const conditions = this.extensions.conditions ?? {};
|
|
200
|
+
const conditionalTokens = [];
|
|
201
|
+
walkObject(conditions, (value, path) => {
|
|
202
|
+
const newPath = path.filter((v) => !isBaseCondition(v));
|
|
203
|
+
if (!newPath.length) return;
|
|
204
|
+
const token = this.clone();
|
|
205
|
+
token.value = value;
|
|
206
|
+
token.extensions.condition = newPath.join(":");
|
|
207
|
+
conditionalTokens.push(token);
|
|
208
|
+
});
|
|
209
|
+
return conditionalTokens;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Add more extensions to the token
|
|
213
|
+
*/
|
|
214
|
+
setExtensions(extensions) {
|
|
215
|
+
this.extensions = {
|
|
216
|
+
...this.extensions,
|
|
217
|
+
...extensions
|
|
218
|
+
};
|
|
219
|
+
this.setType();
|
|
220
|
+
return this;
|
|
221
|
+
}
|
|
222
|
+
setType() {
|
|
223
|
+
if (this.type) return;
|
|
224
|
+
if (this.extensions.category) this.type = TOKEN_TYPES[this.extensions.category];
|
|
225
|
+
}
|
|
247
226
|
};
|
|
248
227
|
function cloneDeep(value) {
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
return value.map((child) => cloneDeep(child));
|
|
254
|
-
}
|
|
255
|
-
if (typeof value === "object" && value !== null) {
|
|
256
|
-
return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, cloneDeep(v)]));
|
|
257
|
-
}
|
|
258
|
-
return value;
|
|
228
|
+
if (value instanceof Token) return value.clone();
|
|
229
|
+
if (Array.isArray(value)) return value.map((child) => cloneDeep(child));
|
|
230
|
+
if (typeof value === "object" && value !== null) return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, cloneDeep(v)]));
|
|
231
|
+
return value;
|
|
259
232
|
}
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
};
|
|
284
|
-
var TOKEN_CATEGORIES = Object.keys(TOKEN_TYPES);
|
|
285
|
-
|
|
286
|
-
// src/middleware.ts
|
|
287
|
-
var addNegativeTokens = {
|
|
288
|
-
enforce: "pre",
|
|
289
|
-
transform(dictionary) {
|
|
290
|
-
const { prefix, hash } = dictionary;
|
|
291
|
-
const tokens = dictionary.filter({
|
|
292
|
-
extensions: { category: "spacing" }
|
|
293
|
-
});
|
|
294
|
-
tokens.forEach((token) => {
|
|
295
|
-
const originalPath = [...token.path];
|
|
296
|
-
const originalVar = dictionary.formatCssVar(originalPath, { prefix, hash });
|
|
297
|
-
if (token.value === "0rem") {
|
|
298
|
-
return;
|
|
299
|
-
}
|
|
300
|
-
const node = token.clone();
|
|
301
|
-
node.setExtensions({
|
|
302
|
-
isNegative: true,
|
|
303
|
-
prop: `-${token.extensions.prop}`,
|
|
304
|
-
originalPath
|
|
305
|
-
});
|
|
306
|
-
node.value = calc.negate(originalVar);
|
|
307
|
-
const last = node.path.at(-1);
|
|
308
|
-
if (last != null) {
|
|
309
|
-
node.path[node.path.length - 1] = `-${last}`;
|
|
310
|
-
}
|
|
311
|
-
if (node.path) {
|
|
312
|
-
node.name = dictionary.formatTokenName(node.path);
|
|
313
|
-
}
|
|
314
|
-
dictionary.registerToken(node);
|
|
315
|
-
});
|
|
316
|
-
}
|
|
317
|
-
};
|
|
318
|
-
var units = /* @__PURE__ */ new Set(["spacing", "sizes", "borderWidths", "fontSizes", "radii"]);
|
|
319
|
-
var addPixelUnit = {
|
|
320
|
-
enforce: "post",
|
|
321
|
-
transform(dictionary) {
|
|
322
|
-
const tokens = dictionary.filter((token) => {
|
|
323
|
-
return units.has(token.extensions.category) && !token.extensions.isNegative;
|
|
324
|
-
});
|
|
325
|
-
tokens.forEach((token) => {
|
|
326
|
-
token.setExtensions({
|
|
327
|
-
pixelValue: toPx(token.value)
|
|
328
|
-
});
|
|
329
|
-
});
|
|
330
|
-
}
|
|
233
|
+
const TOKEN_TYPES = {
|
|
234
|
+
colors: "color",
|
|
235
|
+
spacing: "dimension",
|
|
236
|
+
sizes: "dimension",
|
|
237
|
+
shadows: "shadow",
|
|
238
|
+
fonts: "fontFamily",
|
|
239
|
+
fontSizes: "fontSize",
|
|
240
|
+
fontWeights: "fontWeight",
|
|
241
|
+
letterSpacings: "letterSpacing",
|
|
242
|
+
lineHeights: "lineHeight",
|
|
243
|
+
durations: "duration",
|
|
244
|
+
transitions: "transition",
|
|
245
|
+
radii: "borderRadius",
|
|
246
|
+
gradients: "gradient",
|
|
247
|
+
easings: "cubicBezier",
|
|
248
|
+
borders: "border",
|
|
249
|
+
borderWidths: "borderWidth",
|
|
250
|
+
zIndex: "zIndex",
|
|
251
|
+
opacity: "opacity",
|
|
252
|
+
blurs: "blur",
|
|
253
|
+
aspectRatios: "aspectRatio",
|
|
254
|
+
cursor: "cursor",
|
|
255
|
+
assets: "asset"
|
|
331
256
|
};
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
});
|
|
360
|
-
keys.forEach((segments) => {
|
|
361
|
-
const node = new Token({
|
|
362
|
-
name: dictionary.formatTokenName(["colors", "colorPalette", ...segments].filter(Boolean)),
|
|
363
|
-
value: dictionary.formatTokenName(["colors", "colorPalette", ...segments].filter(Boolean)),
|
|
364
|
-
path: ["colors", "colorPalette", ...segments]
|
|
365
|
-
});
|
|
366
|
-
node.setExtensions({
|
|
367
|
-
category: "colors",
|
|
368
|
-
prop: dictionary.formatTokenName(["colorPalette", ...segments].filter(Boolean)),
|
|
369
|
-
isVirtual: true
|
|
370
|
-
});
|
|
371
|
-
dictionary.registerToken(node, "pre");
|
|
372
|
-
});
|
|
373
|
-
}
|
|
257
|
+
const TOKEN_CATEGORIES = Object.keys(TOKEN_TYPES);
|
|
258
|
+
//#endregion
|
|
259
|
+
//#region src/middleware.ts
|
|
260
|
+
const addNegativeTokens = {
|
|
261
|
+
enforce: "pre",
|
|
262
|
+
transform(dictionary) {
|
|
263
|
+
const { prefix, hash } = dictionary;
|
|
264
|
+
dictionary.filter({ extensions: { category: "spacing" } }).forEach((token) => {
|
|
265
|
+
const originalPath = [...token.path];
|
|
266
|
+
const originalVar = dictionary.formatCssVar(originalPath, {
|
|
267
|
+
prefix,
|
|
268
|
+
hash
|
|
269
|
+
});
|
|
270
|
+
if (token.value === "0rem") return;
|
|
271
|
+
const node = token.clone();
|
|
272
|
+
node.setExtensions({
|
|
273
|
+
isNegative: true,
|
|
274
|
+
prop: `-${token.extensions.prop}`,
|
|
275
|
+
originalPath
|
|
276
|
+
});
|
|
277
|
+
node.value = calc.negate(originalVar);
|
|
278
|
+
const last = node.path.at(-1);
|
|
279
|
+
if (last != null) node.path[node.path.length - 1] = `-${last}`;
|
|
280
|
+
if (node.path) node.name = dictionary.formatTokenName(node.path);
|
|
281
|
+
dictionary.registerToken(node);
|
|
282
|
+
});
|
|
283
|
+
}
|
|
374
284
|
};
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
285
|
+
const units = new Set([
|
|
286
|
+
"spacing",
|
|
287
|
+
"sizes",
|
|
288
|
+
"borderWidths",
|
|
289
|
+
"fontSizes",
|
|
290
|
+
"radii"
|
|
291
|
+
]);
|
|
292
|
+
const middlewares = [
|
|
293
|
+
addNegativeTokens,
|
|
294
|
+
{
|
|
295
|
+
enforce: "post",
|
|
296
|
+
transform(dictionary) {
|
|
297
|
+
if (!(dictionary.colorPalette?.enabled ?? true)) return;
|
|
298
|
+
const tokens = dictionary.filter({ extensions: { category: "colors" } });
|
|
299
|
+
const keys = /* @__PURE__ */ new Map();
|
|
300
|
+
const colorPalettes = /* @__PURE__ */ new Map();
|
|
301
|
+
tokens.forEach((token) => {
|
|
302
|
+
const { colorPalette, colorPaletteRoots, colorPaletteTokenKeys } = token.extensions;
|
|
303
|
+
if (!colorPalette) return;
|
|
304
|
+
colorPaletteTokenKeys.forEach((keyPath) => {
|
|
305
|
+
keys.set(dictionary.formatTokenName(keyPath), keyPath);
|
|
306
|
+
});
|
|
307
|
+
/**
|
|
308
|
+
* Assign nested tokens to their respective color palette list.
|
|
309
|
+
*
|
|
310
|
+
* Iteration 1:
|
|
311
|
+
* If `colorPaletteRoots` is this array ['button']
|
|
312
|
+
* and the token name is 'colors.button.light' it will be added to the 'button' color palette list.
|
|
313
|
+
* ```
|
|
314
|
+
* const colorPalettes = {
|
|
315
|
+
* 'button': ['colors.button.light'],
|
|
316
|
+
* }
|
|
317
|
+
*
|
|
318
|
+
* Itteration 2:
|
|
319
|
+
* If `colorPaletteRoots` is this array ['button', 'button.light']
|
|
320
|
+
* and the token name is 'colors.button.light.accent' it will be added to the 'button' and 'button.light' color palette lists.
|
|
321
|
+
* ```
|
|
322
|
+
* const colorPalettes = {
|
|
323
|
+
* 'button': ['colors.button.light', 'colors.button.light.accent'],
|
|
324
|
+
* 'button.light': ['colors.button.light.accent'],
|
|
325
|
+
* }
|
|
326
|
+
*
|
|
327
|
+
* Itteration 3:
|
|
328
|
+
* If `colorPaletteRoots` is this array ['button', 'button.light', 'button.light.accent']
|
|
329
|
+
* and the token name is 'colors.button.light.accent.secondary' it will be added to the 'button', 'button.light' and 'button.light.accent' color palette lists.
|
|
330
|
+
* ```
|
|
331
|
+
* const colorPalettes = {
|
|
332
|
+
* 'button': ['colors.button.light', 'colors.button.light.accent', 'colors.button.light.accent.secondary'],
|
|
333
|
+
* 'button.light': ['colors.button.light.accent', 'colors.button.light.accent.secondary'],
|
|
334
|
+
* 'button.light.accent': ['colors.button.light.accent.secondary'],
|
|
335
|
+
* }
|
|
336
|
+
*/
|
|
337
|
+
colorPaletteRoots.forEach((colorPaletteRoot) => {
|
|
338
|
+
const formated = dictionary.formatTokenName(colorPaletteRoot);
|
|
339
|
+
const colorPaletteList = colorPalettes.get(formated) || [];
|
|
340
|
+
colorPaletteList.push(token);
|
|
341
|
+
colorPalettes.set(formated, colorPaletteList);
|
|
342
|
+
if (token.extensions.isDefault && colorPaletteRoot.length === 1) {
|
|
343
|
+
const keyPath = colorPaletteTokenKeys[0]?.filter(Boolean);
|
|
344
|
+
if (!keyPath.length) return;
|
|
345
|
+
const path = colorPaletteRoot.concat(keyPath);
|
|
346
|
+
keys.set(dictionary.formatTokenName(path), []);
|
|
347
|
+
}
|
|
348
|
+
});
|
|
349
|
+
});
|
|
350
|
+
keys.forEach((segments) => {
|
|
351
|
+
const node = new Token({
|
|
352
|
+
name: dictionary.formatTokenName([
|
|
353
|
+
"colors",
|
|
354
|
+
"colorPalette",
|
|
355
|
+
...segments
|
|
356
|
+
].filter(Boolean)),
|
|
357
|
+
value: dictionary.formatTokenName([
|
|
358
|
+
"colors",
|
|
359
|
+
"colorPalette",
|
|
360
|
+
...segments
|
|
361
|
+
].filter(Boolean)),
|
|
362
|
+
path: [
|
|
363
|
+
"colors",
|
|
364
|
+
"colorPalette",
|
|
365
|
+
...segments
|
|
366
|
+
]
|
|
367
|
+
});
|
|
368
|
+
node.setExtensions({
|
|
369
|
+
category: "colors",
|
|
370
|
+
prop: dictionary.formatTokenName(["colorPalette", ...segments].filter(Boolean)),
|
|
371
|
+
isVirtual: true
|
|
372
|
+
});
|
|
373
|
+
dictionary.registerToken(node, "pre");
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
},
|
|
377
|
+
{
|
|
378
|
+
enforce: "post",
|
|
379
|
+
transform(dictionary) {
|
|
380
|
+
dictionary.allTokens = dictionary.allTokens.filter((token) => token.value !== "");
|
|
381
|
+
}
|
|
382
|
+
},
|
|
383
|
+
{
|
|
384
|
+
enforce: "post",
|
|
385
|
+
transform(dictionary) {
|
|
386
|
+
dictionary.filter((token) => {
|
|
387
|
+
return units.has(token.extensions.category) && !token.extensions.isNegative;
|
|
388
|
+
}).forEach((token) => {
|
|
389
|
+
token.setExtensions({ pixelValue: toPx(token.value) });
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
];
|
|
394
|
+
//#endregion
|
|
395
|
+
//#region src/mini-svg-uri.ts
|
|
396
|
+
const shorterNames = {
|
|
397
|
+
aqua: /#00ffff(ff)?(?!\w)|#0ff(f)?(?!\w)/gi,
|
|
398
|
+
azure: /#f0ffff(ff)?(?!\w)/gi,
|
|
399
|
+
beige: /#f5f5dc(ff)?(?!\w)/gi,
|
|
400
|
+
bisque: /#ffe4c4(ff)?(?!\w)/gi,
|
|
401
|
+
black: /#000000(ff)?(?!\w)|#000(f)?(?!\w)/gi,
|
|
402
|
+
blue: /#0000ff(ff)?(?!\w)|#00f(f)?(?!\w)/gi,
|
|
403
|
+
brown: /#a52a2a(ff)?(?!\w)/gi,
|
|
404
|
+
coral: /#ff7f50(ff)?(?!\w)/gi,
|
|
405
|
+
cornsilk: /#fff8dc(ff)?(?!\w)/gi,
|
|
406
|
+
crimson: /#dc143c(ff)?(?!\w)/gi,
|
|
407
|
+
cyan: /#00ffff(ff)?(?!\w)|#0ff(f)?(?!\w)/gi,
|
|
408
|
+
darkblue: /#00008b(ff)?(?!\w)/gi,
|
|
409
|
+
darkcyan: /#008b8b(ff)?(?!\w)/gi,
|
|
410
|
+
darkgrey: /#a9a9a9(ff)?(?!\w)/gi,
|
|
411
|
+
darkred: /#8b0000(ff)?(?!\w)/gi,
|
|
412
|
+
deeppink: /#ff1493(ff)?(?!\w)/gi,
|
|
413
|
+
dimgrey: /#696969(ff)?(?!\w)/gi,
|
|
414
|
+
gold: /#ffd700(ff)?(?!\w)/gi,
|
|
415
|
+
green: /#008000(ff)?(?!\w)/gi,
|
|
416
|
+
grey: /#808080(ff)?(?!\w)/gi,
|
|
417
|
+
honeydew: /#f0fff0(ff)?(?!\w)/gi,
|
|
418
|
+
hotpink: /#ff69b4(ff)?(?!\w)/gi,
|
|
419
|
+
indigo: /#4b0082(ff)?(?!\w)/gi,
|
|
420
|
+
ivory: /#fffff0(ff)?(?!\w)/gi,
|
|
421
|
+
khaki: /#f0e68c(ff)?(?!\w)/gi,
|
|
422
|
+
lavender: /#e6e6fa(ff)?(?!\w)/gi,
|
|
423
|
+
lime: /#00ff00(ff)?(?!\w)|#0f0(f)?(?!\w)/gi,
|
|
424
|
+
linen: /#faf0e6(ff)?(?!\w)/gi,
|
|
425
|
+
maroon: /#800000(ff)?(?!\w)/gi,
|
|
426
|
+
moccasin: /#ffe4b5(ff)?(?!\w)/gi,
|
|
427
|
+
navy: /#000080(ff)?(?!\w)/gi,
|
|
428
|
+
oldlace: /#fdf5e6(ff)?(?!\w)/gi,
|
|
429
|
+
olive: /#808000(ff)?(?!\w)/gi,
|
|
430
|
+
orange: /#ffa500(ff)?(?!\w)/gi,
|
|
431
|
+
orchid: /#da70d6(ff)?(?!\w)/gi,
|
|
432
|
+
peru: /#cd853f(ff)?(?!\w)/gi,
|
|
433
|
+
pink: /#ffc0cb(ff)?(?!\w)/gi,
|
|
434
|
+
plum: /#dda0dd(ff)?(?!\w)/gi,
|
|
435
|
+
purple: /#800080(ff)?(?!\w)/gi,
|
|
436
|
+
red: /#ff0000(ff)?(?!\w)|#f00(f)?(?!\w)/gi,
|
|
437
|
+
salmon: /#fa8072(ff)?(?!\w)/gi,
|
|
438
|
+
seagreen: /#2e8b57(ff)?(?!\w)/gi,
|
|
439
|
+
seashell: /#fff5ee(ff)?(?!\w)/gi,
|
|
440
|
+
sienna: /#a0522d(ff)?(?!\w)/gi,
|
|
441
|
+
silver: /#c0c0c0(ff)?(?!\w)/gi,
|
|
442
|
+
skyblue: /#87ceeb(ff)?(?!\w)/gi,
|
|
443
|
+
snow: /#fffafa(ff)?(?!\w)/gi,
|
|
444
|
+
tan: /#d2b48c(ff)?(?!\w)/gi,
|
|
445
|
+
teal: /#008080(ff)?(?!\w)/gi,
|
|
446
|
+
thistle: /#d8bfd8(ff)?(?!\w)/gi,
|
|
447
|
+
tomato: /#ff6347(ff)?(?!\w)/gi,
|
|
448
|
+
violet: /#ee82ee(ff)?(?!\w)/gi,
|
|
449
|
+
wheat: /#f5deb3(ff)?(?!\w)/gi,
|
|
450
|
+
white: /#ffffff(ff)?(?!\w)|#fff(f)?(?!\w)/gi
|
|
444
451
|
};
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
452
|
+
const REGEX = {
|
|
453
|
+
whitespace: /\s+/g,
|
|
454
|
+
urlHexPairs: /%[\dA-F]{2}/g,
|
|
455
|
+
quotes: /"/g
|
|
449
456
|
};
|
|
450
457
|
function collapseWhitespace(str) {
|
|
451
|
-
|
|
458
|
+
return str.trim().replace(REGEX.whitespace, " ");
|
|
452
459
|
}
|
|
453
460
|
function dataURIPayload(string) {
|
|
454
|
-
|
|
461
|
+
return encodeURIComponent(string).replace(REGEX.urlHexPairs, specialHexEncode);
|
|
455
462
|
}
|
|
456
463
|
function colorCodeToShorterNames(string) {
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
});
|
|
462
|
-
return string;
|
|
464
|
+
objectKeys(shorterNames).forEach((key) => {
|
|
465
|
+
if (shorterNames[key].test(string)) string = string.replace(shorterNames[key], key);
|
|
466
|
+
});
|
|
467
|
+
return string;
|
|
463
468
|
}
|
|
464
|
-
function specialHexEncode(
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
case "%2F":
|
|
473
|
-
return "/";
|
|
474
|
-
default:
|
|
475
|
-
return match3.toLowerCase();
|
|
476
|
-
}
|
|
469
|
+
function specialHexEncode(match) {
|
|
470
|
+
switch (match) {
|
|
471
|
+
case "%20": return " ";
|
|
472
|
+
case "%3D": return "=";
|
|
473
|
+
case "%3A": return ":";
|
|
474
|
+
case "%2F": return "/";
|
|
475
|
+
default: return match.toLowerCase();
|
|
476
|
+
}
|
|
477
477
|
}
|
|
478
478
|
function svgToDataUri(svgString) {
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
if (svgString.charCodeAt(0) === 65279) {
|
|
483
|
-
svgString = svgString.slice(1);
|
|
484
|
-
}
|
|
485
|
-
const body = colorCodeToShorterNames(collapseWhitespace(svgString)).replace(REGEX.quotes, "'");
|
|
486
|
-
return "data:image/svg+xml," + dataURIPayload(body);
|
|
479
|
+
if (typeof svgString !== "string") throw new TypeError("Expected a string, but received " + typeof svgString);
|
|
480
|
+
if (svgString.charCodeAt(0) === 65279) svgString = svgString.slice(1);
|
|
481
|
+
return "data:image/svg+xml," + dataURIPayload(colorCodeToShorterNames(collapseWhitespace(svgString)).replace(REGEX.quotes, "'"));
|
|
487
482
|
}
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
483
|
+
const objectKeys = (obj) => Object.keys(obj);
|
|
484
|
+
//#endregion
|
|
485
|
+
//#region src/transform.ts
|
|
491
486
|
function toUnit(v) {
|
|
492
|
-
|
|
487
|
+
return isCssUnit(v) || hasReference(v.toString()) ? v : `${v}px`;
|
|
493
488
|
}
|
|
494
489
|
function px(v) {
|
|
495
|
-
|
|
490
|
+
return isString(v) ? v : `${v}px`;
|
|
496
491
|
}
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
492
|
+
const transforms = [
|
|
493
|
+
{
|
|
494
|
+
name: "tokens/shadow",
|
|
495
|
+
match: (token) => token.extensions.category === "shadows",
|
|
496
|
+
transform(token, dict) {
|
|
497
|
+
if (isString(token.value)) return token.value;
|
|
498
|
+
if (Array.isArray(token.value)) return token.value.map((value) => this.transform({ value }, dict)).join(", ");
|
|
499
|
+
if (isCompositeShadow(token.value)) {
|
|
500
|
+
const { offsetX, offsetY, blur, spread, color, inset } = token.value;
|
|
501
|
+
return [
|
|
502
|
+
inset ? "inset " : "",
|
|
503
|
+
px(offsetX),
|
|
504
|
+
px(offsetY),
|
|
505
|
+
px(blur),
|
|
506
|
+
px(spread),
|
|
507
|
+
color
|
|
508
|
+
].filter(Boolean).join(" ");
|
|
509
|
+
}
|
|
510
|
+
return token.value;
|
|
511
|
+
}
|
|
512
|
+
},
|
|
513
|
+
{
|
|
514
|
+
name: "tokens/gradient",
|
|
515
|
+
match: (token) => token.extensions.category === "gradients",
|
|
516
|
+
transform(token) {
|
|
517
|
+
if (isString(token.value)) return token.value;
|
|
518
|
+
if (isCompositeGradient(token.value)) {
|
|
519
|
+
const { type, stops, placement } = token.value;
|
|
520
|
+
return `${type}-gradient(${placement}, ${stops.map((stop) => {
|
|
521
|
+
if (isString(stop)) return stop;
|
|
522
|
+
const { color, position } = stop;
|
|
523
|
+
return `${color} ${position}px`;
|
|
524
|
+
}).join(", ")})`;
|
|
525
|
+
}
|
|
526
|
+
return token.value;
|
|
527
|
+
}
|
|
528
|
+
},
|
|
529
|
+
{
|
|
530
|
+
name: "tokens/fonts",
|
|
531
|
+
match: (token) => token.extensions.category === "fonts",
|
|
532
|
+
transform(token) {
|
|
533
|
+
if (Array.isArray(token.value)) return token.value.join(", ");
|
|
534
|
+
return token.value;
|
|
535
|
+
}
|
|
536
|
+
},
|
|
537
|
+
{
|
|
538
|
+
name: "tokens/easings",
|
|
539
|
+
match: (token) => token.extensions.category === "easings",
|
|
540
|
+
transform(token) {
|
|
541
|
+
if (isString(token.value)) return token.value;
|
|
542
|
+
if (Array.isArray(token.value)) return `cubic-bezier(${token.value.join(", ")})`;
|
|
543
|
+
return token.value;
|
|
544
|
+
}
|
|
545
|
+
},
|
|
546
|
+
{
|
|
547
|
+
name: "tokens/borders",
|
|
548
|
+
match: (token) => token.extensions.category === "borders",
|
|
549
|
+
transform(token) {
|
|
550
|
+
if (isString(token.value)) return token.value;
|
|
551
|
+
if (isCompositeBorder(token.value)) {
|
|
552
|
+
const { width, style, color } = token.value;
|
|
553
|
+
return `${toUnit(width)} ${style} ${color}`;
|
|
554
|
+
}
|
|
555
|
+
return token.value;
|
|
556
|
+
}
|
|
557
|
+
},
|
|
558
|
+
{
|
|
559
|
+
name: "tokens/assets",
|
|
560
|
+
match: (token) => token.extensions.category === "assets",
|
|
561
|
+
transform(token) {
|
|
562
|
+
const raw = token.value;
|
|
563
|
+
return match(raw).with(P.string, (value) => value).with({ type: "url" }, ({ value }) => `url("${value}")`).with({ type: "svg" }, ({ value }) => `url("${svgToDataUri(value)}")`).exhaustive();
|
|
564
|
+
}
|
|
565
|
+
},
|
|
566
|
+
{
|
|
567
|
+
type: "extensions",
|
|
568
|
+
name: "tokens/css-var",
|
|
569
|
+
transform(token, dictionary) {
|
|
570
|
+
const { prefix, hash } = dictionary;
|
|
571
|
+
const { isNegative, originalPath } = token.extensions;
|
|
572
|
+
const pathValue = isNegative ? originalPath : token.path;
|
|
573
|
+
const variable = dictionary.formatCssVar(pathValue.filter(Boolean), {
|
|
574
|
+
prefix,
|
|
575
|
+
hash
|
|
576
|
+
});
|
|
577
|
+
return {
|
|
578
|
+
var: variable.var,
|
|
579
|
+
varRef: variable.ref
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
},
|
|
583
|
+
{
|
|
584
|
+
name: "tokens/color-mix",
|
|
585
|
+
match: (token) => {
|
|
586
|
+
return token.extensions.category === "colors" && token.value.includes("/");
|
|
587
|
+
},
|
|
588
|
+
transform(token, dict) {
|
|
589
|
+
if (!token.value.includes("/")) return token;
|
|
590
|
+
return expandReferences(token.value, (path) => {
|
|
591
|
+
const tokenFn = (tokenPath) => {
|
|
592
|
+
return dict.getByName(tokenPath)?.extensions.varRef;
|
|
593
|
+
};
|
|
594
|
+
const mix = dict.colorMix(path, tokenFn);
|
|
595
|
+
if (mix.invalid) throw new BambooError("INVALID_TOKEN", "Invalid color mix at " + path + ": " + mix.value);
|
|
596
|
+
return mix.value;
|
|
597
|
+
});
|
|
598
|
+
}
|
|
599
|
+
},
|
|
600
|
+
{
|
|
601
|
+
enforce: "post",
|
|
602
|
+
name: "tokens/conditionals",
|
|
603
|
+
transform(token, dictionary) {
|
|
604
|
+
const { prefix, hash } = dictionary;
|
|
605
|
+
const refs = getReferences(token.value);
|
|
606
|
+
if (!refs.length) return token.value;
|
|
607
|
+
if (!refs.some((ref) => ref.includes("/"))) refs.forEach((ref) => {
|
|
608
|
+
const variable = dictionary.formatCssVar(ref.split("."), {
|
|
609
|
+
prefix,
|
|
610
|
+
hash
|
|
611
|
+
}).ref;
|
|
612
|
+
token.value = token.value.replaceAll(`{${ref}}`, variable);
|
|
613
|
+
});
|
|
614
|
+
else {
|
|
615
|
+
const tokenFn = (name) => {
|
|
616
|
+
return dictionary.getByName(name)?.extensions.varRef;
|
|
617
|
+
};
|
|
618
|
+
token.value = expandReferences(token.value, (path) => {
|
|
619
|
+
const mix = dictionary.colorMix(path, tokenFn);
|
|
620
|
+
if (mix.invalid) throw new BambooError("INVALID_TOKEN", "Invalid color mix at " + path + ": " + mix.value);
|
|
621
|
+
return mix.value;
|
|
622
|
+
});
|
|
623
|
+
}
|
|
624
|
+
return token.value;
|
|
625
|
+
}
|
|
626
|
+
},
|
|
627
|
+
{
|
|
628
|
+
type: "extensions",
|
|
629
|
+
name: "tokens/colors/colorPalette",
|
|
630
|
+
match(token) {
|
|
631
|
+
return token.extensions.category === "colors" && !token.extensions.isVirtual;
|
|
632
|
+
},
|
|
633
|
+
transform(token, dict) {
|
|
634
|
+
const colorPaletteConfig = dict.colorPalette;
|
|
635
|
+
const enabled = colorPaletteConfig?.enabled ?? true;
|
|
636
|
+
const include = colorPaletteConfig?.include;
|
|
637
|
+
const exclude = colorPaletteConfig?.exclude;
|
|
638
|
+
if (!enabled) return {};
|
|
639
|
+
let colorPath = token.path.slice(1, -1);
|
|
640
|
+
if (colorPath.length === 0) {
|
|
641
|
+
colorPath = token.path.slice(1);
|
|
642
|
+
if (colorPath.length === 0) return {};
|
|
643
|
+
}
|
|
644
|
+
const colorPathString = colorPath.join(".");
|
|
645
|
+
if (exclude?.length) {
|
|
646
|
+
if (exclude.map((pattern) => picomatch(pattern)).some((matcher) => matcher(colorPathString))) return {};
|
|
647
|
+
}
|
|
648
|
+
if (include?.length) {
|
|
649
|
+
if (!include.map((pattern) => picomatch(pattern)).some((matcher) => matcher(colorPathString))) return {};
|
|
650
|
+
}
|
|
651
|
+
/**
|
|
652
|
+
* Generate all possible color palette roots from the color path.
|
|
653
|
+
*
|
|
654
|
+
* For ['button', 'light', 'accent']:
|
|
655
|
+
* - ['button']
|
|
656
|
+
* - ['button', 'light']
|
|
657
|
+
* - ['button', 'light', 'accent']
|
|
658
|
+
*
|
|
659
|
+
* These represent all possible values you can pass to the css `colorPalette` property.
|
|
660
|
+
*/
|
|
661
|
+
const colorPaletteRoots = [];
|
|
662
|
+
for (let i = 0; i < colorPath.length; i++) colorPaletteRoots.push(colorPath.slice(0, i + 1));
|
|
663
|
+
const colorPaletteRoot = colorPath[0];
|
|
664
|
+
const colorPalette = dict.formatTokenName(colorPath);
|
|
665
|
+
/**
|
|
666
|
+
* If this is the nested color palette:
|
|
667
|
+
* ```json
|
|
668
|
+
* {
|
|
669
|
+
* "colors": {
|
|
670
|
+
* "button": {
|
|
671
|
+
* "light": {
|
|
672
|
+
* "accent": {
|
|
673
|
+
* "secondary": {
|
|
674
|
+
* value: 'blue',
|
|
675
|
+
* },
|
|
676
|
+
* },
|
|
677
|
+
* },
|
|
678
|
+
* },
|
|
679
|
+
* },
|
|
680
|
+
* },
|
|
681
|
+
* ```
|
|
682
|
+
*
|
|
683
|
+
* The `colorPaletteTokenKeys` will be `['light.accent.secondary', 'accent.secondary', 'secondary']`
|
|
684
|
+
* It is used by the `addVirtualPalette` middleware to build the virtual `colorPalette` token for each color pattern root.
|
|
685
|
+
*
|
|
686
|
+
* Examples:
|
|
687
|
+
*
|
|
688
|
+
* If the `colorPalette` is `button` then the virtual token will be `colorPalette.light.accent.secondary`.
|
|
689
|
+
* ```jsx
|
|
690
|
+
* <button
|
|
691
|
+
* className={css({
|
|
692
|
+
* colorPalette: 'button',
|
|
693
|
+
* color: 'colorPalette.light.accent.secondary',
|
|
694
|
+
* })}
|
|
695
|
+
* />
|
|
696
|
+
* ```
|
|
697
|
+
*
|
|
698
|
+
* If the `colorPalette` is `button.light` then the virtual token will be `colorPalette.accent.secondary`.
|
|
699
|
+
* ```jsx
|
|
700
|
+
* <button
|
|
701
|
+
* className={css({
|
|
702
|
+
* colorPalette: 'button.light',
|
|
703
|
+
* color: 'colorPalette.accent.secondary',
|
|
704
|
+
* })}
|
|
705
|
+
* />
|
|
706
|
+
* ```
|
|
707
|
+
*
|
|
708
|
+
* If the `colorPalette` is `button.light.accent` then the virtual token will be `colorPalette.secondary`.
|
|
709
|
+
* ```jsx
|
|
710
|
+
* <button
|
|
711
|
+
* className={css({
|
|
712
|
+
* colorPalette: 'button.light.accent',
|
|
713
|
+
* color: 'colorPalette.secondary',
|
|
714
|
+
* })}
|
|
715
|
+
* />
|
|
716
|
+
*/
|
|
717
|
+
const startIndex = token.path.indexOf(colorPaletteRoot) + 1;
|
|
718
|
+
const remainingPath = token.path.slice(startIndex);
|
|
719
|
+
const colorPaletteTokenKeys = [];
|
|
720
|
+
for (let i = 0; i < remainingPath.length; i++) colorPaletteTokenKeys.push(remainingPath.slice(i));
|
|
721
|
+
if (colorPaletteTokenKeys.length === 0) colorPaletteTokenKeys.push([""]);
|
|
722
|
+
return {
|
|
723
|
+
colorPalette,
|
|
724
|
+
colorPaletteRoots,
|
|
725
|
+
colorPaletteTokenKeys
|
|
726
|
+
};
|
|
727
|
+
}
|
|
728
|
+
}
|
|
707
729
|
];
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
}
|
|
801
|
-
if (state === "token") {
|
|
802
|
-
tokenPath += char;
|
|
803
|
-
}
|
|
804
|
-
if (state === "fallback") {
|
|
805
|
-
fallback += char;
|
|
806
|
-
}
|
|
807
|
-
continue;
|
|
808
|
-
}
|
|
809
|
-
const tokenIndex = str.indexOf("token(", index);
|
|
810
|
-
if (tokenIndex !== -1) {
|
|
811
|
-
const innerTokenIndex = tokenIndex + "token(".length;
|
|
812
|
-
expanded += str.slice(index, tokenIndex);
|
|
813
|
-
index = innerTokenIndex;
|
|
814
|
-
state = "token";
|
|
815
|
-
currentStates.push(state);
|
|
816
|
-
continue;
|
|
817
|
-
}
|
|
818
|
-
expanded += char;
|
|
819
|
-
index++;
|
|
820
|
-
}
|
|
821
|
-
return expanded;
|
|
730
|
+
//#endregion
|
|
731
|
+
//#region src/expand-token-references.ts
|
|
732
|
+
/**
|
|
733
|
+
* Recursively parse a string to extract Bamboo token references (curly or with the `token` function syntax)
|
|
734
|
+
* Allows nested token references, e.g. `token(colors.xxx.yyy, token(colors.aaa.bbb, blue))`
|
|
735
|
+
* Properly ignore CSS vars in fallback syntax, e.g. `token(colors.xxx.yyy, var(--some-var, var(--can-be-nested, blue)))`
|
|
736
|
+
*/
|
|
737
|
+
const expandTokenReferences = (str, resolve) => {
|
|
738
|
+
let expanded = "";
|
|
739
|
+
let index = 0;
|
|
740
|
+
let state = "char";
|
|
741
|
+
let tokenPath = "";
|
|
742
|
+
let fallback = "";
|
|
743
|
+
const currentStates = [];
|
|
744
|
+
while (index < str.length) {
|
|
745
|
+
const char = str[index];
|
|
746
|
+
if (char === "{") {
|
|
747
|
+
const endIndex = str.indexOf("}", index);
|
|
748
|
+
if (endIndex === -1) break;
|
|
749
|
+
const path = str.slice(index + 1, endIndex);
|
|
750
|
+
const resolved = resolve(path);
|
|
751
|
+
expanded += resolved ?? path;
|
|
752
|
+
index = endIndex + 1;
|
|
753
|
+
continue;
|
|
754
|
+
}
|
|
755
|
+
if (state === "token") {
|
|
756
|
+
if (char === ",") {
|
|
757
|
+
if (str[index] === "") index++;
|
|
758
|
+
state = "fallback";
|
|
759
|
+
currentStates.push(state);
|
|
760
|
+
const resolved = resolve(tokenPath);
|
|
761
|
+
if (resolved?.endsWith(")")) expanded += resolved.slice(0, -1);
|
|
762
|
+
tokenPath = "";
|
|
763
|
+
fallback = "";
|
|
764
|
+
continue;
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
if (state === "fallback") {
|
|
768
|
+
if (fallback + char === ", var(") {
|
|
769
|
+
const endIndex = cssVarParser(str.slice(index + 1)) + index + 1;
|
|
770
|
+
const cssVar = str.slice(index + 1, endIndex);
|
|
771
|
+
if (endIndex === -1) break;
|
|
772
|
+
expanded += ", var(" + cssVar + ")";
|
|
773
|
+
index = endIndex + 1;
|
|
774
|
+
state = currentStates.pop() ?? state;
|
|
775
|
+
fallback = "";
|
|
776
|
+
continue;
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
if (state === "token" || state === "fallback") {
|
|
780
|
+
index++;
|
|
781
|
+
if (char === ")") {
|
|
782
|
+
state = currentStates.pop() ?? state ?? "char";
|
|
783
|
+
fallback += char;
|
|
784
|
+
const resolved = tokenPath ? resolve(tokenPath) ?? esc(tokenPath) : tokenPath;
|
|
785
|
+
if (fallback) {
|
|
786
|
+
fallback = fallback.slice(1).trim();
|
|
787
|
+
if (!fallback.startsWith("token(") && fallback.endsWith(")")) fallback = fallback.slice(0, -1);
|
|
788
|
+
if (fallback.includes("token(")) {
|
|
789
|
+
const parsed = expandTokenReferences(fallback, resolve);
|
|
790
|
+
if (parsed) fallback = parsed.slice(0, -1);
|
|
791
|
+
} else if (fallback) {
|
|
792
|
+
const resolvedFallback = resolve(fallback);
|
|
793
|
+
if (resolvedFallback) fallback = resolvedFallback;
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
const lastChar = expanded.at(-1);
|
|
797
|
+
if (fallback) if (lastChar?.trim()) expanded += resolved.slice(0, -1) + (", " + fallback + ")");
|
|
798
|
+
else expanded += fallback;
|
|
799
|
+
else expanded += resolved || ")";
|
|
800
|
+
tokenPath = "";
|
|
801
|
+
fallback = "";
|
|
802
|
+
state = "char";
|
|
803
|
+
continue;
|
|
804
|
+
}
|
|
805
|
+
if (state === "token") tokenPath += char;
|
|
806
|
+
if (state === "fallback") fallback += char;
|
|
807
|
+
continue;
|
|
808
|
+
}
|
|
809
|
+
const tokenIndex = str.indexOf("token(", index);
|
|
810
|
+
if (tokenIndex !== -1) {
|
|
811
|
+
const innerTokenIndex = tokenIndex + 6;
|
|
812
|
+
expanded += str.slice(index, tokenIndex);
|
|
813
|
+
index = innerTokenIndex;
|
|
814
|
+
state = "token";
|
|
815
|
+
currentStates.push(state);
|
|
816
|
+
continue;
|
|
817
|
+
}
|
|
818
|
+
expanded += char;
|
|
819
|
+
index++;
|
|
820
|
+
}
|
|
821
|
+
return expanded;
|
|
822
822
|
};
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
823
|
+
/**
|
|
824
|
+
* Finds the closing parenthesis index in a string starting from the start of a CSS var
|
|
825
|
+
* @example cssVarParser('--colors-red-100, var(--colors-blue-200, var(--fallback)))')
|
|
826
|
+
*/
|
|
827
|
+
const cssVarParser = (str) => {
|
|
828
|
+
let index = 0;
|
|
829
|
+
const openedParenthesises = ["("];
|
|
830
|
+
while (index < str.length) {
|
|
831
|
+
const char = str[index];
|
|
832
|
+
if (char === "(") openedParenthesises.push(char);
|
|
833
|
+
else if (char === ")") {
|
|
834
|
+
openedParenthesises.pop();
|
|
835
|
+
if (openedParenthesises.length === 0) return index;
|
|
836
|
+
}
|
|
837
|
+
index++;
|
|
838
|
+
}
|
|
839
|
+
return index;
|
|
839
840
|
};
|
|
840
|
-
|
|
841
|
-
|
|
841
|
+
//#endregion
|
|
842
|
+
//#region src/dictionary.ts
|
|
842
843
|
function expandBreakpoints(breakpoints) {
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
844
|
+
if (!breakpoints) return {
|
|
845
|
+
breakpoints: {},
|
|
846
|
+
sizes: {}
|
|
847
|
+
};
|
|
848
|
+
return {
|
|
849
|
+
breakpoints: mapObject(breakpoints, (value) => ({ value })),
|
|
850
|
+
sizes: Object.fromEntries(Object.entries(breakpoints).map(([key, value]) => [`breakpoint-${key}`, { value }]))
|
|
851
|
+
};
|
|
848
852
|
}
|
|
849
853
|
function filterDefault(path) {
|
|
850
|
-
|
|
851
|
-
|
|
854
|
+
if (path[0] === "DEFAULT") return path;
|
|
855
|
+
return path.filter((item) => item !== "DEFAULT");
|
|
852
856
|
}
|
|
853
857
|
var TokenDictionary = class {
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
if (next.startsWith("var(")) {
|
|
1152
|
-
const ref = this.view.nameByVar.get(next);
|
|
1153
|
-
if (ref) {
|
|
1154
|
-
stack.push(this.resolveReference(`{${ref}}`));
|
|
1155
|
-
continue;
|
|
1156
|
-
}
|
|
1157
|
-
}
|
|
1158
|
-
return next;
|
|
1159
|
-
}
|
|
1160
|
-
}
|
|
1161
|
-
isDeprecated(name) {
|
|
1162
|
-
return this.deprecated.has(name);
|
|
1163
|
-
}
|
|
1164
|
-
build() {
|
|
1165
|
-
this.applyMiddlewares("pre");
|
|
1166
|
-
this.transformTokens("pre");
|
|
1167
|
-
this.addConditionalTokens();
|
|
1168
|
-
this.addReferences();
|
|
1169
|
-
this.expandTokenReferences();
|
|
1170
|
-
this.applyMiddlewares("post");
|
|
1171
|
-
this.transformTokens("post");
|
|
1172
|
-
this.setComputedView();
|
|
1173
|
-
return this;
|
|
1174
|
-
}
|
|
1175
|
-
get isEmpty() {
|
|
1176
|
-
return this.allTokens.length === 0;
|
|
1177
|
-
}
|
|
1178
|
-
view;
|
|
1179
|
-
setComputedView() {
|
|
1180
|
-
this.view = new TokenDictionaryView(this).getTokensView();
|
|
1181
|
-
return this;
|
|
1182
|
-
}
|
|
858
|
+
options;
|
|
859
|
+
allTokens = [];
|
|
860
|
+
byName = /* @__PURE__ */ new Map();
|
|
861
|
+
deprecated = /* @__PURE__ */ new Set();
|
|
862
|
+
constructor(options) {
|
|
863
|
+
this.options = options;
|
|
864
|
+
}
|
|
865
|
+
init() {
|
|
866
|
+
this.registerTokens();
|
|
867
|
+
this.registerTransform(...transforms);
|
|
868
|
+
this.registerMiddleware(...middlewares);
|
|
869
|
+
this.build();
|
|
870
|
+
return this;
|
|
871
|
+
}
|
|
872
|
+
get prefix() {
|
|
873
|
+
return this.options.prefix;
|
|
874
|
+
}
|
|
875
|
+
get hash() {
|
|
876
|
+
return this.options.hash;
|
|
877
|
+
}
|
|
878
|
+
get colorPalette() {
|
|
879
|
+
return this.options.colorPalette;
|
|
880
|
+
}
|
|
881
|
+
getByName = (path) => {
|
|
882
|
+
return this.byName.get(path);
|
|
883
|
+
};
|
|
884
|
+
formatTokenName = (path) => path.join(".");
|
|
885
|
+
formatCssVar = (path, options) => cssVar(path.join("-"), options);
|
|
886
|
+
registerTokens() {
|
|
887
|
+
const { tokens = {}, semanticTokens = {}, breakpoints, themes = {} } = this.options;
|
|
888
|
+
const breakpointTokens = expandBreakpoints(breakpoints);
|
|
889
|
+
const computedTokens = compact({
|
|
890
|
+
...tokens,
|
|
891
|
+
breakpoints: breakpointTokens.breakpoints,
|
|
892
|
+
sizes: {
|
|
893
|
+
...tokens.sizes,
|
|
894
|
+
...breakpointTokens.sizes
|
|
895
|
+
}
|
|
896
|
+
});
|
|
897
|
+
const processToken = (token, path) => {
|
|
898
|
+
const isDefault = path.includes("DEFAULT");
|
|
899
|
+
path = filterDefault(path);
|
|
900
|
+
assertTokenFormat(token);
|
|
901
|
+
const category = path[0];
|
|
902
|
+
const name = this.formatTokenName(path);
|
|
903
|
+
const node = new Token({
|
|
904
|
+
...token,
|
|
905
|
+
name,
|
|
906
|
+
path
|
|
907
|
+
});
|
|
908
|
+
node.setExtensions({
|
|
909
|
+
category,
|
|
910
|
+
prop: this.formatTokenName(path.slice(1))
|
|
911
|
+
});
|
|
912
|
+
if (isDefault) node.setExtensions({ isDefault });
|
|
913
|
+
return node;
|
|
914
|
+
};
|
|
915
|
+
const processSemantic = (token, path) => {
|
|
916
|
+
const isDefault = path.includes("DEFAULT");
|
|
917
|
+
path = filterDefault(path);
|
|
918
|
+
assertTokenFormat(token);
|
|
919
|
+
const category = path[0];
|
|
920
|
+
const name = this.formatTokenName(path);
|
|
921
|
+
const { value, ...restData } = isString(token.value) || isCompositeTokenValue(token.value) ? { value: { base: token.value } } : token;
|
|
922
|
+
const node = new Token({
|
|
923
|
+
...restData,
|
|
924
|
+
name,
|
|
925
|
+
value: value.base || "",
|
|
926
|
+
path
|
|
927
|
+
});
|
|
928
|
+
node.setExtensions({
|
|
929
|
+
category,
|
|
930
|
+
conditions: value,
|
|
931
|
+
rawValue: value,
|
|
932
|
+
prop: this.formatTokenName(path.slice(1))
|
|
933
|
+
});
|
|
934
|
+
if (isDefault) node.setExtensions({ isDefault });
|
|
935
|
+
return node;
|
|
936
|
+
};
|
|
937
|
+
walkObject(computedTokens, (token, path) => {
|
|
938
|
+
const node = processToken(token, path);
|
|
939
|
+
this.registerToken(node);
|
|
940
|
+
}, { stop: isToken });
|
|
941
|
+
walkObject(semanticTokens, (token, path) => {
|
|
942
|
+
const node = processSemantic(token, path);
|
|
943
|
+
this.registerToken(node);
|
|
944
|
+
}, { stop: isToken });
|
|
945
|
+
Object.entries(themes).forEach(([theme, themeVariant]) => {
|
|
946
|
+
const condName = "_theme" + capitalize(theme);
|
|
947
|
+
walkObject(themeVariant.tokens ?? {}, (token, path) => {
|
|
948
|
+
const node = processSemantic({ value: { [condName]: token.value } }, path);
|
|
949
|
+
node.setExtensions({
|
|
950
|
+
theme,
|
|
951
|
+
isVirtual: true
|
|
952
|
+
});
|
|
953
|
+
this.registerToken(node);
|
|
954
|
+
}, { stop: isToken });
|
|
955
|
+
walkObject(themeVariant.semanticTokens ?? {}, (token, path) => {
|
|
956
|
+
const node = processSemantic({ value: { [condName]: token.value } }, path);
|
|
957
|
+
node.setExtensions({
|
|
958
|
+
theme,
|
|
959
|
+
isSemantic: true,
|
|
960
|
+
isVirtual: true
|
|
961
|
+
});
|
|
962
|
+
this.registerToken(node);
|
|
963
|
+
}, { stop: isToken });
|
|
964
|
+
});
|
|
965
|
+
return this;
|
|
966
|
+
}
|
|
967
|
+
registerToken = (token, transformPhase) => {
|
|
968
|
+
this.allTokens.push(token);
|
|
969
|
+
this.byName.set(token.name, token);
|
|
970
|
+
if (token.deprecated) this.deprecated.add(token.name);
|
|
971
|
+
if (transformPhase) this.transforms.forEach((transform) => {
|
|
972
|
+
if (transform.enforce === transformPhase) this.execTransformOnToken(transform, token);
|
|
973
|
+
});
|
|
974
|
+
};
|
|
975
|
+
transforms = /* @__PURE__ */ new Map();
|
|
976
|
+
registerTransform(...transforms) {
|
|
977
|
+
transforms.forEach((transform) => {
|
|
978
|
+
transform.type ||= "value";
|
|
979
|
+
transform.enforce ||= "pre";
|
|
980
|
+
this.transforms.set(transform.name, transform);
|
|
981
|
+
});
|
|
982
|
+
return this;
|
|
983
|
+
}
|
|
984
|
+
execTransform(name) {
|
|
985
|
+
const transform = this.transforms.get(name);
|
|
986
|
+
if (!transform) return;
|
|
987
|
+
this.allTokens.forEach((token) => {
|
|
988
|
+
this.execTransformOnToken(transform, token);
|
|
989
|
+
});
|
|
990
|
+
}
|
|
991
|
+
execTransformOnToken(transform, token) {
|
|
992
|
+
if (token.extensions.hasReference) return;
|
|
993
|
+
if (typeof transform.match === "function" && !transform.match(token)) return;
|
|
994
|
+
const exec = (v) => transform.transform(v, this);
|
|
995
|
+
const transformed = exec(token);
|
|
996
|
+
match(transform).with({ type: "extensions" }, () => {
|
|
997
|
+
token.setExtensions(transformed);
|
|
998
|
+
}).with({ type: "value" }, () => {
|
|
999
|
+
token.value = transformed;
|
|
1000
|
+
if (token.isComposite) token.originalValue = transformed;
|
|
1001
|
+
if (token.extensions.conditions) {
|
|
1002
|
+
const conditions = token.extensions.conditions;
|
|
1003
|
+
const transformedConditions = walkObject(conditions, (value) => exec({ value }), { stop: isCompositeTokenValue });
|
|
1004
|
+
token.setExtensions({ conditions: transformedConditions });
|
|
1005
|
+
}
|
|
1006
|
+
}).otherwise(() => {
|
|
1007
|
+
token[transform.type] = transformed;
|
|
1008
|
+
});
|
|
1009
|
+
}
|
|
1010
|
+
transformTokens(enforce) {
|
|
1011
|
+
this.transforms.forEach((transform) => {
|
|
1012
|
+
if (transform.enforce === enforce) this.execTransform(transform.name);
|
|
1013
|
+
});
|
|
1014
|
+
return this;
|
|
1015
|
+
}
|
|
1016
|
+
middlewares = [];
|
|
1017
|
+
registerMiddleware(...middlewares) {
|
|
1018
|
+
for (const middleware of middlewares) {
|
|
1019
|
+
middleware.enforce ||= "pre";
|
|
1020
|
+
this.middlewares.push(middleware);
|
|
1021
|
+
}
|
|
1022
|
+
return this;
|
|
1023
|
+
}
|
|
1024
|
+
applyMiddlewares(enforce) {
|
|
1025
|
+
this.middlewares.forEach((middleware) => {
|
|
1026
|
+
if (middleware.enforce === enforce) middleware.transform(this);
|
|
1027
|
+
});
|
|
1028
|
+
}
|
|
1029
|
+
getReferences(value) {
|
|
1030
|
+
return getReferences(value).map((ref) => this.getByName(ref)).filter(Boolean);
|
|
1031
|
+
}
|
|
1032
|
+
usesReference(value) {
|
|
1033
|
+
if (!isString(value)) return false;
|
|
1034
|
+
return this.getReferences(value).length > 0;
|
|
1035
|
+
}
|
|
1036
|
+
addReferences() {
|
|
1037
|
+
this.allTokens.forEach((token) => {
|
|
1038
|
+
if (!this.usesReference(token.value)) return;
|
|
1039
|
+
const references = this.getReferences(token.value);
|
|
1040
|
+
token.setExtensions({ references: references.reduce((object, reference) => {
|
|
1041
|
+
object[reference.name] = reference;
|
|
1042
|
+
return object;
|
|
1043
|
+
}, {}) });
|
|
1044
|
+
});
|
|
1045
|
+
return this;
|
|
1046
|
+
}
|
|
1047
|
+
filter(pattern) {
|
|
1048
|
+
const predicate = typeof pattern === "function" ? pattern : isMatching(pattern);
|
|
1049
|
+
return this.allTokens.filter(predicate);
|
|
1050
|
+
}
|
|
1051
|
+
addConditionalTokens() {
|
|
1052
|
+
this.allTokens.forEach((token) => {
|
|
1053
|
+
const conditionalTokens = token.getConditionTokens();
|
|
1054
|
+
if (conditionalTokens && conditionalTokens.length > 0) conditionalTokens.forEach((token) => {
|
|
1055
|
+
this.registerToken(token);
|
|
1056
|
+
});
|
|
1057
|
+
});
|
|
1058
|
+
return this;
|
|
1059
|
+
}
|
|
1060
|
+
expandTokenReferences() {
|
|
1061
|
+
this.allTokens.forEach((token) => {
|
|
1062
|
+
token.expandReferences();
|
|
1063
|
+
});
|
|
1064
|
+
return this;
|
|
1065
|
+
}
|
|
1066
|
+
colorMix = (value, tokenFn) => {
|
|
1067
|
+
if (!value || typeof value !== "string") return {
|
|
1068
|
+
invalid: true,
|
|
1069
|
+
value
|
|
1070
|
+
};
|
|
1071
|
+
const [colorPath, rawOpacity] = value.split("/");
|
|
1072
|
+
if (!colorPath || !rawOpacity) return {
|
|
1073
|
+
invalid: true,
|
|
1074
|
+
value: colorPath
|
|
1075
|
+
};
|
|
1076
|
+
const colorToken = tokenFn(colorPath);
|
|
1077
|
+
const opacityToken = this.getByName(`opacity.${rawOpacity}`)?.value;
|
|
1078
|
+
if (!opacityToken && isNaN(Number(rawOpacity))) return {
|
|
1079
|
+
invalid: true,
|
|
1080
|
+
value: colorPath
|
|
1081
|
+
};
|
|
1082
|
+
const percent = opacityToken ? Number(opacityToken) * 100 + "%" : `${rawOpacity}%`;
|
|
1083
|
+
const color = colorToken ?? colorPath;
|
|
1084
|
+
return {
|
|
1085
|
+
invalid: false,
|
|
1086
|
+
color,
|
|
1087
|
+
value: `color-mix(in srgb, ${color} ${percent}, transparent)`
|
|
1088
|
+
};
|
|
1089
|
+
};
|
|
1090
|
+
/**
|
|
1091
|
+
* Expand token references to their CSS variable
|
|
1092
|
+
*/
|
|
1093
|
+
expandReferenceInValue(value) {
|
|
1094
|
+
return expandTokenReferences(value, (path) => {
|
|
1095
|
+
if (!path) return;
|
|
1096
|
+
if (path.includes("/")) {
|
|
1097
|
+
const mix = this.colorMix(path, this.view.getVar.bind(this.view));
|
|
1098
|
+
if (mix.invalid) throw new BambooError("INVALID_TOKEN", "Invalid color mix at " + path + ": " + mix.value);
|
|
1099
|
+
return mix.value;
|
|
1100
|
+
}
|
|
1101
|
+
const resolved = this.view.getVar(path);
|
|
1102
|
+
if (resolved) return resolved;
|
|
1103
|
+
return tokenPathRegex.test(path) ? esc(path) : path;
|
|
1104
|
+
});
|
|
1105
|
+
}
|
|
1106
|
+
/**
|
|
1107
|
+
* Get the value of a token reference
|
|
1108
|
+
*/
|
|
1109
|
+
resolveReference(value) {
|
|
1110
|
+
return expandReferences(value, (key) => this.getByName(key)?.value);
|
|
1111
|
+
}
|
|
1112
|
+
/**
|
|
1113
|
+
* Resolve token references to their actual raw value (recursively resolves references)
|
|
1114
|
+
*/
|
|
1115
|
+
deepResolveReference(originalValue) {
|
|
1116
|
+
const stack = [originalValue];
|
|
1117
|
+
while (stack.length) {
|
|
1118
|
+
const next = stack.pop();
|
|
1119
|
+
if (next.startsWith("{")) {
|
|
1120
|
+
stack.push(this.resolveReference(next));
|
|
1121
|
+
continue;
|
|
1122
|
+
}
|
|
1123
|
+
if (next.startsWith("var(")) {
|
|
1124
|
+
const ref = this.view.nameByVar.get(next);
|
|
1125
|
+
if (ref) {
|
|
1126
|
+
stack.push(this.resolveReference(`{${ref}}`));
|
|
1127
|
+
continue;
|
|
1128
|
+
}
|
|
1129
|
+
}
|
|
1130
|
+
return next;
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
1133
|
+
isDeprecated(name) {
|
|
1134
|
+
return this.deprecated.has(name);
|
|
1135
|
+
}
|
|
1136
|
+
build() {
|
|
1137
|
+
this.applyMiddlewares("pre");
|
|
1138
|
+
this.transformTokens("pre");
|
|
1139
|
+
this.addConditionalTokens();
|
|
1140
|
+
this.addReferences();
|
|
1141
|
+
this.expandTokenReferences();
|
|
1142
|
+
this.applyMiddlewares("post");
|
|
1143
|
+
this.transformTokens("post");
|
|
1144
|
+
this.setComputedView();
|
|
1145
|
+
return this;
|
|
1146
|
+
}
|
|
1147
|
+
get isEmpty() {
|
|
1148
|
+
return this.allTokens.length === 0;
|
|
1149
|
+
}
|
|
1150
|
+
view;
|
|
1151
|
+
setComputedView() {
|
|
1152
|
+
this.view = new TokenDictionaryView(this).getTokensView();
|
|
1153
|
+
return this;
|
|
1154
|
+
}
|
|
1183
1155
|
};
|
|
1184
1156
|
var TokenDictionaryView = class {
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
}
|
|
1295
|
-
processVars(token, group) {
|
|
1296
|
-
const { condition, isNegative, isVirtual, var: varName, theme } = token.extensions;
|
|
1297
|
-
if (isNegative || !theme && isVirtual || !condition) return;
|
|
1298
|
-
if (!group.has(condition)) group.set(condition, /* @__PURE__ */ new Map());
|
|
1299
|
-
group.get(condition).set(varName, token.value);
|
|
1300
|
-
}
|
|
1157
|
+
dictionary;
|
|
1158
|
+
constructor(dictionary) {
|
|
1159
|
+
this.dictionary = dictionary;
|
|
1160
|
+
this.dictionary = dictionary;
|
|
1161
|
+
}
|
|
1162
|
+
getTokensView() {
|
|
1163
|
+
const conditionMap = /* @__PURE__ */ new Map();
|
|
1164
|
+
const categoryMap = /* @__PURE__ */ new Map();
|
|
1165
|
+
const colorPalettes = /* @__PURE__ */ new Map();
|
|
1166
|
+
const colorPaletteTokens = /* @__PURE__ */ new Map();
|
|
1167
|
+
const valuesByCategory = /* @__PURE__ */ new Map();
|
|
1168
|
+
const flatValues = /* @__PURE__ */ new Map();
|
|
1169
|
+
const rawValues = /* @__PURE__ */ new Map();
|
|
1170
|
+
const nameByVar = /* @__PURE__ */ new Map();
|
|
1171
|
+
const vars = /* @__PURE__ */ new Map();
|
|
1172
|
+
this.dictionary.allTokens.forEach((token) => {
|
|
1173
|
+
this.processCondition(token, conditionMap);
|
|
1174
|
+
this.processColorPalette(token, colorPalettes, colorPaletteTokens, this.dictionary.byName);
|
|
1175
|
+
this.processCategory(token, categoryMap);
|
|
1176
|
+
this.processValue(token, valuesByCategory, flatValues, rawValues, nameByVar);
|
|
1177
|
+
this.processVars(token, vars);
|
|
1178
|
+
});
|
|
1179
|
+
const json = mapToJson(valuesByCategory);
|
|
1180
|
+
return {
|
|
1181
|
+
conditionMap,
|
|
1182
|
+
categoryMap,
|
|
1183
|
+
colorPalettes,
|
|
1184
|
+
vars,
|
|
1185
|
+
values: flatValues,
|
|
1186
|
+
nameByVar,
|
|
1187
|
+
json,
|
|
1188
|
+
valuesByCategory,
|
|
1189
|
+
get: memo((path, fallback) => {
|
|
1190
|
+
return rawValues.get(path) ?? fallback;
|
|
1191
|
+
}),
|
|
1192
|
+
getVar: memo((path, fallback) => {
|
|
1193
|
+
return flatValues.get(path) ?? fallback;
|
|
1194
|
+
}),
|
|
1195
|
+
getCategoryValues: memo((category) => {
|
|
1196
|
+
const result = json[category];
|
|
1197
|
+
if (result != null) return result;
|
|
1198
|
+
}),
|
|
1199
|
+
getColorPaletteValues: memo((palette) => {
|
|
1200
|
+
const tokens = colorPaletteTokens.get(palette);
|
|
1201
|
+
if (!tokens) return [];
|
|
1202
|
+
return Array.from(tokens).sort();
|
|
1203
|
+
})
|
|
1204
|
+
};
|
|
1205
|
+
}
|
|
1206
|
+
processCondition(token, group) {
|
|
1207
|
+
const { condition } = token.extensions;
|
|
1208
|
+
if (!condition) return;
|
|
1209
|
+
if (!group.has(condition)) group.set(condition, /* @__PURE__ */ new Set());
|
|
1210
|
+
group.get(condition).add(token);
|
|
1211
|
+
}
|
|
1212
|
+
processColorPalette(token, group, tokenPaths, byName) {
|
|
1213
|
+
const extensions = token.extensions;
|
|
1214
|
+
const { colorPalette, colorPaletteRoots, isVirtual } = extensions;
|
|
1215
|
+
if (!colorPalette || isVirtual) return;
|
|
1216
|
+
colorPaletteRoots.forEach((colorPaletteRoot) => {
|
|
1217
|
+
const formated = this.dictionary.formatTokenName(colorPaletteRoot);
|
|
1218
|
+
if (!group.has(formated)) group.set(formated, /* @__PURE__ */ new Map());
|
|
1219
|
+
if (!tokenPaths.has(formated)) tokenPaths.set(formated, /* @__PURE__ */ new Set());
|
|
1220
|
+
const virtualPath = replaceRootWithColorPalette([...token.path], [...colorPaletteRoot]);
|
|
1221
|
+
const virtualName = this.dictionary.formatTokenName(virtualPath);
|
|
1222
|
+
const virtualToken = byName.get(virtualName);
|
|
1223
|
+
if (!virtualToken) return;
|
|
1224
|
+
const virtualVar = virtualToken.extensions.var;
|
|
1225
|
+
group.get(formated).set(virtualVar, token.extensions.varRef);
|
|
1226
|
+
const tokenPath = this.dictionary.formatTokenName(virtualPath.slice(1));
|
|
1227
|
+
tokenPaths.get(formated).add(tokenPath);
|
|
1228
|
+
if (extensions.isDefault && colorPaletteRoot.length === 1) {
|
|
1229
|
+
const colorPaletteName = this.dictionary.formatTokenName(["colors", "colorPalette"]);
|
|
1230
|
+
const colorPaletteToken = byName.get(colorPaletteName);
|
|
1231
|
+
if (!colorPaletteToken) return;
|
|
1232
|
+
const name = this.dictionary.formatTokenName(token.path);
|
|
1233
|
+
const virtualToken = byName.get(name);
|
|
1234
|
+
if (!virtualToken) return;
|
|
1235
|
+
const keyPath = extensions.colorPaletteTokenKeys[0]?.filter(Boolean);
|
|
1236
|
+
if (!keyPath.length) return;
|
|
1237
|
+
const formated = this.dictionary.formatTokenName(colorPaletteRoot.concat(keyPath));
|
|
1238
|
+
if (!group.has(formated)) group.set(formated, /* @__PURE__ */ new Map());
|
|
1239
|
+
group.get(formated).set(colorPaletteToken.extensions.var, virtualToken.extensions.varRef);
|
|
1240
|
+
}
|
|
1241
|
+
});
|
|
1242
|
+
}
|
|
1243
|
+
processCategory(token, group) {
|
|
1244
|
+
const { category, prop } = token.extensions;
|
|
1245
|
+
if (!category) return;
|
|
1246
|
+
if (!group.has(category)) group.set(category, /* @__PURE__ */ new Map());
|
|
1247
|
+
group.get(category).set(prop, token);
|
|
1248
|
+
}
|
|
1249
|
+
processValue(token, byCategory, flatValues, rawValues, nameByVar) {
|
|
1250
|
+
const { category, prop, varRef, isNegative, isVirtual, condition } = token.extensions;
|
|
1251
|
+
if (!category) return;
|
|
1252
|
+
if (!byCategory.has(category)) byCategory.set(category, /* @__PURE__ */ new Map());
|
|
1253
|
+
const value = isNegative ? token.extensions.condition !== "base" ? token.originalValue : token.value : varRef;
|
|
1254
|
+
byCategory.get(category).set(prop, value);
|
|
1255
|
+
flatValues.set(token.name, value);
|
|
1256
|
+
nameByVar.set(value, token.name);
|
|
1257
|
+
const rawValue = isVirtual || condition !== "base" ? varRef : token.value;
|
|
1258
|
+
rawValues.set(token.name, rawValue);
|
|
1259
|
+
}
|
|
1260
|
+
processVars(token, group) {
|
|
1261
|
+
const { condition, isNegative, isVirtual, var: varName, theme } = token.extensions;
|
|
1262
|
+
if (isNegative || !theme && isVirtual || !condition) return;
|
|
1263
|
+
if (!group.has(condition)) group.set(condition, /* @__PURE__ */ new Map());
|
|
1264
|
+
group.get(condition).set(varName, token.value);
|
|
1265
|
+
}
|
|
1301
1266
|
};
|
|
1267
|
+
/**
|
|
1268
|
+
* Replaces the colorPaletteRoot in token.path with 'colorPalette'
|
|
1269
|
+
* @example replaceRootWithColorPalette(['colors', 'button', 'red', '500'], ['button', 'red'])
|
|
1270
|
+
* // => ["colors", "colorPalette", "500"]
|
|
1271
|
+
*/
|
|
1302
1272
|
function replaceRootWithColorPalette(path, colorPaletteRoot) {
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
}
|
|
1309
|
-
path.splice(startIndex, colorPaletteRoot.length);
|
|
1310
|
-
path.splice(startIndex, 0, "colorPalette");
|
|
1311
|
-
return path;
|
|
1273
|
+
const startIndex = path.findIndex((element, index) => colorPaletteRoot.every((rootElement, rootIndex) => path[index + rootIndex] === rootElement));
|
|
1274
|
+
if (startIndex === -1) return path;
|
|
1275
|
+
path.splice(startIndex, colorPaletteRoot.length);
|
|
1276
|
+
path.splice(startIndex, 0, "colorPalette");
|
|
1277
|
+
return path;
|
|
1312
1278
|
}
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
Token,
|
|
1317
|
-
TokenDictionary,
|
|
1318
|
-
assertTokenFormat,
|
|
1319
|
-
expandReferences,
|
|
1320
|
-
getReferences,
|
|
1321
|
-
hasReference,
|
|
1322
|
-
isToken,
|
|
1323
|
-
mapToJson
|
|
1324
|
-
};
|
|
1279
|
+
const tokenPathRegex = /\w+\.\w+/;
|
|
1280
|
+
//#endregion
|
|
1281
|
+
export { TOKEN_CATEGORIES, Token, TokenDictionary, assertTokenFormat, expandReferences, getReferences, hasReference, isToken, mapToJson };
|