Modal.vue 3.1 KB

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