-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathModal.svelte
80 lines (73 loc) · 1.57 KB
/
Modal.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<script>
import { onDestroy } from "svelte";
import { fade } from "svelte/transition";
import { modalStore, open, close } from "./modalStore.js";
let state = {};
let isOpen = false;
let props = null;
let Component = null;
let background;
let wrap;
const handleKeyup = ({ key }) => {
if (Component && key === "Escape") {
event.preventDefault();
close();
}
};
const handleOuterClick = event => {
if (event.target === background || event.target === wrap) {
event.preventDefault();
close();
}
};
const unsubscribe = modalStore.subscribe(value => {
Component = value.Component;
props = value.props;
isOpen = value.isOpen;
});
onDestroy(unsubscribe);
</script>
<style>
* {
box-sizing: border-box;
}
.bg {
position: fixed;
z-index: 1000;
display: flex;
flex-direction: column;
justify-content: center;
width: 100vw;
height: 100vh;
background: rgba(0, 0, 0, 0.66);
transition: opacity 200ms ease 0s;
top: 0;
left: 0;
}
.window-wrap {
position: relative;
}
.content {
position: relative;
}
</style>
<svelte:window on:keyup={handleKeyup} />
<div>
{#if isOpen}
<div
class="bg"
on:click={handleOuterClick}
bind:this={background}
transition:fade={{ duration: 300 }}>
<div
class="window-wrap"
bind:this={wrap}
transition:fade={{ duration: 300 }}>
<div class="content">
<svelte:component this={Component} {...props} />
</div>
</div>
</div>
{/if}
<slot />
</div>