What I am trying to do?
I have a component library website where I want to show the different color themes. I have a select box where the user can switch between different themes.
I have two css files, lets name them watermelon and blueberry.
// blueberry/index/.css
:root {
--color-1: indigo;
}
// watermelon/index/.css
:root {
--color-1: green;
}
and on my tailwind.config.js
//tailwind.config.js
theme: {
extend: {
color: {
primary: "var(--color-1)"
Whats happening on the code
I have a watcher on selectedTheme, so everytime value changes, I import the correct theme css file.
import { ref, watch } from "vue"
export default {
setup() {
const selectedTheme = ref("watermelon")
const themeOptions = [
{ name: "Blueberry", value: "blueberry" },
{ name: "Watermelon", value: "watermelon" },
]
async function importTheme(theme) {
try {
await import(`../themes/${theme}/index.css`)
} catch (error) {
console.log(error)
}
}
watch(
selectedTheme,
async newValue => {
console.log("changing", newValue)
await importTheme(newValue)
},
{ immediate: true }
)
return { themeOptions, selectedTheme }
},
}
</script>
<style>
#app {
font-family: "Poppins", sans-serif;
}
</style>
What is happening right now
On the first switch -> The theme is switched from watermelon to blueberry -> component color changes from green to indigo.
On second switch and after -> nothing happens, component color does not change.
I'm not sure what's happening here. Can someone enlighten me or point me to the right direction?
What is supposed to happen
Switching works even after the first. Switch from green to indigo and then back to green.
selectedTheme, and then each theme defines styles like:.indigo .mydiv {}<div :class="selectedTheme">would be sufficient