flotilla/src/lib/components/Tippy.svelte

37 lines
870 B
Svelte
Raw Normal View History

2024-09-25 20:46:10 +00:00
<script lang="ts">
2024-10-15 19:02:11 +00:00
import "tippy.js/animations/shift-away.css"
2024-10-14 19:35:08 +00:00
2025-02-03 23:40:00 +00:00
import tippy from "tippy.js"
import {onMount, mount, unmount} from "svelte"
2024-09-25 20:46:10 +00:00
2025-02-03 23:40:00 +00:00
let {component, children = undefined, props = {}, params = {}, popover = $bindable(), instance = $bindable(), ...restProps} = $props()
2024-09-25 20:46:10 +00:00
2025-02-03 23:40:00 +00:00
let reactiveProps = $derived(props)
2024-09-25 20:46:10 +00:00
2025-02-03 23:40:00 +00:00
let element: Element
2024-09-25 20:46:10 +00:00
onMount(() => {
2024-10-08 18:39:16 +00:00
if (element) {
const target = document.createElement("div")
2024-09-25 20:46:10 +00:00
2024-10-15 21:00:15 +00:00
popover = tippy(element, {
content: target,
animation: "shift-away",
2024-10-17 23:03:50 +00:00
appendTo: document.querySelector(".tippy-target")!,
2024-10-15 21:00:15 +00:00
...params,
})
2024-09-25 20:46:10 +00:00
2025-02-03 23:40:00 +00:00
instance = mount(component, {target, props: reactiveProps})
2024-09-25 20:46:10 +00:00
2024-10-08 18:39:16 +00:00
return () => {
popover?.destroy()
2025-02-03 23:40:00 +00:00
unmount(instance)
2024-10-08 18:39:16 +00:00
}
2024-09-25 20:46:10 +00:00
}
})
</script>
2025-02-03 23:40:00 +00:00
<div bind:this={element} class={restProps.class}>
{@render children?.()}
2024-10-03 21:32:55 +00:00
</div>