2024-03-02 09:00:20 +00:00
|
|
|
<script lang="ts">
|
2024-09-21 19:43:54 +00:00
|
|
|
import DOMPurify from 'dompurify';
|
|
|
|
|
|
2024-03-02 09:00:20 +00:00
|
|
|
import { onDestroy } from 'svelte';
|
2024-06-15 10:32:18 +00:00
|
|
|
|
2024-03-02 09:00:20 +00:00
|
|
|
import tippy from 'tippy.js';
|
|
|
|
|
|
2025-07-22 07:19:40 +00:00
|
|
|
export let elementId = '';
|
|
|
|
|
|
2025-09-14 22:49:01 +00:00
|
|
|
export let as = 'div';
|
|
|
|
|
export let className = 'flex';
|
|
|
|
|
|
2024-03-02 09:00:20 +00:00
|
|
|
export let placement = 'top';
|
|
|
|
|
export let content = `I'm a tooltip!`;
|
2024-03-02 09:27:01 +00:00
|
|
|
export let touch = true;
|
2024-07-08 22:26:43 +00:00
|
|
|
export let theme = '';
|
2025-01-12 04:23:26 +00:00
|
|
|
export let offset = [0, 4];
|
2024-09-21 19:43:54 +00:00
|
|
|
export let allowHTML = true;
|
2024-09-23 13:48:12 +00:00
|
|
|
export let tippyOptions = {};
|
2025-08-08 10:04:44 +00:00
|
|
|
export let interactive = false;
|
2024-03-02 09:00:20 +00:00
|
|
|
|
|
|
|
|
let tooltipElement;
|
|
|
|
|
let tooltipInstance;
|
|
|
|
|
|
2025-07-22 07:19:40 +00:00
|
|
|
$: if (tooltipElement && (content || elementId)) {
|
|
|
|
|
let tooltipContent = null;
|
|
|
|
|
|
|
|
|
|
if (elementId) {
|
|
|
|
|
tooltipContent = document.getElementById(`${elementId}`);
|
|
|
|
|
} else {
|
|
|
|
|
tooltipContent = DOMPurify.sanitize(content);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-02 09:00:20 +00:00
|
|
|
if (tooltipInstance) {
|
2025-07-22 07:19:40 +00:00
|
|
|
tooltipInstance.setContent(tooltipContent);
|
2024-03-02 09:20:50 +00:00
|
|
|
} else {
|
2025-07-22 07:19:40 +00:00
|
|
|
if (content) {
|
|
|
|
|
tooltipInstance = tippy(tooltipElement, {
|
|
|
|
|
content: tooltipContent,
|
|
|
|
|
placement: placement,
|
|
|
|
|
allowHTML: allowHTML,
|
|
|
|
|
touch: touch,
|
|
|
|
|
...(theme !== '' ? { theme } : { theme: 'dark' }),
|
|
|
|
|
arrow: false,
|
|
|
|
|
offset: offset,
|
2025-08-11 13:13:22 +00:00
|
|
|
...(interactive ? { interactive: true } : {}),
|
2025-07-22 07:19:40 +00:00
|
|
|
...tippyOptions
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-03-02 09:00:20 +00:00
|
|
|
}
|
2024-06-04 18:13:43 +00:00
|
|
|
} else if (tooltipInstance && content === '') {
|
|
|
|
|
if (tooltipInstance) {
|
|
|
|
|
tooltipInstance.destroy();
|
|
|
|
|
}
|
2024-03-02 09:00:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onDestroy(() => {
|
|
|
|
|
if (tooltipInstance) {
|
2024-03-02 09:20:50 +00:00
|
|
|
tooltipInstance.destroy();
|
2024-03-02 09:00:20 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
</script>
|
|
|
|
|
|
2025-09-14 22:49:01 +00:00
|
|
|
<svelte:element this={as} bind:this={tooltipElement} class={className}>
|
2024-03-02 09:00:20 +00:00
|
|
|
<slot />
|
2025-09-14 22:49:01 +00:00
|
|
|
</svelte:element>
|
2025-07-22 07:19:40 +00:00
|
|
|
|
|
|
|
|
<slot name="tooltip"></slot>
|