Skip to content

Commit

Permalink
Remove event listener when the component is destroyed
Browse files Browse the repository at this point in the history
  • Loading branch information
DCzajkowski committed Aug 25, 2017
1 parent 5dfb644 commit 890c6f7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-pure-lightbox",
"version": "2.1.3",
"version": "2.1.4",
"description": "Very lightweight, JavaScript-only, lightbox plugin for Vue.js",
"main": "dist-module/main.js",
"scripts": {
Expand Down
44 changes: 24 additions & 20 deletions src/Components/Lightbox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,10 @@
}
},
mounted() {
window.addEventListener('keydown', (e) => {
if (this.visible) {
switch (e.key) {
case 'ArrowRight':
this.next()
break
case 'ArrowLeft':
this.prev()
break
case 'ArrowDown':
case 'ArrowUp':
case ' ':
e.preventDefault()
break
case 'Escape':
this.hide()
break
}
}
})
window.addEventListener('keydown', this.eventListener)
},
destroyed() {
window.removeEventListener('keydown', this.eventListener)
},
methods: {
show() {
Expand All @@ -94,6 +78,26 @@
this.index += 1
}
},
eventListener(e) {
if (this.visible) {
switch (e.key) {
case 'ArrowRight':
this.next()
break
case 'ArrowLeft':
this.prev()
break
case 'ArrowDown':
case 'ArrowUp':
case ' ':
e.preventDefault()
break
case 'Escape':
this.hide()
break
}
}
},
},
}
</script>
Expand Down

0 comments on commit 890c6f7

Please sign in to comment.