From 51c800d302c82031612dac1c48a35a3314075577 Mon Sep 17 00:00:00 2001 From: paidge <paidge_cs@hotmail.com> Date: Mon, 10 Jan 2022 02:52:22 +0100 Subject: [PATCH] improve theme management --- components/btn/Theme.vue | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/components/btn/Theme.vue b/components/btn/Theme.vue index d772d2a..dc2f65c 100644 --- a/components/btn/Theme.vue +++ b/components/btn/Theme.vue @@ -12,8 +12,7 @@ <script> export default { mounted() { - const initUserTheme = this.getMediaPreference(); - this.setTheme(initUserTheme); + this.setTheme(localStorage.getItem("user-theme")) }, data() { @@ -24,23 +23,19 @@ export default { methods: { toggleTheme() { - const activeTheme = localStorage.getItem("user-theme"); - if (activeTheme === "light-theme") { - this.setTheme("dark-theme"); + if (this.userTheme === "light-theme") { + this.setTheme("dark-theme") } else { - this.setTheme("light-theme"); + this.setTheme("light-theme") } }, setTheme(theme) { - localStorage.setItem("user-theme", theme); - this.userTheme = theme; - document.documentElement.className = theme; - }, - - getMediaPreference() { - return localStorage.getItem("user-theme"); - }, + if (theme == null) {theme="light-theme"} + localStorage.setItem("user-theme", theme) + this.userTheme = theme + document.documentElement.className = theme + } }, }; </script> -- GitLab