From a9d3d80959be383eeee64e0b3127ea7526ef9968 Mon Sep 17 00:00:00 2001 From: Ward Truyen Date: Mon, 9 Sep 2024 22:19:29 +0200 Subject: [PATCH] chore: added missing theme.js --- src/js/theme.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/js/theme.js diff --git a/src/js/theme.js b/src/js/theme.js new file mode 100644 index 0000000..22aa963 --- /dev/null +++ b/src/js/theme.js @@ -0,0 +1,38 @@ +function toggleTheme() { + const theme = "theme-dark";//localStorage.getItem('theme'); + const root = document.querySelector(":root"); + root.classList.toggle(theme); + if (root.classList.contains(theme)) { + localStorage.setItem('theme', theme); + } else { + localStorage.setItem('theme', "theme-light"); + } +} + +function setTheme(themeName) { + console.log("setting theme: " + themeName); + const root = document.querySelector(":root"); + root.classList.add(themeName); + localStorage.setItem('theme', themeName); +} + +function detectTheme() { + const theme = localStorage.getItem('theme'); + if (theme === 'theme-dark' || theme === 'theme-light') { + setTheme(theme); + return; + } + + if (window.matchMedia('(prefers-color-scheme: dark)').matches) { + setTheme('theme-dark'); + return; + } + + if (window.matchMedia('(prefers-color-scheme: light)').matches) { + setTheme('theme-light'); + return; + } + + setTheme('theme-light'); +} +detectTheme();