Colors not fading when using @nuxtjs/color-mode
Hey, I am trying to implement a dark/light mode system using the
@nuxtjs/color-mode package, but for some reason, the background color is not playing a fading animation when switching color. It however, plays the fade when reloading the page, just not when I manually change it. Furthermore, I have pretty much copied the code of the sandbox showcase (where the fade works), so I have no idea what is going on. (Just to clarify, when I switch colors, it does switch the colors, but it does so without fading)
app.vue:
nuxt.config.ts:
app/pages/index.vue:
GitHub
color-mode/playground at main · nuxt-modules/color-mode
Dark and Light mode with auto detection made easy with Nuxt 🌗 - nuxt-modules/color-mode
5 Replies
Hi @0Julian0. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
The issue you're experiencing—where the background color does not fade when switching color modes with @nuxtjs/color-mode, but does fade on page reload—is likely due to how CSS transitions interact with class changes on the <body> element.
From your code, you have:
However, when @nuxtjs/color-mode toggles between modes, it adds or removes classes like .dark or .sepia on the <html> or <body> element. The transition on background-color should work, but sometimes, if the class is toggled on <html> instead of <body>, or if the CSS selector specificity doesn't match, the transition may not trigger as expected.
What to check and try:
1. Ensure the transition is on the correct element:
If @nuxtjs/color-mode is toggling the class on <html> (which is the default), your transition should be on html, not body: 2. Check for specificity issues:
If both html and body have background-color set, only the more specific one will apply. Make sure only one is setting it, or that your transition is on the element actually being updated. 3. Nuxt UI and color mode integration:
Nuxt UI automatically registers @nuxtjs/color-mode and uses CSS variables for theming. If you use Nuxt UI's design tokens (e.g., --ui-bg), you can leverage their system for smooth transitions and dark mode support. Example from the docs: Nuxt UI CSS Variables 4. Transition on CSS variables:
If you use CSS variables for background color, the transition should still work as long as the variable changes and the property is transitioned. 5. No evidence of built-in fade for color mode in Nuxt UI:
The knowledge sources do not mention any built-in fade animation for color mode changes in @nuxtjs/color-mode or Nuxt UI. The fade effect relies on your CSS transition. Summary:
Make sure the transition is applied to the element that actually receives the class change (.dark, .sepia, etc.). If @nuxtjs/color-mode toggles the class on <html>, move your transition to html. If you use Nuxt UI's variables, ensure your transition is on the property using those variables. There is no special built-in fade for color mode changes; it is handled by your CSS transition. If after these adjustments the fade still does not work, the knowledge sources do not provide further troubleshooting steps for this specific issue. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
If @nuxtjs/color-mode is toggling the class on <html> (which is the default), your transition should be on html, not body: 2. Check for specificity issues:
If both html and body have background-color set, only the more specific one will apply. Make sure only one is setting it, or that your transition is on the element actually being updated. 3. Nuxt UI and color mode integration:
Nuxt UI automatically registers @nuxtjs/color-mode and uses CSS variables for theming. If you use Nuxt UI's design tokens (e.g., --ui-bg), you can leverage their system for smooth transitions and dark mode support. Example from the docs: Nuxt UI CSS Variables 4. Transition on CSS variables:
If you use CSS variables for background color, the transition should still work as long as the variable changes and the property is transitioned. 5. No evidence of built-in fade for color mode in Nuxt UI:
The knowledge sources do not mention any built-in fade animation for color mode changes in @nuxtjs/color-mode or Nuxt UI. The fade effect relies on your CSS transition. Summary:
Make sure the transition is applied to the element that actually receives the class change (.dark, .sepia, etc.). If @nuxtjs/color-mode toggles the class on <html>, move your transition to html. If you use Nuxt UI's variables, ensure your transition is on the property using those variables. There is no special built-in fade for color mode changes; it is handled by your CSS transition. If after these adjustments the fade still does not work, the knowledge sources do not provide further troubleshooting steps for this specific issue. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
<@438416989422354434> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@kapa.ai ColorPicker.vue:
Let me check the knowledge base for you again. One moment! :books:
----------------------
Your
Your component follows the recommended practices for interacting with Nuxt's color mode system. The logic for setting and displaying the color mode is correct according to the official documentation. If you encounter any issues, ensure that your icon component is properly set up and that your CSS variables are defined for all color modes you support. If you have a specific issue or error with this component, please provide more details so I can assist further. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
ColorPicker.vue component is a custom theme switcher for Nuxt's color mode system. It uses $colorMode to read and set the user's color mode preference, and displays icons for each theme option. Here’s a review based on the knowledge sources:
Key Points:
- The approach of using $colorMode.preference = theme to set the color mode is correct and matches the usage described in the Nuxt UI and @nuxtjs/color-mode documentation. The $colorMode object exposes .preference and .value for reading and setting the current and preferred color mode, respectively Nuxt UI Color Mode Usage.
- The use of a computed icon name for each theme is a standard pattern and aligns with how Nuxt UI allows for custom icons in color mode controls ColorModeButton.
- The UI logic for highlighting the preferred and selected theme is clear and matches the expected behavior for a theme switcher.
- The <ColorScheme> component is used to display the current preference and detected/forced mode, which is a recommended way to show color mode state in Nuxt Nuxt UI Color Mode Usage.
What the knowledge sources do not cover:
- The sources do not provide a direct example of a custom theme switcher exactly like yours, but your implementation is consistent with the documented API and usage patterns.
- There is no mention of a built-in Icon component in Nuxt UI; ensure you are using a compatible icon component (such as one from Nuxt UI or Iconify, which Nuxt UI supports Nuxt UI Icons).
Summary:Your component follows the recommended practices for interacting with Nuxt's color mode system. The logic for setting and displaying the color mode is correct according to the official documentation. If you encounter any issues, ensure that your icon component is properly set up and that your CSS variables are defined for all color modes you support. If you have a specific issue or error with this component, please provide more details so I can assist further. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
<@438416989422354434> Kapa.ai is still learning and improving, please let me know how I did by reacting below.