Button to change color-mode not working
Here is my code:
The color mode doesn't change at all, it keeps to dark all the time
5 Replies
Hi @LocTran016. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Your approach to toggling the color mode is close, but there are a few issues:
1. You should update
colorMode.preference
directly based on the current value, not set it to 'light'
first.
2. You should use colorMode.value
for both reading and updating.
3. The $colorMode
property in the template is not standard; you should use the colorMode
variable you defined in your script.
Here’s a corrected version based on the official Nuxt UI and Nuxt Color Mode integration:
Key points:
- Use colorMode.value
in both your script and template.
- Do not use $colorMode
in the template unless you have explicitly provided it as a global property (which is not standard in Nuxt 3).
- The logic for cycling through the modes should not set 'light'
by default before checking the current value.
This approach matches the recommended usage for color mode switching in Nuxt UI and Nuxt Color Mode integrations Nuxt UI Color Mode Usage.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@845978022732759071> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
what does curColorMode log?
I haven't used color-mode, but shouldn't you be changing
.value
instead of .preference
?
@LocTran016
nvmIt seem that I remove the 'system' and things work just fine, kinda weird tbh