index.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <transition name="slide-fade">
  3. <div
  4. :class="[
  5. 'm-message',
  6. customClass,
  7. {'is-center': center}
  8. ]"
  9. :style="messagesStyle[type]"
  10. v-show="isShow"
  11. >
  12. <template v-if="isText">
  13. <i class="iconfont" :class="[messagesStyle[type].icon]"></i>
  14. <span class="text">{{ text }}</span>
  15. <i class="iconfont icon-close" @click="close" v-if="showClose"></i>
  16. </template>
  17. <template v-else>
  18. <slot />
  19. </template>
  20. </div>
  21. </transition>
  22. </template>
  23. <script setup>
  24. import { onMounted, ref, reactive, computed } from 'vue-demi'
  25. const props = defineProps({
  26. text: {
  27. type: [String, Object],
  28. default: ''
  29. },
  30. type: {
  31. type: String,
  32. default: 'info'
  33. },
  34. timeout: {
  35. type: Number,
  36. default: 3000
  37. },
  38. icon: String,
  39. textColor: String,
  40. bgColor: String,
  41. customClass: String,
  42. center: {
  43. type: Boolean,
  44. default: false
  45. },
  46. showClose: {
  47. type: Boolean,
  48. default: false
  49. }
  50. })
  51. const state = reactive({
  52. messagesStyle: {
  53. success: {
  54. icon: props.icon || 'icon-success',
  55. color: '#67c23a',
  56. backgroundColor: '#f0f9eb',
  57. borderColor: '#f0f9eb'
  58. },
  59. warning: {
  60. icon: props.icon || 'icon-warning',
  61. color: '#e6a23c',
  62. backgroundColor: '#fdf6ec',
  63. borderColor: '#fdf6ec'
  64. },
  65. info: {
  66. icon: props.icon || 'icon-prompt-filling',
  67. color: '#909399',
  68. backgroundColor: '#f4f4f5',
  69. borderColor: '#f4f4f5'
  70. },
  71. error: {
  72. icon: props.icon || 'icon-delete-filling',
  73. color: '#f56c6c',
  74. backgroundColor: '#fef0f0',
  75. borderColor: '#fef0f0'
  76. },
  77. custom: {
  78. icon: props.icon,
  79. color: props.textColor,
  80. backgroundColor: props.bgColor,
  81. borderColor: props.bgColor
  82. }
  83. }
  84. })
  85. const isShow = ref(false)
  86. const isText = computed(() => {
  87. return typeof props.text === 'string'
  88. })
  89. onMounted(() => {
  90. isShow.value = true
  91. })
  92. const close = () => {
  93. isShow.value = false
  94. }
  95. const { messagesStyle } = state
  96. </script>
  97. <style lang="scss" scoped>
  98. @import '../../styles/components/message.scss';
  99. .slide-fade-enter-active {
  100. transition: all 0.3s ease-out;
  101. }
  102. .slide-fade-leave-active {
  103. transition: all 0.3s ease-out;
  104. }
  105. .slide-fade-enter-from,
  106. .slide-fade-leave-to {
  107. transform: translate3d(-50%, -75px, 0);
  108. }
  109. </style>