Hacker News new | ask | show | jobs
by CGamesPlay 963 days ago
Two improvements that could be made:

1. My dark mode preferences change throughout the day. Caching the value in localStorage means that it will require manual updates twice a day. Add a behavior that automatically unsets the localStorage key whenever it would set it to the currently system-preferred value.

2. Apply the override using a CSS class on the body element calculated from a synchronous JavaScript block to avoid the flash.

1 comments

Thanks, and yeah I was on the fence about #1 (I also like to auto-toggle at night sometimes). My hesitation is that some people might just like one theme better on my website and don't want it to keep resetting based on OS preference. Not sure which case is more common...

#2 is a great idea. I made a todo for myself to do that at some point.

The nice thing about only storing the value when it's different from the current default and unsetting it otherwise is that anyone who prefers one theme never has to change it (because their system is already in that theme) or only has to change it one time and it will stay that way forever. But users who prefer the site to just Do The Right Thing can also have that behavior.