Creating a Custom Tooltip Component in Vue
There are plenty of libraries out there that will have you up and running with a good tooltip solution in minutes. However, if you are like me, you are sick and tired of giant dependency trees that have the distinct possibility of breaking at any time. For that reason, we’re going to build a custom single file tooltip component that you can build yourself and tweak to your heart’s content. It might take 15 minutes instead of 3… sorry about that.
As it happens, this is also the boilerplate for the tooltip component we use on boot.dev’s coding app.
The End Goal
We are building a single file component, as such it will be a single file with the following structure:
<template> </template>
<script></script>
<style scoped></style>
At the end of this walkthrough we will have a tooltip component that floats above the target element(s), fades in and out, activates on hover, and is reusable across our entire app. Let’s take each section at a time.
The HTML
<template>
<div class="tooltip-box">
<slot />
<div class="tooltip">
<span class="text">{{ text }}</span>
</div>
</div>
</template>
Fairly simple setup here. We need:
- An outer
tooltip-boxto encapsulate the entire component and ensure positioning. - A slot tag to inject the child component (whatever is hovered over to show the tooltip).
- A
spanthat will house the text of our tooltip.
The JavaScript
export default {
props: {
text: {
type: String,
required: true,
},
},
};
Pretty straightforward, all we need is a required prop for our users to specify what they want the tooltip to say.
The CSS
.tooltip-box {
position: relative;
display: inline-block;
}
.tooltip-box:hover .tooltip {
opacity: 1;
}
.tooltip {
color: #ffffff;
text-align: center;
padding: 5px 0;
border-radius: 2px;
width: 120px;
bottom: 100%;
left: 50%;
margin-left: -60px;
opacity: 0;
transition: opacity 1s;
position: absolute;
z-index: 1;
background: #a782e8;
}
.text::after {
content: " ";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: #a782e8 transparent transparent transparent;
}
The CSS is certainly the most interesting part. A couple key points:
- We need to ensure the top-level element,
tooltip-boxis positioned relatively so that our absolute positioning works. - We use an opacity transition to fade the tooltip in and out of view.
- The
z-indexproperty ensures that the tooltip is on top of whatever it needs to hover over. - The
.text::afterproperty creates the little triangle pointer at the bottom of the tooltip
The full component:
<template>
<div class="tooltip-box">
<slot />
<div class="tooltip">
<span class="text">{{ text }}</span>
</div>
</div>
</template>
<script>
export default {
props: {
text: {
type: String,
required: true,
},
},
};
</script>
<style scoped>
.tooltip-box {
position: relative;
display: inline-block;
}
.tooltip-box:hover .tooltip {
opacity: 1;
}
.tooltip {
color: #ffffff;
text-align: center;
padding: 5px 0;
border-radius: 2px;
width: 120px;
bottom: 100%;
left: 50%;
margin-left: -60px;
opacity: 0;
transition: opacity 1s;
position: absolute;
z-index: 1;
background: #a782e8;
}
.text::after {
content: " ";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: #a782e8 transparent transparent transparent;
}
</style>
And how to use it:
<Tooltip text="Difficulty">
<span> hover over me </span>
</Tooltip>
Related Articles
How to Create a Custom Toggle Switch Component in Vue.js
Jul 21, 2020 by Lane Wagner - Boot.dev co-founder and backend engineer
Custom toggle switches are a pain to code from scratch. So many lines for such a simple UI widget! In this quick tutorial, we will learn how to build a fully encapsulated toggle switch component in Vue.js. The component we’re building is used currently on boot.dev’s login page. Go take a look to see a live demo.
Vue History Mode - Support Legacy Hash URLs
Jul 15, 2020 by Lane Wagner - Boot.dev co-founder and backend engineer
When we first launched the boot.dev’s single-page-app, we were using Vue Router’s default hash routing. Hash routing looks ugly to the end-user, and when you want to be able to share parts of your app via direct link those hashes can get really annoying.
How to Rerender a Vue Route When Path Parameters Change
Jul 07, 2020 by Lane Wagner - Boot.dev co-founder and backend engineer
In single-page apps that use the Vue Router, it’s common to create a path parameter that changes the behavior of a route. Often a problem occurs however when a user alters the path manually in the address bar. Manually changing the URL does not rerender the view! This can cause unexpected behavior because mounted() hooks don’t fire and nested components don’t reload.
Simple Setup - Vue Linting in VS Code
Jun 17, 2020 by Lane Wagner - Boot.dev co-founder and backend engineer
I’m a gopher by nature, so I expect consistent styling and linting in my codebases. More importantly, I don’t like to think about styling. I like to type haphazardly and then have my editor apply styles automatically on save (ctrl+s, cmd+s). If you are the same way, hopefully, this will help you in your next Vue.js project.