Search code examples
vue.jsvuetify.jstooltipv-tooltip

Make tooltip appear clear icon in v-text-field using v-tooltip (vuetify)


I'm interested in creating a tooltip for the clear icon within the v-text-field. Could you please provide guidance on how to achieve this?

          <v-text-field
            ref="newSearchField"
            v-model="text"
            class="search-input"
            :class="{
              'rounded-r-lg': $vuetify.breakpoint.smAndDown,
              'rounded-l-lg': $vuetify.breakpoint.xsOnly
            }"
            type="text"
            solo
            :label="label"
            :disabled="disabled"
            :placeholder="label"
            persistent-placeholder
            autofocus
            data-test="search-input"
            hide-details
            clearable
            autocomplete="off"
            @keyup="nextItem"
            @click:clear="
              clear()
              $nuxt.$data.layoutName === 'home' ? (text = '') : $emit('clear')
            "
            @input="autocomplete"
            @keydown="blured = false"
            @focus="blured = false"
          >

Solution

  • Pass the class of the clear icon (.v-field__clearable) to the :activator prop of v-tooltip:

    <v-tooltip activator=".v-field__clearable">clear input</v-tooltip>
    

    Make sure that the selector references a single element, for example by using a class on the v-text-field (in your example, you could do .search-input .v-field__clearable).

    See how it works in the snippet:

    const { createApp, ref } = Vue;
    const { createVuetify } = Vuetify
    const vuetify = createVuetify()
    const app = {
      template: `
          <v-app>
            <v-main>
              <v-text-field
                v-model="msg"
                class="search-input"
                clearable
              />
              <v-tooltip activator=".search-input .v-field__clearable">
                  clear it
              </v-tooltip>
            </v-main>
          </v-app>
      `,
      setup(){
        return {
          msg: ref('hello world')
        }
      }
    
    }
    createApp(app).use(vuetify).mount('#app')
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/vuetify@3/dist/vuetify.min.css" />
    <link href="https://cdn.jsdelivr.net/npm/@mdi/font/css/materialdesignicons.min.css" rel="stylesheet">
    <div id="app"></div>
    <script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vuetify@3/dist/vuetify.min.js"></script>