-
-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #485 from pinkary-project/feat/image-lightbox
Lightbox implementation for Post Images
- Loading branch information
Showing
6 changed files
with
113 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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 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,72 @@ | ||
const lightBox = () => ({ | ||
open: false, | ||
imgSrc: '', | ||
currentIndex: 0, | ||
images: [], | ||
init() { | ||
let self = this; | ||
|
||
let hasLightboxImageElements = document.querySelectorAll('[data-has-lightbox-images]'); | ||
|
||
hasLightboxImageElements.forEach((lightboxImageElement) => { | ||
let images = lightboxImageElement.querySelectorAll('img'); | ||
|
||
images.forEach((img, index) => { | ||
img.classList.add('cursor-pointer'); | ||
img.dataset.navigateIgnore = true; | ||
img.addEventListener('click', function (e) { | ||
self.currentIndex = index; | ||
self.images = images; | ||
self.updateImageSrc(); | ||
self.$dispatch('open-modal', 'image-lightbox'); | ||
self.attachKeyboardEvents(); | ||
}); | ||
}); | ||
}); | ||
|
||
window.addEventListener('modal-opened', (e) => { | ||
if (e.detail === 'image-lightbox') { | ||
this.open = true; | ||
} | ||
}); | ||
|
||
window.addEventListener('modal-closed', (e) => { | ||
if (e.detail === 'image-lightbox') { | ||
this.open = false; | ||
} | ||
}); | ||
}, | ||
nextImage() { | ||
this.currentIndex = (this.currentIndex + 1) % this.images.length; | ||
this.updateImageSrc() | ||
}, | ||
prevImage() { | ||
this.currentIndex = (this.currentIndex - 1 + this.images.length) % this.images.length; | ||
this.updateImageSrc() | ||
}, | ||
updateImageSrc() { | ||
this.imgSrc = this.images[this.currentIndex].src; | ||
}, | ||
shouldShowNextButton() { | ||
return this.canScrollImages() && this.images.length - 1 !== this.currentIndex; | ||
}, | ||
shouldShowPrevButton() { | ||
return this.canScrollImages() && this.currentIndex !== 0; | ||
}, | ||
canScrollImages() { | ||
return this.images.length > 1; | ||
}, | ||
attachKeyboardEvents() { | ||
document.addEventListener('keydown', (e) => { | ||
if (this.open && this.canScrollImages()) { | ||
if (e.key === 'ArrowRight' && this.shouldShowNextButton()) { | ||
this.nextImage(); | ||
} else if (e.key === 'ArrowLeft' && this.shouldShowPrevButton()) { | ||
this.prevImage(); | ||
} | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
export {lightBox} |
This file contains 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,8 @@ | ||
<!-- Lightbox Modal --> | ||
<x-modal name="image-lightbox" close-button-outside-modal should-center-modal-content> | ||
<div x-data="lightBox" class="relative md:flex md:items-center"> | ||
<img :src="imgSrc" alt="image" class="max-w-full rounded-lg"/> | ||
<button x-show="shouldShowPrevButton" class="absolute left-0 md:-ml-8 text-white cursor-pointer text-2xl" @click="prevImage">←</button> | ||
<button x-show="shouldShowNextButton" class="absolute right-0 md:-mr-8 text-white cursor-pointer text-2xl" @click="nextImage">→</button> | ||
</div> | ||
</x-modal> |
This file contains 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 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 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