-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.js
58 lines (47 loc) · 1.26 KB
/
lib.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
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.captureTabNavigation = factory());
}(this, (function () { 'use strict';
const selector = [
'input',
'textarea',
'select',
'[tabindex]',
'a[href]',
'button',
'object'
].join(', ');
var getFocusableChildren = node => {
const children = [...node.querySelectorAll(selector)];
// ensure they are all in the dom
return children.filter(child => child.offsetParent);
};
const emptyObject = {};
var index = (node, event, config) => {
config = config || emptyObject;
const { key, target, shiftKey } = event;
if (key != 'Tab') {
return;
}
const focusableChildren = (config.getFocusableChildren ||
getFocusableChildren)(node);
const last = focusableChildren[focusableChildren.length - 1];
const first = focusableChildren[0];
if (target === first && first && shiftKey) {
event.preventDefault();
if (last && last.focus) {
last.focus();
}
return true;
}
if (target === last && last && !shiftKey) {
event.preventDefault();
if (first && first.focus) {
first.focus();
}
return true;
}
};
return index;
})));