Skip to content

Instantly share code, notes, and snippets.

@tunaitis
Last active October 6, 2024 06:08
Show Gist options
  • Save tunaitis/a85a589ec3bb43351bbef82625cfc0ee to your computer and use it in GitHub Desktop.
Save tunaitis/a85a589ec3bb43351bbef82625cfc0ee to your computer and use it in GitHub Desktop.
This function generates custom color utility classes for any CSS property in Tailwind CSS. It extends Tailwind's color palette to properties not covered by default utilities, such as `stop-color` for SVG gradients or `caret-color` for text inputs.
function generateColorUtilities(cssProperty) {
return function({ addUtilities, theme }) {
const newUtilities = {}
Object.entries(theme('colors')).forEach(([key, value]) => {
if (typeof value === 'object') {
Object.entries(value).forEach(([shade, color]) => {
newUtilities[`.${cssProperty}-${key}-${shade}`] = {
[cssProperty]: color,
}
})
} else {
newUtilities[`.${cssProperty}-${key}`] = {
[cssProperty]: value,
}
}
})
addUtilities(newUtilities)
}
}
module.exports = {
// ... other config options
plugins: [
generateColorUtilities('stop-color'),
generateColorUtilities('caret-color'),
// ... other plugins
],
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment