In this tutorial, you'll learn how to create a Light and Dark mode for your Bubble app. With a few conditional statements in your Bubble application, you can make it more sophisticated and user-friendly.
To enable dark mode in your Bubble app, you will need the following:
- Toggle button to switch between the two themes
- Custom state to save the selected theme choice
- Conditional statements on the visual elements
In the end, we will learn how to store the theme preference in the database and apply the styling changes directly to the element styles. It is a faster way to do things compared to using custom states.
Toggle button to allow switching
You can install the Ionic elements plugin if you don't have it in your Bubble app. This plugin will let you place a toggle button in your app.
Once you have installed it, you can place the toggle button anywhere. The profile section is a good place for users to choose their preferences.
I'll leave the Preset status of this toggle unchecked, but you can check it if you like.
Saving the user's theme choice
We'll use a custom state to keep track of the theme. We only need a simple Yes/No type custom state.
You can create a custom text or number state, but you'll need more complex conditional statements.
We'll use the Toggle button to change the custom state. Having a custom state of Yes/No will help us because we need only one workflow to update it.
The value might be confusing at first, but this is the correct way to toggle a Yes/no custom state. The custom state value will change to Yes if the current value is No, and vice versa.
You can also store the value in the database at this point. I'll go into more detail on this below.
Changing the colour of visual elements
The last step is to change the colour of the visual elements.
I've created a text, a group and a button to help you understand. The conditions remain the same to use this on all sorts of elements.
For example, we can change the font colour.
The conditional statement will need to check if the custom state value is Yes. If Yes, the font will be white. Otherwise, the text will be the same colour as the Style attached.
We can also change the Group's background colour.
Finally, we want to change the background colour of the entire page. We'll use the same conditional statement to change the background colour.
Can we store the user's theme selection in the database?
You can create a Yes/No field in the User table to store user preferences and modify the conditional statement. Instead of the custom state, you'll check this field to update the element styling.
Just make sure that if you're saving the choice, the Preset status of the Toggle will be Dynamic to pull the value from the database.
Can we apply the conditional statements directly to the element styles?
- Go to the Styles section.
- Select the element.
- Click on Conditional.
- Add the condition and the changes you'd like.
Adding conditional statements to the Style will save you time and keep your app organised.