Skip to content

Commit 794907c

Browse files
committed
feat: Escape to close the Popup
1 parent 8e2bc46 commit 794907c

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

Diff for: src/components/Popup.vue

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<script>
3131
import { computed, inject } from 'vue'
3232
import useDisclosure from '@/composables/useDisclosure'
33+
import useEventListener from '@/composables/useEventListener'
3334
import { vueAxe } from '@/utils/constants'
3435
3536
import PopupBody from '@/components/PopupBody'
@@ -62,13 +63,15 @@ export default {
6263
6364
setup () {
6465
const { results } = inject(vueAxe)
65-
const { isOpen, toggle: togglePopup } = useDisclosure()
66+
const { isOpen, onClose, toggle: togglePopup } = useDisclosure()
6667
6768
const issuesFound = computed(() => {
6869
if (!results.value.issuesFound) return 0
6970
return results.value.issuesFound
7071
})
7172
73+
useEventListener('keydown', e => (e.key === 'Escape' && isOpen.value) && onClose())
74+
7275
return {
7376
isOpen,
7477
togglePopup,

Diff for: src/composables/useEventListener.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { onMounted, onUnmounted, ref } from 'vue'
2+
3+
export default function useEventListener (type, listener, options = {}, target) {
4+
const eventOptions = { capture: false, ...options }
5+
const el = ref(null)
6+
7+
onMounted(() => {
8+
el.value = typeof target === 'string' ? document.querySelector(target) : window
9+
el.value.addEventListener(type, listener, eventOptions)
10+
})
11+
12+
onUnmounted(() => {
13+
el.value.removeEventListener(type, listener, eventOptions)
14+
})
15+
}

0 commit comments

Comments
 (0)