3 ms·
This still requires JS and localstorage. I've been considering this recently for my site and I settled on having prefers-color-scheme on the root domain with d
by onion2k 3y ago
This still requires JS and localstorage.
I've been considering this recently for my site and I settled on having prefers-color-scheme on the root domain with dark. and light. subdomains. If the user chooses a color they just get bumped to a subdomain that sets a cookie with their preference. If they come to the site from an external link and they have a preference cookie they get redirected to the subdomain again. Absolutely no clientside JS is needed anywhere, no FOUC issues, and it's trivially simple to code because it's literally just a redirect in the server config based on a cookie value.
I really should spend the time to actually build it, but Diablo 4 came along and I got sidetracked.
- gwern 3y agoI don't know whether to be fascinated or horrified by this proposal, but I admire the novelty - I can definitely say that in all the dark-mode proposals/implementations I've looked at over the years we've been fiddling with ours, I've never seen that suggested.
- porridgeraisin 3y agoWouldn't the following be a better version of the above. Use css variables everywhere, and have a defs-dark.css and a defs-light.css that define the colors. Then, on nginx, serve the correct stylesheet based on cookie value. Better than subdomains for dark and light, no?
- onion2k 3y agoMy idea has the benefit that users can block the cookie if they want to and still use whatever mode they want, although they'd lose the redirection 'functionality' if they do that.
- account42 3y agoThis sounds like it would have the same problem as redirects to mobile subdomains - users will share links to light. and dark. subdomains, unknowingly forcing their preference onto the recipient. The proper solution is to only implement the media queries and yell at browser vendors to make this easier to toggle either for all websites or for specific websites. Implementing a light/dark mode switch in every website is absurd.
- onion2k 3y agounknowingly forcing their preference onto the recipient I'd suggest knowingly rather than unknowingly. It's a link to a website after all. It should link to the same thing that the person who shared it was viewing. That's the point of a URL - when you share one the recipient should see what you were seeing. FWIW I would also like browser vendors to do a better job at enabling users to manage their preferences on a per site basis though.