forked from slab/quill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpicker.ts
198 lines (178 loc) · 5.6 KB
/
picker.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
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
import DropdownIcon from '../assets/icons/dropdown.svg';
let optionsCounter = 0;
function toggleAriaAttribute(element, attribute) {
element.setAttribute(
attribute,
!(element.getAttribute(attribute) === 'true'),
);
}
class Picker {
select: HTMLSelectElement;
container: HTMLElement;
label: HTMLElement;
constructor(select: HTMLSelectElement) {
this.select = select;
this.container = document.createElement('span');
this.buildPicker();
this.select.style.display = 'none';
// @ts-expect-error Fix me later
this.select.parentNode.insertBefore(this.container, this.select);
this.label.addEventListener('mousedown', () => {
this.togglePicker();
});
this.label.addEventListener('keydown', event => {
switch (event.key) {
case 'Enter':
this.togglePicker();
break;
case 'Escape':
this.escape();
event.preventDefault();
break;
default:
}
});
this.select.addEventListener('change', this.update.bind(this));
}
togglePicker() {
this.container.classList.toggle('ql-expanded');
// Toggle aria-expanded and aria-hidden to make the picker accessible
toggleAriaAttribute(this.label, 'aria-expanded');
// @ts-expect-error
toggleAriaAttribute(this.options, 'aria-hidden');
}
buildItem(option: HTMLOptionElement) {
const item = document.createElement('span');
// @ts-expect-error
item.tabIndex = '0';
item.setAttribute('role', 'button');
item.classList.add('ql-picker-item');
if (option.hasAttribute('value')) {
// @ts-expect-error Fix me later
item.setAttribute('data-value', option.getAttribute('value'));
}
if (option.textContent) {
item.setAttribute('data-label', option.textContent);
}
item.addEventListener('click', () => {
this.selectItem(item, true);
});
item.addEventListener('keydown', event => {
switch (event.key) {
case 'Enter':
this.selectItem(item, true);
event.preventDefault();
break;
case 'Escape':
this.escape();
event.preventDefault();
break;
default:
}
});
return item;
}
buildLabel() {
const label = document.createElement('span');
label.classList.add('ql-picker-label');
label.innerHTML = DropdownIcon;
// @ts-expect-error
label.tabIndex = '0';
label.setAttribute('role', 'button');
label.setAttribute('aria-expanded', 'false');
this.container.appendChild(label);
return label;
}
buildOptions() {
const options = document.createElement('span');
options.classList.add('ql-picker-options');
// Don't want screen readers to read this until options are visible
options.setAttribute('aria-hidden', 'true');
// @ts-expect-error
options.tabIndex = '-1';
// Need a unique id for aria-controls
options.id = `ql-picker-options-${optionsCounter}`;
optionsCounter += 1;
this.label.setAttribute('aria-controls', options.id);
// @ts-expect-error
this.options = options;
Array.from(this.select.options).forEach(option => {
const item = this.buildItem(option);
options.appendChild(item);
if (option.selected === true) {
this.selectItem(item);
}
});
this.container.appendChild(options);
}
buildPicker() {
Array.from(this.select.attributes).forEach(item => {
this.container.setAttribute(item.name, item.value);
});
this.container.classList.add('ql-picker');
this.label = this.buildLabel();
this.buildOptions();
}
escape() {
// Close menu and return focus to trigger label
this.close();
// Need setTimeout for accessibility to ensure that the browser executes
// focus on the next process thread and after any DOM content changes
setTimeout(() => this.label.focus(), 1);
}
close() {
this.container.classList.remove('ql-expanded');
this.label.setAttribute('aria-expanded', 'false');
// @ts-expect-error
this.options.setAttribute('aria-hidden', 'true');
}
selectItem(item: HTMLElement | null, trigger = false) {
const selected = this.container.querySelector('.ql-selected');
if (item === selected) return;
if (selected != null) {
selected.classList.remove('ql-selected');
}
if (item == null) return;
item.classList.add('ql-selected');
// @ts-expect-error Fix me later
this.select.selectedIndex = Array.from(item.parentNode.children).indexOf(
item,
);
if (item.hasAttribute('data-value')) {
// @ts-expect-error Fix me later
this.label.setAttribute('data-value', item.getAttribute('data-value'));
} else {
this.label.removeAttribute('data-value');
}
if (item.hasAttribute('data-label')) {
// @ts-expect-error Fix me later
this.label.setAttribute('data-label', item.getAttribute('data-label'));
} else {
this.label.removeAttribute('data-label');
}
if (trigger) {
this.select.dispatchEvent(new Event('change'));
this.close();
}
}
update() {
let option;
if (this.select.selectedIndex > -1) {
const item =
// @ts-expect-error Fix me later
this.container.querySelector('.ql-picker-options').children[
this.select.selectedIndex
];
option = this.select.options[this.select.selectedIndex];
// @ts-expect-error
this.selectItem(item);
} else {
this.selectItem(null);
}
const isActive =
option != null &&
option !== this.select.querySelector('option[selected]');
this.label.classList.toggle('ql-active', isActive);
}
}
export default Picker;