-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathfocus-target.js
43 lines (34 loc) · 922 Bytes
/
focus-target.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
/*
Identify the first focusable element in the element's ancestry, including itself
*/
import getFocusRedirectTarget from './focus-redirect-target';
import getParents from '../get/parents';
import isFocusable from '../is/focusable';
import contextToElement from '../util/context-to-element';
export default function({context, except} = {}) {
const element = contextToElement({
label: 'get/focus-target',
context,
});
let result = null;
const getTarget = function(_element) {
const focusable = isFocusable.rules({
context: _element,
except,
});
if (focusable) {
result = _element;
return true;
}
result = getFocusRedirectTarget({
context: _element,
skipFocusable: true,
});
return Boolean(result);
};
if (getTarget(element)) {
return result;
}
getParents({context: element}).slice(1).some(getTarget);
return result;
}