-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathfocusable.js
More file actions
91 lines (83 loc) · 2.25 KB
/
focusable.js
File metadata and controls
91 lines (83 loc) · 2.25 KB
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
/**
* References:
*
* Focusable:
* - https://www.w3.org/TR/html5/editing.html#focus-management
*
* Sequential focus navigation:
* - https://www.w3.org/TR/html5/editing.html#sequential-focus-navigation-and-the-tabindex-attribute
*
* Disabled elements:
* - https://www.w3.org/TR/html5/disabled-elements.html#disabled-elements
*
* getClientRects algorithm (requiring layout box):
* - https://www.w3.org/TR/cssom-view-1/#extension-to-the-element-interface
*
* AREA elements associated with an IMG:
* - https://w3c.github.io/html/editing.html#data-model
*/
const SELECTOR = [
'[tabindex]',
'a[href]',
'button:not([disabled])',
'input:not([type="hidden"]):not([disabled])',
'select:not([disabled])',
'textarea:not([disabled])',
'iframe',
'object',
'embed',
'area[href]',
'[contenteditable]:not([contenteditable=false])',
].join( ',' );
/**
* Returns true if the specified element is visible (i.e. neither display: none
* nor visibility: hidden).
*
* @param {Element} element DOM element to test.
*
* @return {boolean} Whether element is visible.
*/
function isVisible( element ) {
return (
element.offsetWidth > 0 ||
element.offsetHeight > 0 ||
element.getClientRects().length > 0
);
}
/**
* Returns true if the specified area element is a valid focusable element, or
* false otherwise. Area is only focusable if within a map where a named map
* referenced by an image somewhere in the document.
*
* @param {Element} element DOM area element to test.
*
* @return {boolean} Whether area element is valid for focus.
*/
function isValidFocusableArea( element ) {
const map = element.closest( 'map[name]' );
if ( ! map ) {
return false;
}
const img = document.querySelector( 'img[usemap="#' + map.name + '"]' );
return !! img && isVisible( img );
}
/**
* Returns all focusable elements within a given context.
*
* @param {Element} context Element in which to search.
*
* @return {Element[]} Focusable elements.
*/
export function find( context ) {
const elements = context.querySelectorAll( SELECTOR );
return [ ...elements ].filter( ( element ) => {
if ( ! isVisible( element ) ) {
return false;
}
const { nodeName } = element;
if ( 'AREA' === nodeName ) {
return isValidFocusableArea( element );
}
return true;
} );
}