From 3afb25f5ec1394365e8300d332c75065569d00e5 Mon Sep 17 00:00:00 2001 From: Sharkbyteprojects <40953479+Sharkbyteprojects@users.noreply.github.com> Date: Tue, 17 Aug 2021 21:28:37 +0200 Subject: [PATCH 1/2] Update dark-mode-switch.js --- dark-mode-switch.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/dark-mode-switch.js b/dark-mode-switch.js index 06edb2b..6402478 100644 --- a/dark-mode-switch.js +++ b/dark-mode-switch.js @@ -26,9 +26,14 @@ window.addEventListener("load", function () { * @return {void} */ function initTheme() { - var darkThemeSelected = - localStorage.getItem("darkSwitch") !== null && - localStorage.getItem("darkSwitch") === "dark"; + var dt = window.matchMedia ? window.matchMedia("(prefers-color-scheme: dark)") : null, + ds=localStorage.getItem("darkSwitch"), + darkThemeSelected = + ds !== "light" && + ds === "dark"; + if(ds === null && dt !== null){ + darkThemeSelected=dt.matches; + } darkSwitch.checked = darkThemeSelected; darkThemeSelected ? document.body.setAttribute("data-theme", "dark") @@ -47,6 +52,6 @@ function resetTheme() { localStorage.setItem("darkSwitch", "dark"); } else { document.body.removeAttribute("data-theme"); - localStorage.removeItem("darkSwitch"); + localStorage.setItem("darkSwitch", "light"); } } From fc09ccb3eec181f8f8351b3b98d2376af01f0ffa Mon Sep 17 00:00:00 2001 From: Sharkbyteprojects <40953479+Sharkbyteprojects@users.noreply.github.com> Date: Tue, 17 Aug 2021 21:33:29 +0200 Subject: [PATCH 2/2] Set init style to browser theme --- dark-mode-switch.min.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dark-mode-switch.min.js b/dark-mode-switch.min.js index d031d10..5b3175e 100644 --- a/dark-mode-switch.min.js +++ b/dark-mode-switch.min.js @@ -3,4 +3,4 @@ * Copyright 2021 C.Oliff * Licensed under MIT (https://github.com/coliff/dark-mode-switch/blob/main/LICENSE) */ -var darkSwitch=document.getElementById("darkSwitch");window.addEventListener("load",(function(){if(darkSwitch){initTheme();darkSwitch.addEventListener("change",(function(){resetTheme()}))}}));function initTheme(){var darkThemeSelected=localStorage.getItem("darkSwitch")!==null&&localStorage.getItem("darkSwitch")==="dark";darkSwitch.checked=darkThemeSelected;darkThemeSelected?document.body.setAttribute("data-theme","dark"):document.body.removeAttribute("data-theme")}function resetTheme(){if(darkSwitch.checked){document.body.setAttribute("data-theme","dark");localStorage.setItem("darkSwitch","dark")}else{document.body.removeAttribute("data-theme");localStorage.removeItem("darkSwitch")}} \ No newline at end of file +var darkSwitch=document.getElementById("darkSwitch");function initTheme(){var e=window.matchMedia?window.matchMedia("(prefers-color-scheme: dark)"):null,t=localStorage.getItem("darkSwitch"),d="light"!==t&&"dark"===t;null===t&&null!==e&&(d=e.matches),darkSwitch.checked=d,d?document.body.setAttribute("data-theme","dark"):document.body.removeAttribute("data-theme")}function resetTheme(){darkSwitch.checked?(document.body.setAttribute("data-theme","dark"),localStorage.setItem("darkSwitch","dark")):(document.body.removeAttribute("data-theme"),localStorage.setItem("darkSwitch","light"))}window.addEventListener("load",function(){darkSwitch&&(initTheme(),darkSwitch.addEventListener("change",function(){resetTheme()}))});