|
5 | 5 | import { formatVariableKey, getMinWidth, isInViewport } from './helpers'; |
6 | 6 | import { inverse } from './constants'; |
7 | 7 |
|
| 8 | + /** @type {'hover' | 'click' | 'prop' | string} */ |
8 | 9 | export let action = 'hover'; |
| 10 | +
|
| 11 | + /** @type {string | {component: any, props?: Record<string, any>}} */ |
9 | 12 | export let content = ''; |
| 13 | +
|
| 14 | + /** @type {'left' | string} */ |
10 | 15 | export let align = 'left'; |
| 16 | +
|
| 17 | + /** @type {'top' | string} */ |
11 | 18 | export let position = 'top'; |
| 19 | +
|
| 20 | + /** @type {number} */ |
12 | 21 | export let maxWidth = 200; |
13 | | - /** |
14 | | - * @type {{ [x: string]: any; } | null} |
15 | | - */ |
16 | | - export let style = null; |
| 22 | +
|
| 23 | + /** @type {Record<string, string> | null} */ |
| 24 | + export let style = null; |
| 25 | +
|
| 26 | + /** @type {string} */ |
17 | 27 | export let theme = ''; |
| 28 | +
|
| 29 | + /** @type {string} */ |
18 | 30 | export let animation = ''; |
| 31 | +
|
| 32 | + /** @type {boolean} */ |
19 | 33 | export let arrow = true; |
| 34 | +
|
| 35 | + /** @type {boolean} */ |
20 | 36 | export let autoPosition = false; |
21 | 37 |
|
22 | | - /** |
23 | | - * @type {HTMLDivElement | null} |
24 | | - */ |
25 | | - let ref = null; |
| 38 | + /** @type {boolean} */ |
| 39 | + export let show = false; |
| 40 | +
|
| 41 | + /** @type {HTMLDivElement | null} */ |
| 42 | + let tooltipRef = null; |
| 43 | +
|
| 44 | + /** @type {number} */ |
26 | 45 | let minWidth = 0; |
27 | | - /** |
28 | | - * @type {{ $destroy: () => void; } | null} |
29 | | - */ |
| 46 | +
|
| 47 | + /** @type {any} */ |
30 | 48 | let component = null; |
31 | | - /** |
32 | | - * @type {string | null} |
33 | | - */ |
| 49 | +
|
| 50 | + /** @type {string | null} */ |
34 | 51 | let animationEffect = null; |
35 | | - let show = false; |
36 | 52 |
|
37 | | - onMount(() => { |
38 | | - const delay = animation ? 200 : 0; |
| 53 | + /** @type {boolean} */ |
| 54 | + let visible = false; |
39 | 55 |
|
40 | | - if (ref !== null) { |
| 56 | + const delay = animation ? 200 : 0; |
| 57 | +
|
| 58 | + onMount(() => { |
| 59 | + if (tooltipRef !== null) { |
41 | 60 | if (isComponent && !component) { |
42 | 61 | // @ts-ignore |
43 | | - component = new content.component({ target: ref, props: { action, ...content.props } }); |
| 62 | + component = new content.component({ target: tooltipRef, props: { action, ...content.props } }); |
44 | 63 | } |
45 | 64 |
|
46 | | - minWidth = getMinWidth(ref, maxWidth); |
| 65 | + minWidth = getMinWidth(tooltipRef, maxWidth); |
47 | 66 |
|
48 | 67 | if (style && typeof style === 'object') { |
49 | 68 | for (let prop in style) { |
50 | 69 | const key = formatVariableKey(prop); |
51 | 70 | const value = style[prop]; |
52 | 71 |
|
53 | | - ref.style.setProperty(`--tooltip-${key}`, value); |
| 72 | + tooltipRef.style.setProperty(`--tooltip-${key}`, value); |
54 | 73 | } |
55 | 74 | } |
56 | 75 | } |
57 | 76 |
|
58 | | - if (autoPosition && !isInViewport(ref)) { |
| 77 | + if (autoPosition && !isInViewport(tooltipRef)) { |
59 | 78 | // @ts-ignore |
60 | 79 | position = inverse[position]; |
61 | 80 | } |
|
64 | 83 | animationEffect = animation; |
65 | 84 | } |
66 | 85 |
|
67 | | - setTimeout(() => (show = true), delay); |
| 86 | + setTimeout(() => (visible = true), delay); |
68 | 87 | }); |
69 | 88 |
|
70 | 89 | onDestroy(() => { |
|
75 | 94 | }); |
76 | 95 |
|
77 | 96 | $: isComponent = typeof content === 'object'; |
| 97 | + $: tooltipRef && show ? setTimeout(() => (visible = true), delay) : (visible = false); |
78 | 98 | </script> |
79 | 99 |
|
80 | 100 | {#if content} |
81 | 101 | <div |
82 | | - bind:this={ref} |
| 102 | + bind:this={tooltipRef} |
83 | 103 | class="tooltip animation-{animationEffect} {position} {theme}" |
84 | | - class:show |
| 104 | + class:show={visible} |
85 | 105 | class:arrowless={!arrow} |
86 | 106 | style="min-width: {minWidth}px; max-width: {maxWidth}px; text-align: {align};" |
87 | 107 | > |
|
0 commit comments