generated from commonknowledge/groundwork-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtailwind-theme-colours-plugin.js
42 lines (39 loc) · 1.25 KB
/
tailwind-theme-colours-plugin.js
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
const plugin = require("tailwindcss/plugin");
function cssVarName(level) {
return `--theme-${level}`;
}
/**
* For each colour group specified in `theme`, generate a `.theme-{colourName}` class
* which can be used in conjunction with `.bg-theme-300` and similar.
*
* Requires a `theme` colour group to be defined in the theme.
* The keys of that group will be picked for the other groups.
*/
const themeColor = plugin(function ({
addUtilities,
matchUtilities,
theme,
e,
}) {
const themeColorLevels = Object.keys(theme("colors.theme"));
const colourGroups = Object.entries(theme("colors")).filter(
([group, value]) => typeof value !== "string",
);
const utilities = colourGroups.map(([group, colours]) => {
const themeClassName = `.${e(`theme-${group}`)}`;
const themeCSSVariables = Object.entries(colours).reduce(
(dict, [level, value]) => {
if (themeColorLevels.includes(level.toString())) {
dict[`--theme-${level}`] = value;
}
return dict;
},
{},
);
return {
[themeClassName]: themeCSSVariables,
};
});
addUtilities(utilities);
});
module.exports = themeColor;