-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodal.js
122 lines (101 loc) · 3.33 KB
/
modal.js
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
//
// VARIABLES
//
let modal = {
container: document.getElementById('global-modal'),
exit: document.getElementById('global-modal__exit'),
overlay: document.getElementById('modal-overlay')
}
//
//FUNCTION(S)
//
/** ACCESSIBLE MODAL
*
* // based off of tips/code in https://youtu.be/JS68faEUduk
* // (a11y casts with Rob Dodson)
*
* Inits click events to handle modal popup
* - modal opens after time specified by param
* - keyboard also locked in
* - can exit via escape key or click event
*
* @param exitButton {Elem} - Element that, when clicked, should close the modal
* @param modal {Elem} - The modal display element
* @param bodyOverlay {String} - Name of the class applied to an overlay div (overlay style controlled by CSS)
*
* TIP: HTML for div should include a container marked up as follows to correctly alert all users:
* <div id="global-modal" class="global-modal" role="alertdialog" aria-labelledby="global-modal__title">
* <!-- MODAL CONTENT, WHICH SHOULD INCLUDE: -->
* <h2 id="global-modal-title"><!-- Title here --></h2>
* </div>
*/
var initModal = function(exitButton, modal, bodyOverlay) {
//set keycodes for keyboard management
var KEYCODE = {
ESC: 27,
TAB: 9,
SHIFT: 16,
ENTER: 13
}
//find focuseable elemets for focus management
var elFocusable = "a[href], area[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), object, embed, *[contenteditable]",
modalFocusableNodes = modal.querySelectorAll(elFocusable),
modalFocusable = Array.prototype.slice.call(modalFocusableNodes);
let firstFocusableEl = modalFocusable[0];
let lastFocusableEl = modalFocusable[ modalFocusable.length - 1 ];
let previousActiveElement;
var openModal = function() {
previousActiveElement = document.activeElement;
modal.classList.add(modal.id + '--active');
bodyOverlay.classList.add(bodyOverlay.id + '--active');
firstFocusableEl.focus();
createExitListener();
activateKeyCheck();
}
initModal.openModal = openModal;
var createExitListener = function() {
exitButton.addEventListener('click', closeModal);
document.addEventListener('keydown', function(e) {
if (e.which === KEYCODE.ESC) {
closeModal();
}
})
}
var activateKeyCheck = function() {
document.addEventListener('keydown', function(e) {
if (e.which == KEYCODE.TAB) {
var currentFocus = document.activeElement,
modalFocusableNum = modalFocusable.length,
focusedIndex = modalFocusable.indexOf(currentFocus);
if (e.shiftKey) {
if (focusedIndex === 0) {
modalFocusable[(modalFocusableNum - 1)].focus();
e.preventDefault();
}
} else {
if (focusedIndex == modalFocusableNum - 1) {
modalFocusable[0].focus();
e.preventDefault();
}
}
}
})
}
var closeModal = function() {
bodyOverlay.classList.remove(bodyOverlay.id + '--active');
modal.classList.remove(modal.id + '--active');
previousActiveElement.focus();
}
bodyOverlay.addEventListener('click', closeModal);
}
//
// APPLY
//
initModal(modal.exit, modal.container, modal.overlay);
//
// OPTIONAL TIME DELAY POPUP
// if you would like this modal to be user-activated only (instead of time-activated), remove the timer and 'role="alertdialog"' from the HTML.
//
document.addEventListener("DOMContentLoaded", function(e) {
setTimeout(initModal.openModal, 5000);
});