tailwind.config.js: parse single-tint values in the matrix

This commit is contained in:
Alex Gleason
2022-03-23 16:51:42 -05:00
parent d57051c41c
commit e0e953647e
3 changed files with 50 additions and 17 deletions

View File

@@ -8,19 +8,35 @@ function withOpacityValue(variable) {
};
}
// Parse list of shades into Tailwind function with CSS variables
const parseShades = (color, shades) => {
return shades.reduce((obj, shade) => {
obj[shade] = withOpacityValue(`--color-${color}-${shade}`);
return obj;
// Parse a single color as a CSS variable
const toColorVariable = (colorName, tint = null) => {
const suffix = tint ? `-${tint}` : '';
const variable = `--color-${colorName}${suffix}`;
return withOpacityValue(variable);
};
// Parse list of tints into Tailwind function with CSS variables
const parseTints = (colorName, tints) => {
return tints.reduce((colorObj, tint) => {
colorObj[tint] = toColorVariable(colorName, tint);
return colorObj;
}, {});
};
// Parse color matrix into Tailwind colors object
const parseColorMatrix = colors => {
return Object.keys(colors).reduce((obj, color) => {
obj[color] = parseShades(color, colors[color]);
return obj;
// Parse color matrix into Tailwind color palette
const parseColorMatrix = colorMatrix => {
return Object.entries(colorMatrix).reduce((palette, colorData) => {
const [colorName, tints] = colorData;
// Conditionally parse array or single-tint colors
if (Array.isArray(tints)) {
palette[colorName] = parseTints(colorName, tints);
} else if (tints === true) {
palette[colorName] = toColorVariable(colorName);
}
return palette;
}, {});
};