How to install a customizable Vue tooltip component to create help text and abbreviations

adamdehaven

Adam DeHaven

Posted on August 21, 2020

How to install a customizable Vue tooltip component to create help text and abbreviations

Tooltips are a great way to provide additional information when a user hovers over an element on a page. In this post, I'll walk you through installing the same tooltip component I wrote for my site via a custom Vue plugin that you can utilize in your own project!

The tooltip component

In Vue applications, it's common practice to define reusable components that can be repeated throughout your project. Since components are reusable Vue instances, they can be used again and again, as many times as you want, even on the same page. Each instance of the component will maintain its own, separate data bindings and attributes.

The reusable vue tooltip component we will be installing has two main use-cases that are both accomplished with the same <VueCustomTooltip> element:

  1. Dynamic tooltips for other structured elements on the page.
  2. Tooltips for providing additional information or abbreviations for text on the page.

Our tooltip component can be used in any project that utilizes Vue, including VuePress sites like this one! 🎉 Before diving into the code, let's take a look at a couple examples of how we can use the plugin.

Tooltips for other elements

The first example, a tooltip for other elements on the page, can be used to provide the user with additional information related to actions that can be taken for a given element.

For example, the button below shows a tooltip that indicates what clicking the button will do (the tooltip is sticky for demo purposes):

Vue customizable tooltip component with twitter button

Tooltips for text

Tooltips added to text within the page can be used to provide contextual information. These helpful tooltips can define acronyms or abbreviations, or even provide help text that corresponds to the word or phrase already being displayed.

For example, the underlined text below is an acronym that, when hovered, will display the definition for the abbreviated text (again, the tooltip is sticky for demo purposes).

Vue tooltip component with abbreviation help text

Now that we've taken a look at the different use-cases for the plugin, let's install it in our project.

Installation

# With npm
npm install @adamdehaven/vue-custom-tooltip

# or Yarn
yarn add @adamdehaven/vue-custom-tooltip
Enter fullscreen mode Exit fullscreen mode

Vue.js (Global Install - Recommended)

It is recommended to install the plugin in your Vue project's entry file. For projects created with @vue/cli, this is likely your main.js file where you are already importing Vue.

// main.js (or your Vue entry file)

// Import Vue... you're probably already doing this
import Vue from 'vue'

// Import the tooltip component before calling 'new Vue()'
import VueCustomTooltip from '@adamdehaven/vue-custom-tooltip'

// Install the plugin using ONE of the options below:
// --------------------------------------------------

// 1. Install with default options
Vue.use(VueCustomTooltip)

// ===== OR  =====

// 2. Install with custom options (defaults shown)
Vue.use(VueCustomTooltip, {
    name: 'VueCustomTooltip',
    color: '#fff',
    background: '#000',
    borderRadius: 12,
    fontWeight: 400,
})
Enter fullscreen mode Exit fullscreen mode

VuePress (Standalone Plugin - Recommended)

I have released a standalone VuePress plugin that wraps this component into an actual VuePress Plugin installable through the .vuepress/config.js or .vuepress/theme/index.js file. If you'd rather use the standalone plugin in your VuePress project, head over to the vuepress-plugin-custom-tooltip repository, or follow these simple install instructions instead:

# With npm
npm install @adamdehaven/vuepress-plugin-custom-tooltip

# or Yarn
yarn add @adamdehaven/vuepress-plugin-custom-tooltip
Enter fullscreen mode Exit fullscreen mode

You can use the plugin by installing it in your .vuepress/config.js file, as shown here:

// .vuepress/config.js
module.exports = {
    // Install the plugin using ONE of the options below:
    // --------------------------------------------------

    // 1. Install with default options
    plugins: [
        '@adamdehaven/vuepress-plugin-custom-tooltip',
    ],

    // ===== OR  =====

    // 2. Install with custom options (defaults shown)
    plugins: [
        ['@adamdehaven/vuepress-plugin-custom-tooltip', {
            name: 'VueCustomTooltip',
            color: '#fff',
            background: '#000',
            borderRadius: 12,
            fontWeight: 400,
        }],
    ],
}
Enter fullscreen mode Exit fullscreen mode

Alternatively, if you are using a theme, you may install in your .vuepress/theme/index.js file:

// .vuepress/theme/index.js
module.exports = (options, ctx) => {
    return {
        // Install the plugin using ONE of the options below:
        // --------------------------------------------------

        // 1. Install with default options
        plugins: [
            '@adamdehaven/vuepress-plugin-custom-tooltip',
        ],

        // ===== OR  =====

        // 2. Install with custom options (defaults shown)
        plugins: [
            ['@adamdehaven/vuepress-plugin-custom-tooltip', {
                name: 'VueCustomTooltip',
                color: '#fff',
                background: '#000',
                borderRadius: 12,
                fontWeight: 400,
            }],
        ],
    }
}
Enter fullscreen mode Exit fullscreen mode

