-
Notifications
You must be signed in to change notification settings - Fork 1
/
Overlay.svelte
52 lines (40 loc) · 1.15 KB
/
Overlay.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<!--
Copyright © 2022 The Radicle Design System Contributors
This file is part of radicle-design-system, distributed under the GPLv3
with Radicle Linking Exception. For full terms see the included
LICENSE file.
-->
<script lang="ts" context="module">
import { writable } from "svelte/store";
const current = writable<HTMLDivElement | undefined>(undefined);
const open = (component: HTMLDivElement): void => {
current.set(component);
};
const close = (): void => {
current.set(undefined);
};
</script>
<script lang="ts">
import { createEventDispatcher } from "svelte";
export let expanded: boolean;
export let style: string | undefined = undefined;
let container: HTMLDivElement;
const dispatch = createEventDispatcher();
const handleClick = (ev: MouseEvent) => {
const component = $current;
const inside = component && component.contains(ev.target as HTMLDivElement);
if (!inside) {
close();
}
};
$: if (expanded) {
open(container);
}
$: if ($current !== container) {
dispatch("hide");
}
</script>
<svelte:window on:click={handleClick} />
<div bind:this={container} {style}>
<slot />
</div>