Sonner
用于展示全局消息、通知和 Toast 反馈
Sonner 基于 sonner 封装,用于展示全局轻提示和通知反馈。组件库导出三层能力:Sonner 作为全局 Toaster 容器,message 用于轻量单句反馈,notification 用于带标题、描述和操作按钮的通知,toast 则保留 sonner 原始 API。
import {
Message,
Notification,
Sonner,
message,
notification,
toast
} from '@skyroc/web-ui';何时使用
- 操作成功、失败、警告或加载状态需要即时反馈。
- 需要不打断用户流程的全局提示。
- 简短单句反馈使用
message;带标题、说明或按钮的反馈使用notification。 - 需要完全使用 sonner 原始能力时直接使用
toast。
全局容器
在应用根部挂载一次 Sonner,之后即可在任意客户端组件中调用 toast、message 或 notification。playground 已在 locale layout 中全局挂载。
<Sonner />
toast.success('Saved', {
description: 'Your changes have been saved'
});Message
message 是轻量提示 API,适合成功、失败、警告、信息和加载等单句反馈。它支持字符串调用、配置对象、Promise 状态和手动关闭。
message.success('Operation successful');
message.error('Operation failed');
const id = message.loading('Loading...');
message.dismiss(id);Notification
notification 用于更丰富的通知内容,支持标题、描述、关闭按钮和操作按钮。
notification.info({
title: 'New Message',
description: 'You have a new message',
action: {
label: 'View',
onClick: () => {}
}
});原生 Toast
toast 直接从 sonner re-export,适合需要使用 sonner 原始 API 的场景,例如 toast.promise、自定义 action、description 或更细粒度的 toast 配置。
toast.promise(
fetchData(),
{
loading: 'Loading...',
success: 'Loaded',
error: 'Failed'
}
);全局配置
message.config 和 notification.config 用于设置默认时长、默认位置和最大展示数量。destroy 是 dismiss 的别名,不传 id 时会清理当前 API 维护的全部实例记录。
message.config({
duration: 1500,
maxCount: 2,
position: 'top-center'
});
notification.config({
closeButton: true,
duration: 0,
maxCount: 2,
position: 'top-right'
});API
Sonner
全局 Toaster 容器。基于 sonner 的 Toaster,默认读取 next-themes 当前主题,并合并项目内置 toast 样式。
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| position | Toast 默认展示位置 | 'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right' | - |
| expand | 是否展开同位置的 toast 列表 | boolean | true |
| richColors | 是否使用 sonner 的富色彩模式 | boolean | - |
| theme | 主题。组件默认跟随 next-themes | 'light' | 'dark' | 'system' | - |
| toastOptions | 传递给 sonner 的默认 toast 配置 | ExternalToast | - |
| closeButton | 是否显示关闭按钮 | boolean | - |
| visibleToasts | 同一时间可见 toast 数量 | number | - |
message
轻量全局消息 API。
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| message.success | 展示成功消息 | (content: ReactNode | MessageConfig, duration?: number, onClose?: () => void) => string | number | - |
| message.error | 展示错误消息 | (content: ReactNode | MessageConfig, duration?: number, onClose?: () => void) => string | number | - |
| message.warning | 展示警告消息 | (content: ReactNode | MessageConfig, duration?: number, onClose?: () => void) => string | number | - |
| message.info | 展示信息消息 | (content: ReactNode | MessageConfig, duration?: number, onClose?: () => void) => string | number | - |
| message.loading | 展示加载消息 | (content: ReactNode | MessageConfig, duration?: number, onClose?: () => void) => string | number | - |
| message.promise | 展示 Promise 加载、成功、失败状态 | (promise, data: MessagePromiseData, options?: Omit<MessageConfig, "content">) => string | number | - |
| message.open | 通过配置对象打开消息 | (config: MessageConfig & { type?: MessageType }) => string | number | - |
| message.dismiss | 关闭指定消息;不传 id 时关闭全部消息 | (id?: string | number) => void | - |
| message.destroy | dismiss 的别名 | (id?: string | number) => void | - |
| message.config | 设置 message 全局配置 | (options: MessageGlobalConfig) => void | - |
notification
富通知 API。
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| notification.success | 展示成功通知 | (config: NotificationConfig) => string | number | - |
| notification.error | 展示错误通知 | (config: NotificationConfig) => string | number | - |
| notification.warning | 展示警告通知 | (config: NotificationConfig) => string | number | - |
| notification.info | 展示信息通知 | (config: NotificationConfig) => string | number | - |
| notification.open | 通过配置对象打开通知 | (config: NotificationConfig & { type?: NotificationType }) => string | number | - |
| notification.dismiss | 关闭指定通知;不传 id 时关闭全部通知 | (id?: string | number) => void | - |
| notification.destroy | dismiss 的别名 | (id?: string | number) => void | - |
| notification.config | 设置 notification 全局配置 | (options: NotificationGlobalConfig) => void | - |
类型
ToasterProps
Sonner 容器属性,来自 sonner ToasterProps,并由组件封装默认主题和样式。
| 字段 | 类型 | 说明 |
|---|---|---|
| position | 'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right' | 默认展示位置。 |
| expand | boolean | 是否展开 toast 列表。 |
| richColors | boolean | 是否使用富色彩模式。 |
| theme | 'light' | 'dark' | 'system' | 主题。 |
| toastOptions | ExternalToast | 默认 toast 配置。 |
| closeButton | boolean | 是否显示关闭按钮。 |
| visibleToasts | number | 可见 toast 数量。 |
MessageConfig
单条 message 配置。
| 字段 | 类型 | 说明 |
|---|---|---|
| content | ReactNode | 消息内容。 |
| duration | number | 自动关闭延迟,单位毫秒。0 表示不自动关闭。 |
| key | string | number | 唯一 key,同时作为 toast id。 |
| icon | ReactNode | 自定义图标。 |
| position | 'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right' | 展示位置。 |
| className | string | 自定义 class。 |
| style | object | 内联样式。 |
| onClose | () => void | 关闭回调。 |
MessageGlobalConfig
message 全局配置。
| 字段 | 类型 | 说明 |
|---|---|---|
| duration | number | 默认自动关闭延迟。 |
| maxCount | number | 最大消息数量,超出时关闭最早消息。 |
| position | 'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right' | 默认展示位置。 |
MessagePromiseData
message.promise 的状态文案配置。
| 字段 | 类型 | 说明 |
|---|---|---|
| loading | ReactNode | 加载中内容。 |
| success | ReactNode | ((data) => ReactNode) | 成功内容。 |
| error | ReactNode | ((error) => ReactNode) | 失败内容。 |
| finally | () => void | Promise<void> | Promise 结束后的回调。 |
NotificationConfig
单条 notification 配置。
| 字段 | 类型 | 说明 |
|---|---|---|
| title | ReactNode | 通知标题。 |
| description | ReactNode | 通知描述。 |
| type | 'error' | 'info' | 'success' | 'warning' | 通知类型。 |
| duration | number | 自动关闭延迟,单位毫秒。0 表示不自动关闭。 |
| key | string | number | 唯一 key,同时作为 toast id。 |
| icon | ReactNode | 自定义图标。 |
| action | ActionConfig | ReactNode | 主操作按钮。 |
| cancel | ActionConfig | ReactNode | 取消按钮。 |
| closeButton | boolean | 是否显示关闭按钮。 |
| position | 'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right' | 展示位置。 |
| className | string | 自定义 class。 |
| style | object | 内联样式。 |
| onClose | () => void | 关闭回调。 |
ActionConfig
notification 操作按钮配置。
| 字段 | 类型 | 说明 |
|---|---|---|
| label | ReactNode | 按钮内容。 |
| onClick | (event) => void | 点击回调。 |
| style | object | 按钮内联样式。 |
NotificationGlobalConfig
notification 全局配置。
| 字段 | 类型 | 说明 |
|---|---|---|
| duration | number | 默认自动关闭延迟。 |
| maxCount | number | 最大通知数量,超出时关闭最早通知。 |
| position | 'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right' | 默认展示位置。 |
| closeButton | boolean | 默认是否显示关闭按钮。 |
MessageType
message 类型
NotificationType
notification 类型
Action
从 sonner re-export 的操作按钮类型
ExternalToast
从 sonner re-export 的 toast 配置类型
ToastClassnames
从 sonner re-export 的 toast classNames 配置类型
ToastT
从 sonner re-export 的 toast 实例类型
ToastToDismiss
从 sonner re-export 的待关闭 toast 类型