Skip to content

Commit

Permalink
Smart Light-Dark mode detection (#40)
Browse files Browse the repository at this point in the history
* Smart Light-Dark mode detection

Use case is where user has already chosen a Dark Mode preference (ie macOS/iOS) and site start in white mode killing your eyes. This will make the site smart enough to detect that preference and switch the Dark Mode on.

Nothing critical changed.

Could be improved to let site author chose if they want to turn this feature on/off (based on style preferences).

* Fixed typos

Fixing typo in comment at #256 (basded) and fixing function invoking at #265 (toggl())

Co-authored-by: Pablo Armentano <pablo@simpat.io>
  • Loading branch information
pabliqe and Pablo Armentano committed Sep 9, 2020
1 parent 891e4b9 commit 5bc77e0
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,18 @@ class BodyRewriter {
el.className = 'toggle-mode';
el.addEventListener('click', toggle);
nav.appendChild(el);
onLight();
// enable smart dark mode based on user-preference
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
onDark();
} else {
onLight();
}
// try to detect if user-preference change
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', e => {
toggle();
});
}
const observer = new MutationObserver(function() {
if (redirected) return;
Expand Down

0 comments on commit 5bc77e0

Please sign in to comment.