Tooltip.svelte 667 B

12345678910111213141516171819202122232425262728293031323334
  1. <script lang="ts">
  2. import { onDestroy } from 'svelte';
  3. import tippy from 'tippy.js';
  4. export let placement = 'top';
  5. export let content = `I'm a tooltip!`;
  6. export let touch = true;
  7. let tooltipElement;
  8. let tooltipInstance;
  9. $: if (tooltipElement && content) {
  10. if (tooltipInstance) {
  11. tooltipInstance.setContent(content);
  12. } else {
  13. tooltipInstance = tippy(tooltipElement, {
  14. content: content,
  15. placement: placement,
  16. allowHTML: true,
  17. touch: touch
  18. });
  19. }
  20. }
  21. onDestroy(() => {
  22. if (tooltipInstance) {
  23. tooltipInstance.destroy();
  24. }
  25. });
  26. </script>
  27. <div bind:this={tooltipElement} aria-label={content} class="flex">
  28. <slot />
  29. </div>