Reputation: 3247
I wanted to trigger Vuetify tooltip VTooltip
only when the activator is clicked rather than hovered. I tried to bind it with a variable but still triggered on hover.
methods: {
doCopy(){
// copy logic
this.showCopied = true;
setTimeout(() => {
this.showCopied = false
}, 1000)
}
}
<VTooltip v-model="showCopied">
<template #activator="{ on }">
<VBtn v-on="on" @click="doCopy"> COPY </VBtn>
</template>
</VTooltip>
Upvotes: 7
Views: 6925
Reputation: 403
In vuetify 2.6.1 you can do it like this now
<v-tooltip
open-on-click
:open-on-hover="false"
>
<template
v-slot:activator="{ on }"
>
<v-btn
v-on="on"
>
button with tooltip
</v-btn>
</template>
<span>tooltip message</span>
</v-tooltip>
Upvotes: 1
Reputation: 3247
It turns out I have to disable the default event handler of the activator.
Simply removing default event object (on)
binding solves the issue.
<VTooltip v-model="showCopied">
<template #activator={}>
<VBtn @click="doCopy"> COPY </VBtn>
</template>
</VTooltip>
[UPDATED] based on @Kael Watts-Deuchar answer NB: the v-model biding is mandatory
Upvotes: 4
Reputation: 4481
This is actually more complicated than I expected thanks to some bugs. You should be able to just do <v-tooltip :open-on-hover="false">
, but a focus listener is still added which causes the click to close the tooltip immediately. Instead you need to bind the click and blur events separately, and add retain-focus-on-click
to the button so it doesn't blur immediately.
Full solution:
<v-tooltip bottom :open-on-hover="false">
<template #activator="{ on }">
<v-btn @click="on.click" @blur="on.blur" retain-focus-on-click>Copy</v-btn>
</template>
<span>Copy</span>
</v-tooltip>
Upvotes: 12