-
-
Notifications
You must be signed in to change notification settings - Fork 71
Add image modal feature for better image viewing experience #849
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
aniirathod
wants to merge
11
commits into
brisbanesocialchess:main
Choose a base branch
from
aniirathod:feat/image-modal
base: main
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.
+136
−13
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2e2e282
Add image modal feature for better image viewing experience
aniirathod 4155e0b
Documented code
aniirathod 01ed3d3
Prevent XSS attack
aniirathod 8ca761e
Variable declartion solved
aniirathod 39b05ae
Variable and documenting code
aniirathod a11dd1b
function reorder and documenting code
aniirathod 9594278
Fixes deployed conflic and disabled scrolling when image is open
aniirathod b32724c
fix: wrap images in <a> tags for non-JS users while keeping modal fun…
aniirathod 0112a7a
Merge branch 'main' into feat/image-modal
jbampton 38aa195
Update frontend/assets/tailwind.css
aniirathod 85b5da1
Merge branch 'main' into feat/image-modal
aniirathod 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| // =========================================== | ||
| // Image Modal (Scoped in IIFE to avoid conflicts) | ||
| // =========================================== | ||
| (() => { | ||
| /** | ||
| * Creates and opens an accessible image modal. | ||
| * @param {HTMLImageElement} imgEl - The clicked image element. | ||
| */ | ||
| function openModal(imgEl) { | ||
| // Save the previously focused element | ||
| const previousActiveElement = document.activeElement; | ||
|
|
||
| // 1. Overlay | ||
| const overlay = document.createElement('div'); | ||
| overlay.className = 'fixed inset-0 bg-black/85 flex items-center justify-center z-[9999]'; | ||
| overlay.setAttribute('role', 'dialog'); | ||
| overlay.setAttribute('aria-modal', 'true'); | ||
| overlay.setAttribute('aria-label', imgEl.alt || 'Image viewer'); | ||
|
|
||
| // 2. Container | ||
| const modalContainer = document.createElement('div'); | ||
| modalContainer.className = 'relative flex items-center justify-center h-[80%] w-full p-4'; | ||
|
|
||
| // 3. Image | ||
| const modalImage = document.createElement('img'); | ||
| modalImage.src = imgEl.src; | ||
aniirathod marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| modalImage.alt = imgEl.alt; | ||
| modalImage.className = 'max-w-full max-h-full object-contain rounded-2xl shadow-[0_4px_25px_rgba(0,0,0,0.4)] animate-fade-in'; | ||
aniirathod marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // 4. Close Button | ||
| const closeButton = document.createElement('button'); | ||
| closeButton.className = | ||
| 'absolute top-4 right-6 text-white text-3xl bg-transparent border-none cursor-pointer transition-transform duration-200 hover:scale-125'; | ||
aniirathod marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| closeButton.setAttribute('aria-label', 'Close image viewer'); | ||
| closeButton.textContent = '×'; | ||
|
|
||
| // --- Assemble and Append --- | ||
| modalContainer.append(modalImage, closeButton); | ||
| overlay.appendChild(modalContainer); | ||
| document.body.appendChild(overlay); | ||
aniirathod marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Disable body scroll while modal is active | ||
| document.body.style.overflow = 'hidden'; | ||
|
|
||
| // --- Focus Handling --- | ||
| closeButton.focus(); | ||
|
|
||
| /** | ||
| * Close modal and restore focus + scroll | ||
| */ | ||
| function closeModal() { | ||
| document.body.style.overflow = ''; // restore scroll | ||
| overlay.remove(); | ||
| document.removeEventListener('keydown', keyHandler); | ||
| if (previousActiveElement) previousActiveElement.focus(); // return focus | ||
| } | ||
|
|
||
| // --- Event Listeners --- | ||
| closeButton.addEventListener('click', closeModal); | ||
|
|
||
| // Simplified backdrop close | ||
| overlay.addEventListener('click', (e) => { | ||
| if (e.target === overlay) closeModal(); | ||
| }); | ||
|
|
||
| /** | ||
| * Handles ESC + focus trapping | ||
| * @param {KeyboardEvent} e | ||
| */ | ||
| function keyHandler(e) { | ||
| if (e.key === 'Escape') { | ||
| closeModal(); | ||
| return; | ||
| } | ||
|
|
||
| // Focus trap: keep tabbing inside modal | ||
| if (e.key === 'Tab') { | ||
| const focusableEls = overlay.querySelectorAll('button, [tabindex]:not([tabindex="-1"])'); | ||
aniirathod marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const focusable = Array.from(focusableEls); | ||
| if (!focusable.length) return; | ||
|
|
||
| const firstEl = focusable[0]; | ||
| const lastEl = focusable[focusable.length - 1]; | ||
|
|
||
| if (e.shiftKey && document.activeElement === firstEl) { | ||
| e.preventDefault(); | ||
| lastEl.focus(); | ||
| } else if (!e.shiftKey && document.activeElement === lastEl) { | ||
| e.preventDefault(); | ||
| firstEl.focus(); | ||
| } | ||
| } | ||
| } | ||
| document.addEventListener('keydown', keyHandler); | ||
| } | ||
|
|
||
| // --- Global Setup (Event Delegation) --- | ||
| document.addEventListener('click', (e) => { | ||
| if (e.target.matches('img.modal-image')) { | ||
| const link = e.target.closest('a'); | ||
| if (link) e.preventDefault(); // prevent navigation | ||
| openModal(e.target); | ||
| } | ||
| }); | ||
| })(); | ||
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.