操作结果与错误的轻提示,不打断操作:任何地方调 toast.show({ kind, message, … }) 弹出,应用根上挂一次
手机:操作结果与错误的轻提示:手机上成功提示默认不弹(看不出结果的传 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 只在应用根上挂一次
不该用
- 为已经看得到的结果再弹一条「操作成功」
- 表单校验错误只放在提示里(贴在字段旁边)
- 用来要用户做决定(用对话框)
- 把必须看到的信息只放在提示里(几秒就消失)
- 同一时刻连弹多条同类提示
API
定位到「API」ToastHost
定位到「ToastHost」没有属性。