forked from Viewtiful/fs-dialog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs-modal-dialog.html
315 lines (286 loc) · 11 KB
/
fs-modal-dialog.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<link rel="import" href="../fs-globals/fs-globals.html">
<link rel="import" href="./fs-dialog-base.html">
<script>
(function () {
const fsModalDialogStyles = document.createElement('template');
fsModalDialogStyles.setAttribute('style', 'display: none;');
fsModalDialogStyles.innerHTML = `
<style data-fs-modal-dialog>
/*
* 1. The display property cannot be animated so we need to use opacity and visibility
* to fade in
* @see https://stackoverflow.com/questions/3331353/transitions-on-the-display-property
* 2. Delay the visibility property so we can see the dialog disappear
* @see https://codepen.io/matthewlein/pen/fvrLD
* 3. Support a fixed modal header/footer and scrollable middle
* 4. Temporary hack until fs-styles removes background image from fs-dialog__close
* 5. Safari's default active button color seems to be white (activebuttontext)
*/
fs-modal-dialog {
position: fixed;
left: 50%;
top: 50%;
transform: translate(-50%, -50%) scale(0.7);
}
fs-modal-dialog:not([no-transition]) {
transition: opacity 0.3s, visibility 0s linear 0.3s, transform 0.3s; /* [2] */
}
</style>
`;
/**
* `<fs-modal-dialog>` is a modal dialog centered on the screen that is displayed above other content, masks the content underneath it, and prevents the user from interacting with the content underneath.
*
* <body>
* <button onclick="dialog.open({
* focusBackElement: this
* });">Open Dialog</button>
* <fs-modal-dialog
* transition="from-bottom"
* no-close-button>
* <header>
* [Header Text]
* </header>
* <div class="fs-dialog__body">
* [Dialog Body Content]
* </div>
* <footer>
* [Buttons or Footer Text]
* </footer>
* </fs-modal-dialog>
* </body>
*
* @demo demo/index.html
*/
class FSModalDialog extends FS.dialog.baseDialogComponent {
// only used for docs.
static get properties () {
return {
/**
* Whether or not the dialog is opened. Do not set or unset this yourself. Instead, use the open and close functions.
* @type {Boolean} [opened=false]
*/
'opened': {
type: Boolean,
value: false
},
/**
* The open modal transition animation for mobile.
* Default: ''. Valid values: 'from-right', 'from-bottom'.
* @type {String} [transition='from-bottom']
*/
'transition': {
type: String,
value: ''
},
/**
* Whether or not the modal should take up the entire screen below 480px.
* Default: false.
* @type {Boolean} [no-fullscreen-mobile=true]
*/
'no-fullscreen-mobile': {
type: Boolean,
value: false
},
/**
* Whether or not the modal should take up the entire screen regardless of screen size.
* Default: false.
* @type {Boolean} [keep-fullscreen=true]
*/
'keep-fullscreen': {
type: Boolean,
value: false
},
/**
* If you add this attribute to any clickable element within the dialog, it will fire the 'fs-dialog-dismiss' event and close the dialog on click.
* @type {Boolean} [data-dialog-dismiss=false]
*/
'data-dialog-dismiss': {
type: Boolean,
value: false
},
/**
* If you add this attribute to any clickable element within the dialog, it will fire the 'fs-dialog-confirm' event on click.
* @type {Boolean} [data-dialog-confirm=false]
*/
'data-dialog-confirm': {
type: Boolean,
value: false
},
/**
* Whether or not to disable transition animation.
* Default: false.
* @type {Boolean} [no-transition]
*/
'no-transition': {
type: Boolean,
value: false
},
/**
* Whether or not to show the close button x in the upper right corner
* Default: false.
* @type {Boolean} [no-close-button]
*/
'no-close-button': {
type: Boolean,
value: false
}
};
}
/**
* Open the dialog.
* @param {object} config - Object optionally containing the focusBackElement (the element to return focus back to after the dialog closes).
*/
open (config) {
super.open(config);
}
/**
* Close the dialog. If focusBackElement exists, focus is returned to that element.
*/
close () {
super.close();
}
/**
* Initialize the dialog, add events, and accessibility features. Should only change
* the DOM on attached callback otherwise it breaks Polymer bindings.
*/
attachedCallback () {
this.appendStyles(fsModalDialogStyles, 'style[data-fs-modal-dialog]', this);
// selectors
var bodyEl = this.querySelector('.fs-dialog__body');
var maskEl;
// initialize
this._a11yOpen = a11yOpen.bind(this);
this._a11yClose = a11yClose.bind(this);
// use the attachedCallback for the base dialog and pass in our open and close functions
super.attachedCallback(openModalFunction, closeModalFunction);
// each modal dialog will get its own mask so when dialogs stack we can
// know exactly which one to close
maskEl = document.createElement('div');
maskEl.setAttribute('data-no-inert', '');
maskEl.setAttribute('tabindex', '-1');
maskEl.classList.add('fs-dialog__mask');
// add mask as a previous sibling to the dialog so it's below it in z-index
this.parentNode.insertBefore(maskEl, this);
// add role=main to body of a modal dialog to improve accessibility for screen
// readers. can't just use the <main> element since modeless dialogs don't
// hide any <main> elements on the page
// @see https://web-a11y.slack.com/archives/C042TSFGN/p1499454746902809
if (bodyEl) {
bodyEl.setAttribute('role', 'main');
}
var keydownHandler = this.keydownHandler.bind(this);
function openModalFunction () {
this._a11yOpen();
maskEl.addEventListener('keydown', keydownHandler);
if (this.getAttribute('keep-fullscreen') !== undefined && this.getAttribute('keep-fullscreen') !== null) {
maskEl.setAttribute('keep-fullscreen', '');
} else {
maskEl.removeAttribute('keep-fullscreen');
}
if (this.getAttribute('no-transition') !== undefined && this.getAttribute('no-transition') !== null) {
maskEl.setAttribute('no-transition', '');
} else {
maskEl.removeAttribute('no-transition');
}
maskEl.setAttribute('opened', '');
maskEl.style.zIndex = this.style.zIndex;
window.addEventListener('fs-dialog-open', dialogOpenEH);
window.addEventListener('fs-dialog-close', dialogCloseEH);
}
function closeModalFunction () {
window.removeEventListener('fs-dialog-open', dialogOpenEH);
window.removeEventListener('fs-dialog-close', dialogCloseEH);
this._a11yClose();
maskEl.removeAttribute('opened');
maskEl.removeEventListener('keydown', keydownHandler);
}
}
}
function a11yOpen () {
this.inert = false;
// we need to inert every subtree except for the one that contains this dialog
// walk up the DOM tree and add inert to all children except for the child
// that contains the dialog's tree. save each node we inerted so we don't have
// to walk the tree again to uninert nodes
this._inertedElements = [];
this._uninertedElements = [];
var el = this;
do {
// an element that is a child of a shadowroot will have a parentNode but not
// a parentElement. a shadowroot element has neither but instead has a host
var parent = el.parentNode || el.host;
// if the dialogs subtree has already been inerted, then we need to uninert it
if (parent.inert) {
parent.inert = false;
this._uninertedElements.push(parent);
}
for (var i = 0; i < parent.children.length; i++) {
var child = parent.children[i];
// by only adding inert to elements that have not been inerted, we can
// preserve a11y through stacking modals
if (!(child === el || child.hasAttribute('data-no-inert') || child.inert)) {
child.inert = true;
this._inertedElements.push(child);
}
}
el = parent;
} while (el !== document.body);
}
function a11yClose () {
// in Safari, removing the overflow style or uninerting nodes causes a
// visual delay in any css animations so we have to wrap it in a set timeout
// to get it off the current frame (50ms doesn't seem to be enough)
setTimeout(function () {
document.body.style.overflow = null;
if (this._inertedElements) {
// uninert all nodes
this._inertedElements.forEach(function (node) {
node.inert = false;
});
this._uninertedElements.forEach(function (node) {
node.inert = true;
});
this._inertedElements = null;
this._uninertedElements = null;
}
}.bind(this), 100);
}
function uninertChildDialog (childDialog) {
childDialog._uninertedElements = [];
if (childDialog.inert) {
childDialog.inert = false;
childDialog._uninertedElements.push(childDialog);
}
// we have the empty inerted elements so that we can reuse a11y close
// to re-inert the necessary elements after the childDialog gets closed again
childDialog._inertedElements = [];
var el = childDialog;
do {
// an element that is a child of a shadowroot will have a parentNode but not
// a parentElement. a shadowroot element has neither but instead has a host
var parent = el.parentNode || el.host;
// if the dialogs subtree has already been inerted, then we need to uninert it
if (parent.inert) {
parent.inert = false;
childDialog._uninertedElements.push(parent);
}
el = parent;
} while (el !== document.body);
}
function dialogOpenEH (event) {
if (event && event.composedPath && event.composedPath().length && (event.composedPath()[0].tagName === 'FS-ANCHORED-DIALOG' || event.composedPath()[0].tagName === 'FS-MODELESS-DIALOG')) {
uninertChildDialog(event.composedPath()[0]);
}
}
function dialogCloseEH (event) {
if (event && event.composedPath && event.composedPath().length && (event.composedPath()[0].tagName === 'FS-ANCHORED-DIALOG' || event.composedPath()[0].tagName === 'FS-MODELESS-DIALOG')) {
a11yClose.bind(event.composedPath()[0])();
}
}
if ('customElements' in window) {
customElements.define('fs-modal-dialog', FSModalDialog);
} else {
document.registerElement('fs-modal-dialog', {prototype: Object.create(FSModalDialog.prototype)});
}
})();
</script>