Chris
Chris

Reputation: 4446

How to disable Vuetify button without changing colors

I'm using Vuetify's v-btn button component with a variety of colors set via the color prop. Once a user clicks the button, I set disabled to true so they can't click it again, but the button loses its color and gets greyed out.

Is there any way to disable the button without changing its color to grey?

Upvotes: 24

Views: 38619

Answers (5)

jTiKey
jTiKey

Reputation: 743

For vuetify 3:

.v-btn__overlay
   opacity: 0!important

Upvotes: 0

Toodoo
Toodoo

Reputation: 8760

I do it by removing v-btn--disabled and playing with vuetify's css classes.


Still grey but with colored text solution

The button will still be grey, but text will be colored, like that you have a visual effect showing that the button is disabled but still have a colored part.

I, personally, also had some custom opacity to disabled buttons.

<v-btn id="btnA" :disabled="true" color="success">Success</v-btn>
button.v-btn[disabled] {
  opacity: 0.6;
}
created(){
    // Trick to remove class after initialising form
    this.$nextTick(() => {
        document.getElementById('btnA').classList.remove('v-btn--disabled')      
    })
}

CodePen


Same display solution

If you really want, the same display you will have to remove [color]--text and add [color] class (and sometimes add white--text class for readability).

created(){
    // Trick to remove class after initialising form
    this.$nextTick(() => {
        document.getElementById('btnA').classList.remove('v-btn--disabled')
        document.getElementById('btnA').classList.remove('success--text')
        document.getElementById('btnA').classList.add('success')
    })
}

CodePen

Upvotes: 6

Dekatron
Dekatron

Reputation: 41

Okay so you can do it by disabling the pointer events as mentioned in other comments but if someone is using a keyboard they can still tab to the control and if you are writing automated tests the button can still be clicked.

You can manually override the style and change the disabled button colour in the css however this will potentially be a problem if you are manually setting the color through the color="" property on v-btn based off a theme (because your application supports branding for different clients for example) because Vuetify doesn't just override the color, it stops adding the color altogether.

So my solution was to simply set the button color via a style attribute and set the important flag (to override the disabled important flag) note that you will need to change the text color as well.

<v-btn
    :style="{
        color: `${getTxtColor()} !important`,
        backgroundColor: `${getBtnColor()} !important`
    }"
    :disabled="status"
    @click="doSomething"
>
  Click Here
</v-btn>

This approach should play nice with testing, themeing, and will not allow users to tab to the button accidentally.

Upvotes: 2

Hexodus
Hexodus

Reputation: 12955

As Vuetify allready use important! in .v-btn--disabled it's not possible to just override this class. But with the use of a higher level selector like id (example: #custom-disabled which selects id="custom-disabled") you can. This doesen't keep the original colors but you are at least able to override the class to your liking.

<v-btn :disabled="true" id="custom-disabled">
    Button
</v-btn>

<style>
#custom-disabled.v-btn--disabled {
    background-color: red !important;
}
</style>

For light and dark theme:

<style>
#custom-disabled.v-btn--disabled.theme--light {
    background-color: red !important;
}
#custom-disabled.v-btn--disabled.theme--dark {
    background-color: brown !important;
}
</style>

Upvotes: 2

Traxo
Traxo

Reputation: 19022

Instead of disabled prop you could use your custom class with pointer-events: none, e.g.

.disable-events {
  pointer-events: none
}

<v-btn :class="{'disable-events': customCondition}">

Then add additional styling to that class if needed.

Upvotes: 43

Related Questions