Skip to content

Commit 1df4617

Browse files
authored
Merge pull request #193 from BeAPI/feat/theme-json-gradient
Dégradés `theme.json` → SCSS et utilitaires `--gradient`
2 parents 622a88c + fe2b319 commit 1df4617

2 files changed

Lines changed: 68 additions & 4 deletions

File tree

‎config/webpack-theme-json-plugin.js‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,22 +105,43 @@ class WebpackThemeJsonPlugin {
105105
'settings-color-' + color.slug
106106
);
107107
result += `${colorVar}: ${color.color};\n`;
108-
palette.push(`${color.slug}: ${colorVar}`);
108+
palette.push(`${formatMapKey(color.slug)}: ${colorVar}`);
109109
}
110110

111111
return (
112112
result +
113113
`$settings-palette: (\n\t${palette.join(',\n\t')}\n);\n`
114114
);
115115
},
116+
'settings-color-gradients'(key, value) {
117+
let result = '';
118+
const gradients = [];
119+
120+
for (const gradient of value) {
121+
const gradientVar = getVariableName(
122+
'settings-gradient-' + gradient.slug
123+
);
124+
result += `${gradientVar}: ${gradient.gradient};\n`;
125+
gradients.push(
126+
`${formatMapKey(gradient.slug)}: ${gradientVar}`
127+
);
128+
}
129+
130+
if (gradients.length === 0) {
131+
return result + '$settings-gradients: ();\n';
132+
}
133+
134+
return (
135+
result +
136+
`$settings-gradients: (\n\t${gradients.join(',\n\t')}\n);\n`
137+
);
138+
},
116139
'settings-custom': 'default',
117140
'settings-spacing-spacingSizes'(key, value) {
118141
let result = '';
119142

120143
for (const spacing of value) {
121-
result += `${getVariableName(
122-
'settings-spacing-' + spacing.slug
123-
)}: ${spacing.size};\n`;
144+
result += `${getVariableName('settings-spacing-' + spacing.slug)}: ${spacing.size};\n`;
124145
}
125146

126147
return result;
@@ -144,6 +165,11 @@ class WebpackThemeJsonPlugin {
144165
return `$${id.replace(/([A-Z])/g, '-$1').toLowerCase()}`;
145166
}
146167

168+
// Quote map keys so slugs like black/white are strings, not Sass color literals.
169+
function formatMapKey(slug) {
170+
return JSON.stringify(String(slug));
171+
}
172+
147173
// traverse the theme.json file and generate the scss variables
148174
function traverse(obj, parents = [], result = '') {
149175
for (const key in obj) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
@use "../00-variables/theme-json.scss" as *;
2+
3+
/*
4+
* Palette : Usefully to get the color or background color of a parent element
5+
* Usage Example:
6+
* Use these classes to store a color / gradient value in a CSS variable.
7+
* This allows any child element to reuse the parent's background color or image.
8+
*
9+
* HTML:
10+
* <div class="has-primary-background-color">
11+
* <span class="child-element">
12+
* I will use the parent's background as my text color
13+
* </span>
14+
* </div>
15+
*
16+
* CSS:
17+
* .child-element {
18+
* color: var(--background-color);
19+
* }
20+
*
21+
* Gradients expose --gradient on .has-{slug}-gradient-background.
22+
*/
23+
24+
@each $color-name, $color-value in $settings-palette {
25+
.has-#{$color-name}-color {
26+
--color: #{$color-value};
27+
}
28+
29+
.has-#{$color-name}-background-color {
30+
--background-color: #{$color-value};
31+
}
32+
}
33+
34+
@each $gradient-name, $gradient-value in $settings-gradients {
35+
.has-#{$gradient-name}-gradient-background {
36+
--gradient: #{$gradient-value};
37+
}
38+
}

0 commit comments

Comments
 (0)