diff --git a/static/utils.js b/static/utils.js index 80104c89..da3dc368 100644 --- a/static/utils.js +++ b/static/utils.js @@ -9,3 +9,28 @@ document.querySelectorAll("button[hx-trigger='confirmed']").forEach((value) => v document.addEventListener("htmx:afterSwap", () => { document.querySelectorAll("button[hx-trigger='confirmed']").forEach((value) => value.addEventListener("click", fireConfirmModal)); }); + +const getCurrentTheme = () => { + let current = document.documentElement.getAttribute("data-theme"); + + if (current === null) { + if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) { + current = "dark"; + } else { + current = "light"; + } + } + + return current; +} + +const switchTheme = () => { + const isCurrentlyLight = getCurrentTheme() === "light"; + + document.documentElement.setAttribute("data-theme", isCurrentlyLight ? "dark" : "light"); + document.querySelector("#theme-switcher use").setAttribute("href", `/static/feather-sprite.svg#${isCurrentlyLight ? "sun" : "moon"}`) +} + +document.getElementById("theme-switcher")?.addEventListener("click", switchTheme); + +document.querySelector("#theme-switcher use")?.setAttribute("href", `/static/feather-sprite.svg#${getCurrentTheme() === "light" ? "moon" : "sun"}`) diff --git a/templates/nav.html b/templates/nav.html index c8e49cd6..3f125d94 100644 --- a/templates/nav.html +++ b/templates/nav.html @@ -73,6 +73,11 @@ Profil + Abmelden