-
Notifications
You must be signed in to change notification settings - Fork 0
/
RadioGroup.ts
241 lines (210 loc) · 8.01 KB
/
RadioGroup.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
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
import * as shadyCss from '@webcomponents/shadycss';
import { isArrowKey, KeyCode } from '../util/KeyCode';
import { RadioButton } from './RadioButton';
import { createEvent } from '../util/EventUtils';
import { arrayFind, getSlottedElements, isElement, noOp, upgradeCustomElementIfNeeded } from '../util/CommonUtils';
import { StateReceiverMixin } from './StateReceiverMixin';
import { Attribute } from '../util/Attribute';
import type { DeviceType } from '../util/DeviceType';
import { navigateByArrowKey } from '../util/KeyboardNavigation';
import { createTemplate } from '../util/TemplateUtils';
const template = createTemplate('theoplayer-radio-group', `<slot></slot>`);
/**
* `<theoplayer-radio-group>` - A group of {@link RadioButton}s. At most one button in the group can be checked.
*
* ## Behavior
* This radio group implements the [roving tabindex](https://www.w3.org/WAI/ARIA/apg/example-index/radio/radio.html) pattern.
* - `Tab` moves focus in or out of the radio group.
* - `Up`/`Left` arrow moves focus to the previous radio button.
* - `Down`/`Right` arrow moves focus to the next radio button.
* - `Home` moves focus to the first radio button.
* - `End` moves focus to the last radio button.
*
* @group Components
*/
// Based on howto-radio-group
// https://github.com/GoogleChromeLabs/howto-components/blob/079d0fa34ff9038b26ea8883b1db5dd6b677d7ba/elements/howto-radio-group/howto-radio-group.js
export class RadioGroup extends StateReceiverMixin(HTMLElement, ['deviceType']) {
private _slot: HTMLSlotElement;
private _radioButtons: RadioButton[] = [];
constructor() {
super();
const shadowRoot = this.attachShadow({ mode: 'open' });
shadowRoot.appendChild(template().content.cloneNode(true));
this._slot = shadowRoot.querySelector('slot')!;
this._upgradeProperty('deviceType');
}
protected _upgradeProperty(prop: keyof this) {
if (this.hasOwnProperty(prop)) {
let value = this[prop];
delete this[prop];
this[prop] = value;
}
}
connectedCallback(): void {
shadyCss.styleElement(this);
if (!this.hasAttribute('role')) {
this.setAttribute('role', 'radiogroup');
}
this.addEventListener('keydown', this._onKeyDown);
this.shadowRoot!.addEventListener('change', this._onButtonChange);
this._slot.addEventListener('slotchange', this._onSlotChange);
this._onSlotChange();
}
disconnectedCallback(): void {
this.removeEventListener('keydown', this._onKeyDown);
this.shadowRoot!.removeEventListener('change', this._onButtonChange);
this._slot.removeEventListener('slotchange', this._onSlotChange);
}
get deviceType(): DeviceType {
return (this.getAttribute(Attribute.DEVICE_TYPE) || 'desktop') as DeviceType;
}
set deviceType(deviceType: DeviceType) {
this.setAttribute(Attribute.DEVICE_TYPE, deviceType);
}
private readonly _onSlotChange = () => {
const children = getSlottedElements(this._slot);
const upgradePromises: Array<Promise<unknown>> = [];
for (const child of children) {
if (!isRadioButton(child)) {
const promise = upgradeCustomElementIfNeeded(child);
if (promise) {
upgradePromises.push(promise);
}
}
}
if (upgradePromises.length > 0) {
Promise.all(upgradePromises).then(this._onSlotChange, noOp);
}
this._radioButtons = children.filter(isRadioButton);
let firstFocusedButton = this.focusedRadioButton;
if (!firstFocusedButton) {
this.firstRadioButton?.setAttribute('tabindex', '0');
}
};
private readonly _onKeyDown = (event: KeyboardEvent) => {
if (this.deviceType === 'tv' && isArrowKey(event.keyCode)) {
if (navigateByArrowKey(this, this._radioButtons, event.keyCode)) {
event.preventDefault();
event.stopPropagation();
}
return;
}
switch (event.keyCode) {
case KeyCode.UP:
case KeyCode.LEFT: {
if (this._focusPrevButton()) {
event.preventDefault();
}
break;
}
case KeyCode.DOWN:
case KeyCode.RIGHT: {
if (this._focusNextButton()) {
event.preventDefault();
}
break;
}
case KeyCode.HOME: {
event.preventDefault();
this.setFocusedRadioButton(this.firstRadioButton);
break;
}
case KeyCode.END: {
event.preventDefault();
this.setFocusedRadioButton(this.lastRadioButton);
break;
}
default: {
break;
}
}
};
get focusedRadioButton(): RadioButton | null {
return arrayFind(this._radioButtons, (button) => button.tabIndex === 0) ?? null;
}
get checkedRadioButton(): RadioButton | null {
return arrayFind(this._radioButtons, (button) => button.checked) ?? null;
}
get firstRadioButton(): RadioButton | null {
return this._radioButtons[0] ?? null;
}
get lastRadioButton(): RadioButton | null {
const radioButtons = this._radioButtons;
return radioButtons.length > 0 ? radioButtons[radioButtons.length - 1] : null;
}
allRadioButtons(): readonly RadioButton[] {
return this._radioButtons;
}
private _prevRadioButton(node: RadioButton): RadioButton | null {
const index = this._radioButtons.indexOf(node);
if (index > 0) {
return this._radioButtons[index - 1];
}
return null;
}
private _nextRadioButton(node: RadioButton): RadioButton | null {
const index = this._radioButtons.indexOf(node);
if (index >= 0 && index < this._radioButtons.length - 1) {
return this._radioButtons[index + 1];
}
return null;
}
private _focusPrevButton(): boolean {
let focusedButton = this.focusedRadioButton || this.firstRadioButton;
if (!focusedButton) {
return false;
}
if (focusedButton === this.firstRadioButton) {
this.setFocusedRadioButton(this.lastRadioButton);
} else {
this.setFocusedRadioButton(this._prevRadioButton(focusedButton));
}
return true;
}
private _focusNextButton(): boolean {
let focusedButton = this.focusedRadioButton || this.firstRadioButton;
if (!focusedButton) {
return false;
}
if (focusedButton === this.lastRadioButton) {
this.setFocusedRadioButton(this.firstRadioButton);
} else {
this.setFocusedRadioButton(this._nextRadioButton(focusedButton));
}
return true;
}
setFocusedRadioButton(button: RadioButton | null): void {
this._unfocusAll();
if (button) {
button.tabIndex = 0;
button.focus();
}
}
private _unfocusAll(): void {
for (const button of this.allRadioButtons()) {
button.tabIndex = -1;
}
}
setCheckedRadioButton(checkedButton: RadioButton | null): void {
for (const button of this.allRadioButtons()) {
button.checked = button === checkedButton;
}
this.dispatchEvent(createEvent('change', { bubbles: true }));
}
private readonly _onButtonChange = (event: Event) => {
const button = event.target as RadioButton | null;
if (button !== null && button.checked) {
this.setCheckedRadioButton(event.target as RadioButton);
}
};
}
customElements.define('theoplayer-radio-group', RadioGroup);
declare global {
interface HTMLElementTagNameMap {
'theoplayer-radio-group': RadioGroup;
}
}
function isRadioButton(node: Node): node is RadioButton {
return node instanceof RadioButton;
}