How to Use and Customize Tailwind CSS in React Native

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 Building React Native Apps for Mac 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 Building React Native Apps for Mac, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course Building React Native Apps for Mac

In the previous lesson, we started customizing the UI of our application. We didn't use any custom colors or grids, we only used the integrated classes from Tailwind, but now we will exactly explore how to add our own colors and our own spacings and our own fonts to it. So the first thing that I'm going to do is on my route of the project, I'm going to create a new file and this is going to be called tailwind.config.js. So basically Tailwind is going to read this file and all of its configurations and it will generate a set of styles that we can import into our application. So basically here on the theme section, I have a extent key, which means I'm not going to replace the base colors that Tailwind offers, but I'm just going to extend them. So here I have the colors key and I'm now adding two more colors, one cyan and tangerine. So basically you can extend a lot of different properties of Tailwind. I would recommend that you go over the documentation, the official website, so you can have a better idea what's possible and what's not. There's also plugins available for Tailwind that extend its functionality, but bear in mind, we're not using pure Tailwind because we're using our React Native port, so maybe not everything is going to work out of the box. It's something that you might have to try in your own. So I actually took this from the lessons in the course, but there's one other command that you could do in order to initialize a blank template, which is NPX, Tail wind CSS in it, right? So it will just create this file for you, but since we already have it, we don 't need it. So now that we have our base configuration file, we can actually generate, or we can tell Tailwind to generate all the styling for us. So it will generate the JSON file, which we will be able to import into our application. So in order to do this, we're going to do NPX create tailwind RN. So you will see now we have a style.json file, and these are all the styles that will contain our changes as well. So how do we take care of this? How do we make it work? So I'm going to go to my source folder, and I'm going to create a tailwind.ts file. So one more time, I'm just going to take the contents of the lesson. And basically, I have a create function, which I'm going to import from tail wind RN from our port of tailwind. Then I'm going to import the JSON file that was created for me. And I'm just going to pass the styles into the create function. So this create function is going to return an object with some properties. The ones that are important are tailwind itself, which is equivalent to the function that we have been using so far to style our component and get color. This is particularly useful if you have any other component that doesn't work with tailwind, but you need to match the colors. So instead of hard coding your color, again, you can just call this function. So now that we have these two, we're just going to export them, and we can use them from our different components. So in order just to give you an example, I already have the app open. From the last lesson, I only move the elements a little bit, so it looks a little bit better. So I'm just going to go back into my component one more time. And this time, instead of importing TW directly from the tailwind package, I am just going to import it from our package. And now this is a named import. So I'm just going to change, let's say, for example, our button. So instead of saying background bloom 500, since I added the color, there you go. That's nice. You will also add the Tangerine. So basically, that's how you create your own colors for your application. You can create your own pairings, your own grid, so you can style the elements. You can create a bunch of different properties, font sizes, animations. I think animations right now are not working with tailwind with this port, but maybe in the future, you should also be able to do that every single detail of tailwind. You can just customize it at once. Great. So that kind of takes care of custom colors. Now there is one more topic about colors that we need to take care of. So let's say, for example, right now I'm using the white theme of my Mac, and let's say I switched to the dark theme. So here it works kind of nicely, mostly because the text I didn't style, I didn 't add any style into my text, and it works occasionally. But sometimes you will see these kind of things, right? Like the placeholder here disappeared. Or if I say, add my favorite books, I actually give it a color, and I said, you know, this is a text created 100. And it doesn't change automatically with my OS setting. And this is kind of not very nice, right? Because nowadays a lot of people, they use the theming options on their phones and also on their computers. So you kind of want your app to work everywhere. So we need to add some sort of dynamic theming. We could do it on application start. That does, let's say, you know, the application starts and then we query for the OS and then we see which theme is the user running at the moment, and we kind of stick to that. But a lot of the applications nowadays, you know, they try to be as smooth as possible. In this regard, React Native macOS has some way of dealing with this, but it's not properly documented. And it also doesn't play very nicely with tailwind itself. So we're going to go with a different approach and we're going to do it via a dynamic hook. So my source folder, I'm going to create a new folder. I'm going to call it leaps. And inside of it, I'm going to create a file that's called hooks. I'm just going to take one more time, the code from the lesson. And basically, there is this function that is provided by React Native itself. It's called use color scheme, which is a hook. So basically, you can put this inside of your component and it's going to return to you the current theme that the OS is running with. So we're going to take advantage of that and we're going to create our very first custom hook. Our hook is just a function that returns another function and internally uses the use color scheme function. So it's going to take two values. The first value is going to be the dark theme value and the second value is going to be the light theme value independent on which team we're using, which is going to return either of them. Great. So I'm just going to follow our structure to support our modules. And now I'm going to use it here. It was use dynamic color. So I'm just going to import it and dynamic color. Let's do something like this. So I'm going to create a dynamic color for, let's say, our button. Yeah, our button looks fine. Yeah, let's start with that. Let's say, bottom color. So my dynamic theme, I'm just going to give it a value. So let's say, when it's on the dark, I like the orange or tangent color. And on the light theme, I want our cyan color. And on my button, I'm going to replace my class. So here's what I meant. It works nice with tailwing classes, right? Because basically we need to compile or we need to provide everything as strings. We can just use string template, which is just concatenating strings, just a JavaScript feature. And now if we save this, let's see if we get any error. Yeah, right. So because react native hot swaps, the code, your code at runtime, sometimes you might get into these small errors with hooks, right? Hooks are also some sort of global state, right? So what happens if you render your component the first time and then on the next render cycle, there is one less hook or one more hook, then react. Cannot know what happened to that state, to that piece of state. And that kind of produced this error. So this is not not a major thing. You can just dismiss it. And if we reload the wrap, then we should be good to go. Yes. Right. So now if I switch one second, if I switch my OS, then I switch my OS. The theme. Okay. Probably small, small debug. That's how you customize. That's the mechanism that we're going to use to customize colors within our app . yeah