van11y-accessible-modal-window-aria.js
16.8 KB
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/**
* ES2015 accessible modal window system, using ARIA
*
* Forked from van11y.net
*
* Website: https://van11y.net/accessible-modal/
* License MIT: https://github.com/nico3333fr/van11y-accessible-modal-window-aria/blob/master/LICENSE
*/
'use strict';
(function (doc) {
'use strict';
var MODAL_JS_CLASS = 'js-modal';
var MODAL_ID_PREFIX = 'label_modal_';
var MODAL_CLASS_SUFFIX = 'modal';
var MODAL_DATA_BACKGROUND_ATTR = 'data-modal-background-click';
var MODAL_PREFIX_CLASS_ATTR = 'data-modal-prefix-class';
var MODAL_TEXT_ATTR = 'data-modal-text';
var MODAL_CONTENT_ID_ATTR = 'data-modal-content-id';
var MODAL_DESCRIBEDBY_ID_ATTR = 'data-modal-describedby-id';
var MODAL_TITLE_ATTR = 'data-modal-title';
var MODAL_FOCUS_TO_ATTR = 'data-modal-focus-toid';
var MODAL_CLOSE_TEXT_ATTR = 'data-modal-close-text';
var MODAL_ROLE = 'dialog';
var MODAL_BUTTON_CLASS_SUFFIX = 'modal-close';
var MODAL_BUTTON_JS_ID = 'js-modal-close';
var MODAL_BUTTON_JS_CLASS = 'js-modal-close';
var MODAL_BUTTON_CONTENT_BACK_ID = 'data-content-back-id';
var MODAL_BUTTON_FOCUS_BACK_ID = 'data-focus-back';
var MODAL_WRAPPER_CLASS_SUFFIX = 'modal__wrapper';
var MODAL_CONTENT_CLASS_SUFFIX = 'modal__content';
var MODAL_CONTENT_JS_ID = 'js-modal-content';
var MODAL_CLOSE_TEXT_CLASS_SUFFIX = 'modal-close__text';
var MODAL_TITLE_ID = 'modal-title';
var MODAL_TITLE_CLASS_SUFFIX = 'modal-title';
var FOCUSABLE_ELEMENTS_STRING = "a[href], area[href], input:not([type='hidden']):not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), iframe, object, embed, *[tabindex], *[contenteditable]";
var WRAPPER_PAGE_JS = 'js-modal-page';
var MODAL_JS_ID = 'js-modal';
var MODAL_OVERLAY_ID = 'js-modal-overlay';
var MODAL_OVERLAY_CLASS_SUFFIX = 'modal-overlay';
var MODAL_OVERLAY_TXT = 'Close modal';
var MODAL_OVERLAY_BG_ENABLED_ATTR = 'data-background-click';
var VISUALLY_HIDDEN_CLASS = 'invisible';
var NO_SCROLL_CLASS = 'mc-no-scroll';
var ATTR_ROLE = 'role';
var ATTR_OPEN = 'open';
var ATTR_LABELLEDBY = 'aria-labelledby';
var ATTR_DESCRIBEDBY = 'aria-describedby';
var ATTR_HIDDEN = 'aria-hidden';
//const ATTR_MODAL = 'aria-modal="true"';
var ATTR_HASPOPUP = 'aria-haspopup';
var ATTR_HASPOPUP_VALUE = 'dialog';
var findById = function findById(id) {
return doc.getElementById(id);
};
var addClass = function addClass(el, className) {
if (el.classList) {
el.classList.add(className); // IE 10+
} else {
el.className += ' ' + className; // IE 8+
}
};
var removeClass = function removeClass(el, className) {
if (el.classList) {
el.classList.remove(className); // IE 10+
} else {
el.className = el.className.replace(new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)', 'gi'), ' '); // IE 8+
}
};
var hasClass = function hasClass(el, className) {
if (el.classList) {
return el.classList.contains(className); // IE 10+
} else {
return new RegExp('(^| )' + className + '( |$)', 'gi').test(el.className); // IE 8+ ?
}
};
/*const wrapInner = (el, wrapper_el) => { // doesn't work on IE/Edge, f…
while (el.firstChild)
wrapper_el.append(el.firstChild);
el.append(wrapper_el);
}*/
function wrapInner(parent, wrapper) {
if (typeof wrapper === "string") wrapper = document.createElement(wrapper);
parent.appendChild(wrapper);
while (parent.firstChild !== wrapper) wrapper.appendChild(parent.firstChild);
}
function remove(el) {
/* node.remove() is too modern for IE≤11 */
el.parentNode.removeChild(el);
}
/* gets an element el, search if it is child of parent class, returns id of the parent */
var searchParent = function searchParent(el, parentClass) {
var found = false;
var parentElement = el.parentNode;
while (parentElement && found === false) {
if (hasClass(parentElement, parentClass) === true) {
found = true;
} else {
parentElement = parentElement.parentNode;
}
}
if (found === true) {
return parentElement.getAttribute('id');
} else {
return '';
}
};
/**
* Create the template for an overlay
* @param {Object} config
* @return {String}
*/
var createOverlay = function createOverlay(config) {
var id = MODAL_OVERLAY_ID;
var overlayText = config.text || MODAL_OVERLAY_TXT;
var overlayClass = config.prefixClass + MODAL_OVERLAY_CLASS_SUFFIX;
var overlayBackgroundEnabled = config.backgroundEnabled === 'disabled' ? 'disabled' : 'enabled';
return '<span\n id="' + id + '"\n class="' + overlayClass + '"\n ' + MODAL_OVERLAY_BG_ENABLED_ATTR + '="' + overlayBackgroundEnabled + '"\n title="' + overlayText + '"\n >\n <span class="' + VISUALLY_HIDDEN_CLASS + '">' + overlayText + '</span>\n </span>';
};
/**
* Create the template for a modal
* @param {Object} config
* @return {String}
*/
var createModal = function createModal(config) {
var id = MODAL_JS_ID;
var modalClassName = config.modalPrefixClass + MODAL_CLASS_SUFFIX;
var modalClassWrapper = config.modalPrefixClass + MODAL_WRAPPER_CLASS_SUFFIX;
var buttonCloseClassName = config.modalPrefixClass + MODAL_BUTTON_CLASS_SUFFIX;
var buttonCloseInner = '<span class="' + config.modalPrefixClass + MODAL_CLOSE_TEXT_CLASS_SUFFIX + '">\n ' + config.modalCloseText + '\n </span>';
var contentClassName = config.modalPrefixClass + MODAL_CONTENT_CLASS_SUFFIX;
var titleClassName = config.modalPrefixClass + MODAL_TITLE_CLASS_SUFFIX;
var title = config.modalTitle !== '' ? '<div class="js-modal-title-container"><h1 id="' + MODAL_TITLE_ID + '" class="' + titleClassName + '">\n ' + config.modalTitle + '\n </h1></div>' : '';
var button_close = '<button type="button" class="' + MODAL_BUTTON_JS_CLASS + ' ' + buttonCloseClassName + '" id="' + MODAL_BUTTON_JS_ID + '" ' + MODAL_BUTTON_CONTENT_BACK_ID + '="' + config.modalContentId + '" ' + MODAL_BUTTON_FOCUS_BACK_ID + '="' + config.modalFocusBackId + '"><span class="dashicons dashicons-no" aria-hidden="true"></span>\n ' + buttonCloseInner + '\n </button>';
var content = config.modalText;
var describedById = config.modalDescribedById !== '' ? ATTR_DESCRIBEDBY + '="' + config.modalDescribedById + '"' : '';
// If there is no content but an id we try to fetch content id
if (content === '' && config.modalContentId) {
var contentFromId = findById(config.modalContentId);
if (contentFromId) {
content = '<div id="' + MODAL_CONTENT_JS_ID + '">\n ' + contentFromId.innerHTML + '\n </div';
// we remove content from its source to avoid id duplicates, etc.
contentFromId.innerHTML = '';
}
}
return '<dialog id="' + id + '" class="' + modalClassName + '" ' + ATTR_ROLE + '="' + MODAL_ROLE + '" ' + describedById + ' ' + ATTR_OPEN + ' ' + ATTR_LABELLEDBY + '="' + MODAL_TITLE_ID + '">\n <div role="document" class="' + modalClassWrapper + '">\n ' + button_close + '\n <div class="' + contentClassName + '">\n ' + title + '\n ' + content + '\n </div>\n </div>\n </dialog>';
};
var closeModal = function closeModal(config) {
remove(config.modal);
remove(config.overlay);
if (config.contentBackId !== '') {
var contentBack = findById(config.contentBackId);
if (contentBack) {
contentBack.innerHTML = config.modalContent;
}
}
if (config.modalFocusBackId) {
var contentFocus = findById(config.modalFocusBackId);
if (contentFocus) {
contentFocus.focus();
}
}
};
/** Find all modals inside a container
* @param {Node} node Default document
* @return {Array}
*/
var $listModals = function $listModals() {
var node = arguments.length <= 0 || arguments[0] === undefined ? doc : arguments[0];
return [].slice.call(node.querySelectorAll('.' + MODAL_JS_CLASS));
};
/**
* Build modals for a container
* @param {Node} node
*/
var attach = function attach(node) {
var addListeners = arguments.length <= 1 || arguments[1] === undefined ? true : arguments[1];
$listModals(node).forEach(function (modal_node) {
var iLisible = Math.random().toString(32).slice(2, 12);
var wrapperBody = findById(WRAPPER_PAGE_JS);
var body = doc.querySelector('body');
modal_node.setAttribute('id', MODAL_ID_PREFIX + iLisible);
modal_node.setAttribute(ATTR_HASPOPUP, ATTR_HASPOPUP_VALUE);
if (wrapperBody === null || wrapperBody.length === 0) {
var wrapper = doc.createElement('DIV');
wrapper.setAttribute('id', WRAPPER_PAGE_JS);
wrapInner(body, wrapper);
}
});
if (addListeners) {
/* listeners */
['click', 'keydown'].forEach(function (eventName) {
doc.body.addEventListener(eventName, function (e) {
// click on link modal
var parentModalLauncher = searchParent(e.target, MODAL_JS_CLASS);
if ((hasClass(e.target, MODAL_JS_CLASS) === true || parentModalLauncher !== '') && eventName === 'click') {
var body = doc.querySelector('body');
var modalLauncher = parentModalLauncher !== '' ? findById(parentModalLauncher) : e.target;
var modalPrefixClass = modalLauncher.hasAttribute(MODAL_PREFIX_CLASS_ATTR) === true ? modalLauncher.getAttribute(MODAL_PREFIX_CLASS_ATTR) + '-' : '';
var modalText = modalLauncher.hasAttribute(MODAL_TEXT_ATTR) === true ? modalLauncher.getAttribute(MODAL_TEXT_ATTR) : '';
var modalContentId = modalLauncher.hasAttribute(MODAL_CONTENT_ID_ATTR) === true ? modalLauncher.getAttribute(MODAL_CONTENT_ID_ATTR) : '';
var modalDescribedById = modalLauncher.hasAttribute(MODAL_DESCRIBEDBY_ID_ATTR) === true ? modalLauncher.getAttribute(MODAL_DESCRIBEDBY_ID_ATTR) : '';
var modalTitle = modalLauncher.hasAttribute(MODAL_TITLE_ATTR) === true ? modalLauncher.getAttribute(MODAL_TITLE_ATTR) : '';
var modalCloseText = modalLauncher.hasAttribute(MODAL_CLOSE_TEXT_ATTR) === true ? modalLauncher.getAttribute(MODAL_CLOSE_TEXT_ATTR) : MODAL_OVERLAY_TXT;
var backgroundEnabled = modalLauncher.hasAttribute(MODAL_DATA_BACKGROUND_ATTR) === true ? modalLauncher.getAttribute(MODAL_DATA_BACKGROUND_ATTR) : '';
var modalGiveFocusToId = modalLauncher.hasAttribute(MODAL_FOCUS_TO_ATTR) === true ? modalLauncher.getAttribute(MODAL_FOCUS_TO_ATTR) : '';
var wrapperBody = findById(WRAPPER_PAGE_JS);
// insert overlay
body.insertAdjacentHTML('beforeEnd', createOverlay({
text: modalCloseText,
backgroundEnabled: backgroundEnabled,
prefixClass: modalPrefixClass
}));
// insert modal
body.insertAdjacentHTML('beforeEnd', createModal({
modalText: modalText,
modalPrefixClass: modalPrefixClass,
backgroundEnabled: modalContentId,
modalTitle: modalTitle,
modalCloseText: modalCloseText,
modalCloseTitle: modalCloseText,
modalContentId: modalContentId,
modalDescribedById: modalDescribedById,
modalFocusBackId: modalLauncher.getAttribute('id')
}));
// hide page
wrapperBody.setAttribute(ATTR_HIDDEN, 'true');
// add class noscroll to body
addClass(body, NO_SCROLL_CLASS);
// give focus to close button or specified element
var closeButton = findById(MODAL_BUTTON_JS_ID);
if (modalGiveFocusToId !== '') {
var focusTo = findById(modalGiveFocusToId);
if (focusTo) {
focusTo.focus();
} else {
closeButton.focus();
}
} else {
closeButton.focus();
}
e.preventDefault();
}
// click on close button or on overlay not blocked
var parentButton = searchParent(e.target, MODAL_BUTTON_JS_CLASS);
if ((e.target.getAttribute('id') === MODAL_BUTTON_JS_ID || parentButton !== '' || e.target.getAttribute('id') === MODAL_OVERLAY_ID || hasClass(e.target, MODAL_BUTTON_JS_CLASS) === true) && eventName === 'click') {
var body = doc.querySelector('body');
var wrapperBody = findById(WRAPPER_PAGE_JS);
var modal = findById(MODAL_JS_ID);
var modalContent = findById(MODAL_CONTENT_JS_ID) ? findById(MODAL_CONTENT_JS_ID).innerHTML : '';
var overlay = findById(MODAL_OVERLAY_ID);
var modalButtonClose = findById(MODAL_BUTTON_JS_ID);
var modalFocusBackId = modalButtonClose.getAttribute(MODAL_BUTTON_FOCUS_BACK_ID);
var contentBackId = modalButtonClose.getAttribute(MODAL_BUTTON_CONTENT_BACK_ID);
var backgroundEnabled = overlay.getAttribute(MODAL_OVERLAY_BG_ENABLED_ATTR);
if (!(e.target.getAttribute('id') === MODAL_OVERLAY_ID && backgroundEnabled === 'disabled')) {
closeModal({
modal: modal,
modalContent: modalContent,
overlay: overlay,
modalFocusBackId: modalFocusBackId,
contentBackId: contentBackId,
backgroundEnabled: backgroundEnabled,
fromId: e.target.getAttribute('id')
});
// show back page
wrapperBody.removeAttribute(ATTR_HIDDEN);
// remove class noscroll to body
removeClass(body, NO_SCROLL_CLASS);
}
}
// strike a key when modal opened
if (findById(MODAL_JS_ID) && eventName === 'keydown') {
var body = doc.querySelector('body');
var wrapperBody = findById(WRAPPER_PAGE_JS);
var modal = findById(MODAL_JS_ID);
var modalContent = findById(MODAL_CONTENT_JS_ID) ? findById(MODAL_CONTENT_JS_ID).innerHTML : '';
var overlay = findById(MODAL_OVERLAY_ID);
var modalButtonClose = findById(MODAL_BUTTON_JS_ID);
var modalFocusBackId = modalButtonClose.getAttribute(MODAL_BUTTON_FOCUS_BACK_ID);
var contentBackId = modalButtonClose.getAttribute(MODAL_BUTTON_CONTENT_BACK_ID);
var $listFocusables = [].slice.call(modal.querySelectorAll(FOCUSABLE_ELEMENTS_STRING));
// esc
if (e.keyCode === 27) {
closeModal({
modal: modal,
modalContent: modalContent,
overlay: overlay,
modalFocusBackId: modalFocusBackId,
contentBackId: contentBackId
});
// show back page
wrapperBody.removeAttribute(ATTR_HIDDEN);
// remove class noscroll to body
removeClass(body, NO_SCROLL_CLASS);
}
// tab or Maj Tab in modal => capture focus
if (e.keyCode === 9 && $listFocusables.indexOf(e.target) >= 0) {
// maj-tab on first element focusable => focus on last
if (e.shiftKey) {
if (e.target === $listFocusables[0]) {
$listFocusables[$listFocusables.length - 1].focus();
e.preventDefault();
}
} else {
// tab on last element focusable => focus on first
if (e.target === $listFocusables[$listFocusables.length - 1]) {
$listFocusables[0].focus();
e.preventDefault();
}
}
}
// tab outside modal => put it in focus
if (e.keyCode === 9 && $listFocusables.indexOf(e.target) === -1) {
e.preventDefault();
$listFocusables[0].focus();
}
}
}, true);
});
}
};
var onLoad = function onLoad() {
attach();
document.removeEventListener('DOMContentLoaded', onLoad);
};
document.addEventListener('DOMContentLoaded', onLoad);
window.van11yAccessibleModalWindowAria = attach;
})(document);