Skip to content

Commit b6bc393

Browse files
authored
Merge pull request #513 from BeAPI/feat/theme-json-gradient
Dégradés `theme.json` → SCSS et utilitaires `--gradient`
2 parents c99fe9c + 2ccc4db commit b6bc393

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,27 @@ class WebpackThemeJsonPlugin {
9191
for (const color of value) {
9292
const colorVar = getVariableName('settings-color-' + color.slug)
9393
result += `${colorVar}: ${color.color};\n`
94-
palette.push(`${color.slug}: ${colorVar}`)
94+
palette.push(`${formatMapKey(color.slug)}: ${colorVar}`)
9595
}
9696

9797
return result + `$settings-palette: (\n\t${palette.join(',\n\t')}\n);\n`
9898
},
99+
'settings-color-gradients'(key, value) {
100+
let result = ''
101+
const gradients = []
102+
103+
for (const gradient of value) {
104+
const gradientVar = getVariableName('settings-gradient-' + gradient.slug)
105+
result += `${gradientVar}: ${gradient.gradient};\n`
106+
gradients.push(`${formatMapKey(gradient.slug)}: ${gradientVar}`)
107+
}
108+
109+
if (gradients.length === 0) {
110+
return result + '$settings-gradients: ();\n'
111+
}
112+
113+
return result + `$settings-gradients: (\n\t${gradients.join(',\n\t')}\n);\n`
114+
},
99115
'settings-custom': 'default',
100116
'settings-spacing-spacingSizes'(key, value) {
101117
let result = ''
@@ -125,6 +141,11 @@ class WebpackThemeJsonPlugin {
125141
return `$${id.replace(/([A-Z])/g, '-$1').toLowerCase()}`
126142
}
127143

144+
// Quote map keys so slugs like black/white are strings, not Sass color literals.
145+
function formatMapKey(slug) {
146+
return JSON.stringify(String(slug))
147+
}
148+
128149
// traverse the theme.json file and generate the scss variables
129150
function traverse(obj, parents = [], result = '') {
130151
for (const key in obj) {

‎src/scss/04-utilities/_palette.scss‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
/*
44
* Palette : Usefully to get the color or background color of a parent element
55
* Usage Example:
6-
* Use these classes to store a color value in a CSS variable.
7-
* This allows any child element to reuse the parent's background color.
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.
88
*
99
* HTML:
1010
* <div class="has-primary-background-color">
@@ -17,6 +17,8 @@
1717
* .child-element {
1818
* color: var(--background-color);
1919
* }
20+
*
21+
* Gradients expose --gradient on .has-{slug}-gradient-background.
2022
*/
2123

2224
@each $color-name, $color-value in $settings-palette {
@@ -28,3 +30,9 @@
2830
--background-color: #{$color-value};
2931
}
3032
}
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)