Skip to content

Commit

Permalink
Always react on color scheme changes, even when permanent is set
Browse files Browse the repository at this point in the history
  • Loading branch information
tomayac committed Oct 22, 2019
1 parent 30c699e commit ac48837
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 101 deletions.
171 changes: 78 additions & 93 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dark-mode-toggle",
"version": "0.4.9",
"version": "0.4.10",
"description": "Web Component that toggles dark mode 🌒",
"main": "./src/dark-mode-toggle.mjs",
"browser": "src/dark-mode-toggle.mjs",
Expand Down Expand Up @@ -38,9 +38,9 @@
"eslint-config-google": "^0.14.0",
"http-server": "^0.11.1",
"shx": "^0.3.2",
"stylelint": "^11.0.0",
"stylelint": "^11.1.1",
"stylelint-config-standard": "^19.0.0",
"terser": "^4.3.4"
"terser": "^4.3.9"
},
"eslintConfig": {
"parserOptions": {
Expand Down
8 changes: 3 additions & 5 deletions src/dark-mode-toggle.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,11 @@ export class DarkModeToggle extends HTMLElement {
// Does the browser support native `prefers-color-scheme`?
const hasNativePrefersColorScheme =
matchMedia(MQ_DARK).media !== NOT_ALL;
// Listen to `prefers-color-scheme` changes, unless `permanent` is true.
// Listen to `prefers-color-scheme` changes.
if (hasNativePrefersColorScheme) {
matchMedia(MQ_DARK).addListener(({matches}) => {
if (!this.permanent) {
this.mode = matches ? DARK : LIGHT;
this._dispatchEvent(COLOR_SCHEME_CHANGE, {colorScheme: this.mode});
}
this.mode = matches ? DARK : LIGHT;
this._dispatchEvent(COLOR_SCHEME_CHANGE, {colorScheme: this.mode});
});
}
// Set initial state, giving preference to a remembered value, then the
Expand Down

0 comments on commit ac48837

Please sign in to comment.