Toast 轻提示

对用户操作做出及时反馈,顶部居中显示并自动消失。比 Notification 更轻量,通常仅一行内容。

Toast 提示是对用户的操作做出及时反馈,由用户的操作触发,反馈信息可以是操作的结果状态,如成功、失败、出错、警告等。

何时使用

  • 操作反馈提示,如成功、失败、警告等。
  • 顶部居中显示并自动消失,是一种不打断用户操作的轻量级提示方式。
  • 与 Notification 的区别:Toast 更轻量、更简洁,通常不包含标题,仅显示一行内容。

导入

import { ToastContainer, useToast } from "@reglow/reui";

useToast() 返回一个 API 对象,包含 info、success、error、warning、close、destroyAll 等方法。

ToastContainer 是渲染提示的容器组件,需在应用根组件挂载一次。

代码演示

普通提示

<script setup>
import { ToastContainer, useToast } from "@reglow/reui";

const toast = useToast();
</script>

<template>
  <ToastContainer />
  <Button @click="toast.info('Hi, Bytedance dance dance')">显示提示</Button>
</template>

其他提示类型

包括成功、失败、警告。

<script setup>
const toast = useToast();
</script>

<template>
  <ToastContainer />
  <Button @click="toast.success('操作成功')">Success</Button>
  <Button @click="toast.warning('警告信息')">Warning</Button>
  <Button @click="toast.error('操作失败')">Error</Button>
</template>

多色样式

使用 theme: 'light' 设置浅色填充样式。

<script setup>
const toast = useToast();
</script>

<template>
  <ToastContainer />
  <Button @click="toast.info({ content: '提示', theme: 'light' })">Info</Button>
  <Button @click="toast.success({ content: '成功', theme: 'light' })">Success</Button>
  <Button @click="toast.warning({ content: '警告', theme: 'light' })">Warning</Button>
  <Button @click="toast.error({ content: '错误', theme: 'light' })">Error</Button>
</template>

修改延时

自定义时长 10s,默认时长为 3s。设为 0 时不自动关闭。

<script setup>
const toast = useToast();
</script>

<template>
  <ToastContainer />
  <Button @click="toast.info({ content: '10秒后关闭', duration: 10 })">
    Close After 10s
  </Button>
</template>

手动关闭

当 duration 设置为 0 时,toast 不会自动关闭,需要通过 close 手动关闭。

<script setup>
import { ref } from "vue";

const toast = useToast();
const toastId = ref("");

function show() {
  toastId.value = toast.info({ content: "Not auto close", duration: 0 });
}

function hide() {
  toast.close(toastId.value);
  toastId.value = "";
}
</script>

<template>
  <ToastContainer />
  <Button @click="show">Show Toast</Button>
  <Button @click="hide" :disabled="!toastId">Hide Toast</Button>
</template>

更新消息内容

通过唯一的 id 来更新已有 toast 的内容。

<script setup>
const toast = useToast();

function show() {
  const id = "toastid";
  toast.info({ content: "Loading...", id });
  setTimeout(() => {
    toast.success({ content: "Done!", id });
  }, 1000);
}
</script>

<template>
  <ToastContainer />
  <Button @click="show">Update Content By Id</Button>
</template>

自定义图标

<script setup>
import { h } from "vue";
import { BellRing } from "lucide-vue-next";

const toast = useToast();
</script>

<template>
  <ToastContainer />
  <Button @click="toast.info({ content: '自定义图标', icon: h(BellRing) })">
    Custom Icon
  </Button>
</template>

关闭按钮

通过 showClose 控制是否显示关闭按钮。

<template>
  <ToastContainer />
  <Button @click="toast.info({ content: '无关闭按钮', showClose: false })">
    No Close Button
  </Button>
</template>

销毁所有

<script setup>
const toast = useToast();
</script>

<template>
  <ToastContainer />
  <Button @click="toast.destroyAll()">Destroy All</Button>
</template>

全局配置

通过 config 方法设置全局配置。

const toast = useToast();

toast.config({
  duration: 5, // 默认自动关闭延时 5 秒
  top: 100, // 距离顶部 100px
  zIndex: 2000, // z-index
  theme: "light", // 默认浅色样式
});

API

useToast()

返回 ToastAPI 对象。

方法说明参数
open(options)打开提示ToastOptions & { type?: ToastType }
info(options | string)信息提示ToastOptions | string
success(options | string)成功提示ToastOptions | string
error(options | string)错误提示ToastOptions | string
warning(options | string)警告提示ToastOptions | string
close(id)手动关闭指定提示string
destroyAll()销毁所有提示-
config(options)全局配置{ duration?, top?, zIndex?, theme? }

所有方法返回 toastId,可用于手动关闭。

ToastOptions

参数说明类型默认值
id自定义 ToastId(用于更新)string自动生成
content提示内容string""
duration自动关闭延时(秒),设为 0 不自动关闭number3
icon自定义图标VNode-
showClose是否显示关闭按钮booleantrue
theme填充样式light | normalnormal
textMaxWidth内容最大宽度number | string450
onClose关闭回调() => void-
onClick点击回调() => void-

Config

参数说明类型默认值
duration默认自动关闭延时number3
top距离顶部位置 (px)number24
zIndexz-indexnumber1010
theme默认填充样式light | normalnormal

ToastType

type ToastType = "info" | "success" | "warning" | "error";

主题样式对照

theme说明
normal白色背景,适合浅色界面
light彩色浅色填充,不同类型有不同的背景色