Observed Attributes

Web Components โŸฉ Custom Elements โŸฉ

attributeChangedCallback() ๅœจ่งฃๆž HTML ๅฎŒๅพŒๅฐฑๆœƒ่‡ชๅ‹•ๅŸท่กŒ๏ผŒไธ้œ€ๅฆๅค–ๅœจ constructor ไธญๅผท่ฟซๅŸท่กŒโ—๏ธ

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.
  }
}

Last updated