-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccessible-modal.js
More file actions
76 lines (66 loc) · 2.75 KB
/
accessible-modal.js
File metadata and controls
76 lines (66 loc) · 2.75 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
$(function() {
var buttonThatOpenedModal;
var findModal = function(elem) {
var tabbable = elem.find('select, input, textarea, button, a').filter(':visible');
var firstTabbable = tabbable.first();
var lastTabbable = tabbable.last();
/*set focus on first input*/
firstTabbable.focus();
/*redirect last tab to first input*/
lastTabbable.on('keydown', function(e) {
if ((e.which === 9 && !e.shiftKey)) {
e.preventDefault();
firstTabbable.focus();
}
});
/*redirect first shift+tab to last input*/
firstTabbable.on('keydown', function(e) {
if ((e.which === 9 && e.shiftKey)) {
e.preventDefault();
lastTabbable.focus();
}
});
/* allow escape key to close insiders div */
elem.on('keydown', function(e) {
if (e.keyCode === 27) {
$(elem).find('.modal-close_btn').click();
};
});
};
var modalOpenButton = $('.modal-open_btn');
modalOpenButton.on('keydown', function(e) {
// Only activate on spacebar and enter
if (e.which !== 13 && e.which !== 32) {
return;
}
e.preventDefault();
// Simulate a mouseclick to trigger Webflow's IX2/Interactions
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
$(this).get(0).dispatchEvent(evt);
});
modalOpenButton.on('click', function() {
$(this).next().show();
findModal($(this).next());
buttonThatOpenedModal = $(this);
});
var modalCloseButton = $('.modal-close_btn, .modal-close_area');
modalCloseButton.on('keydown', function(e) {
// Only activate on spacebar and enter
if (e.which !== 13 && e.which !== 32) {
return;
}
e.preventDefault();
// Simulate a mouseclick to trigger Webflow's IX2/Interactions
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
$(this).get(0).dispatchEvent(evt);
});
modalCloseButton.on('click', function() {
$(this).closest('.modal-wrapper').hide();
if (buttonThatOpenedModal) {
buttonThatOpenedModal.focus();
buttonThatOpenedModal = null;
}
});
});