File size: 3,061 Bytes
89ce340 |
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
<template>
<Teleport to="body">
<Transition name="modal-fade">
<div class="modal" ref="modalRef" v-show="visible" tabindex="-1" @keyup.esc="onEsc()">
<div class="mask" @click="onClickMask()"></div>
<Transition name="modal-zoom"
@afterLeave="contentVisible = false"
@before-enter="contentVisible = true"
>
<div class="modal-content" v-show="visible" :style="contentStyle">
<span class="close-btn" v-if="closeButton" @click="close()"><IconClose /></span>
<slot v-if="contentVisible"></slot>
</div>
</Transition>
</div>
</Transition>
</Teleport>
</template>
<script lang="ts" setup>
import { computed, nextTick, ref, watch, type CSSProperties } from 'vue'
import { icons } from '@/plugins/icon'
const { IconClose } = icons
const props = withDefaults(defineProps<{
visible: boolean
width?: number
closeButton?: boolean
closeOnClickMask?: boolean
closeOnEsc?: boolean
contentStyle?: CSSProperties
}>(), {
width: 480,
closeButton: false,
closeOnClickMask: true,
closeOnEsc: true,
})
const modalRef = ref<HTMLDivElement>()
const emit = defineEmits<{
(event: 'update:visible', payload: boolean): void
(event: 'closed'): void
}>()
const contentVisible = ref(false)
const contentStyle = computed(() => {
return {
width: props.width + 'px',
...(props.contentStyle || {})
}
})
watch(() => props.visible, () => {
if (props.visible) {
nextTick(() => modalRef.value!.focus())
}
})
const close = () => {
emit('update:visible', false)
emit('closed')
}
const onEsc = () => {
if (props.visible && props.closeOnEsc) close()
}
const onClickMask = () => {
if (props.closeOnClickMask) close()
}
</script>
<style lang="scss" scoped>
.modal, .mask {
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 5000;
}
.modal {
position: fixed;
display: flex;
justify-content: center;
align-items: center;
outline: 0;
border: 0;
}
.mask {
position: absolute;
background: rgba(0, 0, 0, .25);
}
.modal-content {
z-index: 5001;
padding: 20px;
background: #fff;
border-radius: $borderRadius;
overflow: hidden;
box-shadow: 0 1px 3px rgba(0, 0, 0, .2);
position: relative;
}
.close-btn {
width: 20px;
height: 20px;
display: flex;
justify-content: center;
align-items: center;
position: absolute;
top: 16px;
right: 16px;
cursor: pointer;
}
.modal-fade-enter-active {
animation: modal-fade-enter .25s both ease-in;
}
.modal-fade-leave-active {
animation: modal-fade-leave .25s both ease-out;
}
.modal-zoom-enter-active {
animation: modal-zoom-enter .25s both cubic-bezier(.4, 0, 0, 1.5);
}
.modal-zoom-leave-active {
animation: modal-zoom-leave .25s both;
}
@keyframes modal-fade-enter {
from {
opacity: 0;
}
}
@keyframes modal-fade-leave {
to {
opacity: 0;
}
}
@keyframes modal-zoom-enter {
from {
transform: scale3d(.3, .3, .3);
}
}
@keyframes modal-zoom-leave {
to {
transform: scale3d(.3, .3, .3);
}
}
</style> |