-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tippy.svelte
57 lines (46 loc) · 1.44 KB
/
Tippy.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
<script>
import { onMount } from 'svelte';
import tippy from 'tippy.js';
let tooltip;
export let Tippy;
export let target = 'prev'; // Element, next, prev(previous);
export let props = {};
let _content, _target, needUpdate;
$: if (props) (_content = props.content), (needUpdate = true);
function getButton(tooltip, target) {
if (!tooltip || !target) return;
if (typeof target !== 'string') _target = target;
else {
let sibling = target.toLowerCase();
if (sibling === 'next') sibling = 'nextElementSibling';
else if (sibling === 'parent') sibling = 'parentElement';
else if (sibling === 'prev' || sibling === 'previous') {
sibling = 'previousElementSibling';
} else sibling = target;
_target = tooltip[sibling];
}
}
$: getButton(tooltip, target);
function destroy() {
Tippy && Tippy.destroy && Tippy.destroy(), (Tippy = null);
}
function createTooltip(tooltip, target) {
destroy();
if (!tooltip || !target) return;
(Tippy = tippy(target, { ...props, content: tooltip })), (needUpdate = 0);
}
$: createTooltip(tooltip, _target);
function updateTooltip(Tippy) {
if (!Tippy || !needUpdate) return;
Tippy.setProps({ ...props, content: tooltip }), (needUpdate = 0);
}
$: if (needUpdate) updateTooltip(Tippy);
onMount(() => {
return () => destroy();
});
</script>
<div bind:this={tooltip}>
<slot>
{@html _content || ''}
</slot>
</div>