Skyroc UI
弹层覆盖

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,之后即可在任意客户端组件中调用 toastmessagenotification。playground 已在 locale layout 中全局挂载。

Preview
Code
Loading...
<Sonner />

toast.success('Saved', {
  description: 'Your changes have been saved'
});

Message

message 是轻量提示 API,适合成功、失败、警告、信息和加载等单句反馈。它支持字符串调用、配置对象、Promise 状态和手动关闭。

Preview
Code
Loading...
message.success('Operation successful');
message.error('Operation failed');

const id = message.loading('Loading...');
message.dismiss(id);

Notification

notification 用于更丰富的通知内容,支持标题、描述、关闭按钮和操作按钮。

Preview
Code
Loading...
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 配置。

Preview
Code
Loading...
toast.promise(
  fetchData(),
  {
    loading: 'Loading...',
    success: 'Loaded',
    error: 'Failed'
  }
);

全局配置

message.confignotification.config 用于设置默认时长、默认位置和最大展示数量。destroydismiss 的别名,不传 id 时会清理当前 API 维护的全部实例记录。

Preview
Code
Loading...
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 样式。

属性说明类型默认值
positionToast 默认展示位置'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right'-
expand是否展开同位置的 toast 列表booleantrue
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.destroydismiss 的别名(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.destroydismiss 的别名(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'默认展示位置。
expandboolean是否展开 toast 列表。
richColorsboolean是否使用富色彩模式。
theme'light' | 'dark' | 'system'主题。
toastOptionsExternalToast默认 toast 配置。
closeButtonboolean是否显示关闭按钮。
visibleToastsnumber可见 toast 数量。

MessageConfig

单条 message 配置。

字段类型说明
contentReactNode消息内容。
durationnumber自动关闭延迟,单位毫秒。0 表示不自动关闭。
keystring | number唯一 key,同时作为 toast id。
iconReactNode自定义图标。
position'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right'展示位置。
classNamestring自定义 class。
styleobject内联样式。
onClose() => void关闭回调。

MessageGlobalConfig

message 全局配置。

字段类型说明
durationnumber默认自动关闭延迟。
maxCountnumber最大消息数量,超出时关闭最早消息。
position'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right'默认展示位置。

MessagePromiseData

message.promise 的状态文案配置。

字段类型说明
loadingReactNode加载中内容。
successReactNode | ((data) => ReactNode)成功内容。
errorReactNode | ((error) => ReactNode)失败内容。
finally() => void | Promise<void>Promise 结束后的回调。

NotificationConfig

单条 notification 配置。

字段类型说明
titleReactNode通知标题。
descriptionReactNode通知描述。
type'error' | 'info' | 'success' | 'warning'通知类型。
durationnumber自动关闭延迟,单位毫秒。0 表示不自动关闭。
keystring | number唯一 key,同时作为 toast id。
iconReactNode自定义图标。
actionActionConfig | ReactNode主操作按钮。
cancelActionConfig | ReactNode取消按钮。
closeButtonboolean是否显示关闭按钮。
position'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right'展示位置。
classNamestring自定义 class。
styleobject内联样式。
onClose() => void关闭回调。

ActionConfig

notification 操作按钮配置。

字段类型说明
labelReactNode按钮内容。
onClick(event) => void点击回调。
styleobject按钮内联样式。

NotificationGlobalConfig

notification 全局配置。

字段类型说明
durationnumber默认自动关闭延迟。
maxCountnumber最大通知数量,超出时关闭最早通知。
position'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right'默认展示位置。
closeButtonboolean默认是否显示关闭按钮。

MessageType

message 类型

'error' | 'info' | 'loading' | 'success' | 'warning'

NotificationType

notification 类型

'error' | 'info' | 'success' | 'warning'

Action

从 sonner re-export 的操作按钮类型

object

ExternalToast

从 sonner re-export 的 toast 配置类型

object

ToastClassnames

从 sonner re-export 的 toast classNames 配置类型

object

ToastT

从 sonner re-export 的 toast 实例类型

object

ToastToDismiss

从 sonner re-export 的待关闭 toast 类型

object

On this page