Last active
February 19, 2025 15:37
-
-
Save aleph-naught2tog/8c2843ed1f62e7db3c483a35cacf0aed to your computer and use it in GitHub Desktop.
Set color variables in CSS from Tailwind
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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