personal-site/scripts/ModalImage.js

74 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2024-10-07 21:42:25 +00:00
class ModalImage extends HTMLElement {
#src = null;
#alt = null;
#imgElem = null;
2024-10-07 21:42:25 +00:00
constructor () {
super();
this.attachShadow({ mode: "open" });
this.shadowRoot.innerHTML = `
<style>
dialog {
border: none;
padding: 0;
background-color: rgba(0,0,0,0);
}
dialog img {
max-width: 100%;
max-height: 100%;
}
dialog #dialog-content {
max-width: 90%;
margin: auto;
background-color: white;
padding: 1em;
}
dialog #close {
cursor: pointer;
}
img {
min-width: 100%;
2024-10-07 21:42:25 +00:00
max-width: 100%;
min-height: 100%;
2024-10-07 21:42:25 +00:00
max-height: 100%;
}
</style>
<img>
`;
this.#imgElem = this.shadowRoot.querySelector("img");
this.#imgElem.onclick = this.modalZoom.bind(this);
2024-10-07 21:42:25 +00:00
}
connectedCallback () {
const thumbnail = this.getAttribute("thumb")
2024-10-07 21:42:25 +00:00
const src = this.getAttribute("src");
const alt = this.getAttribute("alt");
this.#src = src;
this.#alt = alt;
this.#imgElem.src = thumbnail;
2024-10-07 21:42:25 +00:00
this.#imgElem.alt = alt;
}
modalZoom () {
const modalBox = document.createElement("dialog");
modalBox.innerHTML = `
<div id="dialog-content">
<button id="close">&times;</button>
<img src="${this.#src}" alt="${this.#alt}">
<div>${this.#alt}</div>
<div>
2024-10-07 21:42:25 +00:00
`;
this.shadowRoot.appendChild(modalBox);
modalBox.showModal();
modalBox.querySelector("#close").onclick = (e) => {
e.preventDefault();
modalBox.close();
modalBox.parentElement.removeChild(modalBox);
};
2024-10-07 21:42:25 +00:00
}
}
customElements.define("modal-image", ModalImage);