-
I would like to say that I'm still quite new to Nuxt, Vue and javascript in general, so I hope to not offend anyone. Inspired by this little switch built-in vitepress While testing a simple ARadial everything worked fine: <template>
<div class="flex flex-wrap gap-4">
<ARadio v-model="colorMode.preference" value="system" label="System"/>
<ARadio v-model="colorMode.preference" value="light" label="Light"/>
<ARadio v-model="colorMode.preference" value="dark" label="Dark"/>
</div>
</template>
<script setup>
const colorMode = useColorMode()
</script> But for the ASwitch, maybe since it has only a boolean v-model, nothing worked: <template>
<div>
<ASwitch v-model="toggle"/>
</div>
</template>
<script setup>
const colorMode = useColorMode()
let toggle = ref(colorMode.value === "dark" ? true : false)
colorMode.preference = (toggle === true) ? "dark" : "light"
</script> As you can see I would like to read the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I'm realizing only now how much I still need to learn about javascript. While looking at others people code about similar tools I've been able to at least make the switch work with: <template>
<div>
<ASwitch v-model="themeToggle" />
</div>
</template>
<script>
export default {
data() {
return {
colorMode: useColorMode()
themeToggle: false,
}
},
watch: {
themeToggle(newValue) {
this.colorMode.preference = newValue ? 'dark' : 'light';
}
},
}
</script> But since I'm using Nuxt mainly for smaller boilerplates (and more battery-included) I've managed somehow to shrink it down like this: <template>
<div>
<ASwitch v-model="themeToggle" />
</div>
</template>
<script setup>
const colorMode = useColorMode()
const themeToggle = ref(false)
watch(themeToggle, (newValue) => {
colorMode.preference = newValue ? 'dark' : 'light'
})
</script> Now, I firstly need to figure out a way to give the default position of the switch from colorMode.value and then to prevent colorMode.preference to be set to dark while system is already in dark. |
Beta Was this translation helpful? Give feedback.
-
Ok looks like I was reinventing the wheel.
|
Beta Was this translation helpful? Give feedback.
Ok looks like I was reinventing the wheel.
Vue does provide a built in feature to bind a checkbox to specific string for when it's true or false, but ASwitch (nor ACheckbox) doesn't take advantage of it.