Exclude drawers from click_outside callbacks, unless registered inside a drawer

This commit is contained in:
Andrew Kingston 2024-02-27 14:30:21 +00:00
parent afbdaac0db
commit e2fe279842
2 changed files with 34 additions and 32 deletions

View File

@ -32,6 +32,13 @@ const handleClick = event => {
return return
} }
// Ignore clicks for drawers, unless the handler is registered from a drawer
const sourceInDrawer = handler.anchor.closest(".drawer-container") != null
const clickInDrawer = event.target.closest(".drawer-container") != null
if (clickInDrawer && !sourceInDrawer) {
return
}
handler.callback?.(event) handler.callback?.(event)
}) })
} }

View File

@ -46,13 +46,14 @@
} }
observer?.disconnect() observer?.disconnect()
observer = null observer = null
modal.set(false)
} }
</script> </script>
<script> <script>
import Portal from "svelte-portal" import Portal from "svelte-portal"
import Button from "../Button/Button.svelte" import Button from "../Button/Button.svelte"
import { setContext, createEventDispatcher, onMount } from "svelte" import { setContext, createEventDispatcher, onDestroy } from "svelte"
import { generate } from "shortid" import { generate } from "shortid"
import { fade } from "svelte/transition" import { fade } from "svelte/transition"
@ -106,9 +107,6 @@
visible = false visible = false
dispatch("drawerHide", drawerId) dispatch("drawerHide", drawerId)
openDrawers.update(state => state.filter(id => id !== drawerId)) openDrawers.update(state => state.filter(id => id !== drawerId))
if (!$openDrawers.length) {
modal.set(false)
}
unobserve() unobserve()
} }
@ -146,41 +144,38 @@
return 1 - lim * 0.1 return 1 - lim * 0.1
} }
onMount(() => { onDestroy(() => {
if (forceModal) { if (visible) {
modal.set(true) hide()
}
return () => {
if (visible) {
hide()
}
} }
}) })
</script> </script>
{#if visible} {#if visible}
<Portal target=".modal-container"> <Portal target=".modal-container">
{#if $modal} <div class="drawer-container">
<div transition:fade={{ duration: 260 }} class="underlay" /> {#if $modal}
{/if} <div transition:fade={{ duration: 260 }} class="underlay" />
<div
class="drawer"
class:stacked={depth > 0}
class:modal={$modal}
transition:slide|local
{style}
>
<header>
<div class="text">{title || "Bindings"}</div>
<div class="buttons">
<Button secondary quiet on:click={hide}>Cancel</Button>
<slot name="buttons" />
</div>
</header>
<slot name="body" />
{#if !$modal && depth > 0}
<div class="overlay" transition:fade|local={{ duration: 260 }} />
{/if} {/if}
<div
class="drawer"
class:stacked={depth > 0}
class:modal={$modal}
transition:slide|local
{style}
>
<header>
<div class="text">{title || "Bindings"}</div>
<div class="buttons">
<Button secondary quiet on:click={hide}>Cancel</Button>
<slot name="buttons" />
</div>
</header>
<slot name="body" />
{#if !$modal && depth > 0}
<div class="overlay" transition:fade|local={{ duration: 260 }} />
{/if}
</div>
</div> </div>
</Portal> </Portal>
{/if} {/if}