Skip to content

Commit

Permalink
Add Tailwind plugin for CSS colour variables
Browse files Browse the repository at this point in the history
  • Loading branch information
tomodwyer committed Sep 6, 2024
1 parent c7b2288 commit 0d516a1
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,23 @@ export default {
plugins: [
tailwindTypography,
tailwindForms,
({ addBase, theme }) => {
function extractColorVars(colorObj, colorGroup = "") {
return Object.keys(colorObj).reduce((vars, colorKey) => {
const value = colorObj[colorKey];

const newVars =
typeof value === "string"
? { [`--color${colorGroup}-${colorKey}`]: value }
: extractColorVars(value, `-${colorKey}`);

return { ...vars, ...newVars };
}, {});
}

addBase({
":root": extractColorVars(theme("colors")),
});
},
],
};

0 comments on commit 0d516a1

Please sign in to comment.