11<template >
22 <teleport to =" body" >
33 <transition name =" fade" >
4- <div v-if =" show" :class =" {'mask-wrapper': true, transparent}" :style =" wrapperStyle " >
4+ <div v-if =" show" :class =" {'mask-wrapper': true, transparent}" :style =" style " v-auto-z-index = " { layer, onEsc } " >
55 <div class =" mask" @click =" () => maskCloseable && $emit('close')" @contextmenu.prevent.stop =" $emit('close')" />
66 <div class =" content" >
77 <slot ></slot >
1212</template >
1313
1414<script lang="ts">
15- import { computed , defineComponent , onBeforeUnmount , onMounted , ref , watch } from ' vue'
16- import { registerHook , removeHook } from ' @fe/core/hook'
17-
18- let zIndex = 199998
19- const maskStack: number [] = []
15+ import { defineComponent } from ' vue'
2016
2117export default defineComponent ({
2218 name: ' x-mask' ,
@@ -38,50 +34,20 @@ export default defineComponent({
3834 type: [Object , String ],
3935 default : () => ({}),
4036 },
37+ layer: {
38+ type: String as () => ' popup' | ' context-menu' | ' max' ,
39+ default: ' popup'
40+ },
4141 },
4242 emits: [' close' ],
4343 setup (props , { emit }) {
44- const zIndexRef = ref (zIndex ++ )
45-
46- function keydownHandler (e : KeyboardEvent ) {
47- if (e .key === ' Escape' && props .show ) {
48- // close top mask
49- if (! maskStack .includes (zIndexRef .value ) || Math .max (... maskStack ) !== zIndexRef .value ) {
50- return
51- }
52-
44+ function onEsc () {
45+ if (props .show ) {
5346 props .escCloseable && emit (' close' )
54- e .stopPropagation ()
5547 }
5648 }
5749
58- watch (() => props .show , (val ) => {
59- if (val ) {
60- maskStack .push (zIndexRef .value )
61- } else {
62- maskStack .splice (maskStack .indexOf (zIndexRef .value ), 1 )
63- }
64- }, { immediate: true , flush: ' post' })
65-
66- watch (() => props .show , (val ) => {
67- if (val ) {
68- zIndex ++
69- zIndexRef .value = zIndex
70- }
71- })
72-
73- const wrapperStyle = computed (() => (typeof props .style === ' string' ? props .style : { zIndex: zIndexRef .value , ... props .style }))
74-
75- onMounted (() => {
76- registerHook (' GLOBAL_KEYDOWN' , keydownHandler )
77- })
78-
79- onBeforeUnmount (() => {
80- removeHook (' GLOBAL_KEYDOWN' , keydownHandler )
81- maskStack .splice (maskStack .indexOf (zIndexRef .value ), 1 )
82- })
83-
84- return { wrapperStyle }
50+ return { onEsc }
8551 },
8652})
8753 </script >
@@ -93,7 +59,6 @@ export default defineComponent({
9359 left : 0 ;
9460 width : 100% ;
9561 height : 100% ;
96- z-index : 199998 ;
9762 padding-top : 6em ;
9863}
9964
0 commit comments