|
1 | 1 | <script setup> |
2 | 2 | import { ref, onMounted, onUnmounted } from 'vue'; |
| 3 | +import tippy from 'tippy.js'; |
| 4 | +import 'tippy.js/animations/shift-away-subtle.css'; |
3 | 5 |
|
4 | 6 | const props = defineProps({ |
5 | | - targetId: String, |
6 | | - alignment: { |
7 | | - type: String, |
8 | | - default: 'center', |
9 | | - validator: (value) => ['left', 'center', 'right'].includes(value) |
10 | | - } |
| 7 | + targetId: String |
11 | 8 | }); |
12 | 9 |
|
13 | | -const visible = ref(false); |
14 | 10 | const tooltipElement = ref(null); |
| 11 | +let tippyInstance = null; |
15 | 12 |
|
16 | | -const showTooltip = () => { |
17 | | - visible.value = true; |
18 | | - updatePosition(); |
19 | | -}; |
20 | | -
|
21 | | -const hideTooltip = () => { |
22 | | - visible.value = false; |
23 | | -}; |
24 | | -
|
25 | | -const updatePosition = () => { |
| 13 | +const createTooltip = () => { |
26 | 14 | const target = document.getElementById(props.targetId); |
27 | | - const targetRect = target.getBoundingClientRect(); |
28 | | - const tooltipRect = tooltipElement.value.getBoundingClientRect(); |
29 | 15 |
|
30 | | - let x = 0; |
31 | | - if (props.alignment === 'left') { |
32 | | - x = targetRect.left; |
33 | | - } else if (props.alignment === 'center') { |
34 | | - x = targetRect.left + (targetRect.width - tooltipRect.width) / 2; |
35 | | - } else if (props.alignment === 'right') { |
36 | | - x = targetRect.right - tooltipRect.width; |
| 16 | + if (target && tooltipElement.value) { |
| 17 | + tippyInstance = tippy(target, { |
| 18 | + content: tooltipElement.value, |
| 19 | + placement: 'top', |
| 20 | + trigger: 'mouseenter focus', |
| 21 | + appendTo: document.body, |
| 22 | + arrow: true, |
| 23 | + animation: 'shift-away-subtle', |
| 24 | + interactive: true, |
| 25 | + onHidden: (instance) => { |
| 26 | + instance.destroy(); |
| 27 | + } |
| 28 | + }); |
37 | 29 | } |
38 | | -
|
39 | | - const y = targetRect.bottom + 10; // Adjust the offset as needed |
40 | | -
|
41 | | - tooltipElement.value.style.left = `${x}px`; |
42 | | - tooltipElement.value.style.top = `${y}px`; |
43 | 30 | }; |
44 | 31 |
|
45 | 32 | onMounted(() => { |
46 | | - const target = document.getElementById(props.targetId); |
47 | | -
|
48 | | - if (target) { |
49 | | - target.addEventListener('mouseenter', showTooltip); |
50 | | - target.addEventListener('mouseleave', hideTooltip); |
51 | | - window.addEventListener('resize', updatePosition); |
52 | | - } |
| 33 | + createTooltip(); |
53 | 34 | }); |
54 | 35 |
|
55 | 36 | onUnmounted(() => { |
56 | | - const target = document.getElementById(props.targetId); |
57 | | -
|
58 | | - if (target) { |
59 | | - target.removeEventListener('mouseenter', showTooltip); |
60 | | - target.removeEventListener('mouseleave', hideTooltip); |
61 | | - window.removeEventListener('resize', updatePosition); |
| 37 | + if (tippyInstance) { |
| 38 | + tippyInstance.destroy(); |
62 | 39 | } |
63 | | -
|
64 | 40 | }); |
65 | 41 | </script> |
66 | 42 |
|
67 | 43 | <template> |
68 | | - <div ref="tooltipElement" class="tooltip-container" v-show="visible"> |
| 44 | + <div ref="tooltipElement" class="tooltip-container"> |
69 | 45 | <slot></slot> |
70 | 46 | </div> |
71 | 47 | </template> |
72 | 48 |
|
73 | 49 | <style> |
74 | 50 | .tooltip-container { |
75 | | - position: fixed; |
76 | 51 | padding: 8px; |
77 | 52 | background-color: #333; |
78 | 53 | color: white; |
|
0 commit comments