I have created three themes, light, default and dark. I'm now trying to create a toggle in the footer so I can switch between each theme.
With the advice of @jogarcia I have changed my code to the following.
The mixin is now in its own file called themes.js
export default {
data() {
return {
currentTheme: "",
};
},
mounted() {
const storedTheme = localStorage.getItem("theme-colour");
if (!storedTheme) {
this.currentTheme = "theme-default";
} else {
this.currentTheme = localStorage.getItem("theme-colour");
}
},
methods: {
toggleTheme() {
const storedTheme = localStorage.getItem("theme-colour");
if (!storedTheme) {
localStorage.setItem("theme-colour", "theme-light");
this.currentTheme = localStorage.getItem("theme-colour");
}
if (storedTheme === "theme-default") {
localStorage.setItem("theme-colour", "theme-light");
this.currentTheme = localStorage.getItem("theme-colour");
} else {
localStorage.setItem("theme-colour", "theme-dark");
this.currentTheme = localStorage.getItem("theme-colour");
}
},
},
};
Therefore I have imported the file in both the app.vue and Footer component by doing import themes from "@/mixins/themes"; and mixins: [themes] in the export respectively.
I have also added :class="currentTheme" to the app.vue file.
However, I'm still running into a few more issues.
- How do I get the theme class to change dynamically?
Here is a gif of what I mean, as you can see I can toggle, the value is being changed in localStorage but the class name isn't changing in the main app div.
https://i.gyazo.com/2044ff9293328aaaae8810272ffc6a5a.mp4
Update:
I've updated the bind in app.vue to :class="[currentTheme]" but this still doesn't work.
Here is a new GIF https://i.gyazo.com/2f1d6a98b51e2d00c8ba748940016f55.mp4
As you can see the default theme loads by default which is now correct, however, when I toggle to dark the data currentTheme doesn't change. It only changes on refresh because of localstorage and I can't select the theme in the dropdown using Vue devtools if that's any issue as well.
App.vue
<template>
<div
id="app"
class="flex flex-col min-h-screen h-full font-sans text-copy-primary leading-normal tracking-wide overflow-x-hidden"
:class="[currentTheme]"
>
<Navigation />
<DealBanner discountValue="20" date="April 26, 2020 12:00" discountCode="RELEASE" />
<router-view class="flex-1" />
<Footer />
</div>
</template>
<script>
import themes from "@/mixins/themes";
import Navigation from "@/components/Navigation";
import DealBanner from "@/components/DealBanner";
import Footer from "@/components/Footer";
export default {
name: "App",
components: {
Navigation,
DealBanner,
Footer
},
mixins: [themes]
};
</script>
Footer.vue (shorted)
<template>
<footer
class="px-6 xl:px-0 py-6 bg-theme-primary text-copy-primary border-t border-theme-ternary"
>
<div class="flex flex-col justify-between lg:flex-row lg:items-center xl:px-
<div class="text-center">
<p>Theme Switcher</p>
<div @click="toggleTheme">Toggle</div>
{{currentTheme}}
</div>
</div>
</footer>
</template>
<script>
import themes from "@/mixins/themes";
export default {
name: "Footer",
mixins: [themes]
};
</script>
...
error 'toggleThemeMixin' is not defined