Skip to content

Instantly share code, notes, and snippets.

@danecando
Created September 5, 2022 13:09
Show Gist options
  • Save danecando/33b63339cfb700899024b030909fd3fa to your computer and use it in GitHub Desktop.
Save danecando/33b63339cfb700899024b030909fd3fa 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