-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
227 lines (192 loc) · 10.4 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<title>Modal examples</title>
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
crossorigin="anonymous">
<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/prismjs@1.17.1/themes/prism-okaidia.css">
<script src="https://cdn.jsdelivr.net/npm/prismjs@1.17.1/prism.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/prismjs@1.17.1/plugins/autoloader/prism-autoloader.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@deleteagency/live-highlight@0.0.1/build/live-highlight.min.js"></script>
<script src="demo/dist/modal.min.js"></script>
</head>
<script>
LiveHighlight.bootstrap((element, type) => {
function getLanguage(type) {
switch (type) {
case LiveHighlight.TYPE_JAVASCRIPT:
return 'js';
case LiveHighlight.TYPE_STYLES:
return 'css';
case LiveHighlight.TYPE_HTML:
return 'markup';
}
}
element.className = `lang-${getLanguage(type)}`;
Prism.highlightElement(element);
});
</script>
<body>
<div class="py-4">
<div class="container">
<div class="mb-5">
<h1>Modal examples</h1>
<p>First of all let's style is-opened class which will be applied to modal root once it's opened</p>
<style data-live-highlight>
.modal.is-opened {
display: block;
background-color: rgba(0, 0, 0, 0.5);
}
</style>
</div>
<div class="mb-5">
<h2>Showing existing modal</h2>
<p>
Let's bind a click to create a modal based on existing element.
</p>
<div data-live-highlight-target="modal"></div>
<div data-modal id="existing-outer-element" class="modal" tabindex="-1" role="dialog"
data-live-highlight="modal" aria-hidden="true">
<div data-modal-wrapper class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button type="button" class="close" data-modal-close aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div data-modal-content class="modal-body">
<div id="existing-inner-element">
<p>Modal body text goes here.</p>
</div>
</div>
</div>
</div>
</div>
<button class="btn btn-primary" id="show-outer-element">Show modal</button>
<button class="btn btn-primary" id="show-inner-element">Show content</button>
<script data-live-highlight="modal">
const showOuterElementButton = document.getElementById('show-outer-element');
const showInnerElementButton = document.getElementById('show-inner-element');
const outerElementModal = Modal.create(document.getElementById('existing-outer-element'));
const innerElementModal = Modal.create(document.getElementById('existing-inner-element'));
showOuterElementButton.addEventListener('click', () => {
outerElementModal.open();
});
showInnerElementButton.addEventListener('click', () => {
innerElementModal.open();
});
</script>
<p class="mt-2">
As you can see it doesn't matter if we create a modal based on the very top element of the modal or the content itself:
because of data-modal attribute we can go up the tree and apply logic to the proper element.
data-modal attribute is a key here and you should always declare it when you modals are already contain modal boilerplate markup.
</p>
</div>
<div class="mb-5">
<h2>Wrap existing element and show it within modal</h2>
<p>
Also it's possible to simplify modals creation and not to provide boilerplate markup every time for every modal.
Instead, you can pass only content of the future modal and it will be wrapped up before first open attempt.
The decision about wrapping will be made after searching data-modal element up the tree.
If such element isn't found the passed content will be treated as a content of the modal.
</p>
<p>
To customize default modal boilerplate pass your own template as defaultModalTemplate option of modalService.init method.
Content will be inserted to data-modal-content node
</p>
<script>
Modal.init({
defaultModalTemplate: (modalOptions) => {
return `<div data-modal class="modal" tabindex="-1" role="dialog" aria-hidden="true">
<div data-modal-wrapper class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-modal-close aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div data-modal-content class="modal-body">
</div>
</div>
</div>
</div>`
},
});
</script>
<div data-live-highlight-target="whole-modal"></div>
<div id="existing-content-element">
<p>Content element</p>
</div>
<button class="btn btn-primary" id="show-content-element">Show modal</button>
<script data-live-highlight="whole-modal">
const showContentElementButton = document.getElementById('show-content-element');
const contentElementModal = Modal.create(document.getElementById('existing-content-element'));
showContentElementButton.addEventListener('click', () => {
contentElementModal.open();
});
</script>
<p>
If your content of the modal is just a string you don't have to create an element just to pass it to Modal.create().
Pass your string to the Modal.create() and it will be wrapped in the modal template and inserted to the DOM.
</p>
<textarea id="custom-content" class="form-control mt-3" rows="3"></textarea>
<button class="btn btn-primary mt-3" id="show-text-modal">Show text modal</button>
<script data-live-highlight="whole-modal">
const showTextModal = document.getElementById('show-text-modal');
const textarea = document.getElementById('custom-content');
showTextModal.addEventListener('click', () => {
const textModal = Modal.create(`<div>${textarea.value}</div>`);
textModal.open();
});
</script>
</div>
<div class="mb-5">
<h2>Do not close modal after click outside</h2>
<p>
Usually you want to close the modal once user clicks outside of it, but sometimes it's not what users expects:
for example if you show a form within the modal and the user copy/paste his email he can accidentally close the modal
because the text selection process was over outside of the modal. Another case is when you render a map in popup - dragging the map can lead to the same bad UX.
</p>
<p>
You can disable that behaviour by setting <b>autoCloseOnClickOutside</b> flag to
<b>false</b> (true is default) either globally via Modal.init({autoCloseOnClickOutside: false})
or on the modal instance level new Modal(element, {autoCloseOnClickOutside : false}). The latter has higher priority and always overrides global value.
</p>
<div data-live-highlight-target="autoCloseOnClickOutside"></div>
<div data-modal id="autoCloseOnClickOutside" class="modal" tabindex="-1" role="dialog"
data-live-highlight="autoCloseOnClickOutside" aria-hidden="true">
<div data-modal-wrapper class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button type="button" class="close" data-modal-close aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div data-modal-content class="modal-body">
<p>Modal body text goes here.</p>
</div>
</div>
</div>
</div>
<button class="btn btn-primary"
id="show-autoCloseOnClickOutside">Show modal without closing on click outside
</button>
<script data-live-highlight="modal">
const showAutoCloseOnClickOutsideButton = document.getElementById('show-autoCloseOnClickOutside');
const autoCloseOnClickOutsideModal = Modal.create(document.getElementById('autoCloseOnClickOutside'), { autoCloseOnClickOutside: false });
showAutoCloseOnClickOutsideButton.addEventListener('click', () => {
autoCloseOnClickOutsideModal.open();
});
</script>
</div>
</div>
</div>
</body>
</html>