-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAriaReflectorMixin.js
79 lines (79 loc) · 2.49 KB
/
AriaReflectorMixin.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
/** @param {typeof import('../core/CustomElement.js').default} Base */
export default function AriaReflectorMixin(Base) {
return Base
.observe({
_ariaRole: 'string',
})
.set({
/**
* Browsers that do no support AriaMixin in ElementInternals need to have
* their attributes after construction.
* @type {Map<string, string>}
*/
onConnectAriaValues: null,
hasFiredConnected: false,
})
.methods({
/**
* @param {keyof HTMLElement & keyof ElementInternals} name
*/
readAriaProperty(name) {
if (this.elementInternals && name in this.elementInternals) {
return this.elementInternals[name];
} if (name in this) {
return this[name];
}
// console.warn('Unknown ARIA property', name, this);
/** @type {string} */
let attrName = name;
if (attrName.startsWith('aria')) {
attrName = `aria-${attrName.slice(4).toLowerCase()}`;
}
return this.getAttribute(name);
},
/**
* @param {keyof HTMLElement & keyof ElementInternals} name
* @param {string} value
*/
updateAriaProperty(name, value) {
if (this.elementInternals && name in this.elementInternals) {
this.elementInternals[name] = value;
} else if (this.isConnected) {
if (name in this) {
this[name] = value;
} else {
// console.warn('Unknown ARIA property', name, this);
/** @type {string} */
let attrName = name;
if (attrName.startsWith('aria')) {
attrName = `aria-${attrName.slice(4).toLowerCase()}`;
}
if (value == null) {
this.removeAttribute(name);
} else {
this.setAttribute(attrName, value);
}
}
} else {
this.onConnectAriaValues ??= new Map();
this.onConnectAriaValues.set(name, value);
// Elements should not add attributes during construction
}
},
})
.on({
_ariaRoleChanged(oldValue, newValue) {
this.updateAriaProperty('role', newValue);
},
constructed() {
this.updateAriaProperty('role', this._ariaRole);
},
connected() {
if (!this.onConnectAriaValues) return;
for (const [key, value] of this.onConnectAriaValues) {
this.updateAriaProperty(key, value);
}
this.onConnectAriaValues = null;
},
});
}