diff options
| author | Dennis Fink | 2026-11-06 20:38:35 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-11-06 20:38:35 +0100 |
| commit | 8963ed31ce537a736ddba99baf68b31858b319b8 (patch) | |
| tree | 5c617043b0e906bdb38ddb723457eba3911a9f5a | |
| parent | 904b29dfa998ca491590cee2241c9ef92e16355c (diff) | |
| download | dennisfink.me-8963ed31ce537a736ddba99baf68b31858b319b8.tar.gz dennisfink.me-8963ed31ce537a736ddba99baf68b31858b319b8.zip | |
Add click-to-enlarge image viewer
This adds a native dialog for enlarging images throughout the site,
including gallery images with previous and next navigation.
The viewer supports keyboard navigation, image captions, and responsive
layouts, while preserving the existing behaviour of linked images and
interactive maps.
| -rw-r--r-- | assets/static/css/style.css | 112 | ||||
| -rw-r--r-- | assets/static/js/image-viewer.js | 100 | ||||
| -rw-r--r-- | templates/base.html | 19 |
3 files changed, 231 insertions, 0 deletions
diff --git a/assets/static/css/style.css b/assets/static/css/style.css index c210a9b..22df240 100644 --- a/assets/static/css/style.css +++ b/assets/static/css/style.css @@ -1252,3 +1252,115 @@ article::after, pagefind-modal .pf-result-card:has([data-pf-selected]) { background: var(--pf-hover) !important; } + +/* --- Click-to-enlarge content images --- */ +main img[data-enlargeable] { + cursor: zoom-in; + + &:focus-visible { + outline: 2px solid var(--blue); + outline-offset: 0.25rem; + } +} + +dialog#image-viewer { + align-items: center; + background: var(--background); + border: 1px solid var(--foreground); + border-radius: var(--default-border-radius); + color: var(--foreground); + column-gap: clamp(0.5rem, 2vw, 1.5rem); + height: calc(100dvh - 2rem); + max-height: 60rem; + max-width: 96rem; + overflow: hidden; + padding: 3.5rem clamp(0.5rem, 1.5vw, 1.5rem) 1rem; + width: calc(100vw - 2rem); + + &[open] { + display: flex; + } + + &::backdrop { + background: rgb(0 0 0 / 85%); + } + + & > figure { + align-items: center; + border: 0; + display: flex; + flex: 1 1 0; + flex-direction: column; + justify-content: center; + gap: 0.75rem; + height: 100%; + margin: 0; + max-width: none; + min-height: 0; + min-width: 0; + padding: 0; + width: auto; + + & > img { + border-radius: var(--default-border-radius); + display: block; + flex-shrink: 1; + height: auto; + max-height: 100%; + max-width: 100%; + min-height: 0; + object-fit: contain; + width: auto; + } + + & > figcaption { + flex: none; + margin: 0; + max-height: 20dvh; + max-width: 100%; + overflow-y: auto; + text-align: center; + font-style: italic; + } + } + + & > button { + background: var(--background); + border: 1px solid var(--dim-foreground); + border-radius: var(--default-border-radius); + color: var(--foreground); + cursor: pointer; + flex: 0 0 2.5rem; + font: inherit; + height: 2.5rem; + line-height: 1; + padding: 0; + width: 2.5rem; + + &:hover, + &:focus-visible { + background-color: var(--blue); + color: var(--foreground); + } + + &[hidden] { + display: none; + } + + &[data-action="close"] { + position: absolute; + right: 0.75rem; + top: 0.75rem; + } + } + + @media (max-width: 500px) { + column-gap: 0.5rem; + + & > button:not([data-action="close"]) { + flex-basis: 2rem; + height: 2rem; + width: 2rem; + } + } +} diff --git a/assets/static/js/image-viewer.js b/assets/static/js/image-viewer.js new file mode 100644 index 0000000..28c44cb --- /dev/null +++ b/assets/static/js/image-viewer.js @@ -0,0 +1,100 @@ +/* Enlarge content images in a native dialog; navigate within galleries. */ +(() => { + const dialog = document.querySelector("#image-viewer"); + if (!dialog || typeof dialog.showModal !== "function") return; + + const enlargedImage = dialog.querySelector("figure > img"); + const caption = dialog.querySelector("figcaption"); + const previousButton = dialog.querySelector('[data-action="previous"]'); + const nextButton = dialog.querySelector('[data-action="next"]'); + const closeButton = dialog.querySelector('[data-action="close"]'); + + // Linked images and interactive maps must keep their existing behaviour. + const images = [...document.querySelectorAll("main img")].filter( + (image) => + image.getAttribute("src") && + !image.closest("a, button, .trail, [data-no-enlarge]"), + ); + + let group = []; + let index = 0; + + function displayCurrentImage() { + const image = group[index]; + enlargedImage.src = image.dataset.fullsize || image.currentSrc || image.src; + enlargedImage.alt = image.alt; + + const sourceCaption = image + .closest("figure:not(.trail)") + ?.querySelector(":scope > figcaption"); + const captionContent = sourceCaption?.cloneNode(true); + captionContent + ?.querySelectorAll("details") + .forEach((detail) => detail.remove()); + caption.textContent = + captionContent?.textContent.replace(/\s+/g, " ").trim() || ""; + caption.hidden = !caption.textContent; + + previousButton.hidden = nextButton.hidden = group.length < 2; + } + + function openViewer(image) { + const gallery = image.closest(".gallery"); + group = gallery + ? images.filter((candidate) => candidate.closest(".gallery") === gallery) + : [image]; + index = group.indexOf(image); + displayCurrentImage(); + dialog.showModal(); + } + + function navigate(direction) { + if (group.length < 2) return; + index = (index + direction + group.length) % group.length; + displayCurrentImage(); + } + + for (const image of images) { + image.dataset.enlargeable = ""; + image.tabIndex = 0; + image.setAttribute("role", "button"); + image.setAttribute("aria-haspopup", "dialog"); + image.setAttribute( + "aria-label", + image.alt ? `Enlarge image: ${image.alt}` : "Enlarge image", + ); + + image.addEventListener("click", () => openViewer(image)); + image.addEventListener("keydown", (event) => { + if (event.key === "Enter" || event.key === " ") { + event.preventDefault(); + openViewer(image); + } + }); + } + + previousButton.addEventListener("click", () => navigate(-1)); + nextButton.addEventListener("click", () => navigate(1)); + closeButton.addEventListener("click", () => dialog.close()); + + dialog.addEventListener("keydown", (event) => { + if (event.key === "ArrowLeft" || event.key === "ArrowRight") { + event.preventDefault(); + navigate(event.key === "ArrowLeft" ? -1 : 1); + } + }); + + // Click the backdrop to close, but not the spacing around the image. + dialog.addEventListener("click", (event) => { + if (event.target !== dialog) return; + const rect = dialog.getBoundingClientRect(); + if ( + event.clientX < rect.left || + event.clientX > rect.right || + event.clientY < rect.top || + event.clientY > rect.bottom + ) { + dialog.close(); + } + }); +})(); diff --git a/templates/base.html b/templates/base.html index df0f76a..ebd3703 100644 --- a/templates/base.html +++ b/templates/base.html @@ -206,6 +206,24 @@ {% block content required %} {% endblock content %} </main> + <dialog id="image-viewer" aria-label="Enlarged image" data-pagefind-ignore> + <button type="button" + data-action="previous" + aria-label="Previous gallery image" + hidden>‹</button> + <figure> + <img alt=""> + <figcaption hidden></figcaption> + </figure> + <button type="button" + data-action="next" + aria-label="Next gallery image" + hidden>›</button> + <button type="button" + data-action="close" + aria-label="Close enlarged image" + autofocus>×</button> + </dialog> <footer> {% block site_footer %} <section aria-label="Follow me"> @@ -239,6 +257,7 @@ </footer> {% endblock body %} {% block scripts %} + <script src="{{ '/static/js/image-viewer.js' | asseturl }}" defer></script> <script src="{{ '/static/vendor/pagefind/pagefind-component-ui.js' | asseturl }}" type="module"></script> {% if needs_trail_assets | default(false) %} |
