Skip to content

Commit c5750e4

Browse files
committed
feat(Theme): add support for gradient settings in theme JSON and SCSS
Quote palette and gradient slugs in generated SCSS so names like black and white are strings, not color literals. Fixes sass-loader warnings in palette utility classes.
1 parent ce60c30 commit c5750e4

1 file changed

Lines changed: 31 additions & 5 deletions

File tree

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

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class WebpackThemeJsonPlugin {
4040
/**
4141
* apply
4242
*/
43-
apply() {}
43+
apply() { }
4444

4545
/**
4646
* Generate theme json file
@@ -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) {

0 commit comments

Comments
 (0)