VuePress (Global Install)

For VuePress projects, the theme/enhanceApp.js is a good location to initialize plugins.

// theme/enhanceApp.js

// Import Vue... you're probably already doing this
import Vue from 'vue'

// Import the tooltip component
import VueCustomTooltip from '@adamdehaven/vue-custom-tooltip'

export default ({
    Vue, // the version of Vue being used in the VuePress app
    options, // the options for the root Vue instance
    router, // the router instance for the app
    siteData, // site metadata
    isServer, // is this enhancement applied in server-rendering or client
}) => {
    // ...apply enhancements to the app

    // Install the plugin using ONE of the options below:
    // --------------------------------------------------

    // 1. Install with default options
    Vue.use(VueCustomTooltip)

    // ===== OR  =====

    // 2. Install with custom options (defaults shown)
    Vue.use(VueCustomTooltip, {
        name: 'VueCustomTooltip',
        color: '#fff',
        background: '#000',
        borderRadius: 12,
        fontWeight: 400,
    })
}
Enter fullscreen mode Exit fullscreen mode

In-Component Install

Alternatively, you may install the component directly within a single file in your project; however, you will not be able to customize the Vue.use() options.

<!-- Single file component -->

<script>
// Import the tooltip component (no options available)
import VueCustomTooltip from '@adamdehaven/vue-custom-tooltip'

// .vue file default export
export default {
    // Register the component
    components: {
        VueCustomTooltip,
    },
}
</script>
Enter fullscreen mode Exit fullscreen mode

NOTE: Installing inside a single component (instead of globally) does not allow you to customize the Plugin Options; however, you may still utilize all props on the <VueCustomTooltip> element.

CDN

Import the tooltip component after importing Vue in your file after importing Vue. Installing via CDN does not allow for customizing Plugin Options.

Installing via the CDN requires using the kebob-case component name.

<!-- Import Vue -->
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<!-- Import tooltip component -->
<script src="https://unpkg.com/@adamdehaven/vue-custom-tooltip"></script>

<!-- Then simply use the component -->
<p>This is a <vue-custom-tooltip label="Neat!" underlined>tooltip</vue-custom-tooltip>.</p>
Enter fullscreen mode Exit fullscreen mode

Manual

Download dist/vue-custom-tooltip.min.js and include it in your file after importing Vue. Installing manually does not allow for customizing Plugin Options.

Installing manually requires using the kebob-case component name.

<!-- Import Vue -->
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<!-- Import tooltip component -->
<script src="/path/to/vue-custom-tooltip.min.js"></script>

<!-- Then simply use the component -->
<p>This is a <vue-custom-tooltip label="Neat!" underlined>tooltip</vue-custom-tooltip>.</p>
Enter fullscreen mode Exit fullscreen mode

Usage

As we already went over, there are several different ways to use the component once installed. Here are a few examples:

<!-- Basic -->
What is <VueCustomTooltip label="This is a tooltip">a tooltip</VueCustomTooltip>?

<!-- With Options -->
What is <VueCustomTooltip label="This is a tooltip" position="is-bottom" abbreviation sticky>a tooltip</VueCustomTooltip>?

<!-- With element(s) -->
<VueCustomTooltip label="View @adamdehaven on Twitter">
    <a class="button" href="https://twitter.com/adamdehaven">
        <span class="icon icon-twitter"></span>
        <span>Twitter</span>
    </a>
</VueCustomTooltip>
Enter fullscreen mode Exit fullscreen mode

Options

Pass any of the options listed below to Vue.use(VueCustomTooltip, {...}) to customize the plugin for your project (not available with in-component installation).

A note on options tied to CSS properties: The color, background, borderRadius, and fontWeight attributes listed below are set on the psuedo element using CSS Variables (Custom Properties), meaning they will fallback to their default values in unsupported browsers (e.g. Internet Explorer).

name

  • Type: String
  • Default: VueCustomTooltip

Customize the name of the component you will use in your project. Camel-case names are preferred, as this allows for camel-case or kebob-case usage within your project.

Vue.use(VueCustomTooltip, {
    name: 'SuperCoolTooltip', // camel-case preferred
})
Enter fullscreen mode Exit fullscreen mode

If you registered the name using camel-case, you can reference the tooltip component via camel-case or kebob-case:

<!-- Default name (user did not pass the 'name' option) -->

<!-- camel-case -->
Nice <VueCustomTooltip label="Neat!">tooltip</VueCustomTooltip>!
<!-- kebob-case -->
Nice <vue-custom-tooltip label="Neat!">tooltip</vue-custom-tooltip>!

