From 917a0417a72a61a50b955643c85f821bbf9ec420 Mon Sep 17 00:00:00 2001 From: Luke Whitehouse Date: Sat, 13 Jul 2019 00:53:46 +0100 Subject: [PATCH] Fix for removeListeners in unsupported browsers 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. --- index.js | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index e2873ca..712509c 100644 --- a/index.js +++ b/index.js @@ -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); + } } } };