forked from KittyGiraudel/a11y-dialog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha11y-dialog.js
98 lines (81 loc) · 3.19 KB
/
a11y-dialog.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
(function (global) {
'use strict';
// Helper function to get all focusable children from a node
function getFocusableChildren (node) {
var focusableElements = ['a[href]', 'area[href]', 'input:not([disabled])', 'select:not([disabled])', 'textarea:not([disabled])', 'button:not([disabled])', 'iframe', 'object', 'embed', '[contenteditable]', '[tabindex]:not([tabindex^="-"])'];
return $$(focusableElements.join(','), node).filter(function (child) {
return !!(child.offsetWidth || child.offsetHeight || child.getClientRects().length);
});
}
// Helper function to get all nodes in context matching selector as an array
function $$ (selector, context) {
return Array.prototype.slice.call((context || document).querySelectorAll(selector));
}
// Helper function trapping the tab key inside a node
function trapTabKey (node, event) {
var focusableChildren = getFocusableChildren(node);
var focusedItemIndex = focusableChildren.indexOf(document.activeElement);
if (event.shiftKey && focusedItemIndex === 0) {
focusableChildren[focusableChildren.length - 1].focus();
event.preventDefault();
} else if (!event.shiftKey && focusedItemIndex === focusableChildren.length - 1) {
focusableChildren[0].focus();
event.preventDefault();
}
}
// Helper function to focus first focusable item in node
function setFocusToFirstItem (node) {
var focusableChildren = getFocusableChildren(node);
if (focusableChildren.length) focusableChildren[0].focus();
}
var focusedBeforeDialog;
/**
* A11yDialog constructor
* @param {Node} node - Dialog element
* @param {Node} main - Main element of the page
*/
var A11yDialog = function (node, main) {
var namespace = 'data-a11y-dialog';
var that = this;
main = main || document.querySelector('#main');
this.shown = false;
this.show = show;
this.hide = hide;
$$('[' + namespace + '-show="' + node.id + '"]').forEach(function (opener) {
opener.addEventListener('click', show);
});
$$('[' + namespace + '-hide]', node).concat($$('[' + namespace + '-hide="' + node.id + '"]')).forEach(function (closer) {
closer.addEventListener('click', hide);
});
document.addEventListener('keydown', function (event) {
if (that.shown && event.which === 27) {
event.preventDefault();
hide();
}
if (that.shown && event.which === 9) {
trapTabKey(node, event);
}
});
function maintainFocus (event) {
if (that.shown && !node.contains(event.target)) {
setFocusToFirstItem(node);
}
}
function show () {
that.shown = true;
node.removeAttribute('aria-hidden');
main.setAttribute('aria-hidden', 'true');
focusedBeforeDialog = document.activeElement;
setFocusToFirstItem(node);
document.body.addEventListener('focus', maintainFocus, true);
}
function hide () {
that.shown = false;
node.setAttribute('aria-hidden', 'true');
main.removeAttribute('aria-hidden');
focusedBeforeDialog && focusedBeforeDialog.focus();
document.body.removeEventListener('focus', maintainFocus, true);
}
};
global.A11yDialog = A11yDialog;
}(window));