通知提供器
管理独立通知、过期和关闭。
用法
在产品界面中挂载一次 ToastProvider,实际结果出现后调用 useToast 的 showToast。可见通知已满时,新通知排队等待空位。
无障碍
成功使用礼貌通知,错误使用警报,出现时不移动焦点。悬停或聚焦期间暂停过期,关闭后将焦点恢复到剩余通知或原控件。
| 属性 | 类型 | 默认值 |
|---|---|---|
| maxVisible | number | 4 |
| dismissLabel | string | Dismiss notification |
| showToast(options) | ToastNotification → string | Required title |
| dismissToast(id) | string → void | None |
| duration (notification) | number | null | 3000; important states persist |
const Controls = () => {
const { showToast } = useToast();
return (
<div>
<Button onClick={() => showToast({ title: 'Project saved', description: 'Your changes are ready.', tone: 'success' })}>
Show saved notification
</Button>
<Button variant="secondary" onClick={() => showToast({ title: 'Save failed', description: 'Your changes could not be synchronized. Check your network connection and retry when you are ready.', tone: 'error' })}>
Show error notification
</Button>
</div>
);
};
<ToastProvider maxVisible={2}>
<Controls />
</ToastProvider>