Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using custom theming in Vuetify and pass color variables to components

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.

like image 531
Narxx Avatar asked Jan 16 '18 12:01

Narxx


People also ask

How do you add your own custom colors to the Vuetify color palette?

Custom theme variants While Vuetify automatically generates lighten and darken variants for theme colors, you may want to control this yourself. Simply pass a theme object that contains the variants that you wish to modify. Anything not provided will still be generated for you.

How do I add color to Vuetify?

By picking a color group (yellow, red, blue, and so on) and combining it with one of the modifying classes ( lighten-{n} , darken-{n} , accent-{n} ) you can get hundreds of colors to add to your Vue app and reflect your brand or style.

How do I use SASS variables in Vuetify?

Vue CLI install Once installed, create a folder called sass , scss or styles in your src directory with a file named variables. scss or variables. sass . The vuetify-loader will automatically bootstrap your variables into Vue CLI's compilation process, overwriting the framework defaults.


1 Answers

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)
like image 162
Traxo Avatar answered Sep 18 '22 07:09

Traxo