Repository navigation
fix: make image modal keyboard accessible and manage focus #1274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Yashisinghal285
wants to merge
1
commit into
layer5io:master
Choose a base branch
from
Yashisinghal285:fix-image-modal-keyboard-access
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,61 +1,205 @@ | ||
| <div id="myModal" class="modal"> | ||
| <button class="modal-close" onclick="closeModal()">close</button> | ||
| <div | ||
| id="myModal" | ||
| class="modal" | ||
| role="dialog" | ||
| aria-modal="true" | ||
| aria-label="Image preview" | ||
| > | ||
| <button | ||
| type="button" | ||
| class="modal-close" | ||
| onclick="closeModal()" | ||
| aria-label="Close image preview" | ||
| > | ||
| close | ||
| </button> | ||
| <div class="modal-cont"> | ||
| <img class="modal-pic" id="modalPic" onclick="closeModal()" style="max-width: 100%; max-height: 80vh; margin: auto;" alt="Modal-pic"> | ||
| <img | ||
| class="modal-pic" | ||
| id="modalPic" | ||
| onclick="closeModal()" | ||
| style="max-width: 100%; max-height: 80vh; margin: auto;" | ||
| alt="" | ||
| > | ||
| </div> | ||
| </div> | ||
|
|
||
| <script> | ||
| var modalTrigger = null; | ||
|
|
||
| // Open the Modal | ||
| function openModal(imageIdOrElement) { | ||
| var src; | ||
| var image = null; | ||
| var trigger = null; | ||
|
|
||
| if (typeof imageIdOrElement === 'string') { | ||
| // If it's a string (image ID), get the source using the ID | ||
| src = document.getElementById(imageIdOrElement).src; | ||
| if (typeof imageIdOrElement === "string") { | ||
| image = document.getElementById(imageIdOrElement); | ||
| } else if (imageIdOrElement instanceof HTMLImageElement) { | ||
| // If <img>, get the source directly. currentSrc is what the browser actually | ||
| // chose, so inside a <picture> the modal reuses the already-fetched WebP | ||
| // rather than downloading the larger original. | ||
| src = imageIdOrElement.currentSrc || imageIdOrElement.src; | ||
| image = imageIdOrElement; | ||
| trigger = image.closest(".md__image-trigger"); | ||
| } else if (imageIdOrElement instanceof HTMLElement) { | ||
| trigger = imageIdOrElement.closest(".md__image-trigger") || imageIdOrElement; | ||
| image = imageIdOrElement.querySelector("img") || imageIdOrElement; | ||
| } | ||
|
|
||
| if (!image || !(image instanceof HTMLImageElement)) { | ||
| return; | ||
| } | ||
|
|
||
| // Use currentSrc if available (e.g. inside a <picture>) so the modal reuses | ||
| // the already-fetched WebP image rather than downloading the larger fallback. | ||
| var src = image.currentSrc || image.src; | ||
| if (src && src.includes("#")) { | ||
| src = src.substring(0, src.indexOf("#")); | ||
| } | ||
|
|
||
| document.getElementById("modalPic").src = src; | ||
| document.getElementById("myModal").style.display = "block"; | ||
| var modal = document.getElementById("myModal"); | ||
| var modalPic = document.getElementById("modalPic"); | ||
| var closeButton = modal ? modal.querySelector(".modal-close") : null; | ||
|
|
||
| // Track the trigger element to restore focus on close: | ||
| // Prefer the button trigger; fall back to the image element itself. | ||
| modalTrigger = trigger || image; | ||
|
|
||
| if (modalTrigger === image && !image.hasAttribute("tabindex")) { | ||
| image.setAttribute("tabindex", "-1"); | ||
| } | ||
|
|
||
| if (modalPic) { | ||
| modalPic.src = src; | ||
| modalPic.alt = image.alt || ""; | ||
| } | ||
|
|
||
| if (modal) { | ||
| modal.style.display = "block"; | ||
| } | ||
|
|
||
| if (closeButton) { | ||
| closeButton.focus(); | ||
| } | ||
| } | ||
|
|
||
| // Close the Modal | ||
| function closeModal() { | ||
| document.getElementById("modalPic").src = ""; | ||
| document.getElementById("myModal").style.display = "none"; | ||
| function closeModal() { | ||
| var modal = document.getElementById("myModal"); | ||
| var modalPic = document.getElementById("modalPic"); | ||
| var trigger = modalTrigger; | ||
|
|
||
| if (modalPic) { | ||
| modalPic.src = ""; | ||
| modalPic.alt = ""; | ||
| } | ||
|
|
||
| if (modal) { | ||
| modal.style.display = "none"; | ||
| } | ||
|
|
||
| modalTrigger = null; | ||
|
|
||
| if (trigger && typeof trigger.focus === "function" && document.contains(trigger)) { | ||
| trigger.focus(); | ||
| } | ||
| } | ||
|
|
||
| function isModalOpen() { | ||
| var modal = document.getElementById("myModal"); | ||
| return modal && modal.style.display === "block"; | ||
| } | ||
|
|
||
| // Listen for the Escape key to close the modal | ||
| // Trap keyboard focus within the modal dialog | ||
| function trapModalFocus(event) { | ||
| if (event.key !== "Tab" || !isModalOpen()) { | ||
| return; | ||
| } | ||
|
|
||
| var modal = document.getElementById("myModal"); | ||
| var focusableElements = modal.querySelectorAll( | ||
| 'button:not([disabled]), [href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])' | ||
| ); | ||
|
|
||
| if (!focusableElements.length) { | ||
| event.preventDefault(); | ||
| return; | ||
| } | ||
|
|
||
| var firstElement = focusableElements[0]; | ||
| var lastElement = focusableElements[focusableElements.length - 1]; | ||
|
|
||
| if (!modal.contains(document.activeElement)) { | ||
| event.preventDefault(); | ||
| firstElement.focus(); | ||
| return; | ||
| } | ||
|
|
||
| if (event.shiftKey && document.activeElement === firstElement) { | ||
| event.preventDefault(); | ||
| lastElement.focus(); | ||
| } else if (!event.shiftKey && document.activeElement === lastElement) { | ||
| event.preventDefault(); | ||
| firstElement.focus(); | ||
| } | ||
| } | ||
|
|
||
| // Listen for Escape key and Tab key | ||
| document.addEventListener("keydown", function (event) { | ||
| if (event.key === "Escape") { | ||
| if (event.key === "Escape" && isModalOpen()) { | ||
| event.preventDefault(); | ||
| closeModal(); | ||
| return; | ||
| } | ||
|
|
||
| trapModalFocus(event); | ||
| }); | ||
|
|
||
| // Attach onclick attribute to all <img> after DOM is loaded | ||
| document.addEventListener("DOMContentLoaded", function () { | ||
| var imgTags = document.querySelectorAll("img"); | ||
| imgTags.forEach(function (img) { | ||
| img.onclick = function () { | ||
| if (img.dataset.modal !== "false") { | ||
| openModal(img); | ||
| var modal = document.getElementById("myModal"); | ||
|
|
||
| // Handle Markdown image triggers | ||
| var imageTriggers = document.querySelectorAll(".md__image-trigger"); | ||
| imageTriggers.forEach(function (trigger) { | ||
| // If the image is inside an anchor link, avoid nesting interactive elements. | ||
| var link = trigger.closest("a"); | ||
| if (link) { | ||
| var linkedImg = trigger.querySelector("img"); | ||
| if (linkedImg) { | ||
| linkedImg.dataset.modal = "false"; | ||
| } | ||
| }; | ||
| while (trigger.firstChild) { | ||
| trigger.parentNode.insertBefore(trigger.firstChild, trigger); | ||
| } | ||
| trigger.remove(); | ||
| return; | ||
| } | ||
|
|
||
| trigger.addEventListener("click", function (event) { | ||
| event.preventDefault(); | ||
| openModal(trigger); | ||
| }); | ||
| }); | ||
|
|
||
| // Close modal when clicking anywhere inside #myModal | ||
| var modal = document.getElementById("myModal"); | ||
| modal.addEventListener("click", function () { | ||
| closeModal(); | ||
| // Attach click handler to any raw content <img> not inside .md__image-trigger or <a> | ||
| document.querySelectorAll("img").forEach(function (img) { | ||
| if ( | ||
| img.dataset.modal === "false" || | ||
| img.closest("#myModal") || | ||
| img.closest("a") || | ||
| img.closest(".md__image-trigger") | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| img.addEventListener("click", function () { | ||
| openModal(img); | ||
| }); | ||
| }); | ||
|
|
||
| // Close modal when clicking directly on the overlay background | ||
| if (modal) { | ||
| modal.addEventListener("click", function (event) { | ||
| if (event.target === modal) { | ||
| closeModal(); | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
| </script> | ||
| </script> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift
Preserve the image role outside the button.
Both branches place the
<img>inside a<button>. Button descendants are presentational in the accessibility tree. Screen-reader users therefore encounter the button label, but cannot find the standalone Markdown image as an image until they open the preview. This conflicts with the stated goal of retaining the image’s role. Render the image beside its button trigger, then update the modal’s image lookup to match. (w3.org)Also applies to: 43-43
🤖 Prompt for AI Agents