I honestly hadn’t thought about “getting caught” by light-dark() — particularly when leaning heavily on it for this site — but Martin Underhill explains:.
Without fallbacks, browsers that don’t support light-dark()typically use black text on a white background in Light Mode, and white text on a dark grey background in Dark Mode. They’re not the exact colours I’d want, but my website looks fine.
Again, it’s risky, so test thoroughly.
In a recent development, like Martin, I initially began implementing color themes based on media queries sniffing out user preferences. But it became super cumbersome to write prefers-color-scheme time and again. I mean, totally DX-world problems. I also remembered the piece Sara Joy wrote with us a while back encouraging a jump into the light-dark() waters and thought it’d be nice to take that advice. (Related: Transformer-based Encoder-Decoder Models...)
Doing that, I ran into another DX annoyance: writing two color values for each light-dark(). CSS variables help, of course, but even then it exhausted me. That states something more about me than the feature, but I digress. So, I took Martin’s process one step further to abstract all the things:.
According to recent reports, very nice, at least for my penchant for laziness. But I ran into another problem because sometimes text needs to go the other direction. Keep abstracting. I could define the background variable which is normally the contrast to text:.
Recent findings suggest that works, but using a helper called “surface” on a property controlling color bothers me too. I may be lazy, but I’m a little anal with semantics. Hence:. (Related: How to train a newly introduced language model from scr...)
Sources indicate that …where “reverse” means it goes the opposite direction of a normal “light-dark” call and the “dark” value is called first. But even that is still too much for my brain to handle because now I’m left with multiple tokens to remember if I want things to be super semantic:.
so I went with an even more general abstraction:.
Perfect, no. But I’m pretty happy with it.
Back to Martin and fallbacks, he goes about it with a classic cascade safeguard:.
According to recent reports, he’s totally right and I ought to be doing that as well. Maybe I will, but I could help thinking that a light-dark() fallback argument would be nice. Spitballing here, of course:.
Now we’re getting verbose. But so are fallbacks in general. Indeed, Martin asked if moving to light-dark() is even worth it given the need to think about fallbacks. Is it really more efficient than a media query at this point? His byte-savings were negligible. I decided to compare what the differences would have been on CSS-Tricks:.
It has been revealed that welp, not totally encouraging. I haven’t run performance tests around it, but it does make me think I chose DX over performance.
