Tooltip.svelte 704 B

1234567891011121314151617181920212223242526272829303132333435
  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. export let className = 'flex';
  8. let tooltipElement;
  9. let tooltipInstance;
  10. $: if (tooltipElement && content) {
  11. if (tooltipInstance) {
  12. tooltipInstance.setContent(content);
  13. } else {
  14. tooltipInstance = tippy(tooltipElement, {
  15. content: content,
  16. placement: placement,
  17. allowHTML: true,
  18. touch: touch
  19. });
  20. }
  21. }
  22. onDestroy(() => {
  23. if (tooltipInstance) {
  24. tooltipInstance.destroy();
  25. }
  26. });
  27. </script>
  28. <div bind:this={tooltipElement} aria-label={content} class={className}>
  29. <slot />
  30. </div>