<!-- Custom name (allows user to rename component) -->

<!-- camel-case -->
Nice <SuperCoolTooltip label="Neat!">tooltip</SuperCoolTooltip>!
<!-- kebob-case -->
Nice <super-cool-tooltip label="Neat!">tooltip</super-cool-tooltip>!
Enter fullscreen mode Exit fullscreen mode

color

  • Type: HEX Color
  • Default: #fff

Customize the color of the text displayed in the tooltip.

Vue.use(VueCustomTooltip, {
    color: '#c1403d', // 3 or 6 digit HEX color, including a leading hash (#)
})
Enter fullscreen mode Exit fullscreen mode

background

  • Type: HEX Color
  • Default: #000

Customize the background color (and the underlined text color) of the tooltip.

Vue.use(VueCustomTooltip, {
    background: '#1b2735', // 3 or 6 digit HEX color, including a leading hash (#)
})
Enter fullscreen mode Exit fullscreen mode

borderRadius

  • Type: Number
  • Default: 12

Customize the border-radius of the tooltip. Must be an integer.

Vue.use(VueCustomTooltip, {
    borderRadius: 4,
})
Enter fullscreen mode Exit fullscreen mode

fontWeight

  • Type: Number
  • Default: 400

Customize the font-weight of the tooltip text. Must be an integer that is a multiple of 100, between 100 - 900.

Vue.use(VueCustomTooltip, {
    fontWeight: 700,
})
Enter fullscreen mode Exit fullscreen mode

Props

In addition to the Plugin Options above, you may also pass props to the component itself to customize both the look and behavior of the tooltip element.

Props that accept a Boolean value may be passed simply by adding the attribute to the component tag, if a true value is desired. See the sticky example here:

<VueCustomTooltip label="Tooltip" sticky>text/element</VueCustomTooltip>
Enter fullscreen mode Exit fullscreen mode

All other props may be passed as normal attributes (if the corresponding value is a String, like the label prop, shown above) or with v-bind directives, as shown here:

<VueCustomTooltip :label="element.helpText" :sticky="false">text/element</VueCustomTooltip>
Enter fullscreen mode Exit fullscreen mode

All available props for the tooltip component are listed below:

label

  • Type: String
  • Default: null

The text that will display inside the tooltip. If the value for label is null, the tooltip will not be displayed.

You may not pass HTML to the label prop.

active

  • Type: Boolean
  • Default: true

Determines whether the tooltip should display when hovered, or if the sticky prop is present, if the tooltip should be visible.

position

  • Type: String
  • Value: is-top / is-bottom / is-left / is-right
  • Default: is-top

The position of the tooltip in relation to the text/element it is surrounding.

abbreviation

  • Type: Boolean
  • Default: false

Swaps out the component's standard <span> element with a semantically-correct <abbr> element, and sets the underlined prop to true. This is useful when adding a tooltip to text within a page's content where you want to provide additional context to a word or phrase, or provide a definition of a word or acronym.

VuePress pages are served as an <VueCustomTooltip label="Single Page Application" abbreviation>SPA</VueCustomTooltip>.
Enter fullscreen mode Exit fullscreen mode

sticky

  • Type: Boolean
  • Default: false

Determines if the tooltip should always be displayed (including on component load/mounting), regardless of the element being hovered.

underlined

  • Type: Boolean
  • Default: false

Add a dotted border under the contained text (the same color as the background HEX value). This value is automatically set to true if the abbreviation prop is set to true.

multiline

  • Type: Boolean
  • Default: false

Allows the tooltip text to wrap to multiple lines as needed. Can be used in conjunction with the size prop to adjust the width of the tooltip.

size

  • Type: String
  • Value: is-small / is-medium / is-large
  • Default: is-medium

The width of the tooltip, if the multiline prop is set to true.

Adding Custom Classes & Styles

Just like any other Vue component, you can add classes or styles directly to the component tag that will be applied to the rendered <span> tag (or <abbr> tag, if abbreviation is set to true).

<!-- Tooltip component with custom classes and styles -->
<VueCustomTooltip class="your-class" :class="{ 'dynamic-class': isDynamic }" :style="{ display: 'inline' }" label="Neat">text</VueCustomTooltip>
Enter fullscreen mode Exit fullscreen mode

This is extremely helpful if you want to extend functionality or tooltip styles within your project, which allows you to tweak things like the display behavior of the tooltip element.

The tooltip component is rendered as a display: inline-block element by default; however, you can override this by binding styles directly to the component, as shown above.

How easy was that?!

Installing the custom Vue tooltip plugin is an easy way to provide a global component for your Vue project.

Tell me what you think below in the comments, and let me know if you're using the plugin!

💖 💪 🙅 🚩
adamdehaven
Adam DeHaven

Posted on August 21, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related