Modal.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <template>
  2. <Teleport to="body">
  3. <Transition name="modal-fade">
  4. <div class="modal" ref="modalRef" v-show="visible" tabindex="-1" @keyup.esc="onEsc()">
  5. <div class="mask" @click="onClickMask()"></div>
  6. <Transition name="modal-zoom"
  7. @afterLeave="contentVisible = false"
  8. @before-enter="contentVisible = true"
  9. >
  10. <div class="modal-content" v-show="visible" :style="contentStyle">
  11. <span class="close-btn" v-if="closeButton" @click="close()"><IconClose /></span>
  12. <slot v-if="contentVisible"></slot>
  13. </div>
  14. </Transition>
  15. </div>
  16. </Transition>
  17. </Teleport>
  18. </template>
  19. <script lang="ts" setup>
  20. import { computed, nextTick, ref, watch, type CSSProperties } from 'vue'
  21. import { icons } from '@/plugins/icon'
  22. const { IconClose } = icons
  23. const props = withDefaults(defineProps<{
  24. visible: boolean
  25. width?: number
  26. closeButton?: boolean
  27. closeOnClickMask?: boolean
  28. closeOnEsc?: boolean
  29. contentStyle?: CSSProperties
  30. }>(), {
  31. width: 480,
  32. closeButton: false,
  33. closeOnClickMask: true,
  34. closeOnEsc: true,
  35. })
  36. const modalRef = ref<HTMLDivElement>()
  37. const emit = defineEmits<{
  38. (event: 'update:visible', payload: boolean): void
  39. (event: 'closed'): void
  40. }>()
  41. const contentVisible = ref(false)
  42. const contentStyle = computed(() => {
  43. return {
  44. width: props.width + 'px',
  45. ...(props.contentStyle || {})
  46. }
  47. })
  48. watch(() => props.visible, () => {
  49. if (props.visible) {
  50. nextTick(() => modalRef.value!.focus())
  51. }
  52. })
  53. const close = () => {
  54. emit('update:visible', false)
  55. emit('closed')
  56. }
  57. const onEsc = () => {
  58. if (props.visible && props.closeOnEsc) close()
  59. }
  60. const onClickMask = () => {
  61. if (props.closeOnClickMask) close()
  62. }
  63. </script>
  64. <style lang="scss" scoped>
  65. .modal, .mask {
  66. top: 0;
  67. left: 0;
  68. width: 100%;
  69. height: 100%;
  70. z-index: 1999;
  71. }
  72. .modal {
  73. position: fixed;
  74. display: flex;
  75. justify-content: center;
  76. align-items: center;
  77. outline: 0;
  78. border: 0;
  79. }
  80. .mask {
  81. position: absolute;
  82. background: rgba(0, 0, 0, .25);
  83. }
  84. .modal-content {
  85. z-index: 5001;
  86. padding: 20px;
  87. background: #fff;
  88. border-radius: $borderRadius;
  89. overflow: hidden;
  90. box-shadow: 0 1px 3px rgba(0, 0, 0, .2);
  91. position: relative;
  92. }
  93. .close-btn {
  94. width: 20px;
  95. height: 20px;
  96. display: flex;
  97. justify-content: center;
  98. align-items: center;
  99. position: absolute;
  100. top: 16px;
  101. right: 16px;
  102. cursor: pointer;
  103. }
  104. .modal-fade-enter-active {
  105. animation: modal-fade-enter .25s both ease-in;
  106. }
  107. .modal-fade-leave-active {
  108. animation: modal-fade-leave .25s both ease-out;
  109. }
  110. .modal-zoom-enter-active {
  111. animation: modal-zoom-enter .25s both cubic-bezier(.4, 0, 0, 1.5);
  112. }
  113. .modal-zoom-leave-active {
  114. animation: modal-zoom-leave .25s both;
  115. }
  116. @keyframes modal-fade-enter {
  117. from {
  118. opacity: 0;
  119. }
  120. }
  121. @keyframes modal-fade-leave {
  122. to {
  123. opacity: 0;
  124. }
  125. }
  126. @keyframes modal-zoom-enter {
  127. from {
  128. transform: scale3d(.3, .3, .3);
  129. }
  130. }
  131. @keyframes modal-zoom-leave {
  132. to {
  133. transform: scale3d(.3, .3, .3);
  134. }
  135. }
  136. </style>