Handling dynamic opacity in token aliases/references and Tailwind CSS #1026
kaelig
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
This post aims to guide you through the steps to extend your design tokens to handle RGB colors and dynamic opacity seamlessly.
Problem Statement
class="text-red/30"
).Assumptions
Step 1: Define Your Color Tokens in HEX
Here's an example of what your color tokens might look like:
We need to convert HEX colors to RGB. To do this, we'll go through the entire color palette and populate RGB equivalents next to HEX values using
tinycolor2
and a custom JavaScript helper.First, install the
tinycolor2
package:Then, before
module.exports = { color }
, add:Step 2: Using the tokens with Tailwind
We want to be able to write code like this:
Update your project's
tailwind.config.js
to refer to the RGB values:Then, add the following transform to your Style Dictionary config:
Incorporate this new transform into your Style Dictionary config like so:
And import the resulting file in your CSS.
Step 4: Handling Opacity in Token References
You can now refer to these RGB color tokens in other tokens and add opacity to them. Here's an example of how you can define a shadow effect token that uses the RGB color and also includes opacity:
Summary
By following these steps, you can make your Style Dictionary tokens more flexible, allowing you to use Tailwind CSS's dynamic opacity features effectively. Now you're ready to leverage the full power of design tokens along with dynamic opacity in your Tailwind projects. Happy coding! 🎉
Beta Was this translation helpful? Give feedback.
All reactions