Skip to content

Instantly share code, notes, and snippets.

@nilportugues
Forked from danecando/tailwind-color-vars.js
Created September 5, 2022 13:21
Show Gist options
  • Save nilportugues/9fbbd8d618d6a097f2a8c1879d26dddc to your computer and use it in GitHub Desktop.
Save nilportugues/9fbbd8d618d6a097f2a8c1879d26dddc to your computer and use it in GitHub Desktop.
A simple plugin that exposes all tailwind colors as css variables on :root
function ({ 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')),
});
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment