mapcomplete/UI/Base/IfHidden.svelte

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

35 lines
943 B
Svelte
Raw Normal View History

2023-05-18 15:44:54 +02:00
<script lang="ts">
import { UIEventSource } from "../../Logic/UIEventSource"
import { onDestroy } from "svelte"
2023-05-18 15:44:54 +02:00
/**
* Functions as 'If', but uses 'display:hidden' instead.
*/
export let condition: UIEventSource<boolean>
let _c = condition.data
let hasBeenShownPositive = false
let hasBeenShownNegative = false
onDestroy(
condition.addCallbackAndRun((c) => {
/* Do _not_ abbreviate this as `.addCallback(c => _c = c)`. This is the same as writing `.addCallback(c => {return _c = c})`,
2023-05-18 15:44:54 +02:00
which will _unregister_ the callback if `c = true`! */
hasBeenShownPositive = hasBeenShownPositive || c
hasBeenShownNegative = hasBeenShownNegative || !c
_c = c
return false
})
)
2023-05-18 15:44:54 +02:00
</script>
{#if hasBeenShownPositive}
<span class={_c ? "" : "hidden"}>
<slot />
</span>
2023-05-18 15:44:54 +02:00
{/if}
{#if hasBeenShownNegative}
<span class={_c ? "hidden" : ""}>
<slot name="else" />
</span>
2023-05-18 15:44:54 +02:00
{/if}