diff --git a/components/btn/Theme.vue b/components/btn/Theme.vue index d772d2a6a7b235709fe5c4f9b1f227914b8a6106..dc2f65c98c216f01da458a2432c65c36e695252f 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>