Skip to content

Instantly share code, notes, and snippets.

@aleph-naught2tog
Last active February 19, 2025 15:37
Show Gist options
  • Save aleph-naught2tog/8c2843ed1f62e7db3c483a35cacf0aed to your computer and use it in GitHub Desktop.
Save aleph-naught2tog/8c2843ed1f62e7db3c483a35cacf0aed to your computer and use it in GitHub Desktop.
Set color variables in CSS from Tailwind
// source: https://gist.github.com/Merott/d2a19b32db07565e94f10d13d11a8574
// this exposes all our colors as CSS variables
// this goes in as the last step in your plugins
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