69

In my index.js file I have manually override the Vuetify theme object with my company's color:

Vue.use(Vuetify, {
  theme: {
    primary: '#377ef9',
    secondary: '#1b3e70',
    accent: '#ff643d',
    error: '#ff643d'
    ...
  }

Now, I can use these colors from my templates like so:

<my-text-field name="input text"
    label="text"
    value="text text text text..."
    type="text"
    color="primary">
</my-text-field>

What I'm after is using the primary or any other variable in the theme object defined above, inside my template style:

<script>
  import { VTextField } from 'vuetify'
  export default {
    extends: VTextField
  }
</script>

<style scoped lang="stylus">
  label
    color: <seconday color> <-- this is what I'm after
    color: #1b3e70 <-- this works, but not quite good enough for me
</style>

I can easily just write the hex value of my colors in the style section, but I don't want to repeat myself, and would rather use my theme object so it will also be easier for my to easily change the colors everywhere, and avoid typos which will lead to mistakes in the colors definitions.

Narxx
  • 7,929
  • 5
  • 26
  • 34
  • Not sure but I believe you need to define them in `.styl` (i.e. `main.styl`) as well. Or in e.g. `custom_colors.styl` which you then import inside components ` – Traxo Jan 16 '18 at 13:09
  • 2
    @Traxo I don't want to define my styles twice. Maintaining will be getting hard. Looking for a perfect solution where I can define my stuff in a single place, and use it throughout the whole system... – Narxx Jan 16 '18 at 13:32
  • I understand, that's why I put comment cuz I'm not 100% sure there is vuetify solution to this. I believe the only vuetify-way to do it is copy those values to `.styl` – Traxo Jan 16 '18 at 13:53
  • See important edit in my [answer](https://stackoverflow.com/a/48285278/1981247). – Traxo Oct 17 '18 at 05:22
  • 1
    I had the same problem and ended up using this.$vuetify.theme.currentTheme.accent in my component – Jaime Noel Alvarez Luna Nov 07 '19 at 20:51
  • See https://vuetifyjs.com/en/customization/theme/#theme, it should be theme: { themes ... – Roger Urscheler Jul 30 '20 at 13:10

7 Answers7

79

Edit (2018/10/11)

Since version 1.2. we can enable CSS variables
NOTE: allegedly it won't work in IE (Edge should work), and possibly some Safari versions?

From docs (see Custom Properties)

Enabling customProperties will also generate a css variable for each theme color, which you can then use in your components' blocks.

Vue.use(Vuetify, {
  options: {
    customProperties: true
  }
})

<style scoped>
  .something {
    color: var(--v-primary-base)
    background-color: var(--v-accent-lighten2)
  }
</style>

For custom values e.g.
yourcustomvariablename: '#607D8B'
use --v-yourcustomvariablename-base (so base is default).



Original answer:

There is a Feature Request on github: Access theme colors in stylus files

@KaelWD (one of devs) wrote:

This is something you'll have to implement yourself. I've tried doing something similar before but it doesn't really work on a framework level.

Issue is labeled wontfix


Edit (2018/10/11)
Also see this updated thread:
https://github.com/vuetifyjs/vuetify/issues/827 (Feature request: Native css variables)
Traxo
  • 18,464
  • 4
  • 75
  • 87
  • Yep, if you just need static colours you may be able to define them in a json file and import that into both sylus and js. To change them dynamically however we'd need to use css variables, which I'm kinda wary about implementing as we still need to support IE11. – Kael Watts-Deuchar Apr 16 '18 at 07:39
  • Not sure the v1.2 of vuetify does that with custom theme. `--v-yourcustomvariablename` won't work – RomOne Jan 02 '19 at 15:18
  • 1
    @RomOne can you try `--v-yourcustomvariablename-base`? – Traxo Jan 02 '19 at 16:33
  • 1
    hey thanks @Traxo! Does work indeed, but not with rgba colors :/ Thanks anyway! – RomOne Jan 03 '19 at 14:02
  • 1
    @RomOne You could as well pass integer representation of your RGB tho (e.g. `yourcustomvariablename: 0xff0000`, not sure why would you need opacity there in the value tho, because you can't modify it anyway. Just set the color you need. – Traxo Jan 03 '19 at 14:19
  • Please keep in mind that `customProperties: true` should be under `options`. For some reason my `customProperties: true` lived in the parent(caused the variables to not generate). Might be that is was generated before v1.2 by the cli. – Jeremy Walters Jan 29 '19 at 07:57
  • 1
    Note that in v2 onwards, set this `options` property in the `theme` property of your exported vuetify object in your `plugins/vuetify.js`: `export default new Vuetify({ theme: { options: { customProperties: true } } })` – naffarn Feb 04 '20 at 22:21
  • bang!. just what I'm looking for all this day. but mention that is a good idea to look at the official documentation for `Custom Properties` change will be made between versions. for it `{theme: {options: { customProperties: true, }}}` – pery mimon Mar 29 '20 at 18:07
26

There is a way to go around this by utilizing :style attributes. It can be used to set custom CSS properties reactively.

Add a computed property:

computed: {
    cssProps () {
        return {
            '--secondary-color': this.$vuetify.theme.secondary
        }
    }

Bind style to cssProps:

<div id="app" :style="cssProps">

Then, in your style:

<style scoped>
    label
        color: var(--secondary-color);
</style>

Adapted from this discussion: https://github.com/vuejs/vue/issues/7346

ggriffin
  • 322
  • 3
  • 9
Vic
  • 369
  • 1
  • 6
  • 13
  • 2
    I don't get this to work. The`var(--secondary-color)` doesn't get parsed. Perhaps the build is not set up properly? – Anima-t3d Jun 18 '18 at 08:51
  • 2
    Did you check out the codepen in the linked discussion? https://codepen.io/richardtallent/pen/yvpERW/ .I initially forgot to add the :style="cssProps" attribute when testing this. – qu1ckdry Jun 21 '18 at 13:34
  • 1
    for lodash fans here is a oneliner to export the whole theme in cssProps(): `_(this.$vuetify.theme).reduce((acc, v, k)=>{acc[\`--${k}\`]=v;return acc;},{});` – hariseldon78 Sep 12 '18 at 15:03
  • I don't understand this. How does `label` in the style block apply to the HTML/JS? – Brandon Dube Sep 19 '18 at 17:04
  • 2
    See important edit in [other answer](https://stackoverflow.com/a/48285278/1981247). No need for workarounds anymore (if you don't like older browsers). – Traxo Oct 22 '18 at 18:54
18

For anyone stumbling over this from Vuetify V2 onwards, you can do the following to get access to the SCSS colour variables.

// Import the Vuetify styles somewhere global
@import '~vuetify/src/styles/styles.sass';

// Now in your components you can access the colour variables using map-get
div {
  background: map-get($grey, lighten-4);
}

All the colours can be found in /node_modules/vuetify/styles/settings/_colors.scss.

Alex McCabe
  • 1,852
  • 2
  • 20
  • 33
  • 1
    Thanks for the tip but I think the idea was to not access the colours using names directly, but e.g. use the background colour set by the theme. At least that's what I read, and what I'm looking for ;-) – thoni56 Dec 30 '19 at 20:34
  • 1
    Yeah, any idea how to access theme colours? – Chris Watts Jan 09 '20 at 17:40
  • 1
    Thanks for this, exactly what I was looking for, not the theme but the actual colors – half of a glazier Feb 19 '20 at 10:48
  • @CJxD unfortunately I don't know I'm afraid, but I _think_ they're available as variables `background-color: $color-primary;` – Alex McCabe Feb 19 '20 at 13:01
  • It seems that `var(--v-primary-base)` works in SASS/SCSS too actually as it is a native CSS variable. You can also do things like `var(--v-primary-lighten4)` – Chris Watts Feb 21 '20 at 14:52
  • That entirely depends on your browser support requirements, but if it works for you that's fantastic! – Alex McCabe Feb 21 '20 at 21:41
11

For vutify 3+: inside vuetify.js file declare theme color variable colors:{green:'#00ff00'}

// src/plugins/vuetify.js
import { createApp } from 'vue'
import { createVuetify } from 'vuetify'

export default createVuetify({
  theme: {
    defaultTheme: 'myCustomTheme',
    themes: {
      myCustomTheme: {
        dark: false,
        colors: {
          ..., // We have omitted the standard color properties here to emphasize the custom one that we've added
          green: '#00ff00'
        }
      }
    }
  }
})

inside .vue component file use rgb(var(--v-theme-green)):

<template>
  <div class="custom-class">background color with appropriate text color contrast</div>
</template>

<style>
  .custom-class {
    background: rgb(var(--v-theme-green))
  }
</style>

Update: Now you can use color defined in myCustomTheme as background and text variant directly, just make the class name-

For background: <div class="bg-green"> For text: <span class="text-green">

RRR
  • 507
  • 4
  • 17
  • 1
    THANK YOU! Took me so long to figure out what I was doing wrong. I tried `--v-green-base`, which (IIRC) was the vuetify2 way... – Fred Feb 20 '23 at 13:01
6

From above answers, if you want to include all vuetify colors, put this code in App.vue template

<v-app :style="cssProps">

App.vue script

computed: {
   cssProps () {
      var themeColors = {}
      Object.keys(this.$vuetify.theme.themes.light).forEach((color) => {
        themeColors[`--v-${color}`] = this.$vuetify.theme.themes.light[color]
      })
      return themeColors
   }
}

Let say if you have this color in vuetify.js

export default new Vuetify({
  treeShake: true,
    theme: {
      themes: {
        light: {
          darkRed: "#CD3300",
        }
      }
    }
})

Then, in any component:

<style scoped>
  .label {
    color: var(--v-darkRed);
  }
</style>
wanjijul
  • 252
  • 3
  • 7
  • Interesting... So, you're placing all color definitions on the `style` attribute of the `` component... this is a nice hack actually :) – Narxx Dec 16 '20 at 13:54
3

Maybe I am late the most efficient way to do is as mentioned in the docs https://vuetifyjs.com/en/features/theme/#custom-properties

I will provide a working example for the same. you need only three changes to be done for this to get working.

  1. Mention the option which does the magic and your theme color
export default new Vuetify({
    theme: {
        options: {
            customProperties: true
        },
        themes: {
            light: {
                primary: "#3DCFD3",
                secondary: "#171b34",
                accent: "3D87E4"
            }
        }
    }
});
  1. Mention the class name in the tag where you want your theme to get applied
<h4 class="blue-header">Yash Oswal</h4>  
  1. CSS to apply your theme.
<style lang="scss">

.blue-header {
  color: var(--v-primary-base);
}

</style>
DaveL17
  • 1,673
  • 7
  • 24
  • 38
N3XT
  • 31
  • 2
  • this work great and doesn't need much engineering. However, it is worthy of note that you need to add the lang attribute as "scss" to the style element for this to work. Thanks once again man – Evidence Ekanem Jun 30 '23 at 09:16
2

Example of switching theme (helpfull link):

    <v-app :dark="setTheme" 
           :style="{background: $vuetify.theme.themes[theme].background}" 
           >

JS:

      computed: {
            setTheme() {
                this.$vuetify.theme.dark = this.goDark;
            }
      },
      data() {
            return { 
                goDark: false
            }
      }
Dmitry Kaltovich
  • 2,046
  • 19
  • 21