Observed Attributes
Last updated
Was this helpful?
Last updated
Was this helpful?
Was this helpful?
Web Components โฉ Custom Elements โฉ
Google โฉ Custom Elements โฉ Observing changes to attributes
class AppDrawer extends HTMLElement {
// Reflecting properties to attributes
get disabled() {
return this.hasAttribute('disabled');
}
set disabled(val) {
if (val) {
this.setAttribute('disabled', '');
} else {
this.removeAttribute('disabled');
}
}
// โญ๏ธ observed attributes
static get observedAttributes() {
return ['disabled', 'open'];
}
// โญ๏ธ attributes changed
// โญ๏ธ (only called for the observed attributes)
attributeChangedCallback(name, oldValue, newValue) {
// When the drawer is disabled, update keyboard/screen reader behavior.
if (this.disabled) {
this.setAttribute('tabindex', '-1');
this.setAttribute('aria-disabled', 'true');
} else {
this.setAttribute('tabindex', '0');
this.setAttribute('aria-disabled', 'false');
}
// TODO: also react to the open attribute changing.
}
}