Tooltip.svelte 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <script lang="ts">
  2. import DOMPurify from 'dompurify';
  3. import { onDestroy } from 'svelte';
  4. import { marked } from 'marked';
  5. import tippy from 'tippy.js';
  6. import { roundArrow } from 'tippy.js';
  7. export let placement = 'top';
  8. export let content = `I'm a tooltip!`;
  9. export let touch = true;
  10. export let className = 'flex';
  11. export let theme = '';
  12. export let allowHTML = true;
  13. let tooltipElement;
  14. let tooltipInstance;
  15. $: if (tooltipElement && content) {
  16. if (tooltipInstance) {
  17. tooltipInstance.setContent(DOMPurify.sanitize(content));
  18. } else {
  19. tooltipInstance = tippy(tooltipElement, {
  20. content: DOMPurify.sanitize(content),
  21. placement: placement,
  22. allowHTML: allowHTML,
  23. touch: touch,
  24. ...(theme !== '' ? { theme } : { theme: 'dark' }),
  25. arrow: false,
  26. offset: [0, 4]
  27. });
  28. }
  29. } else if (tooltipInstance && content === '') {
  30. if (tooltipInstance) {
  31. tooltipInstance.destroy();
  32. }
  33. }
  34. onDestroy(() => {
  35. if (tooltipInstance) {
  36. tooltipInstance.destroy();
  37. }
  38. });
  39. </script>
  40. <div bind:this={tooltipElement} aria-label={DOMPurify.sanitize(content)} class={className}>
  41. <slot />
  42. </div>