forked from element-plus/element-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
44 lines (38 loc) · 946 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { watch } from 'vue'
import { on } from '@element-plus/utils/dom'
import { EVENT_CODE } from '@element-plus/utils/aria'
import isServer from '@element-plus/utils/isServer'
import type { Ref, ComputedRef } from 'vue'
type ModalInstance = {
handleClose: () => void
};
const modalStack: ModalInstance[] = []
const closeModal = (e: KeyboardEvent) => {
if (modalStack.length === 0) return
if (e.code === EVENT_CODE.esc) {
e.stopPropagation()
const topModal = modalStack[modalStack.length - 1]
topModal.handleClose()
}
}
export default (
instance: ModalInstance,
visibleRef: Ref<boolean> | ComputedRef,
) => {
watch(
() => visibleRef.value,
val => {
if (val) {
modalStack.push(instance)
} else {
modalStack.splice(
modalStack.findIndex(modal => modal === instance),
1,
)
}
},
)
}
if (!isServer) {
on(document, 'keydown', closeModal)
}