Skip to content

Commit

Permalink
Fix for removeListeners in unsupported browsers
Browse files Browse the repository at this point in the history
Calling removeListeners will call MediaQueryList.removeListner, which requires support for the prefer-color-scheme media query. This was causing browsers that did not support said query to fall over themselves.
  • Loading branch information
lukewhitehouse committed Jul 12, 2019
1 parent e0eabb2 commit 917a041
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions index.js
Expand Up @@ -13,21 +13,23 @@ module.exports = ({ onChange = () => {} }) => {
const darkQueryListener = q => queryListener(q, themes.DARK);
const lightQueryListener = q => queryListener(q, themes.LIGHT);

if (!isSupported) {
return onChange(themes.NO_SUPP, themes);
}

if (darkQuery.matches) onChange(themes.DARK, themes);
if (lightQuery.matches) onChange(themes.LIGHT, themes);
if (noPrefQuery.matches) onChange(themes.NO_PREF, themes);
if (isSupported) {
if (darkQuery.matches) onChange(themes.DARK, themes);
if (lightQuery.matches) onChange(themes.LIGHT, themes);
if (noPrefQuery.matches) onChange(themes.NO_PREF, themes);

darkQuery.addListener(darkQueryListener);
lightQuery.addListener(lightQueryListener);
darkQuery.addListener(darkQueryListener);
lightQuery.addListener(lightQueryListener);
} else {
onChange(themes.NO_SUPP, themes);
}

return {
removeListeners: () => {
darkQuery.removeListener(darkQueryListener);
lightQuery.removeListener(lightQueryListener);
if (isSupported) {
darkQuery.removeListener(darkQueryListener);
lightQuery.removeListener(lightQueryListener);
}
}
}
};

0 comments on commit 917a041

Please sign in to comment.