-
Notifications
You must be signed in to change notification settings - Fork 245
/
index.html
248 lines (202 loc) · 7.69 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<!--
Copyright 2020 Google LLC.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!DOCTYPE html>
<html>
<head>
<title>Dialog and polyfill demos</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script src="dist/dialog-polyfill.js"></script>
<link rel="stylesheet" href="dist/dialog-polyfill.css" />
<style>
.demo {
border-top: 1px solid #ccc;
padding-top: 16px;
margin: 16px 0;
}
.demo button {
margin-right: 12px;
}
template {
display: none !important; /* in case we're loaded in an ancient browser */
}
*[hidden] {
display: none !important;
}
dialog.sidebar {
position: fixed;
top: 0;
left: 0;
width: 90%;
max-width: 240px;
bottom: 0;
margin: 0;
height: 100%;
border: none;
box-shadow: 0 0 4px rgba(0, 0, 0, 0.125);
}
</style>
<style id="backdrop-style">
</style>
</head>
<body>
<h1>HTML Dialog Demos</h1>
<p>This page demonstrates HTML's <code><dialog></code> element. If your browser does not have native support, then support is provided by the <a href="https://github.com/GoogleChrome/dialog-polyfill">polyfill</a>.</p>
<p hidden style="color: red;" id="error"></p>
<p><strong>Native support: <span id="support"></span></strong></p>
<template id="demo-modal">
<p>I'm a modal dialog! This blocks the page from being accessed behind me, and is a core reason <code><dialog></code> is useful.</p>
<p>You can also hit the <code>ESC</code> on your keyboard to close the top-most modal dialog.</p>
</template>
<template id="demo-nonmodal">
<p>This dialog opens but does not block the page from being accessed. It was opened with <code>dialog.show()</code>.</p>
</template>
<template id="demo-form">
<p>Dialog adds a new HTML form type: <code><form method="dialog"></code>.</p>
<p>Submitting the form doesn't submit to a new page, but rather, closes the dialog by default.</p>
<p>(It generates a <code>submit</code> event, which if not prevented, closes the dialog.)</p>
<p>Try clicking on one of the buttons below!</p>
<form method="dialog">
<label>Enter an ice-cream flavor: <input type="text" name="icecream" placeholder="ice-cream name" /></label>
<input type="submit" value="Vote for it" />
<input type="submit" value="Vote against it" />
</form>
</template>
<template id="demo-noesc">
<p>This dialog adds a handler on its <code>cancel</code> event, preventing it. This stops the <code>ESC</code> key from closing the dialog.</p>
<p>You have to click the button below (or you could not have a button, and only close the dialog some other way).</p>
</template>
<template id="demo-multiple">
<p>This shows opening multiple modal dialogs.</p>
</template>
<template id="demo-sidebar">
<p>This repurposes a <code>dialog</code> as a sidebar!</p>
<p>While the dialog, by default, shows centered where it was opened, it's easy to place it in fixed positions on your page.</p>
</template>
<template id="demo-backdrop">
<p>Click the backdrop of the dialog to change its color.</p>
<p>This works by adding a click handler on the <code><dialog></code> but wrapping the normal contents with another element.
If clicks are detected on the dialog directly, then we know the backdrop was clicked.</p>
<p>See the polyfill README on how to style the backdrop itself.</p>
</template>
<script>
window.addEventListener('error', function(event) {
var errorNode = document.getElementById('error');
errorNode.textContent = event.error.toString();
errorNode.removeAttribute('hidden');
});
</script>
<script>
(function() {
const supportNode = document.getElementById('support');
const testDialog = document.createElement('dialog');
if (testDialog.showModal) {
supportNode.style.color = 'blue';
supportNode.textContent = 'YES';
} else {
supportNode.style.color = 'red';
supportNode.textContent = 'NO';
}
function createDemo(title, content, options) {
options = Object.assign({
modal: true,
close: true,
className: '',
wrap: false,
}, options);
const dialog = document.createElement('dialog');
dialogPolyfill.registerDialog(dialog);
const dialogContent = options.wrap ? document.createElement('div') : dialog;
if (dialogContent !== dialog) {
dialog.append(dialogContent);
}
if (typeof content === 'string') {
dialogContent.innerHTML = content;
} else {
dialogContent.append(content);
}
if (options.className) {
dialog.className = options.className;
}
const button = createButton('Show', options.modal ? () => dialog.showModal() : () => dialog.show());
const description = Object.assign(document.createElement('span'), {textContent: title});
const holder = Object.assign(document.createElement('div'), {className: 'demo'});
holder.append(dialog, button, description);
if (options.close) {
dialogContent.append(createButton('Close', () => dialog.close()));
}
document.body.append(holder);
return dialog;
}
function createButton(title, handler) {
const button = document.createElement('button');
button.textContent = title;
button.addEventListener('click', handler);
return button;
}
function fromTemplate(id) {
const template = document.getElementById(id);
return template.content.cloneNode(true);
}
// Demos below.
createDemo('Basic modal', fromTemplate('demo-modal'));
createDemo('Basic non-modal', fromTemplate('demo-nonmodal'), {modal: false});
const demoFormDialog = createDemo('Dialog form', fromTemplate('demo-form'), {close: false});
const parentDemo = demoFormDialog.parentNode;
const resultNode = document.createElement('span');
parentDemo.append(resultNode);
demoFormDialog.addEventListener('close', (ev) => {
if (demoFormDialog.returnValue) {
resultNode.textContent = `, closed with return value: ${demoFormDialog.returnValue}`;
}
});
const demoNoEscape = createDemo('Prevent ESC key', fromTemplate('demo-noesc'));
demoNoEscape.addEventListener('cancel', (ev) => {
ev.preventDefault();
});
const demoMultiple = createDemo('Multiple modals', fromTemplate('demo-multiple'));
const createAnotherModal = (count = 1) => {
const dialog = document.createElement('dialog');
dialogPolyfill.registerDialog(dialog);
const notes = Object.assign(document.createElement('p'), {textContent: `Modal ${count}`});
const buttonMore = createButton(`Open Another Modal (${count})`, createAnotherModal.bind(null, count + 1));
const buttonClose = createButton('Close', () => dialog.close());
dialog.append(notes, buttonMore, buttonClose);
document.body.append(dialog);
dialog.showModal();
};
demoMultiple.prepend(createButton('Open Another Modal', () => createAnotherModal()))
createDemo('Dialog as sidebar', fromTemplate('demo-sidebar'), {className: 'sidebar'});
const demoBackdrop = createDemo('Backdrop demo', fromTemplate('demo-backdrop'), {className: 'backdrop-demo', wrap: true});
const updateBackdrop = (ev) => {
if (ev && ev.target !== demoBackdrop) {
return;
}
const styleNode = document.getElementById('backdrop-style');
const color = `hsla(${Math.random() * 360}, 100%, 85%, 0.5)`;
styleNode.textContent = `
dialog.backdrop-demo + .backdrop {
background: ${color};
cursor: pointer;
}
dialog.backdrop-demo::backdrop {
background: ${color};
cursor: pointer;
}
`;
};
demoBackdrop.addEventListener('click', updateBackdrop);
updateBackdrop();
}());
</script>
</body>
</html>