Tooltip.svelte 1004 Bytes
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
2
<script lang="ts">
	import { onDestroy } from 'svelte';
Timothy J. Baek's avatar
Timothy J. Baek committed
3
4
	import { marked } from 'marked';

Timothy J. Baek's avatar
Timothy J. Baek committed
5
	import tippy from 'tippy.js';
Timothy J. Baek's avatar
Timothy J. Baek committed
6
	import { roundArrow } from 'tippy.js';
Timothy J. Baek's avatar
Timothy J. Baek committed
7
8
9

	export let placement = 'top';
	export let content = `I'm a tooltip!`;
Timothy J. Baek's avatar
Timothy J. Baek committed
10
	export let touch = true;
11
	export let className = 'flex';
Timothy J. Baek's avatar
Timothy J. Baek committed
12
	export let theme = '';
Timothy J. Baek's avatar
Timothy J. Baek committed
13
14
15
16
17
18

	let tooltipElement;
	let tooltipInstance;

	$: if (tooltipElement && content) {
		if (tooltipInstance) {
Timothy J. Baek's avatar
Timothy J. Baek committed
19
20
21
			tooltipInstance.setContent(content);
		} else {
			tooltipInstance = tippy(tooltipElement, {
Timothy J. Baek's avatar
Timothy J. Baek committed
22
				content: content,
Timothy J. Baek's avatar
Timothy J. Baek committed
23
				placement: placement,
Timothy J. Baek's avatar
Timothy J. Baek committed
24
				allowHTML: true,
Timothy J. Baek's avatar
Timothy J. Baek committed
25
26
27
28
				touch: touch,
				...(theme !== '' ? { theme } : { theme: 'dark' }),
				arrow: false,
				offset: [0, 4]
Timothy J. Baek's avatar
Timothy J. Baek committed
29
			});
Timothy J. Baek's avatar
Timothy J. Baek committed
30
		}
31
32
33
34
	} else if (tooltipInstance && content === '') {
		if (tooltipInstance) {
			tooltipInstance.destroy();
		}
Timothy J. Baek's avatar
Timothy J. Baek committed
35
36
37
38
	}

	onDestroy(() => {
		if (tooltipInstance) {
Timothy J. Baek's avatar
Timothy J. Baek committed
39
			tooltipInstance.destroy();
Timothy J. Baek's avatar
Timothy J. Baek committed
40
41
42
43
		}
	});
</script>

44
<div bind:this={tooltipElement} aria-label={content} class={className}>
Timothy J. Baek's avatar
Timothy J. Baek committed
45
46
	<slot />
</div>