Skip to content

Commit cec33c1

Browse files
committed
feat: added navigation buttons
1 parent c4f393a commit cec33c1

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

src/components/Gallery.vue

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<ImageModal ref="imageModal" :image="selectedImage" />
2+
<ImageModal ref="imageModal" :image="selectedImage" :next="gotoNextImage" :prev="gotoPrevImage" />
33
<div class="gallery-container">
44
<div class="gallery-item" v-for="(image, index) in galleryImages" :key="index">
55
<v-lazy-image :src="image" alt="random image" @click="openModal(image)" />
@@ -25,6 +25,7 @@
2525
display: inline-block;
2626
margin: 0 0 1em;
2727
width: 100%;
28+
cursor: zoom-in;
2829
}
2930
3031
.gallery-item>img {
@@ -73,9 +74,22 @@ export default {
7374
this.selectedImage = image;
7475
this.$refs.imageModal.toggleModal();
7576
},
76-
},
77-
mounted() {
78-
console.log(this.images);
77+
gotoNextImage() {
78+
const index = this.galleryImages.indexOf(this.selectedImage);
79+
if (index === this.galleryImages.length - 1) {
80+
this.selectedImage = this.galleryImages[0];
81+
} else {
82+
this.selectedImage = this.galleryImages[index + 1];
83+
}
84+
},
85+
gotoPrevImage() {
86+
const index = this.galleryImages.indexOf(this.selectedImage);
87+
if (index === 0) {
88+
this.selectedImage = this.galleryImages[this.galleryImages.length - 1];
89+
} else {
90+
this.selectedImage = this.galleryImages[index - 1];
91+
}
92+
},
7993
},
8094
}
8195
</script>

src/components/ImageModal.vue

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
<template>
22
<div id="defaultModal" tabindex="-1" aria-hidden="true"
3-
class="fixed top-0 left-0 right-0 z-50 hidden w-full p-4 overflow-x-hidden overflow-y-auto md:inset-0 h-modal md:h-full backdrop-blur-lg bg-white/30">
3+
class="fixed top-0 left-0 right-0 z-50 hidden w-full p-4 overflow-x-hidden overflow-y-auto md:inset-0 h-modal md:h-full backdrop-blur-lg bg-white/30 dark:bg-black/70">
44
<div class="relative mx-auto top-50 w-full h-full max-w-5xl md:h-auto ">
55
<div class="absolute top-0 right-0 z-50 p-4">
66
<button @click="toggleModal">
77
<carbon-close class="text-4xl text-white hover:text-white-500 hover:text-5xl ease-in duration-100"
88
aria-hidden="true" />
99
</button>
1010
</div>
11+
<!-- Left and Right image buttons -->
12+
<div class="absolute bottom-0 left-0 z-50 p-4">
13+
<button @click="prev">
14+
<carbon-chevron-left
15+
class="text-4xl text-white hover:text-white-500 hover:text-5xl ease-in duration-100"
16+
aria-hidden="true" />
17+
</button>
18+
19+
</div>
20+
21+
<div class="absolute bottom-0 right-0 z-50 p-4">
22+
<button @click="next">
23+
<carbon-chevron-right
24+
class="text-4xl text-white hover:text-white-500 hover:text-5xl ease-in duration-100"
25+
aria-hidden="true" />
26+
</button>
27+
</div>
28+
1129
<div class="relative w-full h-full">
1230
<img :src="image" class="object-cover" />
1331
</div>
@@ -25,6 +43,14 @@ export default {
2543
type: String,
2644
required: true,
2745
},
46+
prev: {
47+
type: Function,
48+
required: true,
49+
},
50+
next: {
51+
type: Function,
52+
required: true,
53+
},
2854
},
2955
setup(props) {
3056
// toggle modal
@@ -47,9 +73,20 @@ export default {
4773
}
4874
};
4975
76+
// shift to next image on right arrow key
77+
const imageShift = (e) => {
78+
if (e.key === 'ArrowRight') {
79+
props.next();
80+
} else if (e.key === 'ArrowLeft') {
81+
props.prev();
82+
}
83+
};
84+
5085
// add event listeners
5186
window.addEventListener('keydown', closeModal);
5287
window.addEventListener('click', closeOnClick);
88+
window.addEventListener('keydown', imageShift);
89+
5390
5491
// remove event listeners
5592
onBeforeUnmount(() => {

0 commit comments

Comments
 (0)