1

I'm using Vue 2 with Nuxt. How can I achieve that Vuetify puts everywhere, where the color is used a CSS Variable use var(--X)?
I want to dynamically change the colors of the Vue theme.

{
  theme: {
    themes: {
      light: {
        primary: 'var(--primaryColor)', // <== Put CSS Variables in here
        secondary: 'var(--secondaryColor)'
      },
    },
  },
}

When I'm doing this all the colors are just white #FFFFFF.

kissu
  • 40,416
  • 14
  • 65
  • 133
Lars Flieger
  • 2,421
  • 1
  • 12
  • 34

1 Answers1

1

You're doing this configuration in nuxt.config.js I guess? That file have no idea about any kind of window or CSS so far.

You would need to either import hardcoded CSS variables from elsewhere or write them there directly (since it's quite at the apex, it's totally fine). The #fff fallback is probably a fallback in case of undefined or alike.

As of directly changing the colors of Vuetify, here is the section regarding the customization, I quote

Under the hood, Vuetify will generate css classes based upon these values that will be accessible in the DOM. These classes will follow the same markup as other helper classes, primary or secondary--text for example. If you supply an entire color object (as in colors.purple above), the lighten/darken variations will be used directly instead of being generated.

These values will also be made available on the instance $vuetify object under the theme property. This allows you to dynamically modify your theme. Behind the scenes, Vuetify will regenerate and update your theme classes, seamlessly updating your application.

Updating those values could hence be done like that

// Light theme
this.$vuetify.theme.themes.light.primary = '#4caf50'

// Dark theme
this.$vuetify.theme.themes.dark.primary = '#4caf50'

PS: The great thing is that the lighten/darken variants will also be done for you.

kissu
  • 40,416
  • 14
  • 65
  • 133
  • Thanks for your answer. This is working when I set it in the mounted hook inside a `setTimeout` with 0 seconds. Do you know why the setTimeout is needed? Without it's not updating... https://github.com/DLesas/Dimitri-Lesas-.com/blob/b6ea0d05c45f018336478d159fe0d75a6c33b512/layouts/default.vue#L131 (example of use) – Lars Flieger Nov 09 '22 at 13:17
  • Or nested with watch here: https://github.com/nhtuan1609/deep-focus/blob/f89f67e813440a2728b1f90b478a6654129a3d18/layouts/default.vue#L21 – Lars Flieger Nov 09 '22 at 13:19
  • 1
    `setTimeout` is usually a bad approach overall. I'd say that you need to wait for your components to be properly mounted with something [like this](https://stackoverflow.com/a/55328457/8816585). [nuxtSeverInit](https://nuxtjs.org/docs/directory-structure/store#the-nuxtserverinit-action) is also a nice approach to do that ASAP. – kissu Nov 09 '22 at 13:35