-
Notifications
You must be signed in to change notification settings - Fork 25
/
focus.js
213 lines (160 loc) · 5.95 KB
/
focus.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { getComposedChildren, getComposedParent, getNextAncestorSibling, getPreviousAncestorSibling, isVisible } from './dom.js';
const focusableElements = {
a: true,
body: true,
button: true,
frame: true,
iframe: true,
input: true,
isindex: true,
object: true,
select: true,
textarea: true
};
export function getComposedActiveElement() {
let node = document.activeElement;
if (!node) return null;
while (node.shadowRoot) {
if (node.shadowRoot.activeElement) node = node.shadowRoot.activeElement;
else break;
}
return node;
}
export function getFirstFocusableDescendant(node, includeHidden, predicate, includeTabbablesOnly) {
if (predicate === undefined) predicate = () => true;
const composedChildren = getComposedChildren(node);
for (let i = 0; i < composedChildren.length; i++) {
if (isFocusable(composedChildren[i], includeHidden, includeTabbablesOnly) && predicate(composedChildren[i])) return composedChildren[i];
const focusable = getFirstFocusableDescendant(composedChildren[i], includeHidden, predicate, includeTabbablesOnly);
if (focusable) return focusable;
}
return null;
}
export function getFocusableDescendants(node, options) {
let focusables = [];
const composedChildren = getComposedChildren(node);
composedChildren.forEach(child => {
if (child.tagName === 'svg') return;
if (options?.predicate) {
if (!options.predicate(child)) return;
}
if (isFocusable(child, options?.hidden, options?.tabbablesOnly, options?.disabled)) focusables.push(child);
if (options?.deep) {
focusables = [...focusables, ...getFocusableDescendants(child, options)];
}
});
return focusables;
}
export function getFocusPseudoClass() {
return isFocusVisibleSupported() ? 'focus-visible' : 'focus';
}
export function getLastFocusableDescendant(node, includeHidden) {
const composedChildren = getComposedChildren(node);
for (let i = composedChildren.length - 1; i >= 0; i--) {
const focusable = getLastFocusableDescendant(composedChildren[i], includeHidden);
if (focusable) return focusable;
if (isFocusable(composedChildren[i], includeHidden)) return composedChildren[i];
}
return null;
}
export function getPreviousFocusable(node, includeHidden) {
if (!node) return null;
if (includeHidden === undefined) includeHidden = false;
const _getPreviousFocusable = (node, ignore, ignoreChildren) => {
if (!ignore && isFocusable(node, includeHidden)) return node;
if (!ignoreChildren) {
const focusable = getLastFocusableDescendant(node, includeHidden);
if (focusable) return focusable;
}
const previousSibling = node.previousElementSibling;
if (previousSibling) {
const siblingFocusable = _getPreviousFocusable(previousSibling, false, false);
if (siblingFocusable) return siblingFocusable;
return null;
}
const previousAncestorSibling = getPreviousAncestorSibling(node);
if (previousAncestorSibling) {
const parentSibingFocusable = _getPreviousFocusable(previousAncestorSibling, false, false);
if (parentSibingFocusable) return parentSibingFocusable;
}
return null;
};
const focusable = _getPreviousFocusable(node, true, true);
return focusable;
}
export function getNextFocusable(node, includeHidden, ignore, ignoreChildren) {
if (!node) return null;
if (includeHidden === undefined) includeHidden = false;
if (ignore === undefined) ignore = true;
if (ignoreChildren === undefined) ignoreChildren = false;
const _getNextFocusable = (node, ignore, ignoreChildren) => {
if (!ignore && isFocusable(node, includeHidden)) return node;
if (!ignoreChildren) {
const focusable = getFirstFocusableDescendant(node, includeHidden);
if (focusable) return focusable;
}
const nextSibling = node.nextElementSibling;
if (nextSibling) {
const siblingFocusable = _getNextFocusable(nextSibling, false, false);
if (siblingFocusable) return siblingFocusable;
return null;
}
const nextParentSibling = getNextAncestorSibling(node);
if (nextParentSibling) {
const parentSibingFocusable = _getNextFocusable(nextParentSibling, false, false);
if (parentSibingFocusable) return parentSibingFocusable;
}
return null;
};
const focusable = _getNextFocusable(node, ignore, ignoreChildren);
return focusable;
}
export function getPreviousFocusableAncestor(node, includeHidden, includeTabbablesOnly) {
if (!node) return null;
if (includeHidden === undefined) includeHidden = false;
if (includeTabbablesOnly === undefined) includeTabbablesOnly = true;
let parentNode = getComposedParent(node);
while (parentNode) {
if (isFocusable(parentNode, includeHidden, includeTabbablesOnly)) return parentNode;
parentNode = getComposedParent(parentNode);
}
return null;
}
export function isFocusable(node, includeHidden, includeTabbablesOnly, includeDisabled) {
if (!node || node.nodeType !== 1 || (!includeDisabled && node.disabled)) return false;
if (includeTabbablesOnly === undefined) includeTabbablesOnly = true;
let _isFocusable;
const _minTabIndex = includeTabbablesOnly ? 0 : -1;
// IE treats all nodes without tabindex explicitly set as having tabindex=0
if (node.getAttributeNode) {
const tabIndexAttr = node.getAttributeNode('tabindex');
if (tabIndexAttr && tabIndexAttr.specified) {
_isFocusable = (tabIndexAttr.value >= _minTabIndex);
}
}
const nodeName = node.nodeName.toLowerCase();
if (_isFocusable === undefined) {
_isFocusable = focusableElements[nodeName];
}
if (_isFocusable && !includeHidden) {
// only perform visibility check if absolutely necessary
if (nodeName !== 'body' && !isVisible(node)) return false;
}
return _isFocusable;
}
let _isFocusVisibleSupported;
export function isFocusVisibleSupported() {
if (_isFocusVisibleSupported === undefined) {
const style = document.createElement('style');
try {
document.head.appendChild(style);
style.sheet.insertRule(':focus-visible { color: inherit; }');
_isFocusVisibleSupported = true;
} catch {
_isFocusVisibleSupported = false;
} finally {
style.remove();
}
}
return _isFocusVisibleSupported;
}