How to Use Tailwind CSS With JavaScript Design Tokens

Styling React components with Tailwind CSS preset

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the Build a Complete Company Design System course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to Build a Complete Company Design System, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course Build a Complete Company Design System

So in the previous lesson, we learned about how to use the design tokens to create our own telewind CSS. So we go to foundation and then source. This is our telewind preset that we used to create our own telewind preset that consumers will consume and start styling their components. So in order to use it, let's go to our React packet and install tailwind. So we are targeting the React packets and we add a Z development dependency tailwind CSS. And we'll go back to the code editor. Packages react. And then let's create a tailwind.config.js file. And import this code snippet. So the first line for anyone that is not familiar, this is about allowing typ ings in our telewind configuration. Here we are consuming the tailwind preset file that we created. The content we are targeting, everything that is HTML, JavaScript, JSX, TSX, and TS. And then we are not using any plugins. And basically it says here that it cannot resolve the preset config. So let's find out why. We'll go here and we'll see that it should find it, which is weird. Maybe we have, yeah, we had an aiming issue and hopefully now it works as expected. So we close this one and again, packages react. And inside the source we can create an input.css. And let's paste the following lines. Here we import everything that tailwind recommends us to import. And in the first line we import the fonts that we will use. Of course, this will be included in our typography CSS if we continue this. And then let's go to our packets.json inside the react packets and add some more scripts. So we will add the following scripts. So in every build step we will first build the CSS and then combine our Type Script components to create native JavaScript. So basically that's it. We can build our react library right now. Perfect. And now our components are built. Everything is compiled. And if we go through the library we can see also our output CSS. And this is the CSS.tailwind generates with everything that we are using. So we are ready to create a new button component. And that will be the button that is using the tailwind. Perfect. And let's paste this code. And you can see here that we have autocomplete if we use the extension that I'm going to show you right now. It's called tailwind. In tail sense. So this one is reading our configuration and it can recommend everything. So for example here we have the background of primary 500. And we can use everything we need. For example the rounded. For sure we have the prefix that we defined and then for the rounded we have the small and large which is inherited from the tokens and generated automatically. So that's how easy it is to work with tailwind. And let's see an example in our storybook so we can have a visual representation of it. And again we made a mistake and we generated some type script file files in our stories. We can skip this. Button tailwind.stories.tsx and copy the button stories inside it. This is our tailwind button and we should import the button tailwind. And let's hope it will work. So here we have the tailwind button and it does not. So let's find out really quick what is happening. Here we have the button and here we have the story of the button so everything should work. But I think we are missing something and what we miss is that we are consuming the button and we are generating an output CSS but we are not consuming the output CSS. So we should consume this output CSS in our story or right now in the story because this is our consumer but if we were a consumer we should import the CSS. So here we are importing directly from the React library the output CSS. This is that we will contain all of our component classes. So let's try and load this page. It says that we cannot find it. So let's go back. We have React library and output CSS. So maybe this is an error with storebook and we need to refresh. So let's try this out. And we are going somewhere for sure it was an issue with storebook but now we can see that we don't have styles. So we are using this class but this class is not where we are to found. So we suspect that this is maybe an issue with how we generate the classes. So let's go back to packages and see the package.json. And let's actually watch our CSS for changes. Yarn. Yeah. And it's done. So it's working and we can make some changes in our button component. For example we can have a margin of four or eight pixels and then four pixels top and bottom. And then the text should be of a neutral and then white color. And this is how you are writing tailwind alongside with your component library. So I think that was a really nice lesson of how you can consume JavaScript tokens, create the preset and then distribute the preset to the team so they can use tailwind as we are using right now to write their own components and be sure that they will use the official colors, the official radises, the official spacing and everything that you provide for the design systems. So we have the JavaScript implementation and the tailwind implementation and yeah, feel free to be creative and use any implementation you need for your own use case.