Replies: 1 comment 1 reply
-
If you search around, you'll find some plugins that do this sort of thing. There are a variety of approaches. Here is a recent one that I think shows some promise: https://github.com/RyanClementsHax/tailwindcss-themer The CSS-variable approach has the great virtue of minimizing the work required to incorporate new themes: instead of combing through your components/templates for all the theming conditionals you've used and augmenting them with new logic-branches and utility-classes, you can just define the variable-values for the new theme. It comes at a cost, though:
Depending on the size of your project, the explicit-utility alternative can be perfectly viable and arguably preferable, especially if you're consistent about how you express the branching logic (so that you can easily do a project-wide search when you need to add a new theme). The explicit-utility method can also be improved with custom variants, allowing for syntax like And of course, the explicit-utility approach isn't mutually exclusive with the CSS-variable approach. Maybe there's a sweet spot that can combine them both, where you use abstract CSS variables for a few key properties that are likely to be consistent across components and themes (like base background-colors and text-colors), and then fill in the gaps as needed with explicit classes? Not sure if there's a good one-size-fits-all solution here. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Imagine a website where every page can be custom with different designs. Particularly, inputs, buttons, cards etc. They cannot always be componentized as we cannot set a generic padding/alignment or even children, so the html needs to be written everytime.
I'm trying to devise a theme system to support passing configs for
colors
,borderRadius
etc that would play well with tailwind.My go-to solution is CSS variables as they are very powerful and would support anything required
The general idea is to pass to tailwind a set of values that are linked to CSS variables, and to override them with a
style="--my-var: blue"
property on a parent html element.I've come up with a proof of concept, and I would love some opinions on whether this(or a better implementation of this) would scale, and what caveats I should be concerned with:
https://play.tailwindcss.com/M1JFeRJGWW?file=config
I feel like this can be optimized perhaps by using a plugin, and auto-generating a lot of these classes. In this sense, from the following config:
would generate the following CSS:
Nice to have:
Support for
--tw-text-opacity
etcBeta Was this translation helpful? Give feedback.
All reactions