How to manage Light and Dark mode

Hi everyone,

I am following up on a question from Neil during the office hours on October 3, 2023. The question was about how to properly implement a light and dark mode in WeWeb. There are multiple approaches to managing this topic.

What I consider to be a best practice is to use CSS variables. Here is how you can utilize this powerful feature in the editor:

  1. First, create two object variables: one for the light colors and another one for the dark colors. As a default value, you need to include all your colors, which we like to refer to as “tokens.” Each key in your objects needs to start with --. This is very important. Below is an example :point_down:

  1. Then, create another variable that you can name Color mode. Remember to toggle Save in local storage, so your users’ browsers remember the value.

  1. Now create a global workflow that you can name Init CSS variables

  1. Add a True/False split and bind the condition like this :point_down:

  1. Then, add Two Custom Javascript actions with this code:
const colors = variables['01232d53-328a-4882-bf10-5a2c64fe2536']; // Update this line
Object.entries(colors).forEach(([label, value])=>{
    wwLib.getFrontDocument().documentElement.style.setProperty(label, value);
})

Update the code with your variables like this:

CleanShot 2023-10-04 at 12.29.25

  1. Now you need to create a workflow that will be triggered On App Load. So the initialization appends every time the application loads.

Execute the global workflow you previously created

  1. Next, you can add a select input in your application. And add a workflow to toggle the color mode and init again the CSS variable.

  1. Now, you can bind! Select any color property and bind to a string like this "var(--side-menu)"

Here you are:

CleanShot 2023-10-04 at 12.58.45

My advice is to save your styles in classes (you can save formulas in classes) so you don’t have to type in “var(…)” every time you need to style an element.

I hope this is helpful. Please let me know in the comments.
Cheers!

9 Likes

Thank you so much Flo!! You’re the best. I might need to return to this at a later time as I wasn’t the smartest and didn’t use CSS classes that well across the application - will need to go in and unify all classes first and then implement this system

Thank you so much again! You guys are all the best at Weweb.

1 Like

Thank you so much for sharing this!

I do have one question. I have a design system I’m working with and initially wanted to use variables with the token – system you mentioned here.

I wasn’t sure if this is efficient or not though? I plan to make all spacing, border-radius, shadows etc etc as variable that I bind to a token.

I just want to make sure if I build my entire project like this, it won’t cause performance issues since it’s not technically a ‘native’ solution.

@Micah I have this question too, and best practice in WeWeb on using the CSS here vs. the libraries feature for typographies, colors, spacings, classes, etc.

Hello @flo Thank you very much for the guide.

Quick question: what is your suggested way and best practice to implement the now mainstream standard approach of the color system flipping into dark mode and then back to light mode automatically when the user’s system changes from light to dark?

In CSS there are some “media” types, which you can use to determine current user’s system palette.

So instead of “color mode: string” you should create var darkmode: object with default = null;
Then we call custom js to get the color mode from the browser:

return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;

then when we set dark mode var like this:

darkmode == null ? step1.result : darkmode