Theming in Unistyles differs from other libraries as it doesn’t impose any specific syntax.
Any JavaScript object can be a Unistyles theme.
There is also no limit to the number of themes. You can even register dozens of them, for example, if your app sells some premium ones.
Theming is optional. If you dont’t register themes with UnistylesRegistry
the library will use an empty object by default.
Create a theme
You can organize your themes however you want:
If you use TypeScript you need to override the library’s type:
Finally, to register the theme, you need to call UnistylesRegistry:
Where name is the unique name of your theme.
Select theme
If you’ve registered more than one theme, Unistyles won’t know which one is the initial one.
At this point, you have 3 options:
If you know the initial theme upfront, select it with addConfig from UnistylesRegistry
If you need to resolve the user-selected theme during runtime, call useInitialTheme before
using your first useStyles hook:
Use adaptive themes, which are described below
Get the current theme
To get the current theme, you can either obtain it from the useStyles hook:
or access it in the createStyleSheet function:
Get the current theme name
To get the current theme name, import UnistylesRuntime:
Adaptive themes
Adaptive themes allow Unistyles to automatically manage the selection of your themes based on device color scheme settings.
To enable this, you need to meet two conditions:
register two themes with reserved names light and dark:
Explicitly enable adaptiveThemes:
In order to support adaptiveThemes with SSR, please follow this guide.
Toggle adaptive themes during runtime
To toggle adaptive theme support at any point, use UnistylesRuntime:
Check if adaptive themes are enabled
To check if adaptive themes are enabled, again use UnistylesRuntime:
Get device color scheme
Check your device color preference with UnistylesRuntime:
Available options are: dark, light or unspecified for devices that don’t support color schemes.
If your app’s theme is not changing based on device settings, please refer to the FAQ
Change theme
With Unistyles 2.0, we no longer use a React Context. However, this doesn’t mean you can’t change the theme.
In fact, it’s even easier as you don’t need to re-render the entire app.
To change the theme at any time, simply call setTheme function:
Update theme during runtime
All platforms
2.2.0
Unistyles allows you to update your theme during runtime. This is useful if you want to show the user interface with default colors and later alter theme based on user preferences.
If you update the currently selected theme, it will be automatically applied, and Unistyles will notify all stylesheets about the change. Otherwise, theme will be updated silently.
To update the theme during runtime, call updateTheme function, and return new theme object:
Update statusBar color during runtime
Android
2.6.0 - 2.8.0
Sometimes your app needs to dynamically change the status bar color, for example, when entering image preview or on some onboarding screens. You can easily update the color with UnistylesRuntime and for example the useEffect hook:
Update navigationBar color during runtime
Android
2.6.0 - 2.8.0
Just as with the statusBar, you can do the same with the navigationBar and set a specified color.
Update rootView background color during runtime
iOS
Android
Web
2.8.0
You can also change dynamically the root view background color with UnistylesRuntime:
Changing rootView background color is useful when your app supports different orientations and you want to match the background color with your theme while transitioning.
System bars and rootView colors
Unistyles supports all 6- and 8-character hex colors with a # prefix, as well as some built-in colors:
translucent red, blue, green, black, white, gray, cyan, magenta, yellow, light gray, dark gray, grey, light grey, dark grey, aqua, fuchsia, lime, maroon, navy, olive, purple, silver, and teal.
2.8.0
Since version 2.8.0, Unistyles supports optional alpha value that can easily convert hex values to transparency.
In case of any issue in parsing Unistyles will silently fail without a crash.