class ModalImage extends HTMLElement { #src = null; #alt = null; #imgElem = null; constructor () { super(); this.attachShadow({ mode: "open" }); this.shadowRoot.innerHTML = ` `; this.#imgElem = this.shadowRoot.querySelector("img"); this.#imgElem.onclick = this.modalZoom.bind(this); } connectedCallback () { const src = this.getAttribute("src"); const alt = this.getAttribute("alt"); this.#src = src; this.#alt = alt; this.#imgElem.src = src; this.#imgElem.alt = alt; } modalZoom () { const modalBox = document.createElement("dialog"); modalBox.innerHTML = `
${this.#alt}
${this.#alt}
`; this.shadowRoot.appendChild(modalBox); modalBox.showModal(); modalBox.querySelector("#close").onclick = (e) => { e.preventDefault(); modalBox.close(); modalBox.parentElement.removeChild(modalBox); }; } } customElements.define("modal-image", ModalImage);