Files
MaszToZadanieDomowe.com/assets/js/akordeon.js

82 lines
2.2 KiB
JavaScript
Raw Normal View History

2025-09-21 20:30:37 +02:00
class Akordeon extends HTMLElement {
constructor() {
super();
2025-09-22 02:54:26 +02:00
2025-09-21 20:30:37 +02:00
this._internals = this.attachInternals();
2025-09-21 21:53:34 +02:00
this.shadow = this.attachShadow({ mode: "open" });
2025-09-22 02:54:26 +02:00
this.przycisk = document.createElement("button");
this.przycisk.onclick = () => {
this.otwarte = !this.otwarte;
2025-09-21 21:53:34 +02:00
};
2025-09-22 02:54:26 +02:00
this.contents = document.createElement("slot");
2025-09-21 20:30:37 +02:00
}
2025-09-21 21:53:34 +02:00
get otwarte() {
return this._internals.states.has("otwarte");
2025-09-21 20:30:37 +02:00
}
2025-09-15 01:58:08 +02:00
2025-09-21 21:53:34 +02:00
set otwarte(flag) {
2025-09-21 20:30:37 +02:00
if (flag) {
2025-09-21 21:53:34 +02:00
this._internals.states.add("otwarte");
2025-09-22 02:54:26 +02:00
this.przycisk.className = "otwarte";
this.contents.className = "otwarte";
2025-09-21 20:30:37 +02:00
} else {
2025-09-21 21:53:34 +02:00
this._internals.states.delete("otwarte");
2025-09-22 02:54:26 +02:00
this.przycisk.className = "";
this.contents.className = "";
2025-09-15 01:58:08 +02:00
}
}
2025-09-21 20:30:37 +02:00
2025-09-21 21:53:34 +02:00
connectedCallback() {
2025-09-22 02:54:26 +02:00
this.przycisk.innerText = this.getAttribute("title") ?? "Nowy akordeon";
this.otwarte = this.getAttribute("otwarte") == "true";
2025-09-21 21:53:34 +02:00
const styl = document.createElement("style");
styl.textContent = `
@import url('/assets/css/baza.css');
:host {
border-radius: 1rem;
display: inline-flex;
flex-direction: column;
width: 100%;
}
:host(:state(otwarte)) {
background-color: var(--colorSurface0);
border: .2rem solid var(--colorOverlay1);
border-top-color: transparent;
}
button {
border: .2rem solid var(--colorOverlay2);
position: relative;
margin: 0%;
text-align: left;
}
2025-09-22 02:54:26 +02:00
button.otwarte {
2025-09-21 21:53:34 +02:00
border-bottom-color: var(--colorSurface2);
2025-09-22 02:54:26 +02:00
scale: 1.006;
2025-09-21 21:53:34 +02:00
}
slot {
2025-09-22 02:54:26 +02:00
display: none;
}
slot.otwarte {
2025-09-21 21:53:34 +02:00
display: inline;
padding: .5rem;
}
`;
this.shadow.appendChild(styl);
2025-09-22 02:54:26 +02:00
this.shadow.appendChild(this.przycisk);
2025-09-21 21:53:34 +02:00
this.shadow.appendChild(this.contents);
2025-09-21 20:30:37 +02:00
}
}
2025-09-21 21:53:34 +02:00
customElements.define("akordeon-comp", Akordeon);