Notes on implementing dark mode
brandur.org
brandur.org
You can read more about it here: https://github.com/WICG/web-preferences-api
Feedback is welcome
Also the problem it’s solving is not that big, it’s literally less than a dozen lines of JS. These “hacks” are exceptionally well documented solutions that exist for about every possible framework or library.
Idk maybe I’ve just built out the solution for it too many times and now I think it’s easy. I am still learning how to better implement the solution, including using inline JS to avoid the color change. But still I have a hard time understanding how moving this logic into the browser would help people.
So there's still some work left here until I'd call the implementation "great".
(A simple css check for prefers-color-scheme handles this.)
I don’t see the value in having this implemented by individual websites.
Having the in-browser toggle would also be rather questionable for anything that has more than just "light" and "dark"; the options suck - either having the browser-based toggle be a "shortcut" for one toggle in your main theme settings (which likely means users are less likely to notice the full settings), or abandon the browser toggle entirely (which likely means a significant amount of users would assume there's no theming at all). And neither really fits for something that has, instead of just "dark" and "light", an additional "high-contrast" mode (which, depending on what the site's designers bothered to do, could be an extension of "dark", or a toggle combinable with either light or dark); so you'd want the browser-based toggle to be at least somewhat customizable. Also worth considering is that if there's some fixed set of things that the browser-level theme toggle can do, sites would be very heavily disincentivized from adding any other things as those would then require an entirely custom theming setting panel.
I'm exaggerating somewhat, sure, but I don't think you'll find any way to have a browser-level theme toggle that doesn't end up pretty bad in a significant number of cases.
(You can do the fallback to the CSS prefers setting, to try to not break dark-mode users, but if at all possible, you do want some sort of widget or toggle - at least for now. I am hopeful that in maybe 5 years it can be removed and users assumed to understand it.)
At least with Firefox it is possible to style it dark while setting prefers-color-scheme to light.
For the next version of my site I'm not going to include the toggle. It's not worth it.
It's a shame there isn't a standard high contrast setting, though. That seems more important than dark mode. Oh well.
The extra effort is only needed on Firefox and Safari, though. Chromium already handles this somehow.
Now I have to check if my implementation supports cross-tab changes...