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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
const NAVIGATION_KEY_NAMES = ['ArrowDown', 'ArrowUp'];
const INTERACTABLE_NODE_NAMES = ['A', 'BUTTON'];
export type configObject = {
listItemClassNames: string;
isRoving?: boolean;
listGroupElement?: HTMLElement;
syncInteractivityWithVisibility?: boolean;
};
type configParams = configObject & { targetElement: HTMLElement };
/**
* A construct that manages keyboard navigation as it relates to lists.
* @class
*/
class ListKeyboardAccess {
private listItemClassNames: Array<string>;
private listParentElement: HTMLElement;
private boundFocusInHandler: EventListener;
private boundKeyDownHandler: EventListener;
private listGroupElement: HTMLElement | undefined;
// a current index based on an ancestor parent i.e. `listGroupElement`.
private currentRootIndex: number = -1;
// a current index based on an immediate list parent i.e. `listParentElement`.
private currentIndex: number = -1;
private isRoving: boolean = false;
private syncInteractivityWithVisibility: boolean | undefined;
private intersectionObserver: IntersectionObserver | undefined;
static isWindowEventBound: boolean = false;
constructor(options: configParams) {
const {
listGroupElement,
targetElement,
syncInteractivityWithVisibility,
} = options;
this.listParentElement = targetElement;
this.listGroupElement = listGroupElement;
this.isRoving = (options.isRoving ?? false) && !!this.listGroupElement;
this.syncInteractivityWithVisibility = syncInteractivityWithVisibility;
// converting a string list into an array of CSS class names (note: not selectors).
this.listItemClassNames = options.listItemClassNames
?.split(',')
.map((className) => className.trim());
// Attempting to only bind this event once for the purpose of list navigation.
if (!ListKeyboardAccess.isWindowEventBound) {
window.addEventListener(
'keydown',
ListKeyboardAccess.windowKeyUpHandler,
);
ListKeyboardAccess.isWindowEventBound = true;
}
if (this.listItemClassNames?.join('').length) {
this.boundFocusInHandler = this.focusInHandler.bind(this);
this.boundKeyDownHandler = this.keyDownHandler.bind(this);
this.listParentElement.addEventListener(
'focusin',
this.boundFocusInHandler,
{
capture: true,
},
);
this.listParentElement.addEventListener(
'keydown',
this.boundKeyDownHandler,
);
} else {
throw Error('ListKeyboardAccess requires listItemClassNames');
}
if (this.syncInteractivityWithVisibility) {
// Create the observer
this.intersectionObserver = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
setItemInteractivity(
entry.target as HTMLElement,
entry.isIntersecting,
);
});
},
{
root: targetElement,
rootMargin: '0px',
threshold: 0.5,
},
);
const listItems = this.getListItems();
for (let i = 0; i < listItems.length; i++) {
this.intersectionObserver.observe(listItems[i]);
}
}
}
destroy() {
if (ListKeyboardAccess.isWindowEventBound) {
window.removeEventListener(
'keydown',
ListKeyboardAccess.windowKeyUpHandler,
);
ListKeyboardAccess.isWindowEventBound = false;
}
this.listParentElement?.removeEventListener(
'focusin',
this.boundFocusInHandler,
{
capture: true,
},
);
this.listParentElement?.removeEventListener(
'keydown',
this.boundKeyDownHandler,
);
this.intersectionObserver?.disconnect();
}
private getListItems(
fromlistGroupElement: boolean = false,
): Array<HTMLElement> {
const { listGroupElement, listParentElement } = this;
const root =
fromlistGroupElement && listGroupElement
? listGroupElement
: listParentElement;
const selectors = getSelectorsFromCSSClassNames(
this.listItemClassNames.join(','),
);
return Array.from(root.querySelectorAll(selectors));
}
private focusInHandler(event: any) {
const currentListItem = this.findListItem(event.target);
const listItems = this.getListItems();
// bail if no list items or currentListItem
if (!listItems.length || !currentListItem) return;
this.currentIndex = listItems.indexOf(currentListItem);
this.currentRootIndex = this.getListItems(this.isRoving)?.indexOf(
currentListItem,
);
if (this.currentIndex >= 0 && this.isRoving) {
for (let i = 0; i < listItems.length; i++) {
setTabFocusable(listItems[i], i === this.currentIndex);
}
}
}
private keyDownHandler(event: any) {
if (
!NAVIGATION_KEY_NAMES.includes(event.key) ||
this.currentIndex < 0
) {
return;
}
const currentIndex = this.isRoving
? this.currentRootIndex
: this.currentIndex;
const listItems = this.getListItems(this.isRoving);
let nextIndex =
event.key === 'ArrowUp'
? Math.max(0, currentIndex - 1)
: Math.min(currentIndex + 1, listItems.length - 1);
focusVisibleItemByIndex(nextIndex, currentIndex, listItems);
}
/**
* A helper method to find the closest focusable list item.
* @param sourceElement origin of traversal
* @returns HTMLElement | null
*/
private findListItem(source: HTMLElement | null): HTMLElement | null {
if (!source || !this.listItemClassNames?.length) return null;
const selector = this.listItemClassNames.map((c) => `.${c}`).join(',');
const hit = source.closest(selector) as HTMLElement | null;
if (hit) return hit;
const parent = source.parentElement;
if (!parent) return null;
// BFS over siblings and their descendants
const q: Element[] = Array.from(parent.children);
const checked = new Set<Element>([parent]);
for (let i = 0; i < q.length; i++) {
const el = q[i] as HTMLElement;
if (checked.has(el)) continue;
checked.add(el);
if (el.matches(selector)) return el;
// enqueue children
for (const child of Array.from(el.children)) {
if (!checked.has(child)) q.push(child);
}
}
return null;
}
/**
* Event handler for the window to stop scrolling the page when users use the arrow keys.
* @param event
*/
static windowKeyUpHandler(event: any) {
if (NAVIGATION_KEY_NAMES.includes(event.key)) {
event.preventDefault();
}
}
}
function focusVisibleItemByIndex(
index: number,
targetIndex: number,
listItems: Array<HTMLElement>,
) {
const direction = index - targetIndex > 0 ? 1 : -1;
const listItem = listItems[index];
if (!listItem) {
return;
}
// Sometimes the list item itself is visible, but the parent
// is not--like the search button in the nav bar.
// Check visibility for the element and its parent before assigning focus.
if (isItemVisible(listItem) && isItemVisible(listItem.parentElement)) {
listItems[index].focus();
} else {
focusVisibleItemByIndex(index + direction, targetIndex, listItems);
}
}
function isItemVisible(element: HTMLElement | null): boolean {
if (element === null) return false;
const { display, visibility, opacity } = window.getComputedStyle(element);
return display !== 'none' && visibility !== 'hidden' && opacity !== '0';
}
function getSelectorsFromCSSClassNames(classes: string): string {
if (!classes) return '';
return classes
.split(',')
.map((name) => `.${name.trim()}`)
.join(',');
}
/**
* sets tabindex for an element following W3C Web standards.
* @param element HTMLElement
* @param isTabFocusable boolean "tab-focusable" refers to whether or not an element is focusable using the Tab key.
*/
export function setTabFocusable(element: HTMLElement, isTabFocusable: boolean) {
if (INTERACTABLE_NODE_NAMES.includes(element.nodeName)) {
const isAnchor = element.nodeName === 'A';
if (isTabFocusable) {
element.removeAttribute(isAnchor ? 'tabindex' : 'disabled');
} else {
const attribtuesToSet: [string, string] = isAnchor
? ['tabindex', '-1']
: ['disabled', 'true'];
element.setAttribute(...attribtuesToSet);
}
} else {
element.setAttribute('tabindex', isTabFocusable ? '0' : '-1');
}
}
export function setItemInteractivity(
shelfItemElement: HTMLElement,
isShelfItemVisible: boolean,
) {
if (
INTERACTABLE_NODE_NAMES.includes(shelfItemElement.nodeName) ||
shelfItemElement.getAttribute('tabindex')
) {
// Handles the shelf item
setTabFocusable(shelfItemElement as HTMLElement, isShelfItemVisible);
}
if (isShelfItemVisible) {
shelfItemElement.removeAttribute('aria-hidden');
} else {
shelfItemElement.setAttribute('aria-hidden', 'true');
}
// handles the children in the item
const selectors: string = INTERACTABLE_NODE_NAMES.map((nodeName) =>
nodeName.toLowerCase(),
).join(',');
const interactiveContent: Array<HTMLAnchorElement | HTMLButtonElement> =
Array.from(shelfItemElement.querySelectorAll(selectors));
for (let el of interactiveContent) {
setTabFocusable(el, isShelfItemVisible);
}
}
/**
* set up mutation observer to ensure tab-focusablility is set appropriately based on the list item's focusability.
* @param listItemNode
* @param interactableTargets
* @returns
*/
export function initListItemObserver(
listItemNode: HTMLElement,
interactableTargets: Array<HTMLElement>,
): MutationObserver {
const observer = new MutationObserver((mutationsList) => {
let tabindex: number;
for (let mutation of mutationsList) {
if (mutation.type === 'attributes' && interactableTargets.length) {
for (let i = 0; i < interactableTargets.length; i++) {
tabindex = Number(
(mutation.target as HTMLElement).getAttribute(
'tabindex',
),
);
setTabFocusable(interactableTargets[i], tabindex >= 0);
}
}
}
});
if (listItemNode) {
observer.observe(listItemNode, { attributes: true });
}
return observer;
}
export function listKeyboardAccess(
targetElement: HTMLElement,
options: configObject = { listItemClassNames: '' },
) {
const listKeyboardAXInstance = new ListKeyboardAccess({
targetElement,
...options,
});
return {
destroy() {
listKeyboardAXInstance.destroy();
},
};
}
export default listKeyboardAccess;
|