Inlining SVGs for Dark Mode
ahelwer.ca
ahelwer.ca
A D3 diagram uses:
@media (prefers-color-scheme: light) {
:root {
--node-text: #444;
--tree-buttons: darkred;
--edge: #ddd;
--edge-temp: red;
--file-buttons: darkblue;
--footer-labels: darkblue;
}
}
.node text {
font: 10px sans-serif;
fill: var(--node-text);
}
.link {
fill: none;
stroke: var(--edge);
stroke-width: 1px;
}
For icons, I usually do this @media (prefers-color-scheme: light) {
:root {
/*
Styles for plus/minus in front of tree param nodes.
Note that currentColor doesn't work when using url(data:) so color is specified - copy of text-color with # escaped.
TODO: figure out a way to share these
*/
--collapsed: url('data:image/svg+xml,<svg fill="red" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24"><g stroke="%23111" stroke-width="2"><line x1="12" y1="5" x2="12" y2="19"></line><line x1="5" y1="12" x2="19" y2="12"></line></g></svg>');
--expanded: url('data:image/svg+xml,<svg fill="red" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24"><g stroke="%23111" stroke-width="2"><line x1="5" y1="12" x2="19" y2="12"></line></g></svg>');*Upsides: the SVG is more portable, it's more self-contained, and I can be sure that, irrespective of how much I customize a given image for dark/light mode, those styles will always be declared in the same place -- right in the file.
Downsides: style declarations are repeated across SVG files, which, when I inevitably update the site's design, will mean that I'll have to do some manual editing. SVG debt.
0: Example here: https://ft.io/blog/shift-left/
Seems like this should be possible to roll into a compile step, defining the style in one place and letting the SSG/bundler/etc copy it into your SVGs.
It generally works fine, despite the extra work, but I've always hated the extra work for being slightly more stupid than I want to put my name to.
That's a separate issue from ensuring an inline SVG is still visible regardless of a site's dark or light mode or visible when a user uses their device's Contrast theme (aka `forced-color`).
[0] https://www.smashingmagazine.com/2021/05/accessible-svg-patt...
> Summary: In people with normal vision (or corrected-to-normal vision), visual performance tends to be better with light mode, whereas some people with cataract and related disorders may perform better with dark mode. On the flip side, long-term reading in light mode may be associated with myopia.
What is seldom discussed is the different types of dark modes, which are quite radically different in their appearance, purposes, pros and cons (unlike light mode, which is much less variable, and where there is marked variation it’s seldom for any particularly good reason). I wrote a bit about the three or four categories I’ve settled on a couple of years ago: https://news.ycombinator.com/item?id=28516862. It depends heavily on what you’re trying to achieve (æsthetics/low-contrast or accessibility), and the ambient lighting conditions and specific screen technology (both statically unknowable).
All up, dark mode is actually pretty risky, surprisingly so, because it can actually be at least three radically different and incompatible things. If you implement only one, go light.
Even for users who need light-on-dark, they may want individual sites light-on-dark if they have other sites or apps they have to use that don't have a dark mode so the user inverts the colors on the whole display.
because light attracts bugs.
— chatgpt