-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathenable-listbox.js
executable file
·255 lines (201 loc) · 7.71 KB
/
enable-listbox.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
'use strict'
/*******************************************************************************
* enable-listbox.js - UI for the ARIA listbox role
*
* Written by Zoltan Hawryluk <[email protected]>
* Part of the Enable accessible component library.
* Version 1.0 released
*
* More information about this script available at:
* https://www.useragentman.com/enable/
*
* Released under the MIT License.
******************************************************************************/
import accessibility from '../../enable-node-libs/accessibility-js-routines/dist/accessibility.module.js';
const enableListbox = new function() {
const showEvent = new CustomEvent('enable-listbox-show', {
bubbles: true
});
const hideEvent = new CustomEvent('enable-listbox-hide', {
bubbles: true
});
this.init = function() {
const { body } = document;
document.addEventListener('click', this.onClick);
body.addEventListener('keyup', this.onKeyup);
body.addEventListener('keydown', this.onKeydown);
body.addEventListener('mousedown', this.onMousedown)
}
this.onMousedown = (e) => {
const { target } = e;
if (target.classList.contains('enable-listbox__button')) {
const root = target.closest('.enable-listbox');
const listboxEl = root.querySelector('[role="listbox"]');
listboxEl.removeEventListener('blur', this.blurEvent, true);
// ensure this gets focus
target.focus();
listboxEl.addEventListener('blur', this.blurEvent, true);
}
}
this.onKeydown = (e) => {
this.lastKeydownEl = document.activeElement;
// If the down arrow is pressed when the listbox is collapsed, fire a click event
const { target, key } = e;
const root = target.closest('.enable-listbox');
if (root) {
const normalizedKey = accessibility.normalizedKey(key);
const listboxEl = root.querySelector('[role="listbox"]');
if ((normalizedKey === 'ArrowDown' || normalizedKey === 'ArrowUp') && this.isCollapsed(listboxEl)) {
e.preventDefault();
this.onClick(e);
}
}
}
this.onKeyup = (e) => {
const { key, target } = e;
const normalizedKey = accessibility.normalizedKey(key);
if (this.lastKeydownEl !== target || (normalizedKey !== 'Enter' && normalizedKey !== ' ' && normalizedKey !== 'Escape' && normalizedKey !== 'ArrowDown')) {
return;
}
const { activeElement } = document;
const root = activeElement.closest('.enable-listbox');
if (root) {
const listboxEl = root.querySelector('[role="listbox"]');
const buttonEl = root.querySelector('[aria-haspopup="listbox"]');
if (activeElement.getAttribute('role') === 'option') {
if (normalizedKey === 'Escape') {
listboxEl.removeEventListener('blur', this.blurEvent, true);
}
this.collapse(buttonEl, listboxEl, true)
if (normalizedKey === 'Escape') {
listboxEl.addEventListener('blur', this.blurEvent, true);
}
}
}
}
this.onClick = (e) => {
const { target } = e;
const root = target.closest('.enable-listbox');
let hasValueChanged = false;
if (root) {
const listboxEl = root.querySelector('[role="listbox"]');
const optionEls = listboxEl.querySelectorAll('[role="option"]');
const buttonEl = root.querySelector('[aria-haspopup="listbox"]');
const activeId = buttonEl.getAttribute('aria-activedescendant');
// ensure all options can have programmatic focus
optionEls.forEach(element => {
element.setAttribute('tabIndex', '-1');
if (activeId && element.id === activeId) {
element.setAttribute('aria-selected', 'true');
} else if (!element.getAttribute('aria-selected')) {
element.setAttribute('aria-selected', 'false');
}
});
if (target.nodeName === 'BUTTON') {
const ariaExpanded = target.getAttribute('aria-expanded');
// if the listbox is already expanded, close it
if (ariaExpanded === 'true') {
this.collapse(buttonEl, listboxEl, true);
// if the listbox is collapsed, expand it.
} else {
this.expand(buttonEl, listboxEl);
}
}
if (target !== buttonEl || listboxEl.contains(target)) {
// the user clicked outside of the control (but inside the root).
// We assume the must want to close all dropdowns on the page.
this.collapseAllListboxes();
}
} else {
// same as above, except outside the whole root.
this.collapseAllListboxes();
}
}
this.collapseAllListboxes = function() {
const roots = document.querySelectorAll('.enable-listbox');
for (let i = 0; i < roots.length; i++) {
const root = roots[i];
const buttonEl = root.querySelector('[aria-haspopup="listbox"]');
const ariaExpanded = buttonEl.getAttribute('aria-expanded');
if (ariaExpanded === 'true') {
const listboxEl = root.querySelector('[role="listbox"]');
this.collapse(buttonEl, listboxEl, false);
}
}
}
this.isCollapsed = (listboxEl) => {
return listboxEl.classList.contains('hidden');
}
this.collapse = (buttonEl, listboxEl, doFocus) => {
if (this.isCollapsed(listboxEl)) {
return;
}
listboxEl.classList.add('hidden');
accessibility.removeMobileFocusLoop();
buttonEl.removeAttribute('aria-expanded');
if (doFocus) {
buttonEl.focus();
}
listboxEl.classList.add('hidden');
listboxEl.dispatchEvent(hideEvent);
}
this.expand = (buttonEl, listboxEl) => {
const optionEls = listboxEl.querySelectorAll('[role="option"]');
console.log('target', buttonEl);
buttonEl.setAttribute('aria-expanded', 'true');
listboxEl.classList.remove('hidden');
// set focus on appropriate option
requestAnimationFrame(() => {
const itemToFocus = listboxEl.querySelector('[aria-selected="true"]') || optionEls[0];
itemToFocus.focus();
accessibility.setMobileFocusLoop(listboxEl);
// make the arrow keyup events happen if needed
if (listboxEl.dataset.enableListboxInit !== 'true') {
this.initListbox(listboxEl, buttonEl);
}
});
}
this.initListbox = (listboxEl, buttonEl) => {
accessibility.initGroup(
listboxEl, {
doKeyChecking: true,
ariaCheckedCallback: (e, currentlyCheckedEl) => {
const { previousValue, previousId } = listboxEl.dataset;
const eventValue = currentlyCheckedEl.innerText;
const eventId = currentlyCheckedEl.id;
if (previousValue !== eventValue && previousId !== eventId) {
buttonEl.innerHTML = currentlyCheckedEl.innerHTML;
this.collapse(buttonEl, listboxEl, true);
accessibility.removeMobileFocusLoop();
const changeEvent = new CustomEvent('enable-listbox-change', {
bubbles: true,
detail: {
value: () => eventValue,
id: () => eventId
}
});
listboxEl.dataset.previousValue = eventValue;
listboxEl.dataset.previousId = eventId;
listboxEl.dispatchEvent(changeEvent);
}
}
}
);
listboxEl.addEventListener('blur', this.blurEvent, true);
listboxEl.dataset.enableListboxInit = 'true';
}
this.blurEvent = (e) => {
const { target } = e;
const root = target.closest('.enable-listbox');
const listboxEl = root.querySelector('[role="listbox"]');
const buttonEl = root.querySelector('[aria-haspopup="listbox"]');
accessibility.doIfBlurred(e, () => {
const { target } = e;
if (target.getAttribute('role') === 'option') {
target.click();
}
this.collapse(buttonEl, listboxEl, true);
});
}
}
export default enableListbox;