Docs Dark/Light Theme #5
Replies: 1 comment 1 reply
-
I think ideally the theme toggle should be part of the client (Backlight shell in this case, like some other tools) and also ideally it should set Since |
Beta Was this translation helpful? Give feedback.
-
Docs Dark/Light Theme
It would be great if the BKL docs section, whether it uses MDJS or MDX or other tech, has by default a dark theme toggler.
What this will allow is that Component/DS authors can create things that have built-in dark-mode support, and without having to think about app/docs structure, they get working demos that can showcase this dark-mode integration. Dark mode is becoming the new responsive in terms of "no longer nice-to-have, but a fundamental requirement", and some developers even argue to build dark-mode-first, light-mode-later, when in a hurry.
In order to provide a working theme switch out of the box, we should align on some conventions, so this discussion is essentially a proposal for that, you can see it working https://backlight.dev/edit/5vtJtbY04aoD1dGKcsu1 here.
Global Theme Attribute
To tell the page that we are in "dark" or in "light" mode, a "theme" attribute is used on the
html
element:This is the attribute that theme controls will interact with.
Theme Togglers
A custom-element that changes the theme between "light" and "dark". There's a few implementation details that are important to note:
prefers-color-scheme
.The idea is that we bring such a toggle into BKL docs, by default, so users have that out of the box with 0 effort.
Components Using ShadowDOM
It is a bit awkward to have components with fully encapsulated styles using ShadowDOM to respond to the global theme which is outside of this encapsulation.
For example, this does not work, by design:
but this does:
There's a pretty straightforward, VanillaJS way to have your class-based components always sync their internal theme attribute to the global theme attribute, and it could be adapted to functional components too. It is essentially an interface implemented on every component with dark-mode-support that handles this:
Again, this can be seen in action here: https://backlight.dev/edit/5vtJtbY04aoD1dGKcsu1 , go to "Infrastructure -> Themes" to see the src code.
Beta Was this translation helpful? Give feedback.
All reactions