145a208687
HD-04 scope, against heroui ce90ea3 cn sources: - stale/fictional content: input.md no longer claims TextField is unmigrated; color-field.md no longer claims ColorPicker is private; header.md drops the fictional Header.Root subcomponent and the stale 'menus not migrated' wording (MenuSection label registration is now documented from HeaderRoot source); radio-group.md drops three copied CheckboxGroup wordings (variant override is Radio's, scalar emits, native single-value submit); input-otp.md drops the fictional data-complete state and lists the real missing upstream props - toggle-button disabled row now matches the real CSS hooks (:disabled / [aria-disabled], with data-disabled as a marker only) - part API / CSS classes / interactive states restored per pinned sources: button (full class lists + real variable-based global CSS), accordion (per-part tables + variant/state lists), card (per-part tables + variant list), switch (SwitchGroup snippet + Switch/ SwitchGroup class lists + states), color-field Group (class list + states), input-otp (variant notes, global CSS, class/state lists); input-group gains the two upstream usage chapters - RelatedComponents restored from the pinned componentRelationships (first-3, filtered against componentsMap): 64 pages rewritten, list-box/slider sections added, 7 empty-mapping pages' fabricated entries removed; RelatedShowcases left unmigrated with its real data inventory recorded (2 cases, showcase-site cards) - defaults unified on implementation evidence: granularity (inferred day/minute) and visibleYears (full min-max span, no "or 20" fallback); HD-03 hand-offs resolved (trigger focus-visible noted for HD-06, width guards kept) - new demos: header-collection-grouping (MenuSection/ListBoxSection label wiring), empty-state-collection (collection empty slot); empty-state page deduplicated with a boundary compatibility note - ComponentPreview minHeight/isBgSolid restored per the 30 upstream marks; tabs-overflow max-w-full kept after measuring internal scroll - tests: +3 header registration/demo cases, +1 empty-state collection case; full suite 110 files / 1630 tests, typecheck and build:docs pass
355 lines
19 KiB
Markdown
355 lines
19 KiB
Markdown
---
|
||
title: Toast 轻提示
|
||
description: 向用户展示临时通知与消息,支持自动消失与可定制的放置位置。
|
||
component: toast
|
||
group: 组件
|
||
---
|
||
|
||
> 基于固定 HeroUI 3.2.6(ce90ea3)迁移。命名组件、作用域插槽与 DOM attrs 为 Vue 适配;预览、复制与高亮来自同一份 SFC。
|
||
|
||
## 用法
|
||
|
||
```ts
|
||
import {
|
||
ToastProvider,
|
||
createToastFunction,
|
||
ToastQueue,
|
||
} from "@heroui-vue/components";
|
||
```
|
||
|
||
<ComponentPreview name="toast-default" />
|
||
|
||
## 组件结构
|
||
|
||
`ToastProvider` 渲染通知区域;`ToastQueue` 管理计时与退出生命周期。默认 `timeout=4000`、`gap=12`、可视上限 `3`、宽度 `460`、热键 `Alt+T`、退出保留 `300ms`。**可视上限不删除队列中的旧通知**,只是隐藏并继续计时。
|
||
|
||
```vue
|
||
<ToastProvider :queue="queue" to="body">
|
||
<template #default="{ toast: item, close }">
|
||
<ToastIndicator :variant="item.variant" />
|
||
<ToastContent>
|
||
<ToastTitle>{{ item.title }}</ToastTitle>
|
||
<ToastDescription>{{ item.description }}</ToastDescription>
|
||
</ToastContent>
|
||
<ToastCloseButton @click="close" />
|
||
</template>
|
||
</ToastProvider>
|
||
```
|
||
|
||
不提供插槽时使用完整默认渲染;正文容器只包标题和说明,指示器、动作与关闭按钮为同级。自定义完整卡片样式通过 `toast-class` 传给 Provider 自动创建的 Toast 根节点。客户端可在应用根挂载一个默认 Provider,使用全局 `toast`;独立演示使用自己的队列,退出页面时销毁。
|
||
|
||
`toast()` 返回稳定 key;`toast.update(id, …)` 原位更新;`toast.promise(…)` 就地结算;`timeout: 0` 保持打开。
|
||
|
||
## 示例
|
||
|
||
### 变体
|
||
|
||
<ComponentPreview name="toast-variants" />
|
||
|
||
### 放置位置
|
||
|
||
<ComponentPreview name="toast-placements" />
|
||
|
||
### 展开堆叠
|
||
|
||
<ComponentPreview name="toast-expanded" />
|
||
|
||
### 简单 Toast
|
||
|
||
<ComponentPreview name="toast-simple" />
|
||
|
||
### 自定义指示器
|
||
|
||
<ComponentPreview name="toast-custom-indicator" />
|
||
|
||
### 自定义 Toast 渲染
|
||
|
||
<ComponentPreview name="toast-custom-toast" />
|
||
|
||
### Promise 与加载中
|
||
|
||
<ComponentPreview name="toast-promise" />
|
||
|
||
### 回调
|
||
|
||
<ComponentPreview name="toast-callbacks" />
|
||
|
||
### 自定义队列
|
||
|
||
<ComponentPreview name="toast-custom-queue" />
|
||
|
||
### 配置
|
||
|
||
在应用根节点渲染 Provider。`timeout` 默认 4000ms;`0` 持久显示。悬停、焦点进入、页面隐藏和外部暂停分别管理,全部原因解除后继续剩余时间。可视上限隐藏旧消息但不删除、不停止它们的倒计时。
|
||
|
||
```vue
|
||
<script setup lang="ts">
|
||
import {
|
||
Button,
|
||
ToastProvider,
|
||
createToastFunction,
|
||
ToastQueue,
|
||
} from "@heroui-vue/components";
|
||
|
||
const queue = new ToastQueue();
|
||
const toast = createToastFunction(queue);
|
||
</script>
|
||
|
||
<template>
|
||
<div>
|
||
<ToastProvider :queue="queue" to="body" />
|
||
<Button @click="toast('简单消息')">显示 Toast</Button>
|
||
</div>
|
||
</template>
|
||
```
|
||
|
||
`toast()` 常用写法:
|
||
|
||
```ts
|
||
// 基础 toast(默认约 4 秒后自动消失)
|
||
toast("活动已创建");
|
||
|
||
// 变体方法(默认同样约 4 秒后自动消失)
|
||
toast.success("文件已保存");
|
||
toast.info("有新更新可用");
|
||
toast.warning("请检查您的设置");
|
||
toast.danger("出了点问题");
|
||
|
||
// 传入 options。利用返回的 id,操作按钮可以关闭它所属的 Toast。
|
||
const eventId = toast("活动已创建", {
|
||
description: "您的活动已安排在明天",
|
||
variant: "default",
|
||
timeout: 5000,
|
||
onClose: () => console.log("已关闭"),
|
||
actionProps: {
|
||
label: "查看",
|
||
onClick: () => toast.close(eventId),
|
||
},
|
||
});
|
||
|
||
// 原地更新已有 Toast(保持其在堆叠中的位置)。
|
||
const id = toast("保存中…", { timeout: 0 });
|
||
toast.update(id, "已保存", { variant: "success", timeout: 4000 });
|
||
|
||
// Promise 支持(自动显示加载指示)。Promise 结束时加载 Toast 会原地更新。
|
||
toast.promise(uploadFile(), {
|
||
loading: "正在上传文件…",
|
||
success: (data) => `文件 ${data.filename} 已上传`,
|
||
error: "上传文件失败",
|
||
});
|
||
|
||
// 手动加载状态(持久 toast:不自动消失)
|
||
const loadingId = toast("正在创建活动…", {
|
||
isLoading: true,
|
||
timeout: 0,
|
||
});
|
||
toast.update(loadingId, "活动已创建", { variant: "success", timeout: 4000 });
|
||
|
||
// 队列方法
|
||
toast.close(key);
|
||
toast.clear();
|
||
toast.pauseAll();
|
||
toast.resumeAll();
|
||
```
|
||
|
||
## 自定义样式
|
||
|
||
### Tailwind CSS
|
||
|
||
<ComponentPreview name="toast-custom-styles" />
|
||
|
||
### 全局 CSS
|
||
|
||
```css
|
||
@layer components {
|
||
.hv-toast {
|
||
@apply rounded-2xl border border-border;
|
||
}
|
||
}
|
||
```
|
||
|
||
## 样式参考
|
||
|
||
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。
|
||
|
||
### CSS 类
|
||
|
||
Toast 使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/ce90ea3ab49d58f6c428f71bfd6fe8ae2a87b960/packages/styles/components/toast.css)):
|
||
|
||
#### 基础类
|
||
|
||
- `.hv-toast` — Toast 根容器
|
||
- `.hv-toast-region` — Toast 区域容器
|
||
- `.hv-toast__content` — 包裹标题与说明的内容容器
|
||
- `.hv-toast__indicator` — 图标/指示器容器
|
||
- `.hv-toast__title` — Toast 标题文本
|
||
- `.hv-toast__description` — Toast 说明文本
|
||
- `.hv-toast__action` — 操作按钮容器
|
||
- `.hv-toast__close-button` — 关闭按钮容器
|
||
|
||
#### 变体类
|
||
|
||
- `.hv-toast--default` — 默认灰色变体
|
||
- `.hv-toast--accent` — 强调蓝色变体
|
||
- `.hv-toast--success` — 成功绿色变体
|
||
- `.hv-toast--warning` — 警告黄/橙色变体
|
||
- `.hv-toast--danger` — 危险红色变体
|
||
|
||
### 交互状态
|
||
|
||
组件支持多种状态:
|
||
|
||
- **最前**:`[data-frontmost]` — 应用于堆叠中最上层可见的 Toast
|
||
- **索引**:`[data-index]` — 基于 Toast 在堆叠中的位置
|
||
- **放置**:`[data-placement="*"]` — 基于 Toast 区域的放置位置
|
||
- **进入中**:`[data-entering]` — Toast 首次绘制时应用
|
||
- **退出中**:`[data-exiting]` — 关闭中的 Toast 播放退出过渡时应用
|
||
- **展开**:`[data-expanded]` — 堆叠展开时应用于每个 Toast
|
||
- **隐藏**:`[data-hidden]` — 应用于超出 `maxVisibleToasts` 的 Toast
|
||
|
||
## API 参考
|
||
|
||
所有部件接受 `class`、`style`、ARIA 和原生 DOM 事件,透传到自身根节点。文本内容支持 Vue `VNodeChild`,也可用具名导出部件与默认插槽组织内容。
|
||
|
||
### ToastProvider
|
||
|
||
| Prop | 类型 | 默认值 | 描述 |
|
||
| ------------------ | --------------------------------------------------------------------------------- | ----------------------- | ------------------------------------------------------- |
|
||
| `placement` | `'top start' \| 'top' \| 'top end' \| 'bottom start' \| 'bottom' \| 'bottom end'` | `'bottom'` | 区域位置 |
|
||
| `gap` | `number` | `12` | 展开间距,px |
|
||
| `isExpanded` | `boolean` | `false` | 固定展开,不单独暂停计时 |
|
||
| `maxVisibleToasts` | `number` | 队列上限,默认 `3` | 只影响可见数量 |
|
||
| `hotkey` | `string[]` | `['altKey','KeyT']` | 修饰键需精确匹配,非修饰键用 event.code;`[]` 禁用 |
|
||
| `scaleFactor` | `number` | `0.05` | 折叠堆叠缩放系数 |
|
||
| `width` | `number \| string` | `460` | 宽度,数字按 px |
|
||
| `queue` | `ToastQueue<ToastContentValue>` | 客户端共享 `toastQueue` | 自定义队列;服务端缺省使用请求局部空队列 |
|
||
| `to` | `string \| HTMLElement` | — | Vue Teleport 目标;文档演示使用 `body` 避免预览容器裁切 |
|
||
| `toastClass` | `string` | — | 自动创建的每条 Toast 根节点的附加类 |
|
||
|
||
| 插槽 / 暴露方法 | 值 | 描述 |
|
||
| --------------- | ------------------------------------------------- | -------------------------------------- |
|
||
| 默认插槽 | `{ toast: ToastContentValue, close: () => void }` | 自定义根内部内容;缺省渲染完整默认布局 |
|
||
| `queue` | 当前队列实例 | 随 queue prop 替换 |
|
||
| `toast()` | `ToastFn` | 返回绑定当前队列的命令式方法 |
|
||
|
||
### Toast / ToastRoot
|
||
|
||
| Prop | 类型 | 默认值 | 描述 |
|
||
| ------------- | ------------------------------------------------------------- | -------------------- | --------------------------------------- |
|
||
| `toastKey` | `string` | 必填 | 队列返回的 key |
|
||
| `scaleFactor` | `number` | 继承 Provider | 可覆盖缩放系数 |
|
||
| `index` | `number` | 必填 | 根在队列中的位置,Provider 自动传入 |
|
||
| `content` | `ToastContentValue` | `{}` | 标题、说明、指示器、动作、变体、loading |
|
||
| `variant` | `'default' \| 'accent' \| 'success' \| 'warning' \| 'danger'` | 内容变体或 `default` | 视觉变体 |
|
||
| `placement` | 与 Provider 相同 | 继承 Provider | 放置位置 |
|
||
| 默认插槽 | `{ toast, close }` | — | 自定义内容 |
|
||
|
||
### ToastContent
|
||
|
||
| Prop / 插槽 | 类型 | 默认值 | 描述 |
|
||
| ----------- | -------- | ------- | ---------- |
|
||
| `as` | `string` | `'div'` | 根标签 |
|
||
| 默认插槽 | 内容 | — | 标题和说明 |
|
||
|
||
### ToastIndicator
|
||
|
||
| Prop / 插槽 | 类型 | 默认值 | 描述 |
|
||
| ----------- | ------------- | ----------- | --------------------------------------------- |
|
||
| `variant` | 与 Toast 相同 | `'default'` | 默认图标变体 |
|
||
| 默认插槽 | 内容 | 变体图标 | 替换默认指示器;命令式 `indicator: null` 隐藏 |
|
||
|
||
### ToastTitle
|
||
|
||
| Prop / 插槽 | 类型 | 默认值 | 描述 |
|
||
| ----------- | -------- | ------- | -------- |
|
||
| `as` | `string` | `'div'` | 根标签 |
|
||
| 默认插槽 | 内容 | — | 通知标题 |
|
||
|
||
### ToastDescription
|
||
|
||
| Prop / 插槽 | 类型 | 默认值 | 描述 |
|
||
| ----------- | -------- | ------- | -------- |
|
||
| `as` | `string` | `'div'` | 根标签 |
|
||
| 默认插槽 | 内容 | — | 通知说明 |
|
||
|
||
### ToastActionButton
|
||
|
||
| Prop / 事件 | 类型 | 默认值 | 描述 |
|
||
| ---------------------------- | ------------ | ----------- | --------------------------------------------------------------- |
|
||
| `variant` | Button 变体 | `'primary'` | 按钮变体(与上游一致,回落到 Button 默认变体) |
|
||
| `size` | Button 尺寸 | `'md'` | 按钮尺寸(与上游一致,回落到 Button 默认尺寸) |
|
||
| `isDisabled` | `boolean` | `false` | 禁用 |
|
||
| `@click` | `MouseEvent` | — | 默认命令式动作先调用用户处理,再关闭;preventDefault 可阻止关闭 |
|
||
| 其余 Button props / 默认插槽 | 见 Button | — | 透传给基础 Button;自定义插槽中的按钮可调用 close |
|
||
|
||
### ToastCloseButton
|
||
|
||
| Prop / 事件 | 类型 | 默认值 | 描述 |
|
||
| ---------------------------- | -------------- | ------- | --------------------------------------------------------- |
|
||
| `isDisabled` | `boolean` | `false` | 禁用关闭 |
|
||
| CloseButton props / 默认插槽 | 见 CloseButton | — | 透传;默认渲染已绑定关闭,自定义插槽使用 `@click="close"` |
|
||
|
||
### ToastQueue
|
||
|
||
| 构造选项 | 类型 | 默认值 | 描述 |
|
||
| ------------------ | -------------------------- | -------- | ------------------------------------ |
|
||
| `exitDuration` | `number` | `300` | 退出保留毫秒数;0 或减少动画立即移除 |
|
||
| `maxVisibleToasts` | `number` | `3` | Provider 缺省可视上限,不删除旧条目 |
|
||
| `wrapUpdate` | `(fn: () => void) => void` | 直接运行 | 包裹通知更新 |
|
||
|
||
| 方法 / 属性 | 类型 | 描述 |
|
||
| ---------------------------------------- | --------------------- | ------------------------------------------------------------------------- |
|
||
| `add(content, options?)` | 返回 `string` key | options 为 `{timeout?, onClose?}` |
|
||
| `update(key, content, options?)` | 返回 `boolean` | 原位更新;省略 timeout 保持剩余计时,0 取消计时;不存在或退出中返回 false |
|
||
| `close(key)` / `clear()` | `void` | 开始退出,onClose 至多一次 |
|
||
| `pauseAll()` / `resumeAll()` | `void` | 外部暂停/恢复,不解除悬停、焦点或页面隐藏暂停 |
|
||
| `visibleToasts` | `QueuedToast<T>[]` | 包含尚未卸载的全部条目,最新在前 |
|
||
| `getExitingKeys()` | `ReadonlySet<string>` | 当前退出 key |
|
||
| `subscribe(fn)` / `subscribeExiting(fn)` | 返回取消订阅函数 | 内容与退出状态订阅 |
|
||
| `destroy()` | `void` | 清理计时器与订阅;实例不再接受新消息 |
|
||
|
||
### toast 函数 / createToastFunction(queue)
|
||
|
||
客户端共享 `toast` 与工厂返回的 `ToastFn` 具有同样方法。`toast(message, options?)`、`success`、`info`、`warning`、`danger`、`update(key,message,options?)`、`promise` 均返回 key;`close`、`clear`、`pauseAll`、`resumeAll` 返回 void。`update` 的目标不存在时创建新通知;Promise 结算只修改仍存活且未被显式替换的原条目。
|
||
|
||
| Option | 类型 | 默认值 | 描述 |
|
||
| --------------------------- | -------------------------------------- | ----------- | ----------------------------------------- |
|
||
| 首个 `message` 参数 | `VNodeChild` | — | 标题 |
|
||
| `description` / `indicator` | `VNodeChild` | — | 说明 / 自定义图标;indicator 为 null 隐藏 |
|
||
| `variant` | Toast 变体 | `'default'` | 外观 |
|
||
| `actionProps` | `ButtonProps & { label?: VNodeChild }` | — | 按钮内容使用 label;事件使用 onClick |
|
||
| `isLoading` | `boolean` | `false` | 用 Spinner 替代默认指示器 |
|
||
| `timeout` | `number` | `4000` | 自动关闭时间;0 持久显示 |
|
||
| `onClose` | `() => void` | — | 关闭开始时回调 |
|
||
|
||
### toast.promise Options
|
||
|
||
| Option | 类型 | 描述 |
|
||
| --------- | ---------------------------------------------- | --------------------------------- |
|
||
| `loading` | `VNodeChild` | 等待消息 |
|
||
| `success` | `VNodeChild \| ((data: T) => VNodeChild)` | 成功消息,保留 Promise 值类型推断 |
|
||
| `error` | `VNodeChild \| ((error: Error) => VNodeChild)` | 失败消息 |
|
||
|
||
## 无障碍
|
||
|
||
对照固定上游 Toast primitive(react-aria `useToast`):
|
||
|
||
- 单条 Toast 为 `role="alertdialog"`、`aria-modal="false"`,并通过 `aria-labelledby` / `aria-describedby` 关联标题与说明
|
||
- 内容层 `ToastContent` 为 `role="alert"`、`aria-atomic="true"`;首次绘制前 `aria-hidden`,挂载后可见以触发读屏播报
|
||
- 区域使用 landmark `role="region"` 与可覆盖的 `aria-label`(默认 `Notifications`)
|
||
- Alt+T 进入有消息的堆叠,Escape 返回之前的触发器;关闭当前条目时聚焦邻近存活条目,最后一条关闭后返回触发器
|
||
- 新通知**不**抢焦点,避免打断用户;隐藏、退出与折叠的非首条通知设置 inert,内部按钮不能进入 Tab 顺序
|
||
- 悬停、焦点进入、页面隐藏暂停倒计时
|
||
|
||
### 上游差异与 SSR
|
||
|
||
React 对象部件改具名导出,ReactNode 改 VNodeChild,onPress 改 DOM click;`actionProps` 的按钮内容字段使用 `label`(上游为 `children`),事件使用 `onClick`(上游为 `onPress`)。Provider 自动创建 Toast 根,作用域插槽负责内部内容,toastClass 定制根样式。采用单一自有队列计时,未运行 React,也未另启 Reka 的第二个倒计时。
|
||
|
||
上游 `toast.getQueue()` 返回 react-stately 队列实例,本库不提供该方法(不暴露底层队列类型)。当前队列实例通过 `ToastProvider` 的 `queue` 插槽/暴露值,或自行创建的 `ToastQueue` 实例访问。
|
||
|
||
服务端全局 toast 为无操作,不保留跨请求消息;SSR 如需通知内容,每请求创建 ToastQueue 并传入 Provider,客户端沿用该请求数据,使用后 destroy。Teleport SSR 目标请依 Vue SSR 规则在客户端装载。
|
||
|
||
## 相关组件
|
||
|
||
[Button 按钮](./button) · [Alert 警告](./alert) · [CloseButton 关闭按钮](./close-button)
|