From 1ce1c216c9d90b0fe8ef65507731375ba823409b Mon Sep 17 00:00:00 2001 From: kimulaco Date: Wed, 29 May 2019 18:56:36 +0900 Subject: [PATCH] Update switch rules --- script.js | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/script.js b/script.js index e7c36cc..e19ee49 100644 --- a/script.js +++ b/script.js @@ -1,7 +1,8 @@ (function () { const switchBtn = document.getElementById('switch'); const stylesheet = document.getElementById('stylesheet'); - const mql = matchMedia('(prefers-color-scheme: dark)'); + const darkMql = matchMedia('(prefers-color-scheme: dark)'); + const lightMql = matchMedia('(prefers-color-scheme: light)'); const themes = { dark: '../dist/dark.css', darkStandalone: '../dist/dark.standalone.css', @@ -10,23 +11,31 @@ }; switchBtn.addEventListener('click', function() { - switch (stylesheet.getAttribute('href')) { - case themes.dark: - stylesheet.href = mql.matches ? themes.lightStandalone : themes.darkStandalone; - break; - case themes.darkStandalone: - stylesheet.href = themes.lightStandalone; - break; - case themes.light || themes.lightStandalone: - stylesheet.href = mql.matches ? themes.lightStandalone : themes.darkStandalone; - break; - case themes.lightStandalone: - stylesheet.href = themes.darkStandalone; - break; + const readTheme = stylesheet.getAttribute('href'); + if (readTheme === themes.dark) { + if (darkMql.matches) { + stylesheet.href = themes.lightStandalone + } else if (lightMql.matches) { + stylesheet.href = themes.darkStandalone + } else { + stylesheet.href = themes.light + } + } else if (readTheme === themes.darkStandalone) { + stylesheet.href = themes.lightStandalone + } else if (readTheme === themes.light) { + if (darkMql.matches) { + stylesheet.href = themes.lightStandalone + } else if (lightMql.matches) { + stylesheet.href = themes.darkStandalone + } else { + stylesheet.href = themes.dark + } + } else if (readTheme === themes.lightStandalone) { + stylesheet.href = themes.darkStandalone } }); - mql.addListener(function (mql) { + darkMql.addListener(function (mql) { if (mql.matches) { stylesheet.href = themes.dark; } else {