How to Add Tailwind CSS to a React Native Mac App

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

So far, we have concerned ourselves with the logical parts of our application, but now the time has come for us to take a look into the UI and how do we style it. So there are many ways to style a React Native application. You could pick a framework library that has already a bunch of components for you, for example, React Native Paper, you could use CSS in JS library, like Style Components, or you could do your own styles. But that approach is even more time consuming because you have to declare your own scales and you have to do your own math to make sure that everything fits within different screens. So I don't like any of them, so I would like to introduce you to Tailwind CSS. Tailwind is a utility framework. So basically it doesn't have any pre-made component, but it does have a lot of different utility classes, which you can just compose to declare the style of your UI elements. So in order to use Tailwind within React Native, we need to use a third-party package. The problem is Tailwind is designed for web and it works with CSS. And even though React Native has CSS-like syntax, it doesn't actually support CSS. Everything is done through a custom engine, so it's only on the surface that it looks like you're using CSS. So in order to do this, we're going to add two packages to our dependencies. One is Tailwind CSS itself, and the other one is Tailwind RN, which is a community package that allows us to use Tailwind classes within React Native. So once I have the classes installed, I'm just going to import TW from Tailwind RN. So TW is going to be the function that is going to take a string, and this string is going to be all the class names from Tailwind. So I'm just going to go to a view, in this case the root view of our books container. I'm going to add the style prop. And inside of this, I'm going to call the TW function. So I need to pass a string that's going to contain the class names. In this case, p3 means padding 3. And if I go into our application, you can see now it has some padding. So let me just now put the app in here so we can better see what we do. So let's say I changed this to p4 or something like that, you can see it's going to apply even more padding. So what does 3 actually means? The thing is Tailwind is based on REM unit, which is scale with the font of your application. So if you're using this on a bigger device, which requires a smaller font, then everything is going to be in a ratio to the font size, which makes it a lot nicer to make sure that your UI fits on all the devices. Unfortunately, there is no easy way to learn this. You just kind of have to learn the name of the classes. So some people are not going to like this. That's fine. You can do styling however you want. But the nice thing about Tailwind is that it actually allows for very fast styling. It allows you to prototype your user interface is quite fast. And I personally don't mind when the style strings get a little bit longer. Of course, they take a little bit time to understand. But I just like having my classes next to my components or my CSS, the styling next to the component, right? Because I don't have to go chasing around all over my application for them. So I'm just going to go on and style our component a little bit more. So on this one, I'm going to once again apply a little bit more padding. Right? That looks nicer. All of her books have now a little bit more space. On my title, I'm just going to make the text apply a little different font size . For my text input, we can do something like give it a white background. That's fine. We can give it some routing corners and we can give it a little bit of padding so it gets easier. I'm also going to take this opportunity and pull a placeholder. That's better. And let me just make this a little bit bigger so we can see it. And for our button, this also needs a little bit of styling. What I'm going to do is I'm going to give it a background. I'm going to change the text. I'm going to justify the text even though that might not be necessary and a little bit of padding. Let me see what has gone wrong here. The button component itself doesn't take any style props. So we're going to change this to an attachable opacity. We're going to get rid of this import. And here I'm going to change this to a touchable opacity. I need to close this and the title, I'm just going to move it. I'm going to put the text component inside of it. I'm just going to say add. And I'm missing this. All right. I have to add one more style property to it. Let's say it's a text white. And this probably is right up center. All right. That looks better. Again, we're going to round it and we're going to add it a little bit marching to the top. So you can just see how fast I can use a lot of these classes. I don't need to worry about if they scale with my device, I don't need to worry about a lot of things, right? a lot of things are just taking care for me. Thank you.