-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathTooltip.svelte
218 lines (194 loc) · 5.98 KB
/
Tooltip.svelte
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<script>
import { fly } from 'svelte/transition'
/** Type (color) of the tooltip
* @svelte-prop {String} [type=is-primary]
* @values <code>is-white</code>, <code>is-black</code>, <code>is-light</code>, <code>is-dark</code>, <code>is-primary</code>, <code>is-info</code>, <code>is-success</code>, <code>is-warning</code>, <code>is-danger</code>, and any other colors you've set in the <code>$colors</code> list on Sass
* */
export let type = 'is-primary'
/** Whether tooltip is active or not
* @svelte-prop {Boolean} [active=true]
* */
export let active = true
/** Tooltip text
* @svelte-prop {String} label
* */
export let label = ''
/** Tooltip position in relation to the element
* @svelte-prop {String} [position=is-top]
* @values <code>is-top</code>, <code>is-bottom</code>, <code>is-top-left</code>, <code>is-top-right</code>, <code>is-bottom-left</code>, <code>is-bottom-right</code>
* */
export let position = 'is-top'
/** Tooltip will be always active
* @svelte-prop {Boolean} [always=false]
* */
export let always = false
/** Tooltip will have fly animation, customizable
* @svelte-prop {Boolean|Object} [animate=true]
* */
export let animate = true
/** Tooltip will be square (not rounded corners)
* @svelte-prop {Boolean} [square=false]
* */
export let square = false
/** Tooltip slot will have a dashed underline
* @svelte-prop {Boolean} [dashed=false]
* */
export let dashed = false
/** Tooltip will be multilined
* @svelte-prop {Boolean} [multilined=false]
* */
export let multilined = false
/** Tooltip multiline size (only works for multilined tooltips)
* @svelte-prop {String} [size=is-medium]
* @values <code>is-small</code>, <code>is-medium</code>, <code>is-large</code>
* */
export let size = 'is-medium'
/** Tooltip will be fully rounded on left and right edges
* @svelte-prop {Boolean} [rounded=false]
* */
export let rounded = false
/** Tooltip style override
* @svelte-prop {String} [style=undefined]
* */
export let style = undefined
let hovering = false
let animationProps
$: {
if (animate === false || animate === 'false') animationProps = { duration: 0 }
else if (animate != null && typeof animate === "object") animationProps = animate
else {
// default animation props
switch (position) {
case 'is-top':
animationProps = { x: 0, y: -10 }
break
case 'is-right':
animationProps = { x: 10, y: 0 }
break
case 'is-bottom':
animationProps = { x: 0, y: 10 }
break
case 'is-left':
animationProps = { x: -10, y: 0 }
break
}
animationProps = { ...animationProps, duration: 200 }
}
}
</script>
<style lang="scss">
$tooltip-arrow-size: 8px;
$tooltip-arrow-margin: 2px;
@mixin tooltip($direction) {
&.#{$direction} {
// for tooltip
@if ($direction == 'is-top') {
top: auto;
right: auto;
bottom: calc(100% + #{$tooltip-arrow-size} + #{$tooltip-arrow-margin});
left: 50%;
transform: translateX(-50%);
} @else if ($direction == 'is-bottom') {
top: calc(100% + #{$tooltip-arrow-size} + #{$tooltip-arrow-margin});
right: auto;
bottom: auto;
left: 50%;
transform: translateX(-50%);
} @else if ($direction == 'is-right') {
top: 50%;
right: auto;
bottom: auto;
left: calc(100% + #{$tooltip-arrow-size} + #{$tooltip-arrow-margin});
transform: translateY(-50%);
} @else if ($direction == 'is-left') {
top: 50%;
right: calc(100% + #{$tooltip-arrow-size} + #{$tooltip-arrow-margin});
bottom: auto;
left: auto;
transform: translateY(-50%);
}
// for tooltip arrow
&:after {
@if ($direction == 'is-top') {
top: auto;
left: auto;
right: auto;
bottom: calc(-1 * #{$tooltip-arrow-size});
transform: translateY(-50%) rotate(45deg);
} @else if ($direction == 'is-bottom') {
top: calc(-1 * #{$tooltip-arrow-size});
left: auto;
right: auto;
bottom: auto;
transform: translateY(50%) rotate(45deg);
} @else if ($direction == 'is-right') {
top: auto;
left: calc(-1 * #{$tooltip-arrow-size});
right: auto;
bottom: auto;
transform: translateX(50%) rotate(45deg);
} @else if ($direction == 'is-left') {
top: auto;
left: auto;
right: calc(-1 * #{$tooltip-arrow-size});
bottom: auto;
transform: translateX(-50%) rotate(45deg);
}
}
}
}
.tooltip-wrapper {
position: relative;
display: inline-flex;
}
.tooltip {
@include tooltip('is-top');
@include tooltip('is-right');
@include tooltip('is-bottom');
@include tooltip('is-left');
position: absolute;
box-shadow: 0px 1px 2px 1px rgba(0, 1, 0, 0.2);
z-index: 888;
&.is-square {
border-radius: 0;
}
&.is-dashed {
text-decoration-style: dashed;
text-decoration-line: underline;
}
&.is-multiline {
height: revert;
padding: 5px 10px;
text-align: center;
white-space: normal;
}
// tooltip arrow
&:after {
content: '';
position: absolute;
width: $tooltip-arrow-size;
height: $tooltip-arrow-size;
background-color: inherit;
overflow: hidden;
}
}
</style>
<div class="tooltip-wrapper">
<div class="tooltip-trigger" on:mouseenter={() => (hovering = true)} on:mouseleave={() => (hovering = false)}>
<slot />
</div>
{#if always || (active && hovering)}
<div
transition:fly={animationProps}
class="tooltip tag {type}
{size}
{position}"
class:is-rounded={rounded}
class:is-dashed={dashed}
class:is-square={square}
class:is-multiline={multilined}
{style}>
{label}
</div>
{/if}
</div>