index.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <template>
  2. <Transition name="toast-fade" @after-leave="onAfterLeave">
  3. <view
  4. :class="['popup-top', 'nut-notify', `nut-notify--${type}`, { className }]"
  5. :style="{ color: color, background: background }"
  6. v-show="state.mounted"
  7. @click="clickCover"
  8. >
  9. <template v-if="$slots.default">
  10. <slot></slot>
  11. </template>
  12. <template v-else>{{ msg }}</template>
  13. </view>
  14. </Transition>
  15. <!-- <nut-popup v-model:visible="state.mounted" position="top" :style="{ color: color, background: background }" :class="['popup-top', 'nut-notify', `nut-notify--${type}`, { className }]" overlay="false">
  16. <template v-if="$slots.default">
  17. <slot></slot>
  18. </template>
  19. <template v-else>{{ msg }}</template>
  20. </nut-popup> -->
  21. </template>
  22. <script lang="ts">
  23. import { toRefs, reactive, onMounted, watch } from 'vue';
  24. import { createComponent } from '@/utils/create';
  25. import Popup from '@/packages/popup/index.vue';
  26. const { componentName, create } = createComponent('notify');
  27. export default create({
  28. components: {
  29. [Popup.name]: Popup
  30. },
  31. props: {
  32. id: String,
  33. color: { type: String, default: '' },
  34. msg: { type: Number, default: '' },
  35. duration: { type: Number, default: 3000 },
  36. className: {
  37. type: String,
  38. default: ''
  39. },
  40. background: { type: String, default: '' },
  41. type: {
  42. type: String,
  43. default: 'danger'
  44. },
  45. showPopup: {
  46. type: Boolean,
  47. default: false
  48. },
  49. onClose: Function,
  50. onClick: Function,
  51. unmount: Function
  52. },
  53. setup(props, { slots }) {
  54. let timer: null | number = null;
  55. const state = reactive({
  56. mounted: false
  57. });
  58. onMounted(() => {
  59. state.mounted = true;
  60. });
  61. const clickCover = () => {
  62. props.onClick && props.onClick();
  63. };
  64. const clearTimer = () => {
  65. if (timer) {
  66. clearTimeout(timer);
  67. timer = null;
  68. }
  69. };
  70. const hide = () => {
  71. state.mounted = false;
  72. };
  73. const show = () => {
  74. clearTimer();
  75. if (props.duration) {
  76. timer = setTimeout(() => {
  77. hide();
  78. }, props.duration);
  79. }
  80. };
  81. if (props.duration) {
  82. show();
  83. }
  84. watch(
  85. () => props.duration,
  86. val => {
  87. if (val) {
  88. show();
  89. }
  90. }
  91. );
  92. const onAfterLeave = () => {
  93. clearTimer();
  94. props.unmount && props.unmount(props.id);
  95. props.onClose && props.onClose();
  96. };
  97. return { state, hide, onAfterLeave, clickCover };
  98. }
  99. });
  100. </script>
  101. <style lang="scss">
  102. @import 'index.scss';
  103. </style>