Giter VIP home page Giter VIP logo

v-tooltip's Introduction

v-tooltip

Vue 3 tooltip custom directive

Table of contents

Installation

Paste tooltip.js in your src folder, I personally create folder directives inside for the sake of clarity. Next step is to paste tooltip.css inside your assets folder.

When you have all the files, open your main.js file (or whatever it is called, the one where you mount your app) and import those files. Then use app.directive to globally add v-tooltip custom directive. It should look similar to that:

import { createApp } from "vue";
import App from "./App.vue";
import tooltip from "./directives/tooltip.js";
import "@/assets/tooltip.css";

const app = createApp(App);
// app.directive's first argument is the directive's name you will use
// it can be whatever you wish
app.directive("tooltip", tooltip);
app.mount("#app");

And that's it! Now you can use it everywhere (there are some limitations though, f.e. it doesn't work for <select> element, you have add v-tooltip to its wrapper).

Usage

Using v-tooltip is really simple. Just like any other directive you add it to the element and provide some data. The easiest example look like this:

<template>
    <button v-tooltip="'This button deletes our universe'">
        Delete universe
    </button>
</template>

You just provide a string to the v-tooltip and it does its magic to create your own reactive tooltip. On hover it will look like that:

basic usage

String notation

V-tooltip accepts both strings as well as objects. If you don't need any local customization, strings are the way to go. V-tooltip updates automatically, so you can have live changing tooltip.

<template>
    <button
    v-tooltip="`You did nothing ${count} times`"
    @click="increaseCount"
    >
        Do nothing
    </button>
</template>

<script>
export default {
    data() {
        return {
            count: 0
        };
    },
    methods: {
        increaseCount() {
            this.count++;
        }
    }
}
</script>

Result after clicking 4 times:

auto update

Object notation

Object notation gives many more possibilities. If you want to have tooltips which differ from each other, this can be easily done (you would probably use computed property to declutter template):

<template>
    <button
        v-tooltip="{
                    text: 'Lorem ipsum dolor',
                    theme: {
                        color: '#000000',
                        border: '1px solid red',
                        'background-color': 'pink',
                        'font-size': '2rem',
                    },
                }"
    >
        Button with fancy/ugly tooltip
    </button>
</template>

fancy tooltip

Worth noting: text property is optional, so you can do some tricky stuff, like adding only v-tooltip theme to some <div> and every tooltip inside that <div> will inherit that theme instead of the default one.

Customizing tooltips globally

It's more likely that you want to have all tooltips look the same, but different than the default. You have two options:

  1. edit tooltip.css file
  2. use object notations with global property
Option 1

At the top of tooltip.css there are a bunch of CSS variables which define all of the customizable properties of v-tooltip. Just change them and that will work globally as a default.

Option 2

You can also don't touch the tooltip.css and do the same thing using v-tooltip with property global: true:

<template>
    <div
        v-tooltip="{
                    global: true,
                    theme: {
                        placement: 'bottom',
                        width: 'fit-content',
                        padding: '2rem',
                    },
                }"
    >
        <button v-tooltip="'tooltip with changed default style'">
            I run out of ideas
        </button>
    </div>
</template>

This will affect every tooltip in the app, because it changes the CSS variables in the :root. You can place it wherever you want, I suggest adding it to the top element in the app, using it as semi-"layout" component.

global prop

API reference

v-tooltip

  • Expects: string | Object
  • Options (all of them are non-mandatory):
    • text - text inside created tooltip
    • theme - takes care of styling tooltips of the element and its children. Accepted properties:
      • placement - top (default), bottom, left, right: placement of the tooltip relative to the element its called on
      • offset - [] top, bottom, left, right: automatically calculate and re-positions the tooltip to prevent overflow from mentioned sides. Will be ignored if global is set. Default []
      • width - default max-content
      • background-color - default #000000
      • color - default #ffffff
      • border-radius - default 0.4rem
      • padding - default 0.6em
      • font-size - default 0.8rem
      • border - default none
      • box-shadow - default none
      • transition-duration - default 0.25s
      • transition-delay - default 0.3s
      • transition-timing-function - default ease
    • global - default false. When added, tooltip won't be shown on this element. Instead it modifies CSS variables in the :root, therefore changing theme for all of the tooltips on the page
    • displayArrow - default false. When set to true tooltip's arrow pointing towards the element will be displayed. It inherits the tooltip's background color. WARNING: it is not optimized for offset theme property containing more than one direction.
  • Modifiers:
    • arrow - add .arrow to tooltip to show the arrow pointing towards the element. It is the same as options' displayArrow property. Works with both string and Object value.

v-tooltip's People

Contributors

m4heshd avatar maciejziemichod avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

v-tooltip's Issues

tooltip arrow

add optional tooltip arrow (probably with another pseudo element and its border)

Accessibility

Is accessibility a goal for this project? The documentation for Bootstrap has some helpful information about accessibility considerations for tooltips (source). Here is the last sentence:

Additionally, do not rely solely on hover as the trigger for your tooltip, as this will make your tooltips impossible to trigger for keyboard users.

box shadow

add option to style tooltip's box shadow

Set for Typescript

Hello.
I am using vue 3 and typescript.
Currently i get an error when importing the js file.
Could this project be made typescript friendly?
Thanks for your time.

The error i get is: Could not find a declaration file for module './directives/tooltip.js'.
and: directives/tooltip.js' implicitly has an 'any' type.

tooltip over header

z-index is always the highest. There should be info in the docs (or tweak the code a little bit) that if it is not desired, they should change css var of z-index to lower than their header's z-index, so that it would not appear

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.