Skip to content
This repository was archived by the owner on Sep 11, 2018. It is now read-only.

Commit e55b253

Browse files
author
Claudio Ludovico Panetta
committed
Add keyboard navigation 🎂🍾🎆
This patch brings a well requested feature: Keyboard navigation! To be fair this is the first attempt to it, basically the theater now has a new keyboard event listener attached to it when it's first created. This allow us to intercept every key stroke (on keyup to be precise) and check whenever the user press the right arrow, left arrow or the escape button.
1 parent 164e694 commit e55b253

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

src/components/Theater.vue

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
<template v-if="isPhotoSelected">
55
<div class="Theater__stage">
66
<figure class="Theater__figure">
7-
<img :src="selectedPhoto.photo" class="Theater__image"/>
7+
<img :src="selectedPhoto.photo" class="Theater__image" />
88
</figure>
99
<div class="Theater__commands">
10-
<a class="Theater__command" href="#previous" @click.prevent="previousPhoto()">
10+
<a class="Theater__command" href="#previous" @click.prevent="previousPhoto">
1111
<i class="fa fa-fw fa-lg fa-4x fa-angle-double-left" aria-hidden="true"></i>
1212
</a>
13-
<a class="Theater__command" href="#next" @click.prevent="nextPhoto()">
13+
<a class="Theater__command" href="#next" @click.prevent="nextPhoto">
1414
<i class="fa fa-fw fa-lg fa-4x fa-angle-double-right" aria-hidden="true"></i>
1515
</a>
1616
</div>
@@ -39,6 +39,14 @@
3939
export default {
4040
name: "theater",
4141
42+
beforeDestroy() {
43+
window.removeEventListener("keyup", this.checkPressedKey, false);
44+
},
45+
46+
created() {
47+
window.addEventListener("keyup", this.checkPressedKey);
48+
},
49+
4250
components: {
4351
Comments,
4452
},
@@ -68,6 +76,22 @@
6876
},
6977
7078
methods: {
79+
checkPressedKey(e) {
80+
e = e || window.event;
81+
82+
switch (e.keyCode) {
83+
case 27: // escape key
84+
this.closeTheater();
85+
break;
86+
case 37: // left arrow key code
87+
this.previousPhoto();
88+
break;
89+
case 39: // right arrow key code
90+
this.nextPhoto();
91+
break;
92+
}
93+
},
94+
7195
previousPhoto() {
7296
this.$store.commit("previousPhotoOfIndex", this.photoIndex - 1);
7397
},
@@ -180,4 +204,4 @@
180204
font-weight: bold;
181205
font-size: 1.8rem;
182206
}
183-
</style>
207+
</style>

0 commit comments

Comments
 (0)