@todojs/

18.wc-closed

HTML, CSS, JS

No description

fork
loading
Files
  • index.html
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Shadow DOM & HTMLELement</title>
</head>
<body>
<my-component num="0"></my-component>
<script>
  {
    const priv = new WeakMap ();

    class MyComponent extends HTMLElement {
      static get observedAttributes () {
        return [ 'num' ];
      }

      constructor () {
        super ();
        const shadowRoot = this.attachShadow ({mode : 'closed'});
        priv.set (this, {shadowRoot: shadowRoot});
      }

      attributeChangedCallback (name, oldValue, value) {
        const shadowRoot = priv.get (this).shadowRoot;
        shadowRoot.innerHTML = '"' + name + '" change from ' +  oldValue + ' to ' + value;
      }
    }

    customElements.define ('my-component', MyComponent);

    setTimeout(function() {
      const el = document.querySelector ('my-component');
      el.setAttribute ('num', 300);                        // Actualizar el atributo
    }, 1000);
  }
</script>
</body>
</html>
result
console