-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrapTabKey.js
33 lines (27 loc) · 1.17 KB
/
trapTabKey.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
var getFocusableElements = function(element) {
var focusElemString = "a[href],button:not([disabled]),area[href],input:not([disabled]):not([type=hidden]),select:not([disabled]),textarea:not([disabled]),iframe,object,embed,*:not(.is-draggabe)[tabindex],*[contenteditable]";
var tempElements = element.querySelectorAll(focusElemString);
tempElements = Array.prototype.slice.call(tempElements);
var focusableElements = [];
for (var i = 0; i < tempElements.length; i++) {
if(tempElements[i].offsetHeight !== 0) focusableElements.push(tempElements[i])
};
var object = {
"all": focusableElements,
"first": focusableElements[0],
"last": focusableElements[focusableElements.length-1]
}
return object;
}
var trapTabKey = function(container) {
var activeElm = document.activeElement;
var focusObj = getFocusableElements(container);
if (event.keyCode !== 9) return false
if (event.shiftKey && activeElm === focusObj.first) {
focusObj.last.focus();
event.preventDefault();
} else if (!event.shiftKey && activeElm === focusObj.last) {
focusObj.first.focus();
event.preventDefault();
}
}