Dynamic Client-Side color themes using x-theme-x
and --var()
#12640
mauricekindermann
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I found mysellf needing two complex color related features that weren't obvious or out-of-the-box features of tailwind.
Each of my categories has a different color spectrum, eg one is Green, another is Red. So in all my compontents I needed a way to access these themes without having to duplicate everything simply for colors.
I also have printable templates that a user can theme (eg select purple, orange, green), and I wanted this to work client-side, and somehow make sure every tailwind class is cpatured by the compliler for every color.
So I came up with this:
First, define your colors in your tailwind config, and then add a "theme" color using vars. (I moved my colors to a JSON file so I can visualize them on the frontend.)
Then add an
@import "themes-color-variables.scss";
at the top of app.css:This creates a relatively small footprint of CSS, but now, like magic, you can do this anywhere:
No safelist, no CSS abstraction, no duplicate code (except the color themes), no javascript, no data-targets, no inline-styles, just simple tailwind that works with your IDE.
The best part is if you want to make color selection dynamic on the frontend, you only need to change the
theme-x
class and this will change all colors on the fly for that element and it's children usingx-theme-x
.I assume this is kind of how light/dark mode works, so maybe this isn't news for some, but I've never built a dark/light mode system myself.
Sharing in case anyone runs into a similar scenario, or has a better way to handle this kind of color selection in Tailwind.
Beta Was this translation helpful? Give feedback.
All reactions