toast.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <template>
  2. <teleport to="body">
  3. <transition name="slide-down" mode="out-in" appear>
  4. <div class="ui-toast" :style="{ zIndex: zIndex }" v-if="show">
  5. <div class="ui-toast__box" :class="[type]">
  6. <i v-if="type !== 'fixed' && type" class="icon"></i>
  7. <div class="ui-toast__msg" v-html="content"></div>
  8. <i class="iconfont icon-close close" @click="close" v-if="showClose"></i>
  9. </div>
  10. </div>
  11. </transition>
  12. </teleport>
  13. </template>
  14. <script lang="ts">
  15. import { defineComponent, nextTick, ref } from 'vue'
  16. import { useZIndex } from '@kankan/hooks'
  17. const { currentZIndex } = useZIndex()
  18. export default defineComponent({
  19. name: 'UIToast',
  20. props: {
  21. type: String,
  22. delay: Number,
  23. content: String,
  24. destroy: Function,
  25. close: Function,
  26. showClose: Boolean,
  27. },
  28. setup: function (props, _) {
  29. const show = ref(true)
  30. const close = () => {
  31. show.value = false
  32. nextTick(() => {
  33. if (typeof props.close == 'function') {
  34. props.close()
  35. }
  36. typeof props.destroy === 'function' && props.destroy()
  37. })
  38. }
  39. if (props.type !== 'fixed') {
  40. setTimeout(() => close(), props.delay || 3000)
  41. }
  42. return {
  43. show,
  44. // type: props.type,
  45. // close,
  46. // content: props.content,
  47. zIndex: currentZIndex,
  48. }
  49. },
  50. })
  51. </script>
  52. <style scoped>
  53. .slide-down-enter-active,
  54. .slide-down-leave-active {
  55. will-change: transform;
  56. transition: all 0.35s ease-in-out;
  57. }
  58. .slide-down-enter-from {
  59. opacity: 0;
  60. transform: translate3d(0, -100%, 0);
  61. }
  62. .slide-down-enter {
  63. opacity: 1;
  64. transform: translate3d(-50%, 100%, 0);
  65. }
  66. .slide-down-leave-active {
  67. opacity: 0;
  68. transform: translate3d(0, -100%, 0);
  69. }
  70. </style>