I don’t know why I dragged my feet on adding prefers-color-scheme
to blog-pwa. The working draft spec has been around a couple years, there’s been various support in most browsers and operating systems in some shape or form since around that time as well. I’d seen various implementations the last year that showed the mode was a win for accessibility and power savings.
Yet, it just didn’t make the list of things-to-do. Until today.
My Approach
Dark mode for me is a user choice that people are really in to for what ever reason they may have. Maybe they like the aesthetic, maybe they need the inversion of color for accessibility reasons. Regardless I set forth to do two things:
- Respect the choice people are making with
prefers-color-scheme
. - Respect the light environment they’re in with
AmbientLightSensor
and adjust as needed.
On the surface these seem simple enough. “That’s what the spec is for Justin” you might be thinking. My primary concern is a case of less complexity for users and a smooth experience.
The Setup
The first thing I did was make sure my CSS custom properties were indeed square and up-to-date, so that I could easily switch between color themes with ease.
The color pallette in blog-pwa is not huge (fairly monochromatic as it is), so this was a simple enough to simply copy some props and fate them in a :root[darkmode]
selector:
A couple of things to note:
- I set
color-scheme
in the my:root
CSS pseudo-class to tell the browser do some magic based on what I’m supporting, like setting my form fields and scroll bars to the correct colors. - I pull the colors down of all my images with
--image-filter: grayscale(50%)
. This is based on Thomas Steiner’s research about re-colorization and is further noted in his excellent reference write-up on web.dev.
This gets me into two modes of color that I can now toggle. Now I just need to figure out which the user prefers.
Respect the choice, JavaScript edition
In a lot of examples floating around, light and dark mode becomes a toggle in some random place in your application. Github has a toggle they’d been testing and there’s even a web component that does this very thing, dark-mode-toggle, from my colleagues over at Google Chrome.
In both cases, I get it: give the user a choice, test some things out in the process. But if the user has already made that call, then why the hunt-and-switch? Just not my style.
Instead, I went with a check-and-set approach, using the window.matchMedia
to handle this when the main blog-pwa web component starts up and then listen for changes if the user flips some switches at the operating system level.
With that little bit of code, we’re good to go. Well, except if JavaScript is disabled.
Respect the choice, no JavaScript edition
Given the static render case, maybe someone still wants a dark theme but isn’t running JavaScript. blog-pwa supports this already due to the architecture (yes, I wanted Lynx support), but could I make dark mode work? Sure enough, we simply set a media query in our static template and we’re good to go:
Boom: more support for users, no matter what their preference.
Stop the eye burning with AmbientLightSensor
The one thing that annoys me most about most light/dark toggles is that I don’t want to have to flip them all the time. If I’m in a sunny room with lots of light, a dark theme does not suffice in most cases. If I’m lying in bed at night reading, I really want dark mode.
Alas, I felt like I could toy with this concept with AmbientLightSensor
. This of course isn’t my first go-around with that API; I built my PWA speedometer with that API a while back (much to the vitriol of the orange site internet trolls who pummeled my inbox for some unknown reason). This time around, I just wanted it to trigger dark mode if the light was low (presuming the user hadn’t already made that choice):
How useful this is given it’s behind a chromium flag is meh (#enable-generic-sensor-extra-classes for those so inclined), but I’m playing with it to see if I gives me an okay experience. Jury is still out, but I think the pattern would hold well in practice for most applications.
The end is into the darkness or something
In all, it’s not a lot of code to write. The standards are still in flux, but are workable to be able to deliver to users who need a darker theme with not much trouble.
The hardest part? Getting the dark mode colors not only pleasing but also WCAG 2.1 compliant. Color is not exactly my thing says the black and white film photographer. Who would have guessed. :-)