-
Notifications
You must be signed in to change notification settings - Fork 6
/
KeyboardNavMixin.js
259 lines (250 loc) · 7.92 KB
/
KeyboardNavMixin.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import { attemptFocus, isFocused, isRtl } from '../core/dom.js';
import AriaReflectorMixin from './AriaReflectorMixin.js';
const DEFAULT_ELEMENT_QUERY = [
'button',
'[href]',
'input',
'select',
'textarea',
'[tabindex]',
].join(', ');
/**
* @param {typeof import('../core/CustomElement.js').default} Base
*/
export default function KeyboardNavMixin(Base) {
return Base
.mixin(AriaReflectorMixin)
.observe({
/** Keyboard navigation attribute */
kbdNav: { empty: 'true' },
_kbdFocusable: { empty: true },
})
.define({
/**
* Query used to find roving tab index children
*/
kbdNavQuery() {
return DEFAULT_ELEMENT_QUERY;
},
/**
* Flag whether disabled elements participating in roving tab index
* should be focusable.
*/
kbdNavFocusableWhenDisabled() { return true; },
/** @return {'horizontal'|'vertical'} */
ariaOrientationDefault() { return 'vertical'; },
})
.define({
/**
* List of roving tab index participating children
* @return {NodeListOf<HTMLElement>}
*/
kbdNavChildren() {
return this.querySelectorAll(this.kbdNavQuery);
},
})
.methods({
_ariaOrientationIsVertical() {
return (this.readAriaProperty('ariaOrientation')
?? this.ariaOrientationDefault) === 'vertical';
},
focusCurrentOrFirst() {
let current;
let first;
for (const candidate of this.kbdNavChildren) {
first = candidate;
if (candidate.tabIndex === 0) {
current = candidate;
break;
}
}
if (attemptFocus(current)) return current;
if (attemptFocus(first)) return first;
return null;
},
/**
* Focuses next element participating in roving tab index list
* @param {HTMLElement} [current]
* @param {boolean} [loop=true]
* @param {boolean} [reverse]
* @return {HTMLElement} focusedElement
*/
focusNext(current = null, loop = true, reverse = false) {
let foundCurrent = false;
const array = reverse ? [...this.kbdNavChildren].reverse() : this.kbdNavChildren;
for (const candidate of array) {
if (!foundCurrent) {
foundCurrent = (current
? (candidate === current)
: (candidate.getAttribute('tabindex') === '0'));
continue;
}
if (!candidate.hasAttribute('tabindex')) {
continue;
}
if (candidate.getAttribute('aria-hidden') === 'true') {
continue;
}
if (attemptFocus(candidate)) {
this.ariaActiveDescendantElement = candidate;
return candidate;
}
}
if (!loop) {
if (!isFocused(current) && current instanceof HTMLElement) {
current.focus();
}
return current;
}
// Loop
for (const candidate of array) {
if (!candidate.hasAttribute('tabindex')) {
continue;
}
if (candidate.getAttribute('aria-hidden') === 'true') {
continue;
}
// Abort if we've looped all the way back to original element
// Abort if candidate received focus
if (attemptFocus(candidate)) {
return candidate;
}
if (candidate === current) {
return candidate;
}
}
return null;
},
/**
* Alias for focusNext(list, current, true).
* Selects previous element
* @param {HTMLElement} [current]
* @param {boolean} [loop=true]
* @return {HTMLElement}
*/
focusPrevious(current, loop = true) {
return this.focusNext(current, loop, true);
},
/** @type {HTMLElement['focus']} */
focus(...options) {
// super.focus(...options);
if (attemptFocus(this.ariaActiveDescendantElement, ...options)) {
return;
}
for (const candidate of this.kbdNavChildren) {
if (candidate.getAttribute('tabindex') === '0' && candidate instanceof HTMLElement) {
this.ariaActiveDescendantElement = candidate;
candidate.focus(...options);
return;
}
}
this.focusNext();
},
/**
* Refreshes roving tab index attributes based on kbdNavChildren
*/
refreshTabIndexes() {
if (this.kbdNav !== 'true') return;
/** @type {HTMLElement} */
let currentlyFocusedChild = null;
/** @type {HTMLElement} */
let currentTabIndexChild = null;
/** @type {HTMLElement} */
let firstFocusableChild = null;
for (const child of this.kbdNavChildren) {
if (!currentlyFocusedChild && isFocused(child)) {
currentlyFocusedChild = child;
} else if (!currentTabIndexChild && child.getAttribute('tabindex') === '0') {
currentTabIndexChild = child;
} else {
if (!firstFocusableChild && child.getAttribute('aria-hidden') !== 'true'
&& (this.kbdNavFocusableWhenDisabled || child.getAttribute('aria-disabled') !== 'true')) {
firstFocusableChild = child;
}
child.tabIndex = -1;
// child.setAttribute('tabindex', '-1');
}
// Bind
if (!child.hasAttribute('tabindex')) {
child.tabIndex = isFocused(child) ? 0 : -1;
}
// this.rtiBindChild(child);
}
if (currentlyFocusedChild) {
currentlyFocusedChild.tabIndex = 0;
// currentlyFocusedChild.setAttribute('tabindex', '0');
} else if (currentTabIndexChild) {
if (currentlyFocusedChild) {
currentTabIndexChild.tabIndex = -1;
// currentTabIndexChild.setAttribute('tabindex', '-1');
}
} else if (firstFocusableChild) {
firstFocusableChild.tabIndex = 0;
// firstFocusableChild.setAttribute('tabindex', '0');
}
},
})
.events({
focusin(event) {
if (this.kbdNav !== 'true') return;
const currentItem = /** @type {HTMLElement} */ (event.target);
const participates = currentItem.matches(this.kbdNavQuery);
if (!participates) return;
this.ariaActiveDescendantElement = currentItem;
if (currentItem.getAttribute('tabindex') !== '0') {
currentItem.tabIndex = 0;
}
for (const item of this.kbdNavChildren) {
if (item !== currentItem && item.hasAttribute('tabindex')) {
item.tabIndex = -1;
// item.setAttribute('tabindex', '-1');
}
}
},
keydown(event) {
if (event.ctrlKey || event.altKey || event.shiftKey || event.metaKey) return;
if (this.kbdNav !== 'true') return;
switch (event.key) {
case 'ArrowUp':
case 'Up':
if (this._ariaOrientationIsVertical()) {
this.focusPrevious();
}
break;
case 'ArrowDown':
case 'Down':
if (this._ariaOrientationIsVertical()) {
this.focusNext();
}
break;
case 'ArrowLeft':
case 'Left':
if (this._ariaOrientationIsVertical()) return;
if (isRtl(this)) {
this.focusNext();
} else {
this.focusPrevious();
}
break;
case 'ArrowRight':
case 'Right':
if (this._ariaOrientationIsVertical()) return;
if (isRtl(this)) {
this.focusPrevious();
} else {
this.focusNext();
}
break;
default:
return;
}
event.stopPropagation(); // Avoid kbd within kbd (sub menus)
event.preventDefault();
},
})
.on({
connected() {
this.refreshTabIndexes();
},
});
}