Last active
September 20, 2021 20:21
-
-
Save djm/bcfdfc1117fbe61931041dcfb65556ee to your computer and use it in GitHub Desktop.
Underline Color Utilities Plugin for Tailwind (v1.2.0)
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
const plugin = require('tailwindcss/plugin') | |
/* | |
Creates colored underline utility classes. | |
Explanation: https://css-tricks.com/almanac/properties/t/text-decoration-color/ | |
Browser Support: https://caniuse.com/#search=text-decoration-color | |
e.g | |
.underline-black => text-decoration: underline #000 !important; | |
.underline-green-400 => text-decoration: underline #68d391 !important; | |
*/ | |
const underlineColorPlugin = plugin(function ({ addUtilities, e, theme, variants }) { | |
const colors = theme('colors', {}) | |
const underlineColorVariants = variants("underlineColor", []); | |
const utilities = Object.entries(colors).map( | |
([color, colorValue], i) => { | |
if (typeof colorValue === 'object') { | |
return Object.entries(colorValue).map( | |
([colorVariant, value]) => { | |
return { | |
[`.underline-${e(color)}-${e(colorVariant)}`]: { | |
textDecoration: `underline ${value}` | |
} | |
}; | |
} | |
) | |
} else { | |
return { | |
[`.underline-${e(color)}`]: { | |
textDecoration: `underline ${colorValue}` | |
} | |
}; | |
} | |
} | |
); | |
addUtilities(utilities, underlineColorVariants); | |
}) |
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
// Usage in tailwind.config.js | |
// 1) Plonk the above plugin code somewhere importable (or at the top of your Tailwind config). | |
// 2) Define in Tailwind config's `plugins` array. | |
// 2) Specify the `underlineColor` variants array (optional, no variants created by default) | |
// The example below will generate .hover:underline-teal-400 etc. | |
module.exports = { | |
... | |
variants: { | |
... | |
underlineColor: ["hover"], | |
}, | |
plugins: [ | |
... | |
underlineColorPlugin, | |
] | |
} |
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
/* With PostCSS handling vendor prefixes */ | |
.underline-black { | |
-webkit-text-decoration: underline #000 !important; | |
text-decoration: underline #000 !important; | |
} | |
.underline-green-100 { | |
-webkit-text-decoration: underline #f0fff4 !important; | |
text-decoration: underline #f0fff4 !important; | |
} | |
.hover:underline-green-200 { | |
-webkit-text-decoration: underline #c6f6d5 !important; | |
text-decoration: underline #c6f6d5 !important; | |
} | |
.etc .etc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment