You don’t have to follow a specific format. Just make an object and add any keys/values you like.
or something more advanced with nested objects / functions:
Create breakpoints
There are no predefined breakpoints. You can name them anything. Just make an object with string keys and number values.
Wrap your app with UnistylesTheme to inject theme
Access createStyleSheet and useStyles with a factory
Basic usage
Requirements
After the initial setup, you only need to focus on two functions responsible for your styles:
createStyleSheet which replaces StyleSheet.create
useStyles which parses your styles and ensures TypeScript compatibility with media queries and breakpoints
createStyleSheet
createStyleSheet is interchangeable with StyleSheet.create. You can use objects, and it will function identically to its React Native counterpart.
The difference is that you can now use breakpoints and media queries:
createStyleSheet also accepts a function, to which the library will inject your theme:
Importantly, you’ll receive the same TypeScript hints as with StyleSheet.create!
useStyles hook
useStyle ties everything together and handles the heavy lifting. Without useStyles, you can’t utilize features like:
breakpoints
media queries
themes
useStyles allows you to skip the stylesheet if you only want to access the theme:
For more advanced usage, pass your stylesheet generated with createStyleSheet:
You can also access the current breakpoint to manipulate the JSX or dynamically select your styles:
Show or hide components based on breakpoint (with your own implementation of Visible/Hidden components):
Access styles based on breakpoint (may be helpful for variants):
Examples
Breakpoints
Any style can change based on breakpoints. To do this, change a value to an object:
You can even use it with nested objects like transform or shadowOffset:
Library will choose the correct value (based on screen width) in the runtime.
Dynamic functions
Every style can be transformed to dynamic function to take additional parameters from JSX:
If you use a dynamic function, library will wrap it in a Proxy to make sure the correct values of breakpoints will be used:
Dynamic themes
You can incorporate as many themes as you desire in your application. While there’s flexibility in how you structure your theme, it’s essential to maintain consistency with the TypeScript type:
To promote reusability and maintainability, it’s a good practice to share as many values between themes as possible:
With the themes set up, modify your createUnistyles to consume your AppTheme type:
The final step is to switch your theme based on certain states, persisted values, databases, etc.:
Media Queries
For more advanced usage and pixel perfect designs you can also use a custom media queries. Library supports 4 types of media queries (w-width, h-height):
Media queries can be mixed with breakpoints, but have a bigger priority:
Variants
react-native-unistyles isn’t a UI/component library, so you’re in charge of designing variants. With no restrictions and using your creativity, you can easily create variants for your components.
Let’s examine variants for the Text component. Imagine you want to create several variants for your Typography components:
Heading
Regular
Thin
To achieve this, add variants to your theme:
Next, create a base component:
Remember, that if you want to spread styles like so you need to export your theme “as const” for TypeScript.
This is how React Native types works, and you can see the same behavior with StyleSheet.create.