# Observed Attributes

[Web Components](/web/component.md) ⟩ [Custom Elements](/web/component/custom-element.md) ⟩

{% tabs %}
{% tab title="📘 手冊" %}

* Google ⟩ Custom Elements ⟩ [Observing changes to attributes](https://developers.google.com/web/fundamentals/web-components/customelements#attrchanges)
  {% endtab %}

{% tab title="⬇️ 應用" %}

* [Box](/web/css/layout/system/box.md)
  {% endtab %}
  {% endtabs %}

{% hint style="info" %}
**attributeChangedCallback**() 在解析 HTML 完後就會**自動執行**，**不需**另外在 **constructor** 中強迫執行❗️
{% endhint %}

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/web/component/custom-element/observed-attributes.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
