-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathModal.svelte
121 lines (106 loc) · 2.68 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<script>
import { onMount, afterUpdate, setContext, createEventDispatcher } from "svelte";
import { writable } from "svelte/store";
import { renderModal, unRenderModal, blockModal, releaseModal } from "./src/modalState";
import "./src/modalInit";
export let open = false;
export let useContentWidth = false;
export let deferStateChangeOnClose = false;
export let modalKey = "";
export let animateResizing = true;
const dispatch = createEventDispatcher();
let onClose = () => {
releaseModal(contentNode);
dispatch("close");
};
const onModalMount = () => {
dispatch("mount");
};
let contentNode;
let currentlyOpen = false;
const isAnimatingResizing = writable(animateResizing);
let closeIt = () => {
currentlyOpen = false;
dispatch("closing");
unRenderModal(contentNode);
};
let onClosed = () => {
dispatch("closed");
};
export const closeModal = () => {
if (deferStateChangeOnClose) {
blockModal(contentNode);
closeIt();
} else {
onClose();
}
};
setContext("svelte-helpers-modal", { isAnimatingResizing, closeModal });
onMount(() => {
sync();
return () => {
if (currentlyOpen) {
closeIt();
}
};
});
afterUpdate(sync);
function sync() {
if (!currentlyOpen && open) {
currentlyOpen = true;
let props = { onClose, onClosed, modalKey, node: contentNode, isAnimatingResizing, useContentWidth, onModalMount };
if (deferStateChangeOnClose) {
props.onClose = closeIt;
props.onHide = onClose;
}
renderModal(props);
} else if (currentlyOpen && !open) {
closeIt();
}
}
</script>
<style>
:global(.svelte-helpers-modal-content) {
--modal-width: 600px;
--modal-content-padding: 2rem;
max-width: var(--modal-width);
width: auto;
margin: 10vh auto;
background: white;
outline: none;
border-radius: 5px;
border: 2px solid darkblue;
overflow: hidden;
}
:global(.svelte-helpers-modal-content > *) {
padding: var(--modal-content-padding);
}
:global(.svelte-helpers-modal-content.content-width) {
display: flex;
width: unset;
max-width: unset;
}
:global(.svelte-helpers-modal-content.content-width > *) {
align-self: baseline;
}
@media (max-width: 800px) {
:global(.svelte-helpers-modal-content) {
--modal-width: 75vw;
}
}
@media (max-width: 600px) {
:global(.svelte-helpers-modal-content) {
--modal-width: 90vw;
}
}
@media (max-width: 500px) {
:global(.svelte-helpers-modal-content) {
--modal-width: 95vw;
}
}
</style>
<div style="display: none;">
<div bind:this={contentNode}>
<slot />
</div>
</div>