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,20 +144,16 @@
return 1 - lim * 0.1 return 1 - lim * 0.1
} }
onMount(() => { onDestroy(() => {
if (forceModal) {
modal.set(true)
}
return () => {
if (visible) { if (visible) {
hide() hide()
} }
}
}) })
</script> </script>
{#if visible} {#if visible}
<Portal target=".modal-container"> <Portal target=".modal-container">
<div class="drawer-container">
{#if $modal} {#if $modal}
<div transition:fade={{ duration: 260 }} class="underlay" /> <div transition:fade={{ duration: 260 }} class="underlay" />
{/if} {/if}
@ -182,6 +176,7 @@
<div class="overlay" transition:fade|local={{ duration: 260 }} /> <div class="overlay" transition:fade|local={{ duration: 260 }} />
{/if} {/if}
</div> </div>
</div>
</Portal> </Portal>
{/if} {/if}