Tooltip.svelte 589 Bytes
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
2
3
4
5
6
7
8
9
10
11
12
<script lang="ts">
	import { onDestroy } from 'svelte';
	import tippy from 'tippy.js';

	export let placement = 'top';
	export let content = `I'm a tooltip!`;

	let tooltipElement;
	let tooltipInstance;

	$: if (tooltipElement && content) {
		if (tooltipInstance) {
Timothy J. Baek's avatar
Timothy J. Baek committed
13
14
15
16
17
18
19
			tooltipInstance.setContent(content);
		} else {
			tooltipInstance = tippy(tooltipElement, {
				content: content,
				placement: placement,
				allowHTML: true
			});
Timothy J. Baek's avatar
Timothy J. Baek committed
20
21
22
23
24
		}
	}

	onDestroy(() => {
		if (tooltipInstance) {
Timothy J. Baek's avatar
Timothy J. Baek committed
25
			tooltipInstance.destroy();
Timothy J. Baek's avatar
Timothy J. Baek committed
26
27
28
29
30
31
32
		}
	});
</script>

<div bind:this={tooltipElement}>
	<slot />
</div>