|
1 | 1 | <script setup lang="ts"> |
2 | | -import { ref } from "vue"; |
| 2 | +import { computed, ref } from "vue"; |
3 | 3 |
|
4 | 4 | interface ATooltipProps { |
5 | 5 | content: string; |
6 | 6 | position?: "top" | "right" | "bottom" | "left"; |
7 | 7 | color?: "default" | "danger"; |
| 8 | + noWrap?: boolean; |
8 | 9 | } |
9 | 10 |
|
10 | 11 | const props = withDefaults(defineProps<ATooltipProps>(), { |
11 | 12 | position: "top", |
12 | 13 | color: "default", |
| 14 | + noWrap: false, |
13 | 15 | }); |
14 | 16 |
|
15 | 17 | const tooltipVisible = ref(false); |
| 18 | +const tooltipTarget = ref<HTMLElement | null>(null); |
| 19 | +const tooltipStyle = computed(() => { |
| 20 | + if (!tooltipTarget.value || !tooltipVisible.value) return {}; |
| 21 | +
|
| 22 | + const targetRect = tooltipTarget.value.getBoundingClientRect(); |
| 23 | + const style: any = { zIndex: 99999 }; |
| 24 | +
|
| 25 | + switch (props.position) { |
| 26 | + case "top": |
| 27 | + style.bottom = `${window.innerHeight - targetRect.top + 5}px`; |
| 28 | + style.left = `${targetRect.left + targetRect.width / 2}px`; |
| 29 | + style.transform = 'translateX(-50%)'; |
| 30 | + break; |
| 31 | + case "bottom": |
| 32 | + style.top = `${targetRect.bottom + 5}px`; |
| 33 | + style.left = `${targetRect.left + targetRect.width / 2}px`; |
| 34 | + style.transform = 'translateX(-50%)'; |
| 35 | + break; |
| 36 | + case "left": |
| 37 | + style.top = `${targetRect.top + targetRect.height / 2}px`; |
| 38 | + style.right = `${window.innerWidth - targetRect.left + 5}px`; |
| 39 | + style.transform = 'translateY(-50%)'; |
| 40 | + break; |
| 41 | + case "right": |
| 42 | + style.top = `${targetRect.top + targetRect.height / 2}px`; |
| 43 | + style.left = `${targetRect.right + 5}px`; |
| 44 | + style.transform = 'translateY(-50%)'; |
| 45 | + break; |
| 46 | + } |
| 47 | + return style; |
| 48 | +}); |
| 49 | +
|
16 | 50 | </script> |
17 | 51 |
|
18 | 52 | <template> |
19 | 53 | <div class="a-tooltip-container"> |
20 | 54 | <div |
| 55 | + ref="tooltipTarget" |
21 | 56 | class="tooltip-target" |
22 | 57 | @mouseenter="tooltipVisible = true" |
23 | 58 | @mouseleave="tooltipVisible = false" |
24 | 59 | > |
25 | 60 | <slot></slot> |
26 | 61 | </div> |
27 | | - <transition name="fade-fast"> |
28 | | - <div v-if="tooltipVisible" class="tooltip" :class="position, color"> |
29 | | - {{ content }} |
30 | | - </div> |
31 | | - </transition> |
| 62 | + <teleport to="body"> |
| 63 | + <transition name="fade-fast"> |
| 64 | + <div v-if="tooltipVisible" class="tooltip" :class="{ 'no-wrap': noWrap, 'danger': color === 'danger' }" :style="tooltipStyle"> |
| 65 | + {{ content }} |
| 66 | + </div> |
| 67 | + </transition> |
| 68 | + </teleport> |
32 | 69 | </div> |
33 | 70 | </template> |
34 | 71 |
|
35 | 72 | <style scoped lang="scss"> |
36 | 73 | .a-tooltip-container { |
37 | 74 | position: relative; |
38 | 75 | width: fit-content; |
| 76 | + display: inline-block; |
| 77 | +} |
39 | 78 |
|
40 | | - .tooltip { |
41 | | - position: absolute; |
42 | | - z-index: 1000; |
43 | | - background-color: var(--a-black); |
44 | | - color: var(--a-white); |
45 | | - padding: 10px; |
46 | | - border-radius: 5px; |
47 | | - font-size: 14px; |
48 | | - pointer-events: none; |
49 | | -
|
50 | | - &.top { |
51 | | - bottom: calc(100% + 5px); |
52 | | - left: 50%; |
53 | | - transform: translateX(-50%); |
54 | | - } |
55 | | -
|
56 | | - &.right { |
57 | | - top: 50%; |
58 | | - left: calc(100% + 5px); |
59 | | - transform: translateY(-50%); |
60 | | - } |
| 79 | +.tooltip { |
| 80 | + position: fixed; |
| 81 | + z-index: 99999; |
| 82 | + background-color: var(--a-black); |
| 83 | + color: var(--a-white); |
| 84 | + padding: 10px; |
| 85 | + border-radius: 5px; |
| 86 | + font-size: 14px; |
| 87 | + pointer-events: none; |
61 | 88 |
|
62 | | - &.bottom { |
63 | | - top: calc(100% + 5px); |
64 | | - left: 50%; |
65 | | - transform: translateX(-50%); |
66 | | - } |
| 89 | + &.danger { |
| 90 | + border: 0.5px solid var(--a-danger); |
| 91 | + background: var(--a-danger-lightest); |
| 92 | + color: var(--a-danger); |
| 93 | + } |
67 | 94 |
|
68 | | - &.left { |
69 | | - top: 50%; |
70 | | - right: calc(100% + 5px); |
71 | | - transform: translateY(-50%); |
72 | | - } |
| 95 | + &.no-wrap { |
| 96 | + white-space: nowrap; |
73 | 97 | } |
74 | | - .danger { |
75 | | - border: 0.5px solid var(--a-danger); |
76 | | - background: var(--a-danger-lightest); |
77 | | - color: var(--a-danger); |
78 | | - } |
79 | 98 | } |
80 | 99 |
|
81 | 100 | </style> |
0 commit comments