-
Notifications
You must be signed in to change notification settings - Fork 5
/
elements.ts
313 lines (261 loc) · 9.81 KB
/
elements.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
247
248
249
250
251
252
253
254
255
256
257
258
259
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import { PreprocessorGroup } from "svelte/types/compiler/preprocess";
import {
rgba,
rem,
em,
px,
breakpoint,
breakpointDown,
breakpointUp,
breakpoints,
BreakpointName,
} from "@carbon/elements";
import { TokenTypeStyles, TokenUI, v10_theme } from "../build/build-elements";
import { MediaFeature, walkAndReplace } from "../walk-and-replace";
import * as carbonElements from "../carbon-elements";
interface PreprocessElementsOptions {
/**
* Specify the Carbon theme
* Setting to "all" will also enable `cssVars`
* @default "white"
*/
theme: v10_theme | "all";
/**
* Set to `true` for tokens to be re-written as CSS variables
* Automatically set to `true` if theme is "all"
* @example
* "spacing-05" --> var(--cds-spacing-05)
* "ui-01" --> var(--cds-ui-01)
* @default false
*/
cssVars: boolean;
}
export type ElementsOptions = Partial<PreprocessElementsOptions>;
type Token = keyof typeof carbonElements.elements.tokens;
const PROPERTY_ALIAS = {
font: "font",
};
const TOKEN_TYPE = {
font: "font",
};
const BREAKPOINTS = Object.keys(breakpoints);
function getMediaFeatureValue(feature: MediaFeature) {
if (feature.value == null) {
if (BREAKPOINTS.includes(feature.name)) {
return breakpoints[feature.name as BreakpointName].width;
} else {
console.warn(
`[carbon:elements] Invalid breakpoint name "${feature.name}"`
);
return null;
}
}
if ("unit" in feature.value) {
return feature.value.value + feature.value.unit;
}
}
export function elements(
options?: ElementsOptions
): Pick<PreprocessorGroup, "style"> {
const theme = options?.theme ?? "white";
const cssVars = options?.cssVars === true || theme === "all";
return {
style({ filename, content }) {
if (filename && !/node_modules/.test(filename)) {
const code = walkAndReplace(
{ type: "style", content, filename },
({ node }, replaceContent, getContent) => {
if (node.type === "Rule") {
node.block.children.forEach((child) => {
let operator: string | undefined = undefined;
let operatorValue: string | undefined = undefined;
const hasOperator = child.value.children.some(
(child, index) => index > 0 && child.type === "Operator"
);
if (hasOperator) {
operator = child.value.children.find(
(child) => child.type === "Operator"
)!.value;
operatorValue = child.value.children.find(
(child) => child.type === "Number"
)?.value;
}
child.value.children.forEach((childValue) => {
if (childValue.type === "String") {
const token = childValue.value.replace(/(\"|\')/g, "");
if (
token !== undefined &&
token in carbonElements.elements.tokens
) {
const tokenValue =
carbonElements.elements.tokens[token as Token];
if (child.property in TOKEN_TYPE) {
if (
(tokenValue as TokenTypeStyles).breakpoints.length > 0
) {
// TODO: generate responsive styles
}
replaceContent(
node,
(tokenValue as TokenTypeStyles).css,
getContent(child)
);
return (tokenValue as TokenTypeStyles).css;
}
if (token in carbonElements.elements.tokens_ui) {
let Value: TokenUI | string;
if (theme === "all" || cssVars)
Value = `var(--cds-${token})`;
else
Value =
carbonElements.elements.tokens_ui[token as TokenUI][
theme
];
const oldValue = getContent(child.value);
const newValue = oldValue.replace(
childValue.value,
Value
);
replaceContent(
node,
`${child.property}: ${newValue}`,
getContent(child)
);
if (theme === "all") return `var(--cds-${token})`;
return carbonElements.elements.tokens_ui[
token as TokenUI
][theme];
}
const oldValue = getContent(child.value);
let newValue = oldValue.replace(
childValue.value,
tokenValue as string
);
if (hasOperator && operatorValue !== undefined) {
newValue = `calc(${tokenValue} ${operator} ${operatorValue})`;
}
replaceContent(
node,
`${child.property}: ${newValue}`,
getContent(child)
);
return tokenValue;
}
}
return getContent(childValue);
});
});
}
if (node.type === "Atrule" && node.name === "media") {
const mediaFeatures: MediaFeature[] = [];
node.prelude.children
.flatMap((child) => child.children ?? [])
.forEach((node) => {
node.children.forEach((child) => {
if (child.type === "MediaFeature") {
mediaFeatures.push(child);
}
});
});
if (mediaFeatures.length === 1) {
const { name, value } = mediaFeatures[0];
let featureValue: string = "";
if (value != null && "name" in value) {
featureValue = value.name;
}
if (BREAKPOINTS.includes(featureValue)) {
switch (name) {
case "up":
replaceContent(
node,
`${breakpointUp(
featureValue as BreakpointName
)} ${getContent(node.block)}`
);
break;
case "down":
replaceContent(
node,
`${breakpointDown(
featureValue as BreakpointName
)} ${getContent(node.block)}`
);
break;
case "bp":
replaceContent(
node,
`${breakpoint(
featureValue as BreakpointName
)} ${getContent(node.block)}`
);
break;
}
} else {
console.warn(
`[carbon:elements] Invalid breakpoint name "${name}"`
);
}
} else {
const firstValue = getMediaFeatureValue(mediaFeatures[0]);
const secondValue = getMediaFeatureValue(mediaFeatures[1]);
if (firstValue && secondValue) {
replaceContent(
node,
`@media (min-width: ${firstValue}) and (max-width: ${secondValue}) ${getContent(
node.block
)}`
);
}
}
}
if (node.type === "Function") {
if (node.name === "rgba") {
const [hex, operator, number] = node.children;
if (hex.value.length < 6) {
console.warn(
`[carbon:elements] rgba hex value must be six characters. Received "#${hex.value}"`
);
return;
}
let opacity = 1;
if (number !== undefined) {
opacity = Number(number.value);
if (opacity > 1) {
console.warn(
`[carbon:elements] rgba opacity value cannot be greater than 1. Received ${opacity}`
);
opacity = 1;
}
if (opacity < 0) {
console.warn(
`[carbon:elements] rgba opacity value cannot be less than 0. Received ${opacity}`
);
opacity = 0;
}
}
replaceContent(node, rgba(hex.value, opacity));
}
if (node.name === "rem") {
const dimension = node.children[0];
const px = Number(dimension?.value ?? 0);
replaceContent(node, rem(px));
}
if (node.name === "em") {
const dimension = node.children[0];
const px = Number(dimension?.value ?? 0);
replaceContent(node, em(px));
}
if (node.name === "px") {
const dimension = node.children[0];
const value = Number(dimension?.value ?? 0);
replaceContent(node, px(value));
}
}
}
);
return { code };
}
return { code: content };
},
};
}