Skip to content

Commit 00ed02f

Browse files
authored
feat: Close modal with ESC key (#338)
1 parent 08f1f8f commit 00ed02f

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

frontend/src/views/Shop.vue

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,16 @@ export default {
232232
code: null,
233233
}
234234
},
235+
beforeUnmount() {
236+
document.removeEventListener('keydown', this.handleEscKey)
237+
},
235238
methods: {
236239
openModal(product) {
237240
this.product = product
238241
this.modalOpen = true
242+
// Remove first to prevent duplicate event listeners
243+
document.removeEventListener('keydown', this.handleEscKey)
244+
document.addEventListener('keydown', this.handleEscKey)
239245
},
240246
closeModal() {
241247
this.product = null
@@ -246,6 +252,12 @@ export default {
246252
this.purchaseSuccess = false
247253
this.code = null
248254
this.purchaseMode = 'myself'
255+
document.removeEventListener('keydown', this.handleEscKey)
256+
},
257+
handleEscKey(event) {
258+
if (event.key === 'Escape' && this.modalOpen) {
259+
this.closeModal()
260+
}
249261
},
250262
async purchase() {
251263
this.loading = true

0 commit comments

Comments
 (0)