Notification通知
逐张关闭的层叠通知,支持装饰图案、自定义图标、操作按钮与四角弹出。
npx https://kalos-ui-six.vercel.app/cli/kalos-ui-0.3.12.tgz add notification文档已更新
使用指南新增了快捷键说明,支持在线查看与下载。
安装
在项目根目录运行,安装源码。
npx https://kalos-ui-six.vercel.app/cli/kalos-ui-0.3.12.tgz add notification使用
React
"use client";
import { useEffect, useRef } from "react";import { Bell, FileText, Users } from "lucide-react";import { KalosNotification, type KalosNotice } from "@/components/ui/kalos-notification";
// 演示异步处理;实际使用时让 action.onClick 返回业务请求的 Promise。function useDemoAction() { const timers = useRef(new Set<ReturnType<typeof setTimeout>>()); useEffect(() => { const pending = timers.current; return () => { pending.forEach(clearTimeout); pending.clear(); }; }, []);
return () => new Promise<void>(resolve => { const timer = setTimeout(() => { timers.current.delete(timer); resolve(); }, 800); timers.current.add(timer); });}
export default function Example() { const performAction = useDemoAction(); const notices: KalosNotice[] = [ { id: "documents", title: "文档已更新", text: "使用指南新增了快捷键说明,支持在线查看与下载。", icon: <FileText />, pattern: "orbit", action: { label: "查看文档", onClick: performAction }, }, { id: "maintenance", title: "文件存储维护", text: "周日 02:00–03:00 暂停文件上传,已保存的文件不受影响。", icon: <Bell />, pattern: "grid", action: { label: "查看安排", onClick: performAction }, }, { id: "contacts", title: "联系人共享邀请", text: "陈晓邀请你查看共享通讯录中的 12 位联系人。", icon: <Users />, pattern: "rays", action: { label: "接受邀请", onClick: performAction }, }, ];
return <KalosNotification notices={notices} />;}四角弹出
floating 变体通过 Portal 固定在视口内;position 默认右上,底部位置向上层叠。通知不会定时消失,点击关闭或操作成功后移除;新增通知使用唯一 id。
右上右下左上左下
API
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
notices | KalosNotice[] | — | 按数组顺序层叠,最多露出三张;每条通知使用唯一且稳定的 id。关闭过的 id 在本次挂载中保持关闭;重新通知使用新 id |
variant | "stack" | "floating" | stack | stack 在文档流中预留空间;floating 通过 Portal 固定在视口四角,不占用页面布局 |
position | "top-right" | "bottom-right" | "top-left" | "bottom-left" | top-right | 浮层弹出位置,分别为右上、右下、左上、左下;底部向上层叠,关闭时朝所在侧滑出。stack 仅使用其层叠与退出方向 |
onDismiss | (notice: KalosNotice) => void | — | 点击关闭按钮或操作成功后调用一次;可据此移除数据或记录已读。通知没有自动消失计时器 |
notice.id / title | string | — | 唯一标识与必填标题;标题同时作为卡片可访问名称 |
notice.badge / text | string | — | 可选标签与正文;默认预览省略标签,未传入时不生成对应占位 |
notice.icon | ReactNode | — | 标题前的自定义图标,例如 <FileText />;装饰图标不单独播报 |
notice.pattern | "orbit" | "grid" | "rays" | "none" | orbit | 右上角的轨道、网格或射线装饰图案;none 隐藏图案 |
notice.action | { label: string; href?: string; onClick?: () => void | Promise<void>; external?: boolean; disabled?: boolean } | — | Primary 操作按钮;复用 Button 的 loading 状态,异步处理时在原有按钮文字前显示加载图标并禁用重复操作,成功后关闭当前通知,失败时保留并提示重试。同步回调执行后直接关闭;href 保留原生链接跳转,异步业务使用不带 href 的按钮。external 使用新标签页与外链图标;disabled 阻止操作 |
className / style / aria-label | string / CSSProperties / string | — / — / 通知 | 根容器样式与可访问名称;多组通知应提供不同名称 |
键盘操作
- Tab / Shift + Tab
- 仅遍历最前方通知的关闭与操作按钮
- Enter / Space
- 关闭当前通知或执行按钮操作;链接使用 Enter
- 关闭后
- 焦点在卡片内时,移到下一张关闭按钮;全部关闭后移到通知区域