MapComplete/src/UI/Base/If.svelte

27 lines
716 B
Svelte
Raw Normal View History

2023-03-24 19:21:15 +01:00
<script lang="ts">
2023-12-19 22:08:00 +01:00
import { Store } from "../../Logic/UIEventSource"
import { onDestroy } from "svelte"
2023-03-24 19:21:15 +01:00
/**
* For some stupid reason, it is very hard to let {#if} work together with UIEventSources, so we wrap then here
*/
2024-12-26 19:38:33 +01:00
export let condition: Store<boolean> | undefined
let _c = condition?.data
if (condition !== undefined) {
onDestroy(
condition.addCallback((c) => {
/* Do _not_ abbreviate this as `.addCallback(c => _c = c)`. This is the same as writing `.addCallback(c => {return _c = c})`,
which will _unregister_ the callback if `c = true`! */
_c = c
return false
})
)
}
2023-03-24 19:21:15 +01:00
</script>
{#if _c}
<slot />
{:else}
<slot name="else" />
2023-03-24 19:21:15 +01:00
{/if}