Skip to content

Commit

Permalink
Rely on prefers-color-scheme w/o night mode set (#1114)
Browse files Browse the repository at this point in the history
Currently when a user visits the page during the night (or rather when
`prefers-color-scheme` is `dark`), the page uses night mode.

This is great, but what's not so great is that it remembers this as a
user choice, and won't automactically change if the user visits the page
at a later time during the day (when `prefers-color-scheme` is `light`
or `no-preference`).

This makes it so that night mode is an explicit choice, which is still
remembered. But if the user turns off night mode it goes back to the
light colors, and if the user later visits the site during night time,
that will be picked up.

The reasoning here is that it's called night mode, which previously was
a opt-in, but the current code makes it set the preference on first
load, and never let's the user change that.
  • Loading branch information
maxnordlund authored and José Valim committed Dec 1, 2019
1 parent 5026bd0 commit bdc9063
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions assets/js/night.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function activateNightMode () {

function deactivateNightMode () {
body.removeClass(nightMode)
try { localStorage.setItem(nightMode, false) } catch (e) { }
try { localStorage.removeItem(nightMode) } catch (e) { }
}

function checkForNightMode () {
Expand All @@ -29,7 +29,7 @@ function checkForNightMode () {
activateNightMode()
}
} else if (matchMedia('(prefers-color-scheme: dark)').matches) {
activateNightMode()
body.addClass(nightMode)
}
} catch (e) { }
}
Expand Down

0 comments on commit bdc9063

Please sign in to comment.