-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathiterate-focusable-elements.ts
138 lines (123 loc) · 4.84 KB
/
iterate-focusable-elements.ts
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
/**
* Options to the focusable elements iterator
*/
export interface IterateFocusableElements {
/**
* (Default: false) Iterate through focusable elements in reverse-order
*/
reverse?: boolean
/**
* (Default: false) Perform additional checks to determine tabbability
* which may adversely affect app performance.
*/
strict?: boolean
/**
* (Default: false) Only iterate tabbable elements, which is the subset
* of focusable elements that are part of the page's tab sequence.
*/
onlyTabbable?: boolean
}
/**
* Returns an iterator over all of the focusable elements within `container`.
* Note: If `container` is itself focusable it will be included in the results.
* @param container The container over which to find focusable elements.
* @param reverse If true, iterate backwards through focusable elements.
*/
export function* iterateFocusableElements(
container: HTMLElement,
options: IterateFocusableElements = {},
): Generator<HTMLElement, undefined, undefined> {
const strict = options.strict ?? false
const acceptFn = (options.onlyTabbable ?? false) ? isTabbable : isFocusable
const walker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, {
acceptNode: node =>
node instanceof HTMLElement && acceptFn(node, strict) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP,
})
let nextNode: Node | null = null
// Allow the container to participate
if (!options.reverse && acceptFn(container, strict)) {
yield container
}
// If iterating in reverse, continue traversing down into the last child until we reach
// a leaf DOM node
if (options.reverse) {
let lastChild = walker.lastChild()
while (lastChild) {
nextNode = lastChild
lastChild = walker.lastChild()
}
} else {
nextNode = walker.firstChild()
}
while (nextNode instanceof HTMLElement) {
yield nextNode
nextNode = options.reverse ? walker.previousNode() : walker.nextNode()
}
// Allow the container to participate (in reverse)
if (options.reverse && acceptFn(container, strict)) {
yield container
}
return undefined
}
/**
* Returns the first focusable child of `container`. If `lastChild` is true,
* returns the last focusable child of `container`.
* @param container
* @param lastChild
*/
export function getFocusableChild(container: HTMLElement, lastChild = false) {
return iterateFocusableElements(container, {reverse: lastChild, strict: true, onlyTabbable: true}).next().value
}
/**
* Determines whether the given element is focusable. If `strict` is true, we may
* perform additional checks that require a reflow (less performant).
* @param elem
* @param strict
*/
export function isFocusable(elem: HTMLElement, strict = false): boolean {
// Certain conditions cause an element to never be focusable, even if they have tabindex="0"
const disabledAttrInert =
['BUTTON', 'INPUT', 'SELECT', 'TEXTAREA', 'OPTGROUP', 'OPTION', 'FIELDSET'].includes(elem.tagName) &&
(elem as HTMLElement & {disabled: boolean}).disabled
const hiddenInert = elem.hidden
const hiddenInputInert = elem instanceof HTMLInputElement && elem.type === 'hidden'
const sentinelInert = elem.classList.contains('sentinel')
if (disabledAttrInert || hiddenInert || hiddenInputInert || sentinelInert) {
return false
}
// Each of the conditions checked below require a reflow, thus are gated by the `strict`
// argument. If any are true, the element is not focusable, even if tabindex is set.
if (strict) {
const style = getComputedStyle(elem)
const sizeInert = elem.offsetWidth === 0 || elem.offsetHeight === 0
const visibilityInert = ['hidden', 'collapse'].includes(style.visibility)
const displayInert = style.display === 'none' || !elem.offsetParent
const clientRectsInert = elem.getClientRects().length === 0
if (sizeInert || visibilityInert || clientRectsInert || displayInert) {
return false
}
}
// Any element with `tabindex` explicitly set can be focusable, even if it's set to "-1"
if (elem.getAttribute('tabindex') != null) {
return true
}
if (elem.getAttribute('contenteditable') === 'true' || elem.getAttribute('contenteditable') === 'plaintext-only') {
return true
}
// One last way `elem.tabIndex` can be wrong.
if (elem instanceof HTMLAnchorElement && elem.getAttribute('href') == null) {
return false
}
return elem.tabIndex !== -1
}
/**
* Determines whether the given element is tabbable. If `strict` is true, we may
* perform additional checks that require a reflow (less performant). This check
* ensures that the element is focusable and that its tabindex is not explicitly
* set to "-1" (which makes it focusable, but removes it from the tab order).
* @param elem
* @param strict
*/
export function isTabbable(elem: HTMLElement, strict = false): boolean {
return isFocusable(elem, strict) && elem.getAttribute('tabindex') !== '-1'
}