Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/components/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import "./pr1/Badge.js";
import "./pr2/Card.js";
import "./pr3/Dynamic.js";
67 changes: 67 additions & 0 deletions src/components/pr3/Dynamic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
class Dynamic extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open" });
}

connectedCallback() {
this.render();
}

disconnectedCallback() {
console.log("Componente Desmontado");
}

// se declaran los atributos
static get observedAttributes() {
return [
"role",
"active",
];
}

// Detecta el cambio de los atributos
attributeChangedCallback(name, oldValue, newValue) {
if (oldValue !== newValue) {
console.log(`Attribute ${name} changed from ${oldValue} to ${newValue}`);
this.render();
}
}

render() {
const role = this.getAttribute("role") || "Default role";
const active = this.hasAttribute("active") || false;
this.shadowRoot.innerHTML = `
<style>
:host {
display: block;
border: 1px solid #ccc;
width: fit-content;
height: auto;
margin: 50px auto;
padding: 10px;
text-align: center;
border-radius: 10px;
}
:host * {
padding: 0;
margin: 0;
}
:host([active]) {
background-color: #8600b3;
color:#f2ccff;
}
:host(:not([active])) {
background-color: #f2ccff;
color:#8600b3;
}
</style>
<div>
<p>Role: ${role}</p>
</div>
`;
}

}

window.customElements.define("wc-dynamic", Dynamic);
2 changes: 2 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<body>
<wc-badge></wc-badge>
<wc-card></wc-card>
<wc-dynamic role="JMZ" active></wc-dynamic>
<wc-dynamic role="JMZ"></wc-dynamic>
<script type="module" src="./components/index.js"></script>
</body>
</html>
Loading