-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModal.svelte
279 lines (243 loc) · 6.65 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<script>
// from https://svelte.dev/repl/033e824fad0a4e34907666e7196caec4?version=3.4.1
// v0.6.1
import * as svelte from 'svelte';
import { fade } from 'svelte/transition';
import { registerModal } from '../libs/modal/modalService';
const baseSetContext = svelte.setContext;
const SvelteComponent = svelte.SvelteComponent;
export let key = 'simple-modal';
export let closeButton = true;
export let closeOnEsc = true;
export let closeOnOuterClick = true;
export let styleBg = { top: 0, left: 0 };
export let styleWindow = {};
export let styleContent = {};
export let styleCloseButton = {};
export let setContext = baseSetContext;
export let transitionBg = fade;
export let transitionBgProps = { duration: 250 };
export let transitionWindow = transitionBg;
export let transitionWindowProps = transitionBgProps;
const defaultState = {
closeButton,
closeOnEsc,
closeOnOuterClick,
styleBg,
styleWindow,
styleContent,
styleCloseButton,
transitionBg,
transitionBgProps,
transitionWindow,
transitionWindowProps,
};
let state = { ...defaultState };
let Component = null;
let props = null;
let background;
let wrap;
let modalWindow;
const camelCaseToDash = str => str
.replace(/([a-zA-Z])(?=[A-Z])/g, '$1-').toLowerCase();
const toCssString = (props) => Object.keys(props)
.reduce((str, key) => `${str}; ${camelCaseToDash(key)}: ${props[key]}`, '');
const isSvelteComponent = component => SvelteComponent && SvelteComponent.isPrototypeOf(component);
$: cssBg = toCssString(state.styleBg);
$: cssWindow = toCssString(state.styleWindow);
$: cssContent = toCssString(state.styleContent);
$: cssCloseButton = toCssString(state.styleCloseButton);
$: currentTransitionBg = state.transitionBg;
$: currentTransitionWindow = state.transitionWindow;
const toVoid = () => {};
let onOpen = toVoid;
let onClose = toVoid;
let onOpened = toVoid;
let onClosed = toVoid;
const open = (
NewComponent,
newProps = {},
options = {},
callback = {}
) => {
Component = NewComponent;
props = newProps;
state = { ...defaultState, ...options };
onOpen = callback.onOpen || toVoid;
onClose = callback.onClose || toVoid;
onOpened = callback.onOpened || toVoid;
onClosed = callback.onClosed || toVoid;
};
const close = (callback = {}) => {
onClose = callback.onClose || onClose;
onClosed = callback.onClosed || onClosed;
Component = null;
props = null;
};
const handleKeydown = (event) => {
if (state.closeOnEsc && Component && event.key === 'Escape') {
event.preventDefault();
close();
}
if (Component && event.key === 'Tab') {
// trap focus
const nodes = modalWindow.querySelectorAll('*');
const tabbable = Array.from(nodes).filter(node => node.tabIndex >= 0);
let index = tabbable.indexOf(document.activeElement);
if (index === -1 && event.shiftKey) index = 0;
index += tabbable.length + (event.shiftKey ? -1 : 1);
index %= tabbable.length;
tabbable[index].focus();
event.preventDefault();
}
};
const handleOuterClick = (event) => {
if (
state.closeOnOuterClick && (
event.target === background || event.target === wrap
)
) {
event.preventDefault();
close();
}
};
const isOpened = () => {
return !!Component;
}
setContext(key, { open, close });
registerModal(open, close, isOpened);
</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);
}
.window-wrap {
position: relative;
margin: 2rem;
max-height: 100%;
}
.window {
position: relative;
width: 40rem;
max-width: 100%;
max-height: 100%;
margin: 2rem auto;
color: black;
border-radius: 0.5rem;
background: white;
}
.modal-content {
position: relative;
max-height: calc(100vh - 4rem);
overflow: auto;
}
.close {
display: block;
box-sizing: border-box;
position: absolute;
z-index: 1000;
top: 1rem;
right: 1rem;
margin: 0;
padding: 0;
width: 1.5rem;
height: 1.5rem;
border: 0;
color: black;
border-radius: 1.5rem;
background: white;
box-shadow: 0 0 0 1px black;
transition: transform 0.2s cubic-bezier(0.25, 0.1, 0.25, 1),
background 0.2s cubic-bezier(0.25, 0.1, 0.25, 1);
-webkit-appearance: none;
}
.close:before, .close:after {
content: '';
display: block;
box-sizing: border-box;
position: absolute;
top: 50%;
width: 1rem;
height: 1px;
background: black;
transform-origin: center;
transition: height 0.2s cubic-bezier(0.25, 0.1, 0.25, 1),
background 0.2s cubic-bezier(0.25, 0.1, 0.25, 1);
}
.close:before {
-webkit-transform: translate(0, -50%) rotate(45deg);
-moz-transform: translate(0, -50%) rotate(45deg);
transform: translate(0, -50%) rotate(45deg);
left: 0.25rem;
}
.close:after {
-webkit-transform: translate(0, -50%) rotate(-45deg);
-moz-transform: translate(0, -50%) rotate(-45deg);
transform: translate(0, -50%) rotate(-45deg);
left: 0.25rem;
}
.close:hover {
background: black;
}
.close:hover:before, .close:hover:after {
height: 2px;
background: white;
}
.close:focus {
border-color: #3399ff;
box-shadow: 0 0 0 2px #3399ff;
}
.close:active {
transform: scale(0.9);
}
.close:hover, .close:focus, .close:active {
outline: none;
}
</style>
<svelte:window on:keydown={handleKeydown}/>
{#if Component}
<div
class="bg"
on:click={handleOuterClick}
bind:this={background}
transition:currentTransitionBg={state.transitionBgProps}
style={cssBg}
>
<div class="window-wrap" bind:this={wrap}>
<div
class="window"
role="dialog"
aria-modal="true"
bind:this={modalWindow}
transition:currentTransitionWindow={state.transitionWindowProps}
on:introstart={onOpen}
on:outrostart={onClose}
on:introend={onOpened}
on:outroend={onClosed}
style={cssWindow}
>
{#if state.closeButton}
{#if isSvelteComponent(state.closeButton)}
<svelte:component this={state.closeButton} onClose={close} />
{:else}
<button on:click={close} class="close" style={cssCloseButton} />
{/if}
{/if}
<div class="modal-content" style={cssContent}>
<svelte:component this={Component} {...props} />
</div>
</div>
</div>
</div>
{/if}
<slot></slot>