-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_theme-applier.scss
402 lines (369 loc) · 12.8 KB
/
_theme-applier.scss
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
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
/*
* FILE: _theme-applier.scss
*
* This file contains mixins for applying theme tokens, generating CSS variables,
* and creating utility classes based on the theme configuration.
*
* MIXINS:
* - generateThemeVariables: Applies tokens for a specified type or all types if
* no type is specified.
* - generateTokenVariables: Generates CSS custom properties for a specific token,
* optionally processing values with a function.
* - generateThemeUtilities: Generates utility classes for theme tokens.
* - generateThemeSpacingUtilities: Generates custom utility classes for theme
* tokens.
*
* VARIABLES:
* - $token-type: Maps different token types and their associated keys.
* - $exception-exclude-base-suffix: List of tokens excluding the base suffix from
* CSS custom property names.
* - $exception-utility-properties: Defines exceptions for certain properties,
* allowing alternative names and modifications to the property.
* - $custom-utility-prefixes: List of custom utility class prefixes.
*/
// Import theme configuration and tokens
@use "../00-settings/theme-config" as config;
@use "../00-settings/color-theme" as color;
// Import functions for generating variables
@use "../10-functions/select-theme" as *;
@use "../10-functions/transform-color-p3" as *;
// Import helper functions
@use "../10-functions/helpers/handle-string" as help-str;
// Define token types and their associated keys
$token-type: (
"global": (
"palette",
"shadow",
"spacing",
"step-size",
"font-family",
"font-weight",
"line-height",
"letter-spacing",
"radius",
"layout",
"measure",
),
"contextual-generic": (
"theme",
"text-size",
"space-gutter",
"space-region",
),
"component-specific": (),
"contextual-content": (),
);
// List of tokens excluding the base suffix from CSS custom property names
$exception-exclude-base-suffix: (
"shadow",
"line-height",
"letter-spacing",
"radius",
"measure",
"space-gutter",
"space-region"
);
// Define exceptions for properties, allowing alternative names and mods
$exception-utility-properties: (
"color": (
"alt-name": "text",
"alt-property": "color",
),
"background-color": (
"alt-name": "background",
"alt-property": "background-color",
),
"box-shadow": (
"alt-name": "shadow",
"alt-property": "box-shadow",
),
"border-color": (
"alt-name": "border",
"alt-property": "border-color",
),
"font-size": (
"alt-name": "text-size",
"alt-property": "font-size",
),
"font-family": (
"alt-name": "font",
"alt-property": "font-family",
),
);
// List of custom utility class prefixes
$custom-utility-prefixes: (
"space-box",
"space-cluster",
"space-columns",
"space-cover",
"space-flow",
"space-grid",
"space-gutter",
"space-region",
"space-repel",
"space-sidebar",
"space-stack",
"space-switcher"
);
// Retrieve the class type prefix for utilities
$class-type-prefix: map-get(config.$class-type-prefixes, utilities);
/**
* Mixin: generateThemeVariables
* Applies tokens for a specified type or all types if no type is specified.
*
* @param {String|null} $type - Type of tokens to apply (e.g., "global", "ctx").
*/
@mixin generateThemeVariables($type: null) {
@each $current-type, $keys in $token-type {
// Check if $type is null or matches the current type
@if $type == null or $type == $current-type {
@each $group in config.$groups {
$group-token-key: map-get($group, "key");
// Check if $group-token-key exists in $token-type under $type
@if index($keys, $group-token-key) {
@include generateTokenVariables($group-token-key);
}
}
}
}
}
/**
* Mixin: generateTokenVariables
* Generates CSS custom properties for a specific token.
*
* @param {String} $group-token-key - The token group key to generate vars.
* @param {Function|null} $function - Optional function to process values.
* @param {String|null} $function-arg - Optional argument for the function.
*/
@mixin generateTokenVariables(
$group-token-key,
$function: null,
$function-arg: null
) {
// Handle exceptions for the theme group-token-key
@if $group-token-key == "theme" {
@if $function == null {
$function: "selectTheme";
}
@if $function-arg == null {
$first-item-name: to-lower-case(
nth(map-get(nth(map-get(color.$token, "items"), 1), "name"), 1)
);
$function-arg: $first-item-name;
}
}
// Loop through each group in the theme configuration
@each $group in config.$groups {
$group-key: map-get($group, "key");
// Check if the current group matches the provided token key
@if $group-key == $group-token-key {
$group-prefix: help-str.slugify(map-get($group, "prefix"));
$items: map-get(config.$theme, $group-token-key);
// Loop through each item in the token
@each $item-name, $item-value in $items {
// Initialize result
$result: null;
// Apply function if provided and exists
@if $function and function-exists($function) {
$result: call(
get-function($function),
$item-name,
$item-value,
if($function-arg != null, to-lower-case($function-arg), null)
);
} @else {
// Warn if function not found, return item name and value without fn
@if $function and not function-exists($function) {
@warn "Invalid function `#{$function}`.";
}
$result: (
name: $item-name,
value: $item-value,
);
}
// Check if $result is not null
@if $result != null {
$item-name: map-get($result, "name");
$item-value: map-get($result, "value");
// Generate CSS custom properties, handling base exceptions
@if $item-value != null {
@if $item-name ==
"base" and
index($exception-exclude-base-suffix, $group-token-key)
{
--#{$group-prefix}: #{$item-value};
} @else {
--#{$group-prefix}-#{$item-name}: #{$item-value};
}
}
}
}
}
}
}
/**
* Mixin: generateThemeUtilities
* Generates utility classes for theme tokens.
*/
@mixin generateThemeUtilities() {
// Loop through each group in the theme configuration
@each $group in config.$groups {
$group-key: map-get($group, "key");
$group-prefix: map-get($group, "prefix");
$group-properties: map-get($group, "properties");
$items: map-get(config.$theme, $group-key);
// Check if the group has properties defined
@if $group-properties != null {
// Loop through each property in the group
@each $group-property in $group-properties {
$property-or-alt-name: $group-property;
$css-property: $group-property;
// Check if the property is in exception-utility-properties
@if map-has-key($exception-utility-properties, $group-property) {
$property-or-alt-name: map-get(
map-get($exception-utility-properties, $group-property),
"alt-name"
);
$css-property: map-get(
map-get($exception-utility-properties, $group-property),
"alt-property"
);
}
// Handle exceptions for specific groups
@if $group-key == "palette" {
@if $group-property == "color" {
@if map-has-key($exception-utility-properties, "color") {
$property-or-alt-name: $group-prefix;
}
}
}
// Generate utility classes for auto and zero spacing
@if $group-key == "spacing" {
// Uncomment to enable abbreviated property names
$property-or-alt-name: help-str.abbreviate(
help-str.listToString($group-property, "-")
);
.#{$class-type-prefix}#{$property-or-alt-name}-auto {
#{$css-property}: auto;
}
.#{$class-type-prefix}#{$property-or-alt-name}-zero {
#{$css-property}: 0;
}
}
// Handle exceptions for the scale-text group
@if $group-key == "step-size" {
@if $group-property == "font-size" {
@if map-has-key($exception-utility-properties, "font-size") {
$property-or-alt-name: $group-prefix;
}
}
}
// Process theme group items
@if $group-key == "theme" {
// Extract theme names from the color.$token map
$themes: ();
@each $item in map-get(color.$token, "items") {
$theme-name: to-lower-case(map-get($item, "name"));
$themes: append($themes, $theme-name);
}
// Remove theme prefixes from item names
$processed-items: ();
@each $item-name, $value in $items {
@each $theme-name in $themes {
@if str-index($item-name, "#{$theme-name}-") {
$item-name: help-str.stringReplace(
$item-name,
"#{$theme-name}-",
""
);
}
}
// Merge the modified item name and value into $processed-items map
$processed-items: map-merge(
$processed-items,
(
$item-name: $value,
)
);
}
$items: $processed-items;
}
@if $items != null {
// Loop through each item in the token
@each $item-name in map-keys($items) {
// Handle theme group items
@if $group-key == "theme" {
$category: nth(help-str.stringToList($item-name, "-"), 1);
$item-name: help-str.stringTrimAfter($item-name, "-", 1);
$item-name-prop: nth(help-str.stringToList($item-name, "-"), 1);
// Generate utility class for theme items
@if $property-or-alt-name == $item-name-prop {
// // Uncomment to see utility classes for general theme items
// @debug ".#{$class-type-prefix}#{$category}-#{$item-name} | #{$css-property}: var(--#{$group-prefix}-#{$category}-#{$item-name})";
.#{$class-type-prefix}#{$category}-#{$item-name} {
#{$css-property}: var(
--#{$group-prefix}-#{$category}-#{$item-name}
);
}
}
} @else {
// Handle base property exceptions
@if $item-name ==
"base" and
index($exception-exclude-base-suffix, $group-key)
{
// // Uncomment to see utility classes for 'base' items from the token theme
// @debug ".#{$class-type-prefix}#{$property-or-alt-name}-base | #{$css-property}: var(--#{$group-prefix})";
.#{$class-type-prefix}#{$property-or-alt-name}-base {
#{$css-property}: var(--#{$group-prefix});
}
} @else {
// // Uncomment to see generated utility classes for token theme items
// @debug ".#{$class-type-prefix}#{$property-or-alt-name}-#{$item-name} | #{$css-property}: var(--#{$group-prefix}-#{$item-name})";
.#{$class-type-prefix}#{$property-or-alt-name}-#{$item-name} {
#{$css-property}: var(--#{$group-prefix}-#{$item-name});
}
}
}
}
} @else {
@warn "No items found for `#{$group-key}`.";
}
}
}
}
}
/**
* Mixin: generateThemeSpacingUtilities
* Generates custom utility classes for theme tokens.
*/
@mixin generateThemeSpacingUtilities() {
// Get the token and prefix for spacing
$token-name: "spacing";
$token: map-get(config.$theme, $token-name);
$token-prefix: null;
// Find the prefix for the token
@each $group in config.$groups {
@if map-get($group, "key") == $token-name {
$token-prefix: map-get($group, "prefix");
}
}
// If the token and prefix are found, generate the custom utility classes
@if $token != null and $token-prefix != null {
@each $prefix in $custom-utility-prefixes {
$property: "--#{$prefix}";
// // Uncomment to see generated utility classes for spacing-related theme items with zero value
// @debug ".#{$class-type-prefix}#{$prefix}-zero | #{$property}: 0";
.#{$class-type-prefix}#{$prefix}-zero {
#{$property}: 0;
}
@each $item-name, $item-value in $token {
// // Uncomment to see generated utility classes for spacing-related theme items in layout utilities
// @debug ".#{$class-type-prefix}#{$prefix}-#{$item-name} | #{$property}: var(--#{$token-prefix}-#{$item-name})";
.#{$class-type-prefix}#{$prefix}-#{$item-name} {
#{$property}: var(--#{$token-prefix}-#{$item-name});
}
}
}
}
}