Customizing the UI theme

In the previous lesson we used the base version of a React Native port of Tailwind, but we did not customize it in any way. However, if you are developing your own app, you will definitely want to use your own colors, font sizes and margins. We're going to achieve this by extending the base Tailwind configuration and auto-generating the custom classes.

In the root of your project create a tailwind.config.js file - here is a template you can use:

You can also initialize an empty configuration by running npx tailwindcss init.

You can read more about setting up variables in the Tailwind documentation. For now we will only add a set of colors but the potential for customization is large - you can change scales, margins, font-sizes, etc. Now it's important to note that not all the configuration items will work, since this is a port that needs to function with the React Native styling system, so always check out the tailwind-rn repo.

With our configuration in place we can now generate the classes for tailwind-rn to consume. Run the following command in your terminal:

This will create a styles.json file containing not only the default Tailwind style classes but also our customized classes.

Loading our customized styles into React Native#

Now before we start using them, we need to load them. We'll create a convenience wrapper around tailwind-rn. Create a Tailwind.ts file in the src folder:

We start by importing the create function from tailwind-rn. This function takes the generated styles we created before and will return an object with two properties and a customized tw function that works with our extended styles. It will also return a getColor function which is useful to get our custom colors from the Tailwind configuration. We simply take these properties and export them under new names to make it easier to import into other files.

We can now replace the default import from tailwind-rn to our wrapper and use our custom classes: