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.
CodePudding user response:
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-btn
into thev-tooltip
'sactivator
slot.In the
v-btn
's click-handler, use theslot
'son
property to simulate amouseenter
to show the tooltip; andmouseleave
after a timeout to hide it. Make sure to clear the timeout inbeforeDestroy
in 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
show
Boolean 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 inbeforeDestroy
in 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)
}
}
}
CodePudding user response:
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.