Themes and darkMode ๐
This boilerplate handle dark themes and theme overrides
What is the default configuration โ
By default, the boilerplate comes with a default
theme and a default dark
theme.
The default theme is build around the different files at the root of src/Theme
, by default:
- Common.js
- Fonts.js
- Gutters.js
- Images.js
- Layout.js
- Variables.js
the default dark
theme is located in src/Theme/themes/default_dark
.
The Boilerplate Theme system is based on layers overriding.
In other words, the default
theme is the "base theme" of the application.
On the top of it, if the darkMode
is on, the base theme is overrided by style classes or variables of the default dark
theme.
Moreover, if we add a new theme into src/Theme/themes
, for example : custom
.
The theme system will override the default
theme classes by the ones of custom
theme.
If the dark mode is activated, the theme system will try to get a custom_dark
directory if exists, and override the theme too.
So, the merge of style and variables classes look like this :
The name of the current theme and if the dark mode is on are stored in redux and persisted in the device. At a given point of the time the state can look like this :
The darkMode
values can be :
- null (by default) : the darkMode is automatically set depending of the scheme of the device
- true : force the theme to be dark
- false : force the theme to be light
There are two Actions availabled to set a new theme :
DefaultTheme
allow to set the theme from parameters only if thestate.theme
is null (used in the initialisation of the app to set the default theme if this is the first time the app is openned or if the data were cleared)ChangeTheme
allow to set a given theme (see example bellow)
Create a new theme ๐งฉ
Basic example
To create a new theme, create a new directory into src/Theme/themes
with the name of your theme. For example custom
.
Add files with the same names of the default theme files.
For example, if custom
theme has to override the primary color, simply create a new file called Variables
into src/Theme/themes/custom
with the following :
๐จ After adding a new override file, don't forget to export it into the root file of the custom
directory with the following :
๐จ Each time a new theme is create, import it into the root of themes directory with the name of the theme :
That's it ! now you can use the ChangeTheme
action to set the theme in a container for example:
Extended example with dark theme
After adding a new theme, to handle the dark theme of it, the same way the new theme was created, the dark will be. ๐
So, the first step is to create the src/Theme/themes/custom_dark
directory. This theme will override the default and the custom theme by :
- adding a new color
- override the React Navigation primary color
- apply the new color by adding a border on the
backgroundPrimary
class.
Let's create the following files :
And,
๐จ After adding a new override file, don't forget to export it into the root file of the custom_dark
directory with the following :
๐จ Each time a new theme is create, import it into the root of themes directory with the name of the theme :
You can add extra buttons to the ExampleContainer
below to test it :
That's it ! you can now enjoy multi theming and dark mode ! ๐