-
-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathfocusUtils.ts
More file actions
176 lines (159 loc) · 4.56 KB
/
focusUtils.ts
File metadata and controls
176 lines (159 loc) · 4.56 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
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
import type * as React from 'react';
const TABBABLE_SELECTOR =
'a[href], button, input, select, textarea, [tabindex]:not([tabindex^="-"])';
/**
* Subtree cannot contain tab stops the browser will use.
* @see https://github.com/KittyGiraudel/a11y-dialog/blob/4674ff3e4d626430a028a64969328e339c533ce8/src/dom-utils.ts
*/
function canHaveTabbableChildren(el: HTMLElement): boolean {
if (el.shadowRoot && el.getAttribute('tabindex') === '-1') {
return false;
}
return !el.matches(':disabled, [hidden], [inert]');
}
function isNonVisibleForInteraction(el: HTMLElement): boolean {
if (
el.matches('details:not([open]) *') &&
!el.matches('details > summary:first-of-type')
) {
return true;
}
return !(
el.offsetWidth ||
el.offsetHeight ||
el.getClientRects().length
);
}
function isTabbable(el: HTMLElement, win: Window): boolean {
if (el.shadowRoot?.delegatesFocus) {
return false;
}
if (!el.matches(TABBABLE_SELECTOR)) {
return false;
}
if (isNonVisibleForInteraction(el)) {
return false;
}
if (el.closest('[aria-hidden="true"]') || el.closest('[inert]')) {
return false;
}
if ('disabled' in el && (el as HTMLButtonElement).disabled) {
return false;
}
if (el instanceof HTMLInputElement && el.type === 'hidden') {
return false;
}
const style = win.getComputedStyle(el);
if (style.display === 'none' || style.visibility === 'hidden') {
return false;
}
return true;
}
function getNextChildEl(parent: ParentNode, forward: boolean): Element | null {
return forward ? parent.firstElementChild : parent.lastElementChild;
}
function getNextSiblingEl(el: Element, forward: boolean): Element | null {
return forward ? el.nextElementSibling : el.previousElementSibling;
}
/**
* First or last tabbable descendant in tree order (light DOM, shadow roots, slots).
* @see https://github.com/KittyGiraudel/a11y-dialog/blob/4674ff3e4d626430a028a64969328e339c533ce8/src/dom-utils.ts
*/
function findTabbableEl(
el: HTMLElement,
forward: boolean,
win: Window,
): HTMLElement | null {
if (forward && isTabbable(el, win)) {
return el;
}
if (canHaveTabbableChildren(el)) {
if (el.shadowRoot) {
let next = getNextChildEl(el.shadowRoot, forward);
while (next) {
const hit = findTabbableEl(next as HTMLElement, forward, win);
if (hit) {
return hit;
}
next = getNextSiblingEl(next, forward);
}
} else if (el.localName === 'slot') {
const assigned = (el as HTMLSlotElement).assignedElements({
flatten: true,
}) as HTMLElement[];
const ordered = forward ? assigned : [...assigned].reverse();
for (let i = 0; i < ordered.length; i += 1) {
const hit = findTabbableEl(ordered[i], forward, win);
if (hit) {
return hit;
}
}
} else {
let next = getNextChildEl(el, forward);
while (next) {
const hit = findTabbableEl(next as HTMLElement, forward, win);
if (hit) {
return hit;
}
next = getNextSiblingEl(next, forward);
}
}
}
if (!forward && isTabbable(el, win)) {
return el;
}
return null;
}
/** First and last tabbable nodes inside `container` (inclusive). `last === first` if only one. */
export function getTabbableEdges(
container: HTMLElement,
): readonly [HTMLElement | null, HTMLElement | null] {
const win = container.ownerDocument.defaultView!;
const first = findTabbableEl(container, true, win);
const last = first
? findTabbableEl(container, false, win) || first
: null;
return [first, last] as const;
}
export function focusPopupRootOrFirst(
container: HTMLElement,
): HTMLElement | null {
const [first] = getTabbableEdges(container);
if (first) {
first.focus();
return first;
}
if (!container.hasAttribute('tabindex')) {
container.setAttribute('tabindex', '-1');
}
container.focus();
return container;
}
export function handlePopupTabTrap(
e: React.KeyboardEvent,
container: HTMLElement,
): void {
if (e.key !== 'Tab' || e.defaultPrevented) {
return;
}
const [first, last] = getTabbableEdges(container);
const active = document.activeElement as HTMLElement | null;
if (!active || !container.contains(active)) {
return;
}
if (!first || !last) {
if (active === container) {
e.preventDefault();
}
return;
}
if (!e.shiftKey) {
if (active === last || active === container) {
e.preventDefault();
first.focus();
}
} else if (active === first || active === container) {
e.preventDefault();
last.focus();
}
}