MapComplete/src/UI/Base/ToSvelte.svelte

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

44 lines
1,021 B
Svelte
Raw Normal View History

<script lang="ts">
import BaseUIElement from "../BaseUIElement.js"
import { onDestroy, onMount } from "svelte"
import SvelteUIElement from "./SvelteUIElement"
export let construct: BaseUIElement | (() => BaseUIElement)
let elem: HTMLElement
let html: HTMLElement
let isSvelte = false
2024-07-21 10:52:51 +02:00
let uiElement: BaseUIElement | SvelteUIElement | undefined
let svelteElem: SvelteUIElement
onMount(() => {
uiElement = typeof construct === "function" ? construct() : construct
if (uiElement?.["isSvelte"]) {
isSvelte = true
2024-07-21 10:52:51 +02:00
svelteElem = <SvelteUIElement>uiElement
return
}
html = uiElement?.ConstructElement()
2024-02-20 13:33:38 +01:00
2023-03-28 05:13:48 +02:00
if (html !== undefined) {
elem?.replaceWith(html)
2023-03-28 05:13:48 +02:00
}
})
2023-03-28 05:13:48 +02:00
onDestroy(() => {
html?.remove()
uiElement?.Destroy()
})
</script>
2023-02-03 14:59:08 +01:00
{#if isSvelte}
2024-07-21 10:52:51 +02:00
<svelte:component
this={svelteElem?._svelteComponent}
{...svelteElem._props}
class={svelteElem.getClass()}
style={svelteElem.getStyle()}
/>
{:else}
<span bind:this={elem} />
{/if}