UNPKG

19.8 kBMarkdownView Raw
1<h1 align="center">v-tooltip</h1>
2
3<p align="center">
4<img src="https://raw.githubusercontent.com/Akryum/v-tooltip/master/v-tooltip.png" width="247" height="172" alt="Screenshot"/>
5</p>
6
7<p align="center">
8<a href="https://www.npmjs.com/package/v-tooltip"><img src="https://img.shields.io/npm/v/v-tooltip.svg"/> <img src="https://img.shields.io/npm/dm/v-tooltip.svg"/></a> <a href="https://vuejs.org/"><img src="https://img.shields.io/badge/vue-2.x-brightgreen.svg"/></a>
9</p>
10
11<p align="center">
12Easy tooltips, popovers and dropdown with <a href="https://github.com/FezVrasta/popper.js">Popper.js</a>
13</p>
14
15<p align="center">
16 <a href="https://www.patreon.com/akryum" target="_blank">
17 <img src="https://c5.patreon.com/external/logo/become_a_patron_button.png" alt="Become a Patreon">
18 </a>
19</p>
20
21## Sponsors
22
23### Gold
24
25<p align="center">
26 <a href="https://www.sumcumo.com/en/" target="_blank">
27 <img src="https://cdn.discordapp.com/attachments/258614093362102272/570728242399674380/logo-sumcumo.png" alt="sum.cumo logo" width="400px">
28 </a>
29</p>
30
31### Silver
32
33<p align="center">
34 <a href="https://vueschool.io/" target="_blank">
35 <img src="https://vueschool.io/img/logo/vueschool_logo_multicolor.svg" alt="VueSchool logo" width="200px">
36 </a>
37
38 <a href="https://www.vuemastery.com/" target="_blank">
39 <img src="https://cdn.discordapp.com/attachments/258614093362102272/557267759130607630/Vue-Mastery-Big.png" alt="Vue Mastery logo" width="200px">
40 </a>
41</p>
42
43### Bronze
44
45<p align="center">
46 <a href="https://vuetifyjs.com" target="_blank">
47 <img src="https://cdn.discordapp.com/attachments/537832759985700914/537832771691872267/Horizontal_Logo_-_Dark.png" width="100">
48 </a>
49
50 <a href="https://www.frontenddeveloperlove.com/" target="_blank" title="Frontend Developer Love">
51 <img src="https://cdn.discordapp.com/attachments/258614093362102272/557267744249085953/frontend_love-logo.png" width="56">
52 </a>
53</p>
54
55<br>
56<br>
57<br>
58
59**Useful Links**
60
61- [Live Demo](https://akryum.github.io/v-tooltip/)
62- [JSFiddle](https://jsfiddle.net/Akryum/tsjco74e/)
63
64<br>
65
66**Table of Contents**
67
68- [Getting started](#getting-started)
69- [Installation](#installation)
70- [Usage](#usage)
71 - [Directive](#directive)
72 - [Object notation](#object-notation)
73 - [Dynamic CSS classes](#dynamic-css-classes)
74 - [Other options](#other-options)
75 - [Tooltip auto-hiding](#tooltip-auto-hiding)
76 - [Disabling tooltips](#disabling-tooltips)
77 - [Component](#component)
78 - [Popover Component Reference](#popover-component-reference)
79 - [Close directive](#close-directive)
80 - [Global options](#global-options)
81- [Style Examples](#style-examples)
82
83<br>
84
85# Getting started
86
87This package offers two different usages: [directive](#directive) or [component](#component). You can use them to create tooltips, popovers or all kinds of dropdowns.
88
891. Install the plugin:
90
91```
92npm install --save v-tooltip
93```
94
952. Add the plugin into your app:
96
97```javascript
98import Vue from 'vue'
99import VTooltip from 'v-tooltip'
100
101Vue.use(VTooltip)
102```
103
104[More info on installation](#installation)
105
1063. Add [some style](#style-examples) to your liking.
107
1084. Use the `v-tooltip` directive:
109
110```html
111<button v-tooltip="'You have ' + count + ' new messages.'">
112```
113
114[More info on the directive](#directive)
115
1165. Use the `v-popover` component:
117
118```html
119<v-popover>
120 <!-- This will be the popover target (for the events and position) -->
121 <button>Click me</button>
122 <!-- This will be the content of the popover -->
123 <MyAwesomeComponent slot="popover"/>
124</v-popover>
125```
126
127[More info on the component](#component)
128
129<br>
130
131# Installation
132
133## Npm
134
135```
136npm install --save v-tooltip
137```
138
139Install the plugin into Vue:
140
141```javascript
142import Vue from 'vue'
143import VTooltip from 'v-tooltip'
144
145Vue.use(VTooltip)
146```
147
148Or use the directives and components directly:
149
150```javascript
151import Vue from 'vue'
152import { VTooltip, VPopover, VClosePopover } from 'v-tooltip'
153
154Vue.directive('tooltip', VTooltip)
155Vue.directive('close-popover', VClosePopover)
156Vue.component('v-popover', VPopover)
157```
158
159## Browser
160
161Include [v-tooltip](/dist/v-tooltip.min.js) in the page.
162
163```html
164<script src="https://unpkg.com/v-tooltip"></script>
165```
166
167**If Vue is detected in the Page, the plugin is installed automatically.**
168
169Manually install the plugin into Vue:
170
171```javascript
172Vue.use(VTooltip)
173```
174
175Or use the directives and components directly:
176
177```javascript
178Vue.directive('tooltip', VTooltip.VTooltip)
179Vue.directive('close-popover', VTooltip.VClosePopover)
180Vue.component('v-popover', VTooltip.VPopover)
181```
182
183# Usage
184
185## Directive
186
187In the template, use the `v-tooltip` directive:
188
189```html
190<button v-tooltip="'You have ' + count + ' new messages.'">
191```
192
193Of course, you can use a reactive property:
194
195```html
196<button v-tooltip="tooltipContent">
197```
198
199You can specify the tooltip position as a modifier:
200
201```html
202<button v-tooltip.bottom-start="'You have ' + count + ' new messages.'">
203```
204
205The available positions are:
206
207 - `'auto'`
208 - `'auto-start'`
209 - `'auto-end'`
210 - `'top'`
211 - `'top-start'`
212 - `'top-end'`
213 - `'right'`
214 - `'right-start'`
215 - `'right-end'`
216 - `'bottom'`
217 - `'bottom-start'`
218 - `'bottom-end'`
219 - `'left'`
220 - `'left-start'`
221 - `'left-end'`
222
223**:warning: You need to add style to the tooltips: [examples](#style-examples).**
224
225### Object notation
226
227You can use an object instead of a simple string:
228
229```html
230<button v-tooltip="{ content: 'You have ' + count + ' new messages.' }">
231```
232
233### Dynamic CSS classes
234
235You can set the tooltip css classes dynamically with the object notation:
236
237```html
238<button v-tooltip="{ content: 'You have ' + count + ' new messages.', classes: ['a', 'b'] }">
239```
240
241This will replace the default CSS classe with 'a b' on the tooltip element.
242
243You can also use the standard class notation:
244
245```html
246<button v-tooltip="{ content: 'You have ' + count + ' new messages.', classes: 'a b' }">
247```
248
249Or a reactive property:
250
251```html
252<button v-tooltip="{ content: 'You have ' + count + ' new messages.', classes: tooltipClasses }">
253```
254
255### Other options
256
257```html
258<button v-tooltip="options">
259```
260
261- `content` - HTML text to be displayed in the tooltip. Can also be a function that returns the content or a Promise.
262- `classes` - *(see above)*
263- `targetClasses` - CSS classes added to the target element of the tooltip.
264- `html` - Boolean: allow HTML tooltip content.
265- `delay` - Show/Hide delay, or object: `{ show: 500, hide: 100 }` (ms).
266- `placement` - *(see above)*
267- `trigger` - Events triggering the tooltip separated with spaces: `'hover'`, `'click'`, `'focus'` or `'manual'` (`'manual'` can't be combined with any other event).
268- `show` - Boolean to manually open or hide the tooltip.
269- `offset` - Offset of the position (px).
270- `container` - Selector: Container where the tooltip will be appended (e.g. `'body'`).
271- `boundariesElement` - DOM element for the tooltip boundaries.
272- `template` - HTML template of the tooltip.
273- `arrowSelector` - CSS selector to get the arrow element in the tooltip template.
274- `innerSelector` - CSS selector to get the inner content element in the tooltip template.
275- `autoHide` - Boolean: automatically close the tooltip on mouseover.
276- `hideOnTargetClick` - Boolean: automatically close the tooltip on target click.
277- `loadingClass` - CSS classes added to the tooltip when content is loading.
278- `loadingContent` - Same as `content`, used when the actual tooltip content is loading.
279- `popperOptions` - Other Popper.js options.
280
281You can change the default values in the [Global options](#global-options).
282
283### Async content example
284
285The `content` option accepts a promise:
286
287```html
288<button
289 v-tooltip="{
290 content: asyncMethod(),
291 loadingContent: 'Please wait...',
292 loadingClass: 'content-is-loading',
293 }"
294>Hover me!</button>
295```
296
297### Manual trigger example
298
299Use the `trigger` and `show` options:
300
301```html
302<button
303 v-tooltip="{
304 content: 'Tooltip content here',
305 show: isOpen,
306 trigger: 'manual',
307 }"
308>A button</button>
309```
310
311### Tooltip auto-hiding
312
313By default, if `trigger` contains `'hover'`, the tooltip is automatically hidden on hover or click. To disable this, set the `autoHide` option to `false`:
314
315```javascript
316VTooltip.options.autoHide = false
317```
318
319### Disabling tooltips
320
321On mobile, you can disable the tooltips with the `VTooltip.enabled` property:
322
323```javascript
324VTooltip.enabled = window.innerWidth > 768
325```
326
327## Component
328
329If you need to display components inside the tooltip (or popover/dropdown, technically it's the same :smile:), use the `v-popover` component:
330
331```html
332<v-popover
333 offset="16"
334>
335 <!-- This will be the popover target (for the events and position) -->
336 <button class="tooltip-target b3">Click me</button>
337
338 <!-- This will be the content of the popover -->
339 <template slot="popover">
340 <input class="tooltip-content" v-model="msg" placeholder="Tooltip content" />
341 <p>
342 {{ msg }}
343 </p>
344
345 <!-- You can put other components too -->
346 <ExampleComponent char="=" />
347 </template>
348</v-popover>
349```
350
351By default, the popover will have the `tooltip` and `popover` classes, so you can easily override [the style](#style-examples):
352
353```scss
354.tooltip {
355 // ...
356
357 &.popover {
358 $color: #f9f9f9;
359
360 .popover-inner {
361 background: $color;
362 color: black;
363 padding: 24px;
364 border-radius: 5px;
365 box-shadow: 0 5px 30px rgba(black, .1);
366 }
367
368 .popover-arrow {
369 border-color: $color;
370 }
371 }
372}
373```
374
375**⚠️ Set the arrow element `z-index` CSS property:**
376
377```scss
378.tooltip-arrow {
379 z-index: 1;
380}
381```
382
383### Popover Component Reference
384
385**Props:**
386
387- `open` - Boolean that shows or hide the popover.
388- `disabled` - Boolean that disables the popover. If it was already open, it will be closed.
389- `placement` - *(see above)*
390- `delay` - *(see above)*
391- `trigger` - *(see above)*
392- `offset` - *(see above)*
393- `container` - *(see above)*
394- `boundariesElement` - *(see above)*
395- `popperOptions` - *(see above)*
396- `popoverClass` - Classes applied to the popover element. Use this to apply different themes to the popover.
397- `popoverBaseClass` - Base classes applied to the popover element (defaults to `'tooltip popover'`).
398- `popoverWrapperClass` - Class of the element that contains the arrow and inner content.
399- `popoverArrowClass` - Class of the arrow element.
400- `popoverInnerClass` - Class of the inner content element.
401- `autoHide` - Hide the popover if clicked outside.
402- `handleResize` - Automatically update the popover position if its size changes.
403- `openGroup` - If set, will close all the open popovers that have a different `open-group` value or unset.
404- `openClass` - Class put on the popover when it's open.
405
406You can change the default values in the [Global options](#global-options).
407
408**Events:**
409
410- `update:open(Boolean)` - This allow you to use the `.sync` modifier on the `open` prop.
411- `show`
412- `apply-show` - Emitted after the show delay
413- `hide`
414- `apply-hide` - Emitted after the hide delay
415- `dispose`
416- `auto-hide` - Emitted when the popover is closed if clicked outside.
417- `close-directive` - Emitted when the popover is closed with the [Close directive](#close-directive).
418- `close-group` - Emitted when the popover is closed because a popover of another `open-group` was shown.
419- `resize` - Emitted when the content size changes. You must set the `handleResize` prop to `true`.
420
421### Disable popover
422
423```html
424<v-popover :disabled="isDisabled"></v-popover>
425```
426
427```js
428data () {
429 return {
430 isDisabled: true,
431 }
432}
433```
434
435### Close directive
436
437Use the `v-close-popover` directive on an element inside the popover to close it when the element is clicked (or touched on mobile):
438
439```html
440<v-popover>
441 <button>Click me</button>
442
443 <template slot="popover">
444 <a v-close-popover>Close</a>
445 </template>
446</v-popover>
447```
448
449You can also set it to true or false to enable or disable the directive (enabled by default):
450
451```html
452<a v-close-popover="false">Close</a>
453<a v-close-popover="true">Close</a>
454```
455
456You can also use a property:
457
458```html
459<a v-close-popover="myBooleanProp">Close</a>
460```
461
462```js
463data () {
464 return {
465 myBooleanProp: true,
466 }
467}
468```
469
470Close all the popovers in the page with the `all` modifier:
471
472```html
473<a v-close-popover.all>Close All</a>
474```
475
476## Global options
477
478The default global options are:
479
480```javascript
481{
482 // Default tooltip placement relative to target element
483 defaultPlacement: 'top',
484 // Default CSS classes applied to the tooltip element
485 defaultClass: 'vue-tooltip-theme',
486 // Default CSS classes applied to the target element of the tooltip
487 defaultTargetClass: 'has-tooltip',
488 // Is the content HTML by default?
489 defaultHtml: true,
490 // Default HTML template of the tooltip element
491 // It must include `tooltip-arrow` & `tooltip-inner` CSS classes (can be configured, see below)
492 // Change if the classes conflict with other libraries (for example bootstrap)
493 defaultTemplate: '<div class="tooltip" role="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>',
494 // Selector used to get the arrow element in the tooltip template
495 defaultArrowSelector: '.tooltip-arrow, .tooltip__arrow',
496 // Selector used to get the inner content element in the tooltip template
497 defaultInnerSelector: '.tooltip-inner, .tooltip__inner',
498 // Delay (ms)
499 defaultDelay: 0,
500 // Default events that trigger the tooltip
501 defaultTrigger: 'hover focus',
502 // Default position offset (px)
503 defaultOffset: 0,
504 // Default container where the tooltip will be appended
505 defaultContainer: 'body',
506 defaultBoundariesElement: undefined,
507 defaultPopperOptions: {},
508 // Class added when content is loading
509 defaultLoadingClass: 'tooltip-loading',
510 // Displayed when tooltip content is loading
511 defaultLoadingContent: '...',
512 // Hide on mouseover tooltip
513 autoHide: true,
514 // Close tooltip on click on tooltip target?
515 defaultHideOnTargetClick: true,
516 // Auto destroy tooltip DOM nodes (ms)
517 disposeTimeout: 5000,
518 // Options for popover
519 popover: {
520 defaultPlacement: 'bottom',
521 // Use the `popoverClass` prop for theming
522 defaultClass: 'vue-popover-theme',
523 // Base class (change if conflicts with other libraries)
524 defaultBaseClass: 'tooltip popover',
525 // Wrapper class (contains arrow and inner)
526 defaultWrapperClass: 'wrapper',
527 // Inner content class
528 defaultInnerClass: 'tooltip-inner popover-inner',
529 // Arrow class
530 defaultArrowClass: 'tooltip-arrow popover-arrow',
531 // Class added when popover is open
532 defaultOpenClass: 'open',
533 defaultDelay: 0,
534 defaultTrigger: 'click',
535 defaultOffset: 0,
536 defaultContainer: 'body',
537 defaultBoundariesElement: undefined,
538 defaultPopperOptions: {},
539 // Hides if clicked outside of popover
540 defaultAutoHide: true,
541 // Update popper on content resize
542 defaultHandleResize: true,
543 },
544}
545```
546
547You can change the options during install with the arguments:
548
549```javascript
550import VTooltip from 'v-tooltip'
551Vue.use(VTooltip, options)
552```
553
554Or directly on package:
555
556```javascript
557import VTooltip from 'v-tooltip'
558// Set custom CSS class
559VTooltip.options.defaultClass = 'my-tooltip'
560```
561
562# Style Examples
563
564Bellow are some examples of style you need. [Here](https://github.com/Akryum/v-tooltip/blob/83615e394c96ca491a4df04b892ae87e833beb97/demo-src/src/App.vue#L179-L303) is another example, used in the [live demo](https://akryum.github.io/v-tooltip/#/).
565
566## Sass / Less
567
568```less
569.tooltip {
570 display: block !important;
571 z-index: 10000;
572
573 .tooltip-inner {
574 background: black;
575 color: white;
576 border-radius: 16px;
577 padding: 5px 10px 4px;
578 }
579
580 .tooltip-arrow {
581 width: 0;
582 height: 0;
583 border-style: solid;
584 position: absolute;
585 margin: 5px;
586 border-color: black;
587 z-index: 1;
588 }
589
590 &[x-placement^="top"] {
591 margin-bottom: 5px;
592
593 .tooltip-arrow {
594 border-width: 5px 5px 0 5px;
595 border-left-color: transparent !important;
596 border-right-color: transparent !important;
597 border-bottom-color: transparent !important;
598 bottom: -5px;
599 left: calc(50% - 5px);
600 margin-top: 0;
601 margin-bottom: 0;
602 }
603 }
604
605 &[x-placement^="bottom"] {
606 margin-top: 5px;
607
608 .tooltip-arrow {
609 border-width: 0 5px 5px 5px;
610 border-left-color: transparent !important;
611 border-right-color: transparent !important;
612 border-top-color: transparent !important;
613 top: -5px;
614 left: calc(50% - 5px);
615 margin-top: 0;
616 margin-bottom: 0;
617 }
618 }
619
620 &[x-placement^="right"] {
621 margin-left: 5px;
622
623 .tooltip-arrow {
624 border-width: 5px 5px 5px 0;
625 border-left-color: transparent !important;
626 border-top-color: transparent !important;
627 border-bottom-color: transparent !important;
628 left: -5px;
629 top: calc(50% - 5px);
630 margin-left: 0;
631 margin-right: 0;
632 }
633 }
634
635 &[x-placement^="left"] {
636 margin-right: 5px;
637
638 .tooltip-arrow {
639 border-width: 5px 0 5px 5px;
640 border-top-color: transparent !important;
641 border-right-color: transparent !important;
642 border-bottom-color: transparent !important;
643 right: -5px;
644 top: calc(50% - 5px);
645 margin-left: 0;
646 margin-right: 0;
647 }
648 }
649
650 &.popover {
651 $color: #f9f9f9;
652
653 .popover-inner {
654 background: $color;
655 color: black;
656 padding: 24px;
657 border-radius: 5px;
658 box-shadow: 0 5px 30px rgba(black, .1);
659 }
660
661 .popover-arrow {
662 border-color: $color;
663 }
664 }
665
666 &[aria-hidden='true'] {
667 visibility: hidden;
668 opacity: 0;
669 transition: opacity .15s, visibility .15s;
670 }
671
672 &[aria-hidden='false'] {
673 visibility: visible;
674 opacity: 1;
675 transition: opacity .15s;
676 }
677}
678```
679
680## CSS
681
682```css
683.tooltip {
684 display: block !important;
685 z-index: 10000;
686}
687
688.tooltip .tooltip-inner {
689 background: black;
690 color: white;
691 border-radius: 16px;
692 padding: 5px 10px 4px;
693}
694
695.tooltip .tooltip-arrow {
696 width: 0;
697 height: 0;
698 border-style: solid;
699 position: absolute;
700 margin: 5px;
701 border-color: black;
702 z-index: 1;
703}
704
705.tooltip[x-placement^="top"] {
706 margin-bottom: 5px;
707}
708
709.tooltip[x-placement^="top"] .tooltip-arrow {
710 border-width: 5px 5px 0 5px;
711 border-left-color: transparent !important;
712 border-right-color: transparent !important;
713 border-bottom-color: transparent !important;
714 bottom: -5px;
715 left: calc(50% - 5px);
716 margin-top: 0;
717 margin-bottom: 0;
718}
719
720.tooltip[x-placement^="bottom"] {
721 margin-top: 5px;
722}
723
724.tooltip[x-placement^="bottom"] .tooltip-arrow {
725 border-width: 0 5px 5px 5px;
726 border-left-color: transparent !important;
727 border-right-color: transparent !important;
728 border-top-color: transparent !important;
729 top: -5px;
730 left: calc(50% - 5px);
731 margin-top: 0;
732 margin-bottom: 0;
733}
734
735.tooltip[x-placement^="right"] {
736 margin-left: 5px;
737}
738
739.tooltip[x-placement^="right"] .tooltip-arrow {
740 border-width: 5px 5px 5px 0;
741 border-left-color: transparent !important;
742 border-top-color: transparent !important;
743 border-bottom-color: transparent !important;
744 left: -5px;
745 top: calc(50% - 5px);
746 margin-left: 0;
747 margin-right: 0;
748}
749
750.tooltip[x-placement^="left"] {
751 margin-right: 5px;
752}
753
754.tooltip[x-placement^="left"] .tooltip-arrow {
755 border-width: 5px 0 5px 5px;
756 border-top-color: transparent !important;
757 border-right-color: transparent !important;
758 border-bottom-color: transparent !important;
759 right: -5px;
760 top: calc(50% - 5px);
761 margin-left: 0;
762 margin-right: 0;
763}
764
765.tooltip.popover .popover-inner {
766 background: #f9f9f9;
767 color: black;
768 padding: 24px;
769 border-radius: 5px;
770 box-shadow: 0 5px 30px rgba(black, .1);
771}
772
773.tooltip.popover .popover-arrow {
774 border-color: #f9f9f9;
775}
776
777.tooltip[aria-hidden='true'] {
778 visibility: hidden;
779 opacity: 0;
780 transition: opacity .15s, visibility .15s;
781}
782
783.tooltip[aria-hidden='false'] {
784 visibility: visible;
785 opacity: 1;
786 transition: opacity .15s;
787}
788```
789
790
791---
792
793LICENCE MIT - Created by Guillaume CHAU (@Akryum)