Last active
October 6, 2024 06:08
-
-
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.
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
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