1
0
Fork 0
mirror of synced 2024-10-02 10:08:09 +13:00

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
}
// 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)
})
}

View file

@ -46,13 +46,14 @@
}
observer?.disconnect()
observer = null
modal.set(false)
}
</script>
<script>
import Portal from "svelte-portal"
import Button from "../Button/Button.svelte"
import { setContext, createEventDispatcher, onMount } from "svelte"
import { setContext, createEventDispatcher, onDestroy } from "svelte"
import { generate } from "shortid"
import { fade } from "svelte/transition"
@ -106,9 +107,6 @@
visible = false
dispatch("drawerHide", drawerId)
openDrawers.update(state => state.filter(id => id !== drawerId))
if (!$openDrawers.length) {
modal.set(false)
}
unobserve()
}
@ -146,41 +144,38 @@
return 1 - lim * 0.1
}
onMount(() => {
if (forceModal) {
modal.set(true)
}
return () => {
if (visible) {
hide()
}
onDestroy(() => {
if (visible) {
hide()
}
})
</script>
{#if visible}
<Portal target=".modal-container">
{#if $modal}
<div transition:fade={{ duration: 260 }} class="underlay" />
{/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 }} />
<div class="drawer-container">
{#if $modal}
<div transition:fade={{ duration: 260 }} class="underlay" />
{/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>
</Portal>
{/if}