How to Style React Components With Design Tokens

Styling React components with JavaScript design tokens

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 we have the store book server running. And so let's go back to the button component and let's see how we can style it with style components. So we'll go back to the terminal and first install the style components for our React packets. And now let's go and consume the tokens from our library. But before doing that, we skipped something when we've built the foundation packets, which we can do now. First of all, we need to extend the TypeScript configuration with the setting, is a really simple setting to extend. Then we need to add those settings, what is our main packets, what we can find the type definitions. And then some additional build steps like the TypeScript compile. And the last one would be to add an index.ts where we re-export the JavaScript token so we can consume them natively in our code. So if we have this, we can run Yarn workspace, foundation and build. It will build everything. And then we can go to our button components and natively find and consume the tokens. So here we have the tokens available for consumption. So what can we do here? We can first create our new button component, which will be this button. And let's start styling this. So the first style we can implement and it's a really easy one would be to add a cursor of a pointer. And here what we are missing, we are missing the import. And then we can add also the definitions. And get back. We still have the same problem. So let's maybe restart the TypeScript server. Maybe this is a bug from the code editor. Yeah. And it looks like it. It works fine now. So here we have the button styled. And then this is a style component. We pass everything here. And we have a cursor of a pointer. We will open Figma to see how we should style our button component. And Figma is open. Let's go to buttons. And let's see. We have a background color. We have some puddings. We have six puddings on the vertical axis and 12 puddings on the horizontal axis. So let's implement this. Padding and we have six and 12 pixels. What else do we have? We have the background color. So let's give a background color. And I love the autocomplete from GitHub Compile Load. But let's see it ourselves. We have the tokens, then colors. And then primary. And we'd say did we have here in our button, we have the primary 500. Now we have an issue, you will see. And the thing is that we did not select the token value. Because if we go back to the tokens, we can see that we have a value, but we can have other attributes like file path, a source, things that we will not need probably. Then we can have the border radius. Let's see how rounded our components are. They use a 100 radius. So in our case border radius. And then reduce, large, and then value. But what is the value of large? We can search our tokens, foundation, source tokens in JavaScript, and see the large. Or maybe even better, we can search here, reduce. And we can see the large that is only 20 pixels. We could create a new radius, call it rounded. Or really large and paste the value of 100. For now we will skip this. But you can make it as an exercise and move forward. So I will copy and paste the code from the text version of this course. So I will not create every single style. So this is the code. We have a disabled state, our paddings. We unset everything from the native stylings. The transition, which is using the animation token we created, our color of the text, the border radius. And now we have here some properties. And those are calculated dynamically. But we don't have the color property in our button. So let's go and add this. And here the color, what would it be, the color for us. So if we search the colors, we have the primary, the neutral, the warning, danger. So for colors. And there is a clever way to do this. And this is also the power of design tokens that they will allow us for type safety. So if it's a type, if it's a key of a type of tokens that colors, we can see here that we have the four colors, primary, neutral, warning and danger. And this can be inferred here and a work appropriately. So if we now remove this button and work with the button, we implement it. So this is a style button. Here we need to spread also the color property. And we have everything. We have a warning. The void is not as high as to react. What does it mean? Yeah, that we don't return it. Perfect. And now we have a button styled with styled components and our JavaScript tokens. And let's see the result. It does not work. So what we are doing wrong. Let's see. It says that it cannot find something with the colors. Okay. The thing is that right now we are passing an undefined color because in our bottom story, we don't define the color and it is breaking. So if we want a default value, so the consumers will use it out of the box, we can define an default value for our color. And here you can see that it works as expected. The only issue we have is the font that we will fix later on. But yeah, that's how you can use the JavaScript tokens to to sell your components and to fix the font, we can import our typography in the store book in the store book files, like we are consumers of the typography and everything will work as expected. So in the next session, we will use tailwind CSS to style our components and we will fix the font issue.