cl-toast.uvue 3.5 KB

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