things turning me on this week
☕enjoy with a hot cupa java/script
‘Dark Mode’ is new - and a great relief to our eyes at night.
Thankfully it’s also not that hard to setup very effectively.
Basics
If you ideally use either minimal css/style color:
and background-color:
or neutral colors (eg: middle grays for accents like #888
) then your work is a lot simpler because you have less overrides to setup.
Effectively, one easy pass through is to find your darker and lighter colors and invert them for when it is dark mode.
You can then set those overrides into a CSS
‘media query’ eg:
@media (prefers-color-scheme: dark) {
body {
color: #ddd;
background-color: #111;
}
}
That might override normal defaults (implicit or explict) for your site like:
body {
color: #111;
background-color: #ddd;
}
The less places that use specific colors - the less you have setup overrides for.
Advanced
At the time of this writing:
- macosx is
Mojave
andCatalina
comes later this month- Mojave has only either everything is dark mode (always) or not
- iOS latest is v13
- which has the same ^^ ability to always be light/default, always be dark, or be time-based
So for non-mobile, if you want time-based dark mode, you can use javascript
to additionally use the browser’s localtime to do dark mode when it’s evening hours and dark mode is enabled (and otherwise do default/light/day mode).
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
log('bring on the darkness!')
const hour = new Date().getHours()
if (7 <= hour && hour < 19) { // override [7am .. 7pm] localtime
log('.. but its vampire sleep time - back to day mode')
$('body').addClass('lite')
}
}
and you can assign some ‘overrides to the overrides’ in your css
rules like:
body.lite {
color: #333;
background-color: #f4f4f4;
}
which will basically disable/revert dark mode during the local daylight hours.
Once Catalina macosx hits, I will likely drop the JS and body.lite
stuff above.
Feel free to see the ‘dark mode’ CSS and JS I’m using here: CSS JS (presently at the bottom of the files).
Comments
Jamie