|
1 | 1 | <script>
|
2 | 2 | import { onMount, onDestroy } from 'svelte';
|
3 |
| - import { formatVariableKey, isInViewport } from './helpers'; |
| 3 | + import { formatVariableKey, getMinWidth, isInViewport } from './helpers'; |
4 | 4 | import { inverse } from './constants';
|
5 | 5 |
|
6 | 6 | export let content = '';
|
|
16 | 16 | let ref = null;
|
17 | 17 | let minWidth = 0;
|
18 | 18 | let component = null;
|
| 19 | + let animationEffect = null; |
| 20 | + let show = false; |
19 | 21 |
|
20 | 22 | onMount(() => {
|
21 |
| - let delay = 0; |
| 23 | + const delay = animation ? 200 : 0; |
22 | 24 |
|
23 | 25 | if (ref !== null) {
|
24 | 26 | if (isComponent && !component) {
|
25 | 27 | component = new content.component({ target: ref, props: content.props });
|
26 | 28 | }
|
27 | 29 |
|
28 |
| - const elementWidth = ref.getBoundingClientRect().width; |
29 |
| - const elementStyle = window.getComputedStyle(ref); |
30 |
| - const elementPaddingLeft = parseInt(elementStyle.getPropertyValue('padding-left'), 10); |
31 |
| - const elementPaddingRight = parseInt(elementStyle.getPropertyValue('padding-right'), 10); |
32 |
| - const elementPadding = elementPaddingLeft + elementPaddingRight; |
33 |
| - const contentWidth = elementWidth - elementPadding; |
34 |
| -
|
35 |
| - minWidth = Math.round(Math.min(maxWidth, contentWidth)); |
| 30 | + minWidth = getMinWidth(ref, maxWidth); |
36 | 31 |
|
37 | 32 | if (style && typeof style === 'object') {
|
38 | 33 | for (let prop in style) {
|
|
46 | 41 |
|
47 | 42 | if (autoPosition && !isInViewport(ref)) {
|
48 | 43 | position = inverse[position];
|
49 |
| - delay = 200; |
50 | 44 | }
|
51 | 45 |
|
52 |
| - setTimeout(() => ref.classList.add('show'), delay); |
| 46 | + if (animation) { |
| 47 | + animationEffect = animation; |
| 48 | + } |
| 49 | +
|
| 50 | + setTimeout(() => (show = true), delay); |
53 | 51 | });
|
54 | 52 |
|
55 | 53 | onDestroy(() => {
|
|
64 | 62 |
|
65 | 63 | <div
|
66 | 64 | bind:this={ref}
|
67 |
| - class="tooltip animation-{animation} {position} {theme}" |
| 65 | + class="tooltip animation-{animationEffect} {position} {theme}" |
| 66 | + class:show |
68 | 67 | class:arrowless={!arrow}
|
69 | 68 | style="min-width: {minWidth}px; max-width: {maxWidth}px; text-align: {align};"
|
70 | 69 | >
|
|
99 | 98 | * Tooltip Styling
|
100 | 99 | *--------------------------*/
|
101 | 100 |
|
102 |
| - .tooltip-container { |
103 |
| - position: relative; |
104 |
| - } |
105 |
| -
|
106 | 101 | .tooltip {
|
107 | 102 | background-color: var(--tooltip-background-color);
|
108 | 103 | box-shadow: var(--tooltip-box-shadow);
|
109 | 104 | border-radius: var(--tooltip-border-radius);
|
110 | 105 | color: var(--tooltip-color);
|
111 |
| - opacity: 0; |
112 | 106 | font-family: var(--tooltip-font-family);
|
113 | 107 | font-size: var(--tooltip-font-size);
|
114 | 108 | font-style: normal;
|
|
117 | 111 | padding: var(--tooltip-padding);
|
118 | 112 | position: absolute;
|
119 | 113 | text-align: left;
|
120 |
| - visibility: hidden; |
121 | 114 | white-space: nowrap;
|
122 | 115 | z-index: var(--tooltip-z-index);
|
123 | 116 | }
|
124 | 117 |
|
125 | 118 | .tooltip.show {
|
126 |
| - opacity: 1; |
127 |
| - visibility: visible; |
128 | 119 | white-space: normal;
|
129 | 120 | }
|
130 | 121 |
|
|
201 | 192 |
|
202 | 193 | .tooltip.animation-fade {
|
203 | 194 | opacity: 0;
|
204 |
| - transition: opacity 0.3s ease-in-out; |
| 195 | + transition: opacity 0.25s ease-in-out; |
205 | 196 | }
|
206 | 197 |
|
207 | 198 | .tooltip.animation-fade.show {
|
|
213 | 204 | .tooltip.top.animation-slide {
|
214 | 205 | margin-top: 10px;
|
215 | 206 | opacity: 0;
|
216 |
| - transition: opacity 0.3s ease-in-out, margin 0.3s ease-in-out; |
| 207 | + transition: opacity 0.25s ease-in-out, margin 0.25s ease-in-out; |
217 | 208 | }
|
218 | 209 |
|
219 | 210 | .tooltip.top.animation-slide.show {
|
|
224 | 215 | .tooltip.bottom.animation-slide {
|
225 | 216 | margin-bottom: 20px;
|
226 | 217 | opacity: 0;
|
227 |
| - transition: opacity 0.3s ease-in-out, margin 0.3s ease-in-out; |
| 218 | + transition: opacity 0.25s ease-in-out, margin 0.25s ease-in-out; |
228 | 219 | }
|
229 | 220 |
|
230 | 221 | .tooltip.bottom.animation-slide.show {
|
|
235 | 226 | .tooltip.right.animation-slide {
|
236 | 227 | margin-right: 20px;
|
237 | 228 | opacity: 0;
|
238 |
| - transition: opacity 0.3s ease-in-out, margin 0.3s ease-in-out; |
| 229 | + transition: opacity 0.25s ease-in-out, margin 0.25s ease-in-out; |
239 | 230 | }
|
240 | 231 |
|
241 | 232 | .tooltip.right.animation-slide.show {
|
|
246 | 237 | .tooltip.left.animation-slide {
|
247 | 238 | margin-left: 20px;
|
248 | 239 | opacity: 0;
|
249 |
| - transition: opacity 0.3s ease-in-out, margin 0.3s ease-in-out; |
| 240 | + transition: opacity 0.25s ease-in-out, margin 0.25s ease-in-out; |
250 | 241 | }
|
251 | 242 |
|
252 | 243 | .tooltip.left.animation-slide.show {
|
|
261 | 252 | opacity: 0;
|
262 | 253 | transform: translate(calc(-100% - var(--tooltip-offset-x)), -50%) scale(2, 2);
|
263 | 254 | transform-origin: 50% 50%;
|
264 |
| - transition: opacity 0.3s ease-in-out, filter 0.3s ease-in-out, transform 0.3s ease-in-out; |
| 255 | + transition: opacity 0.25s ease-in-out, filter 0.25s ease-in-out, transform 0.25s ease-in-out; |
265 | 256 | }
|
266 | 257 |
|
267 | 258 | .tooltip.left.animation-puff.show {
|
|
275 | 266 | opacity: 0;
|
276 | 267 | transform: translate(calc(100% + var(--tooltip-offset-x)), -50%) scale(2, 2);
|
277 | 268 | transform-origin: 50% 50%;
|
278 |
| - transition: opacity 0.3s ease-in-out, filter 0.3s ease-in-out, transform 0.3s ease-in-out; |
| 269 | + transition: opacity 0.25s ease-in-out, filter 0.25s ease-in-out, transform 0.25s ease-in-out; |
279 | 270 | }
|
280 | 271 |
|
281 | 272 | .tooltip.right.animation-puff.show {
|
|
289 | 280 | opacity: 0;
|
290 | 281 | transform: translate(-50%, calc(-100% - var(--tooltip-offset-y))) scale(2, 2);
|
291 | 282 | transform-origin: 50% 50%;
|
292 |
| - transition: opacity 0.3s ease-in-out, filter 0.3s ease-in-out, transform 0.3s ease-in-out; |
| 283 | + transition: opacity 0.25s ease-in-out, filter 0.25s ease-in-out, transform 0.25s ease-in-out; |
293 | 284 | }
|
294 | 285 |
|
295 | 286 | .tooltip.top.animation-puff.show {
|
|
303 | 294 | opacity: 0;
|
304 | 295 | transform: translate(-50%, calc(100% + var(--tooltip-offset-y))) scale(2, 2);
|
305 | 296 | transform-origin: 50% 50%;
|
306 |
| - transition: opacity 0.3s ease-in-out, filter 0.3s ease-in-out, transform 0.3s ease-in-out; |
| 297 | + transition: opacity 0.25s ease-in-out, filter 0.25s ease-in-out, transform 0.25s ease-in-out; |
307 | 298 | }
|
308 | 299 |
|
309 | 300 | .tooltip.bottom.animation-puff.show {
|
|
318 | 309 | opacity: 0;
|
319 | 310 | transform: translate(calc(-100% - var(--tooltip-offset-x)), -50%) scale(1.2, 1.2);
|
320 | 311 | transform-origin: 50% 50%;
|
321 |
| - transition: opacity 0.3s ease-in-out, transform 0.3s cubic-bezier(0.5, -1, 0.5, 3); |
| 312 | + transition: opacity 0.25s ease-in-out, transform 0.25s cubic-bezier(0.5, -1, 0.5, 3); |
322 | 313 | }
|
323 | 314 |
|
324 | 315 | .tooltip.left.animation-bounce.show {
|
|
330 | 321 | opacity: 0;
|
331 | 322 | transform: translate(calc(100% + var(--tooltip-offset-x)), -50%) scale(1.2, 1.2);
|
332 | 323 | transform-origin: 50% 50%;
|
333 |
| - transition: opacity 0.3s ease-in-out, transform 0.3s cubic-bezier(0.5, -1, 0.5, 3); |
| 324 | + transition: opacity 0.25s ease-in-out, transform 0.25s cubic-bezier(0.5, -1, 0.5, 3); |
334 | 325 | }
|
335 | 326 |
|
336 | 327 | .tooltip.right.animation-bounce.show {
|
|
342 | 333 | opacity: 0;
|
343 | 334 | transform: translate(-50%, calc(-100% - var(--tooltip-offset-y))) scale(1.2, 1.2);
|
344 | 335 | transform-origin: 50% 50%;
|
345 |
| - transition: opacity 0.3s ease-in-out, transform 0.3s cubic-bezier(0.5, -1, 0.5, 3); |
| 336 | + transition: opacity 0.25s ease-in-out, transform 0.25s cubic-bezier(0.5, -1, 0.5, 3); |
346 | 337 | }
|
347 | 338 |
|
348 | 339 | .tooltip.top.animation-bounce.show {
|
|
354 | 345 | opacity: 0;
|
355 | 346 | transform: translate(-50%, calc(100% + var(--tooltip-offset-y))) scale(1.2, 1.2);
|
356 | 347 | transform-origin: 50% 50%;
|
357 |
| - transition: opacity 0.3s ease-in-out, transform 0.3s cubic-bezier(0.5, -1, 0.5, 3); |
| 348 | + transition: opacity 0.25s ease-in-out, transform 0.25s cubic-bezier(0.5, -1, 0.5, 3); |
358 | 349 | }
|
359 | 350 |
|
360 | 351 | .tooltip.bottom.animation-bounce.show {
|
|
0 commit comments