How to Set Global Styles in React Native

Global styles help lay the design structure the app should follow. We'll extend our typography from the previous lesson to define styles, mainly for atoms, molecules and organisms.

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 The newline Guide to React Native for JavaScript Developers using TypeScript 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 The newline Guide to React Native for JavaScript Developers using TypeScript, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course The newline Guide to React Native for JavaScript Developers using TypeScript

Now that we have covered the atomic design pattern and the typography of the application, styles is the last piece before we start building the components as in the atoms, molecules, and the organisms. So styles will be based a lot on the typography, all the variables, the font size, the colors, everything that we use. We should have already defined in the typography. As we'll cover what we have seen earlier, it will cover styles for our default button, our secondary buttons, then how the input boxes are supposed to look. Additionally, it will also cover the paddings and the margins that we are supposed to add to a particular page or a component if a component has some margins inside and the space between the components. So all of this is going to be a part of styles. Now if you go back to the application and look into the styles folder, we have a global.tsx. And this is where we're going to define all the different styles, like we have styles for layout, the click to actions, fonts, the text boxes, the title subtitle in the paragraph and the text input. We'll start with these. And what we'll try to do is we'll use a lot of typography in this, like we are doing it here, but these values, ideally they should come from the typography. So we go and define those if they are not available or we'll use it. One thing what I've done is I've extended the typography a little to incorporate for most of these use cases. But as I said, this is supposed to be a base setup and we can build on top of it. So let's do that. And we move ahead. Let's define all of these. Let's get started with that. We have a layout and it's mostly correct. The background correct is set to default. That is white and the padding, the padding needs to come from typography. So let's do that typography dot. So I've defined this spacing dot page. So if you go to this, it says that at the page level, you need to have a spacing of 16. And for shadows, what I've done is I've declared a global shadow. So wherever we use shadows in the application for buttons or for icons or something else, they all need to be consistent. So we'll just extend it from that type of ref dot shadow. So as you see the global.tss, we'll start using the typography a lot more. So let's look at next CTA primary and hide again. This is supposed to come from typography dot. Parton dot primary dot hide. So that is the whole idea that we define everything in the typography and bare minimum supposed to come into the styles. Now in the application, these are all the global styles. I'll go ahead and change the rest of it. There's also styles along with the components, like this is a home component and there's a style file here of ID need. This file should be as many of possible and in a perfect scenario, there will be absolutely no styles and it is achievable. So that requires a cohesion between the design and development. There needs to be a few iteration before we can achieve this kind of clarity, but it is doable and we are going to do that in this particular application. One more thing. If you do have to put styles for some reason, I find them putting along with the component easier. Now in web applications generally, we put all the styles together and then bundle them, but that's not the case with react native. The styles go hand in hand with the component that we are building and generally, whenever we're trying to make any changes to the application, the styles are required at that time. So given that I kept the styles along with the components. So let's see what we have here. There's the button primary and button secondary. Let me just go ahead and add one to the home screen and be able to see that or maybe we just name this one as button secondary and once we save, we'll see the changes. If their content is not showing up in the middle and we can fix that, we'll just go to secondary and we'll say justify content center and that should do it. Yeah. Let's also define a few of these how the titles and the subrattled and the paragraphs that we had in the designs, if you recall. So we had these titles, subtitles and paragraph texts. So we should define styles for that. I have already added some text and let's just see how typography and styles are going to help us achieve this. So let's just add some style to it and we'll say global dot. This is a font dot title and let's save it. So yeah, that mostly does it actually what and if we go back to the definition, we have a font size and if we look at it, yeah, that's mostly that's it for the font size. It's a little bigger. So probably we can adjust that and you can see what's happening. Right. Ideally, if this definition would have come from the design team, it would have never have been incorrect and we're still not sure whether it's matching or not. So we put in 24, but let's just go ahead with that. Next, let's try to do the see all one. So I'll name that as a subtitle. So we'll say style equals global dot, fonts dot subtitle. Let's save that and let's see how that looks. Okay. Yeah, that is okay. The font size also looks okay to me. And finally, we have the paragraph. So I'll do a body. Just save it. Okay. So let's check for body. We have color as default. That's white. That's not correct. So for body, we have color this one. Let's just change it to that for body will change it to paragraph and once we save it, we have the right color. And the font size needs to change. So we just saw how defining these typography and global styles is going to really ease the process for us. Now when we build these screens, we'll start with atom the molecules. And when we build these screens, we already have a lot of typography defined for these elements when we use them. So for example, when we start building this carousel, we would already have the title for it for this product widget. We will know what element to use for particular title and text. And probably for price, we'll have to build something else. But we can do that. We already have a custom text and we'll have something for price to. We'll keep adding to typography wherever required. But over the time, the number of new attributes that we need to add should reduce drastically. Otherwise, we need to check if you're defining multiple primary or multiple secondary colors. And similarly for typography, we already have setups for text boxes and click to actions and fonts and text and text inputs. So that should mostly cover a lot in the application. What we probably need to add for is the page layouts. It's very basic right now. that should be it. Thank you and see you in the next module.