cl-toast.uvue 3.5 KB

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