-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtooltip.js
198 lines (196 loc) · 10.1 KB
/
tooltip.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
export default {
updateTooltip(el, { value, modifiers }) {
if (typeof value === "string") {
// we can pass either a string
el.setAttribute("data-v-tooltip", value);
// this check if when v-tooltip receives string with .arrow modifier
if (modifiers.arrow) {
el.style.setProperty("--v-tooltip-arrow-display", "inline");
}
} else {
// or an object
if (value.text) {
el.setAttribute("data-v-tooltip", value.text);
}
if (value.displayArrow || modifiers.arrow) {
// if there is a prop global: true then mutate the :root css variables
// otherwise it adds given variables to the element, which makes it possible to be different than others
const targetEl = value.global ? document.documentElement : el;
targetEl.style.setProperty(
"--v-tooltip-arrow-display",
"inline"
);
}
if (value.theme) {
// if there is a prop global: true then mutate the :root css variables
// otherwise it adds given variables to the element, which makes it possible to be different than others
const targetEl = value.global ? document.documentElement : el;
for (const [key, val] of Object.entries(value.theme)) {
if (key === "placement") {
switch (val) {
case "top":
targetEl.style.setProperty(
"--v-tooltip-left",
"50%"
);
targetEl.style.setProperty(
"--v-tooltip-top",
"0%"
);
targetEl.style.setProperty(
"--v-tooltip-translate",
"translate(-50%, -110%)"
);
if (value.displayArrow || modifiers.arrow) {
targetEl.style.setProperty(
"--v-tooltip-arrow-border-color",
"var(--v-tooltip-background-color) transparent transparent transparent"
);
targetEl.style.setProperty(
"--v-tooltip-arrow-top",
"calc(var(--v-tooltip-top) - var(--v-tooltip-top-offset) + 8px)"
);
}
break;
case "bottom":
targetEl.style.setProperty(
"--v-tooltip-left",
"50%"
);
targetEl.style.setProperty(
"--v-tooltip-top",
"100%"
);
targetEl.style.setProperty(
"--v-tooltip-translate",
"translate(-50%, 10%)"
);
if (value.displayArrow || modifiers.arrow) {
targetEl.style.setProperty(
"--v-tooltip-arrow-border-color",
"transparent transparent var(--v-tooltip-background-color) transparent"
);
targetEl.style.setProperty(
"--v-tooltip-arrow-top",
"calc(var(--v-tooltip-top) - var(--v-tooltip-top-offset) - 7px)"
);
}
break;
case "left":
targetEl.style.setProperty(
"--v-tooltip-left",
"0%"
);
targetEl.style.setProperty(
"--v-tooltip-top",
"50%"
);
targetEl.style.setProperty(
"--v-tooltip-translate",
"translate(-110%, -50%)"
);
if (value.displayArrow || modifiers.arrow) {
targetEl.style.setProperty(
"--v-tooltip-arrow-border-color",
"transparent transparent transparent var(--v-tooltip-background-color)"
);
targetEl.style.setProperty(
"--v-tooltip-arrow-top",
"calc(var(--v-tooltip-top)"
);
targetEl.style.setProperty(
"--v-tooltip-arrow-left",
"calc( var(--v-tooltip-left) - var(--v-tooltip-left-offset) + 1.5px)"
);
}
break;
case "right":
targetEl.style.setProperty(
"--v-tooltip-left",
"100%"
);
targetEl.style.setProperty(
"--v-tooltip-top",
"50%"
);
targetEl.style.setProperty(
"--v-tooltip-translate",
"translate(10%, -50%)"
);
if (value.displayArrow || modifiers.arrow) {
targetEl.style.setProperty(
"--v-tooltip-arrow-border-color",
"transparent var(--v-tooltip-background-color) transparent transparent"
);
targetEl.style.setProperty(
"--v-tooltip-arrow-top",
"calc(var(--v-tooltip-top)"
);
targetEl.style.setProperty(
"--v-tooltip-arrow-left",
"calc( var(--v-tooltip-left) - var(--v-tooltip-left-offset) - 2px)"
);
}
break;
default:
break;
}
} else if (key === "offset" && !value.global) {
for (const direction of val) {
if (direction === "left") {
targetEl.style.setProperty(
"--v-tooltip-left-offset",
`-${
targetEl.scrollWidth -
targetEl.clientWidth
}px`
);
} else if (direction === "right") {
targetEl.style.setProperty(
"--v-tooltip-left-offset",
`${
targetEl.scrollWidth -
targetEl.clientWidth
}px`
);
} else if (direction === "top") {
targetEl.style.setProperty(
"--v-tooltip-top-offset",
`-${
targetEl.scrollHeight -
targetEl.clientHeight
}px`
);
} else if (direction === "bottom") {
targetEl.style.setProperty(
"--v-tooltip-top-offset",
`${
targetEl.scrollHeight -
targetEl.clientHeight
}px`
);
}
}
} else {
targetEl.style.setProperty(`--v-tooltip-${key}`, val);
}
}
}
}
},
// hooks
mounted(el, { value, dir, modifiers }) {
// v-tooltips with global prop won't show the tooltip
// also object notation without text prop won't show neither
if (typeof value === "object" && !value.global && value.text) {
el.classList.add("data-v-tooltip");
} else if (typeof value === "string") {
el.classList.add("data-v-tooltip");
}
// to use functions in Vue's directives which are inside this object, we can't use this, we have to use dir, which is the directive object
dir.updateTooltip(el, { value, modifiers });
},
updated(el, { value, dir, modifiers }) {
dir.updateTooltip(el, { value, modifiers });
},
};