跳转到内容
Nacre
组件配色
EN

通知 Toast

stableToastHost在画廊运行
ToastHost
在浏览器里运行真实组件。

操作结果与错误的轻提示,不打断操作:任何地方调 toast.show({ kind, message, … }) 弹出,应用根上挂一次 负责画(窗口顶部居中,最多叠 3 条,默认停 3 秒,错误与带动作的停 10 秒,悬停或键盘焦点在里面时暂停计时)。手机端换一套规则:success 默认不弹(结果在界面上看不出来的传 essential: true 才弹,画成状态栏下的胶囊、停 2 秒);error / warning 是常驻横幅,不计时,手动关闭或被同 key 替换。

手机:操作结果与错误的轻提示:手机上成功提示默认不弹(看不出结果的传 essential),警告和错误是导航栏下方的横幅。

状态:stable · 组件:ToastHost

import { ToastHost } from 'nacre'

toast.show 弹一条;kind 默认 info。ToastHost 在应用根上挂一次(本页所在的应用已经挂好)。

import { Button, toast, TOUCH } from 'nacre'
export function ToastUsage() {
return (
<Button onPress={() => toast.show({ kind: 'success', message: '已保存', essential: TOUCH })}>
保存
</Button>
)
}

info / success / warning / error:图标与颜色区分,错误是圆圈叉、警告是三角;警告与错误带关闭键。

import { View } from 'nsh'
import { Button, toast, TOUCH } from 'nacre'
export function ToastKinds() {
return (
<View class="flex-row flex-wrap items-center gap-2">
<Button variant="outline" onPress={() => toast.show({ message: '已复制会话链接' })}>提示</Button>
<Button variant="outline" onPress={() => toast.show({ kind: 'success', message: '已转接给陈智源', essential: TOUCH })}>成功</Button>
<Button variant="outline" onPress={() => toast.show({ kind: 'warning', message: '客户已离线,消息将在上线后送达' })}>警告</Button>
<Button variant="outline" onPress={() => toast.show({ kind: 'error', message: '发送失败,请检查网络' })}>错误</Button>
</View>
)
}

action 给一个按钮(「重试」),点了执行并收起;带动作的提示可以手动关闭。

import { Text, View, signal } from 'nsh'
import { Button, toast, TEXT_SIZE } from 'nacre'
export function ToastRetry() {
const attempts = signal(0)
const send = () => {
attempts.update((n) => n + 1)
toast.show({ kind: 'error', message: '网络异常,消息未发出', action: { label: '重试', onPress: send } })
}
return (
<View class="flex-row items-center gap-3">
<Button variant="outline" onPress={send}>发送消息</Button>
<Text class={`${TEXT_SIZE.small} text-muted-foreground tabular-nums`}>{`$已尝试 ${attempts()}$ 次`}</Text>
</View>
)
}

description 是第二行说明(最多 3 行);同 key 的提示还在时替换它而不是再叠一条,适合同类请求错误;duration 改停留时间。

import { Button, toast } from 'nacre'
export function ToastKeyed() {
return (
<Button
variant="outline"
onPress={() =>
toast.show({
kind: 'error',
key: 'customer-profile-error',
message: '客户资料加载失败',
description: '服务暂时不可用,请稍后重试;多次失败请联系管理员。',
duration: 10000,
})
}
>
加载客户资料
</Button>
)
}
  • 错误、警告是 role=“alert”,出现时读屏立即播报;成功与普通提示礼貌播报(polite),不打断正在读的内容。
  • 错误、警告与带动作的提示有关闭按钮;动作是普通按钮,Tab 可达。
  • 鼠标停在提示上时暂停计时,离开后按剩余时间继续(至少再停 1 秒)。
  • 手机端的失败横幅不自动消失,关闭键 40pt;轻提示胶囊点一下就收起。

该用

  • 先问界面上看不看得出结果:看得出就不弹(行移走、计数变就是反馈)
  • 复制到剪贴板、要等对方确认这类看不出结果的成功传 essential: true
  • 错误给重试动作
  • 同类请求错误用 key 去重
  • 文案写结果(「已转接给陈智源」),不写「操作成功」
  • ToastHost 只在应用根上挂一次

不该用

  • 为已经看得到的结果再弹一条「操作成功」
  • 表单校验错误只放在提示里(贴在字段旁边)
  • 用来要用户做决定(用对话框)
  • 把必须看到的信息只放在提示里(几秒就消失)
  • 同一时刻连弹多条同类提示

没有属性。