JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script>
<div class="main-content-area">
<my-custom-button component-type="my-custom-button" ></my-custom-button>
<my-custom-button component-type="my-custom-button"></my-custom-button>
<my-custom-button component-type="my-custom-button"></my-custom-button>
</div>
JavaScript
Vue.component("my-custom-button",{
template : '<div class="btn" :style="buttonStyles" @click="activeThisButton" >Click me!</div>',
data(){
return {
isActive : false,
}
},
computed : {
buttonStyles(){
return {
backgroundColor : this.isActive ? 'green' : '',
}
}
},
methods : {
activeThisButton(){
// call inactiveAllButtons on parent to deselect all buttons
this.$root.inactiveAllButtons();
// active current button
this.isActive = true;
}
}
})
const Example = new Vue
({
el: '.main-content-area',
methods : {
// filter children and find buttons ( by component-type property )
// and inactive all .
inactiveAllButtons(){
var buttons = this.$children.filter(function(child) {
return child.$attrs['component-type'] === 'my-custom-button';
});
for(var i = 0 ; i < buttons.length ; i++ ){
buttons[i].isActive = false;
}
}
}
});