From 52890f19008d383bdcc8f5eeec28dd4c8a8bbf77 Mon Sep 17 00:00:00 2001 From: oitsjustjose Date: Tue, 24 Mar 2020 10:25:08 -0400 Subject: [PATCH] Use prefers-color-scheme --- Libraries/js/app.js | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/Libraries/js/app.js b/Libraries/js/app.js index 5d34505..f8b899f 100644 --- a/Libraries/js/app.js +++ b/Libraries/js/app.js @@ -16,32 +16,44 @@ const toggleDisplayMode = () => { if (currentDisplayMode == "dark") { window.localStorage.setItem("displayMode", "moonlight"); currentDisplayMode = "moonlight"; - document.getElementById("themekit-css").href = "Libraries/css/themekit/main-moonlight.css"; + document.getElementById("themekit-css").href = "/Libraries/css/themekit/main-moonlight.css"; } else if (currentDisplayMode == "moonlight") { window.localStorage.setItem("displayMode", "light"); currentDisplayMode = "light"; - document.getElementById("themekit-css").href = "Libraries/css/themekit/main-light.css"; + document.getElementById("themekit-css").href = "/Libraries/css/themekit/main-light.css"; } else { window.localStorage.setItem("displayMode", "dark"); currentDisplayMode = "dark"; - document.getElementById("themekit-css").href = "Libraries/css/themekit/main-dark.css"; + document.getElementById("themekit-css").href = "/Libraries/css/themekit/main-dark.css"; } updateDisplayModeIcons(); }; window.addEventListener("DOMContentLoaded", (evt) => { - if (window.localStorage.getItem("displayMode") != null) { - currentDisplayMode = window.localStorage.getItem("displayMode"); - } else { - window.localStorage.setItem("displayMode", "dark"); + if (window.localStorage.getItem("displayMode") == null) { + // Match with system dark mode + if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) { + window.localStorage.setItem("displayMode", "dark"); + } + // Match with system light mode + else if (window.matchMedia && window.matchMedia('(prefers-color-scheme: light)').matches) { + window.localStorage.setItem("displayMode", "light"); + } + // Literally choose the middle option + else { + window.localStorage.setItem("displayMode", "moonlight"); + } } + currentDisplayMode = window.localStorage.getItem("displayMode"); + if (currentDisplayMode == "dark") { - document.getElementById("themekit-css").href = "Libraries/css/themekit/main-dark.css"; + document.getElementById("themekit-css").href = "/Libraries/css/themekit/main-dark.css"; } else if (currentDisplayMode == "moonlight") { - document.getElementById("themekit-css").href = "Libraries/css/themekit/main-moonlight.css"; + document.getElementById("themekit-css").href = "/Libraries/css/themekit/main-moonlight.css"; } else { - document.getElementById("themekit-css").href = "Libraries/css/themekit/main-light.css"; + document.getElementById("themekit-css").href = "/Libraries/css/themekit/main-light.css"; } + updateDisplayModeIcons(); }); \ No newline at end of file