cl-toast.uvue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <template>
  2. <cl-popup
  3. v-for="(item, id) in list"
  4. :key="id"
  5. :direction="item.position"
  6. :show-mask="false"
  7. :show-header="false"
  8. :swipe-close="false"
  9. :pt="{
  10. inner: {
  11. className: '!bg-transparent'
  12. }
  13. }"
  14. keep-alive
  15. pointer-events="none"
  16. v-model="item.visible"
  17. >
  18. <view class="cl-toast-wrapper">
  19. <view
  20. class="cl-toast"
  21. :class="[
  22. {
  23. 'is-dark': isDark,
  24. 'is-open': item.isOpen
  25. },
  26. `cl-toast--${item.position}`
  27. ]"
  28. >
  29. <view class="flex flex-row justify-center">
  30. <cl-icon
  31. :name="item.icon"
  32. :size="56"
  33. :pt="{
  34. className: `mb-1 !text-white`
  35. }"
  36. v-if="item.icon != null"
  37. ></cl-icon>
  38. </view>
  39. <text class="cl-toast__text">{{ item.message }}</text>
  40. </view>
  41. </view>
  42. </cl-popup>
  43. </template>
  44. <script setup lang="ts">
  45. import { reactive, ref } from "vue";
  46. import type { ClToastOptions } from "../../types";
  47. import { isDark, isNull } from "@/cool";
  48. defineOptions({
  49. name: "cl-toast"
  50. });
  51. // ToastItem 类型定义,表示单个toast的属性
  52. type ToastItem = {
  53. id: number; // 唯一标识
  54. visible: boolean; // 是否显示
  55. isOpen: boolean; // 是否打开
  56. icon?: string; // 可选,图标名称
  57. image?: string; // 可选,图片地址
  58. message: string; // 显示的文本内容
  59. position: "top" | "center" | "bottom"; // 显示位置
  60. duration: number; // 显示时长(毫秒)
  61. };
  62. // toast列表,当前仅用于v-for结构,实际只显示一个
  63. const list = ref<ToastItem[]>([]);
  64. // 用于生成唯一id
  65. let id = 0;
  66. // 关闭toast的方法
  67. function close(id: number) {
  68. const item = list.value.find((item) => item.id == id);
  69. if (item != null) {
  70. item.visible = false;
  71. }
  72. }
  73. // 打开toast的方法,传入配置信息
  74. function open(options: ClToastOptions) {
  75. // 创建一个新的 ToastItem 实例,包含所有配置信息
  76. const item = reactive<ToastItem>({
  77. id: id++,
  78. visible: true,
  79. isOpen: false,
  80. icon: options.icon,
  81. image: options.image,
  82. duration: options.duration ?? 2000,
  83. position: options.position ?? "center",
  84. message: options.message
  85. });
  86. // 如果有icon或image,强制居中显示
  87. if (!isNull(item.icon) || !isNull(item.image)) {
  88. item.position = "center";
  89. }
  90. switch (options.type) {
  91. case "success":
  92. item.icon = "checkbox-circle-line";
  93. break;
  94. case "warn":
  95. item.icon = "error-warning-line";
  96. break;
  97. case "error":
  98. item.icon = "close-circle-line";
  99. break;
  100. case "question":
  101. item.icon = "question-line";
  102. break;
  103. case "disabled":
  104. item.icon = "prohibited-line";
  105. break;
  106. case "stop":
  107. item.icon = "indeterminate-circle-line";
  108. break;
  109. }
  110. // 如果clear为true,则只保留当前toast,否则追加到列表
  111. if (options.clear == true) {
  112. list.value = [item];
  113. } else {
  114. list.value.push(item);
  115. }
  116. // 延迟打开toast,避免闪烁
  117. setTimeout(() => {
  118. item.isOpen = true;
  119. // 如果duration不为0,则自动关闭toast
  120. if (item.duration != 0) {
  121. setTimeout(() => {
  122. close(item.id); // 到时自动关闭
  123. }, item.duration!);
  124. }
  125. }, 50);
  126. }
  127. defineExpose({
  128. open,
  129. close
  130. });
  131. </script>
  132. <style lang="scss" scoped>
  133. .cl-toast-wrapper {
  134. @apply flex flex-col items-center justify-center;
  135. padding: 50rpx 0;
  136. }
  137. .cl-toast {
  138. @apply px-[32rpx] py-[24rpx] rounded-2xl;
  139. background-color: rgba(50, 50, 50, 0.9);
  140. max-width: 600rpx;
  141. opacity: 0;
  142. &__text {
  143. @apply text-md text-center font-bold text-white;
  144. }
  145. &.is-open {
  146. opacity: 1;
  147. }
  148. &.is-dark {
  149. background-color: rgba(70, 70, 70, 0.9);
  150. }
  151. }
  152. </style>