'Vuetify - Show tooltip on click
I have button that trigger a tooltip when my user clicked Copy
<v-btn @click="showCopiedText = !showCopiedText; copyHtmlText()">Copy</v-btn>
<v-tooltip v-model="showCopiedText" right>
<span>Copied!</span>
</v-tooltip>
It works but my tool tip kept showing the tip left of my windows instead of next to my button.
Solution 1:[1]
The v-btn should be inside the v-tooltip's activator slot, so that the component can position itself around the contents:
Move the
v-btninto thev-tooltip'sactivatorslot.In the
v-btn's click-handler, use theslot'sonproperty to simulate amouseenterto show the tooltip; andmouseleaveafter a timeout to hide it. Make sure to clear the timeout inbeforeDestroyin case the component is unmounted before the timeout.
<v-tooltip bottom>
<template v-slot:activator="{ on, attrs }">
1??
<v-btn v-bind="attrs" @click="toClick(on, $event)">Copy</v-btn>
</template>
<span>Copied</span>
</v-tooltip>
export default {
beforeDestroy() {
clearTimeout(this._timerId)
},
methods: {
2??
toClick({ mouseenter, mouseleave }, e) {
clearTimeout(this._timerId)
mouseenter(e)
this._timerId = setTimeout(() => mouseleave(e), 1000)
}
}
}
Alternatively, you can use a v-model on the v-tooltip to show/hide the tooltip instead of the mouseenter/mouseleave simulations:
Declare a
showBoolean prop, and use the prop as thev-tooltip'sv-model.In the
v-btn's click-handler, set the prop totrue, and use a timeout to reset it tofalse. Make sure to clear the timeout inbeforeDestroyin case the component is unmounted before the timeout.
<v-tooltip bottom v-model="show"> 1??
<template v-slot:activator="{ attrs }">
<v-btn v-bind="attrs" @click="showTooltip">Copy</v-btn>
</template>
<span>Copied</span>
</v-tooltip>
export default {
data() {
return {
show: false, 1??
}
},
beforeDestroy() {
clearTimeout(this._timerId)
},
methods: {
showTooltip() {
2??
this.show = true;
this._timerId = setTimeout(() => this.show = false, 1000)
}
}
}
Solution 2:[2]
From VueJS docs a tooltip still needs to be positioned. In this example the tooltip is inside a vueJS grid and is positioned correctly. Hopefully it helps.
Solution 3:[3]
Here is working demo. Try this:
new Vue({
el: '#app',
vuetify: new Vuetify(),
data(){
return {
isShowTooltip: false
}
},
methods:{
hideToolTip(){
setTimeout(()=>this.isShowTooltip = false, 2000);
}
}
})
<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
<script src="https://unpkg.com/[email protected]/dist/vuetify.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/vuetify.min.css"/>
<div id="app">
<v-app id="inspire">
<v-form>
<v-container>
<v-row>
<v-col cols="4" sm="6" class="pl-0">
<v-tooltip
v-model="isShowTooltip"
open-on-click
:open-on-hover="false"
close-delay='3000'
right
>
<template v-slot:activator="{ on, attrs }">
<v-btn
large outlined color="indigo"
v-bind="attrs"
v-on="on"
@click="hideToolTip"
>
Copy
</v-btn>
</template>
<span>Copied</span>
</v-tooltip>
</v-col>
</v-row>
</v-container>
</v-form>
</v-app>
</div>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | aglamadrid19 |
| Solution 3 | Nabil Ahmad |


