From b43431bd51f64d16bbd6f4ae5f5f9008629dde3d Mon Sep 17 00:00:00 2001 From: Wonderfall Date: Fri, 29 Jul 2022 16:15:22 +0200 Subject: [PATCH] refactor theme.js --- assets/js/theme.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/assets/js/theme.js b/assets/js/theme.js index be17dc90..079c0906 100644 --- a/assets/js/theme.js +++ b/assets/js/theme.js @@ -1,8 +1,11 @@ -// for now this is assuming default theme is set to dark -// will probably refactor in the future for much better handling function loadPreferredTheme() { - if (localStorage.getItem("pref-theme") === "light") { + const preferredTheme = localStorage.getItem("pref-theme"); + const isDarkTheme = document.body.classList.contains("dark"); + + if (preferredTheme === "light" && isDarkTheme) { document.body.classList.remove('dark') + } else if (preferredTheme === "dark" && !isDarkTheme) { + document.body.classList.add('dark') } }