Feb 17, 2024
Learn how to use design tokens to create Tailwind themes and how to use them for my website.
Tailwind Themes with Design Tokens
For my website, I want to use Tailwind CSS and shadcn/ui to create and style my compoonents. I want to use design tokens to create a theme for my website. I will use the design tokens to create multiple themes for my website.
Design Tokens
Design tokens are a way to abstract the design of a website into a set of variables. These variables can be used as fundimental building blocks for creating a design system.
Design tokens are the visual design atoms of the design system — specifically, they are named entities that store visual design attributes. We use them in place of hard-coded values (such as hex values for color or pixel values for spacing) in order to maintain a scalable and consistent visual system for UI development.
While I could use something like Token Dictionary to create my design tokens, I will instead just use the css variables directly in my Tailwind CSS themes.
CSS Variables
From shadcn/ui, there are a set of css variables that are used as a basis for the functional design tokens. To explan how to use these variables, I will create a set of core design tokens for things like colors, spacing, and typography. Then reference these in the shadcn/ui tokens.
Creating this abstraction allows me to create a theme for my website that can be easily changed and updated. I can also create multiple themes for my website that can be easily switched between.
In tailwind.config.ts
, I can specify the funcational tokens that each theme
can use.
Multi-Themes
Normally, for dark and light themes, I would use tailwind to create a dark class that would change the colors of the website. However, I want to create a set of themes that can be easily switched between. To do this I'm using next-themes to manage the themes and help persist the theme between visits.
Next-theme uses a ThemeProvider
to manage the theme state. I can use this to
create a set of themes that can be easily switched between.
This works for light and dark but what about more themes? I can use the
ThemeProvider
to create a set of themes that can be easily switched between.
Now I can create a set of themes using css classes on the html
element. These
will be in their own folder and imported into the global.css
file.
Okay, this changes the colors, but now tailwind is not using the correct colors
for light and dark themes. Thankfully, I can use the darkMode
property in the
tailwind config to change the color mode based on a class selector.
Now I can create a set of themes that can be easily switched between. And for
any themes that are dark, I just need to prefix the class with dark-
and it
will automatically switch to the dark mode.
Conclusion
Using design tokens and tailwind themes, I can create a set of themes that can be easily switched between. This allows me to create a set of themes for my website that can be easily switched between. This works for both light and dark themes and can be easily extended to create more themes.
Give it a try now, the theme toggle is at the top right of the page.