2020-10-22 14:00:33 +08:00
|
|
|
<script lang="ts">
|
2020-12-31 14:33:17 +08:00
|
|
|
import { createVNode, defineComponent, renderSlot, h } from 'vue'
|
2020-10-22 14:00:33 +08:00
|
|
|
import { PatchFlags } from '@element-plus/utils/vnode'
|
|
|
|
|
2020-09-09 21:18:08 +08:00
|
|
|
export default defineComponent({
|
|
|
|
name: 'ElOverlay',
|
|
|
|
props: {
|
|
|
|
mask: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true,
|
|
|
|
},
|
|
|
|
overlayClass: {
|
2021-01-15 12:13:06 +08:00
|
|
|
type: [String, Array, Object],
|
2020-09-09 21:18:08 +08:00
|
|
|
},
|
|
|
|
zIndex: {
|
|
|
|
type: Number,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
emits: ['click'],
|
|
|
|
setup(props, { slots, emit }) {
|
2021-01-14 17:01:37 +08:00
|
|
|
let mousedownTarget = false
|
|
|
|
let mouseupTarget = false
|
|
|
|
// refer to this https://javascript.info/mouse-events-basics
|
|
|
|
// events fired in the order: mousedown -> mouseup -> click
|
|
|
|
// we need to set the mousedown handle to false after click
|
|
|
|
// fired.
|
|
|
|
const onMaskClick = (e: MouseEvent) => {
|
|
|
|
// due to these two value were set only when props.mask is true
|
|
|
|
// so there is no need to do any extra judgment here.
|
|
|
|
// if and only if
|
|
|
|
if (mousedownTarget && mouseupTarget) {
|
|
|
|
emit('click', e)
|
|
|
|
}
|
|
|
|
mousedownTarget = mouseupTarget = false
|
2020-09-09 21:18:08 +08:00
|
|
|
}
|
2021-01-14 17:01:37 +08:00
|
|
|
|
2020-09-09 21:18:08 +08:00
|
|
|
// init here
|
|
|
|
return () => {
|
2020-12-31 14:33:17 +08:00
|
|
|
// when the vnode meets the same structure but with different change trigger
|
|
|
|
// it will not automatically update, thus we simply use h function to manage updating
|
2020-09-09 21:18:08 +08:00
|
|
|
return props.mask
|
2020-10-22 14:00:33 +08:00
|
|
|
? createVNode(
|
2020-09-09 21:18:08 +08:00
|
|
|
'div',
|
|
|
|
{
|
|
|
|
class: ['el-overlay', props.overlayClass],
|
|
|
|
style: {
|
|
|
|
zIndex: props.zIndex,
|
|
|
|
},
|
|
|
|
onClick: onMaskClick,
|
2021-01-14 17:01:37 +08:00
|
|
|
onMousedown: (e: MouseEvent) => {
|
|
|
|
// marking current mousedown target.
|
|
|
|
if (props.mask) {
|
|
|
|
mousedownTarget = e.target === e.currentTarget
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onMouseup: (e: MouseEvent) => {
|
|
|
|
if (props.mask) {
|
|
|
|
mouseupTarget = e.target === e.currentTarget
|
|
|
|
}
|
|
|
|
},
|
2020-09-09 21:18:08 +08:00
|
|
|
},
|
2020-10-22 14:00:33 +08:00
|
|
|
[renderSlot(slots, 'default')],
|
|
|
|
PatchFlags.STYLE | PatchFlags.CLASS | PatchFlags.PROPS,
|
2021-01-14 17:01:37 +08:00
|
|
|
['onClick', 'onMouseup', 'onMousedown'],
|
2020-09-09 21:18:08 +08:00
|
|
|
)
|
2021-01-14 17:01:37 +08:00
|
|
|
: h(
|
|
|
|
'div',
|
|
|
|
{
|
2021-05-06 19:35:29 +08:00
|
|
|
class: props.overlayClass,
|
2021-01-14 17:01:37 +08:00
|
|
|
style: {
|
|
|
|
zIndex: props.zIndex,
|
|
|
|
position: 'fixed',
|
|
|
|
top: '0px',
|
|
|
|
right: '0px',
|
|
|
|
bottom: '0px',
|
|
|
|
left: '0px',
|
|
|
|
},
|
2020-12-31 14:33:17 +08:00
|
|
|
},
|
2021-01-14 17:01:37 +08:00
|
|
|
[renderSlot(slots, 'default')],
|
|
|
|
)
|
2020-09-09 21:18:08 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
</script>
|