[v4] Improve the usage of CSS variables for dark/light mode #15083
IonianPlayboy
started this conversation in
Ideas
Replies: 1 comment 1 reply
-
If you are supporting only relative newish browsers you can use light-dark css function. --color-primary: light-dark(red, magenta); and the only thing you need to change is |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello everyone, congratulations to the team for the launch of the v4 beta ! It's very exciting, and it looks amazing.
I have started playing with the new version with the 100% CSS config, and the only thing that did not convince so far is setting up dark/light mode colors with CSS variables.
Note
I am only talking about dark/light mode to keep this simple, but this could apply to any number of theme configured in your main CSS file.
The problem
dark
variant usageIt's very easy to declare a custom
dark
variant to customize dark mode detection, as showed in the beta docs :You can then use the
dark
variant anywhere in your app, and it will work just fine.Even if at some point you need to change your dark mode detection strategy, or you create a lib that is consumed by other projects with a different dark mode logic, you just need to update/set one line of CSS to have all your app updated.
CSS variables usage
This is sadly not the case if you are working with CSS variables, instead of relying on the
dark
variant.Your config would most likely look like something like this (see #14091 & #14095) :
On a local/personal project, this will work exactly like using the
dark
variant, and it is still pretty easy to change the detection logic by updating only one line of CSS.However, this is much more problematic if you are publishing a lib. Your users can't override your dark mode detection logic without rewriting all the dark mode values themselves or with a hacky way to change the css file you provide.
Compared to v3
If I am being 100% honest, working with CSS variables in dark mode was not that much easier in v3. The main difference I see is that since v3 is based on a JS config file, libraries would most of the time read the
darkMode
config option to generate their colors in a way that would be compatible with the user's dark mode detection.Since in v4 the JS config is optional, it is not a reliable technique anymore, and I don't see an alternative solution to solve this issue without forcing libraries to create their own, self-maintained custom config in JS to handle this.
Proposed solution
In my opinion, this is a pain point that is going to be encountered by a lot of users, since CSS variables for dark/light mode are very common when using design systems with design tokens. I find it worthwhile to improve the UX out of the box for this setup, and I believe that you should be able to stay 100% in your main CSS file to configure your dark mode to stay in the spirit of v4.
A potential solution could look like this :
This would compile to the same CSS as the example above, but the mode detection should be overridable by users of the lib by simply overriding the
dark
mode variant definition in their own CSS.Now obviously this is most likely not the best syntax possible, but I think that the mental model of separating your color definitions from the theme/mode detection is sound.
Beta Was this translation helpful? Give feedback.
All reactions