跳至正文
获取 Pro
业务区块B.06

通知中心

FreeRegistry
components/linyi/notification-stack.tsx
lucide-reactmotion
"use client";
 
import * as React from "react";
import { motion, AnimatePresence } from "motion/react";
import { Bell, CheckCheck, Trash2, RotateCcw, X } from "lucide-react";
import { cn } from "@/lib/cn";
import { SPRING_SNAPPY, SPRING_SMOOTH } from "@/lib/motion";
 
export interface NotificationItem {
id: string;
title: string;
description: string;
time: string;
category: string;
read: boolean;
}
 
export interface NotificationStackProps {
initialItems?: NotificationItem[];
compact?: boolean;
className?: string;
}
 
const DEFAULT_NOTIFICATIONS: NotificationItem[] = [
{
id: "n-1",
title: "组件库发布 v2.4 更新",
description: "新增 6 个光学玻璃动效组件与双主题语义令牌。",
time: "2 分钟前",
category: "版本",
read: false,
},
{
id: "n-2",
title: "生产环境部署已通过校验",
description: "全部 46 项组件静态检查与无障碍对比度均达标。",
time: "18 分钟前",
category: "部署",
read: false,
},
{
id: "n-3",
title: "设计令牌同步完成",
description: "浅色宣纸与暖曜石圆角梯阶变量已写入主分支。",
time: "1 小时前",
category: "系统",
read: false,
},
{
id: "n-4",
title: "团队成员加入了协作空间",
description: "已为其开通组件文档与源码查看权限。",
time: "昨天",
category: "成员",
read: true,
},
];
 
export function NotificationStack({
initialItems = DEFAULT_NOTIFICATIONS,
compact = false,
className,
}: NotificationStackProps) {
const uid = React.useId();
const [items, setItems] = React.useState<NotificationItem[]>(initialItems);
const [filter, setFilter] = React.useState<"all" | "unread">("all");
 
const unreadCount = items.filter((item) => !item.read).length;
const visibleItems = filter === "unread" ? items.filter((i) => !i.read) : items;
 
const toggleRead = (id: string) => {
setItems((prev) =>
prev.map((item) => (item.id === id ? { ...item, read: !item.read } : item))
);
};
 
const dismissItem = (id: string) => {
setItems((prev) => prev.filter((item) => item.id !== id));
};
 
const markAllRead = () => {
setItems((prev) => prev.map((item) => ({ ...item, read: true })));
};
 
const clearAll = () => {
setItems([]);
};
 
const restoreSample = () => {
setItems(initialItems);
setFilter("all");
};
 
if (compact) {
const compactList = items.slice(0, 2);
return (
<div className={cn("w-full max-w-[254px] space-y-2", className)}>
<div className="flex items-center justify-between px-0.5 text-[11px]">
<div className="flex items-center gap-1.5">
<Bell className="h-3.5 w-3.5 text-fg-secondary" />
<span className="font-medium text-fg">通知</span>
<span className="rounded-pill bg-accent-soft px-1.5 py-0.5 font-mono-num text-[10px] font-medium text-accent">
{unreadCount}
</span>
</div>
<button
type="button"
onClick={items.length > 0 ? clearAll : restoreSample}
className="cursor-pointer rounded-pill bg-surface-2 px-2 py-0.5 text-[10px] text-fg-secondary transition-colors hover:bg-well hover:text-fg"
>
{items.length > 0 ? "清空" : "恢复示例"}
</button>
</div>
 
<AnimatePresence mode="popLayout" initial={false}>
{compactList.length === 0 ? (
<motion.div
key="compact-empty"
initial={{ opacity: 0, y: 4 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -4 }}
transition={SPRING_SNAPPY}
className="rounded-control border border-line bg-surface px-3 py-4 text-center text-[11px] text-fg-muted"
>
暂无待处理通知
</motion.div>
) : (
compactList.map((item) => (
<motion.button
key={item.id}
type="button"
layout
initial={{ opacity: 0, y: 6 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, scale: 0.96 }}
transition={SPRING_SNAPPY}
onClick={() => toggleRead(item.id)}
className="flex w-full cursor-pointer items-start gap-2 rounded-control border border-line bg-surface px-2.5 py-2 text-left transition-colors hover:border-line-strong"
>
<span
className={cn(
"mt-1.5 h-1.5 w-1.5 flex-shrink-0 rounded-full",
item.read ? "bg-line-strong" : "bg-accent"
)}
/>
<div className="min-w-0 flex-1">
<div
className={cn(
"truncate text-[11px] font-medium",
item.read ? "text-fg-secondary" : "text-fg"
)}
>
{item.title}
</div>
<div className="mt-0.5 font-mono-num text-[10px] text-fg-muted">
{item.time}
</div>
</div>
</motion.button>
))
)}
</AnimatePresence>
</div>
);
}
 
return (
<div
className={cn(
"w-full max-w-lg overflow-hidden rounded-panel border border-line bg-surface shadow-panel",
className
)}
>
{/* Header Bar */}
<div className="flex flex-wrap items-center justify-between gap-2 border-b border-line bg-surface-2 px-4 py-3">
<div className="flex items-center gap-2">
<Bell className="h-4 w-4 text-fg" aria-hidden="true" />
<span className="text-xs font-semibold text-fg">通知中心</span>
<span className="rounded-pill border border-accent-border bg-accent-soft px-2 py-0.5 font-mono-num text-[10px] font-medium text-accent">
未读 {unreadCount}
</span>
</div>
 
<div className="flex items-center gap-1.5">
<button
type="button"
disabled={unreadCount === 0}
onClick={markAllRead}
className="inline-flex cursor-pointer items-center gap-1 rounded-pill border border-line bg-surface px-2.5 py-1 text-[11px] text-fg-secondary transition-colors hover:border-line-strong hover:text-fg disabled:cursor-not-allowed disabled:opacity-45"
>
<CheckCheck className="h-3 w-3" />
<span>全部已读</span>
</button>
<button
type="button"
disabled={items.length === 0}
onClick={clearAll}
className="inline-flex cursor-pointer items-center gap-1 rounded-pill border border-line bg-surface px-2.5 py-1 text-[11px] text-fg-secondary transition-colors hover:border-danger-border hover:bg-danger-soft hover:text-danger disabled:cursor-not-allowed disabled:opacity-45"
>
<Trash2 className="h-3 w-3" />
<span>清空</span>
</button>
</div>
</div>
 
{/* Filter Tabs */}
<div className="flex items-center justify-between border-b border-line px-4 py-2">
<div
className="inline-flex items-center gap-1 rounded-pill border border-line bg-well p-0.5"
role="tablist"
aria-label="通知状态过滤"
>
{(
[
{ id: "all", label: `全部 (${items.length})` },
{ id: "unread", label: `未读 (${unreadCount})` },
] as const
).map((tab) => {
const active = filter === tab.id;
return (
<button
key={tab.id}
type="button"
role="tab"
aria-selected={active}
onClick={() => setFilter(tab.id)}
className={cn(
"relative cursor-pointer rounded-pill px-3 py-1 font-mono-num text-[11px] font-medium transition-colors",
active ? "text-fg" : "text-fg-muted hover:text-fg-secondary"
)}
>
{active && (
<motion.span
layoutId={`notification-filter-pill-${uid}`}
transition={SPRING_SNAPPY}
className="absolute inset-0 rounded-pill border border-line bg-surface shadow-panel"
/>
)}
<span className="relative z-10">{tab.label}</span>
</button>
);
})}
</div>
 
<span className="text-[11px] text-fg-muted">点击条目切换已读状态</span>
</div>
 
{/* Notification List */}
<div className="min-h-[220px] p-3">
<AnimatePresence mode="popLayout" initial={false}>
{visibleItems.length === 0 ? (
<motion.div
key="empty-state"
initial={{ opacity: 0, y: 8 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -6 }}
transition={SPRING_SMOOTH}
className="flex flex-col items-center justify-center gap-2.5 py-12 text-center"
>
<span className="flex h-10 w-10 items-center justify-center rounded-full border border-line bg-surface-2 text-fg-muted">
<Bell className="h-4 w-4" />
</span>
<div className="text-xs font-medium text-fg">
{items.length === 0 ? "通知列表已清空" : "暂无未读通知"}
</div>
<p className="text-[11px] text-fg-muted">
{items.length === 0
? "点击下方按钮可恢复默认示例数据。"
: "所有消息均已标记为已读。"}
</p>
{items.length === 0 ? (
<button
type="button"
onClick={restoreSample}
className="mt-1 inline-flex cursor-pointer items-center gap-1.5 rounded-pill border border-line-strong bg-surface-2 px-3.5 py-1.5 text-xs font-medium text-fg transition-colors hover:bg-well"
>
<RotateCcw className="h-3.5 w-3.5" />
<span>恢复示例</span>
</button>
) : (
<button
type="button"
onClick={() => setFilter("all")}
className="mt-1 cursor-pointer rounded-pill border border-line bg-surface-2 px-3 py-1 text-[11px] text-fg-secondary hover:text-fg"
>
查看全部通知
</button>
)}
</motion.div>
) : (
<div className="space-y-2">
{visibleItems.map((item) => (
<motion.div
key={item.id}
layout
initial={{ opacity: 0, y: 8 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, x: 16 }}
transition={SPRING_SNAPPY}
className={cn(
"group flex items-start gap-2.5 rounded-well border p-3 transition-colors",
item.read
? "border-line bg-surface-2/60"
: "border-line-strong bg-surface shadow-panel"
)}
>
<button
type="button"
onClick={() => toggleRead(item.id)}
className="flex min-w-0 flex-1 cursor-pointer items-start gap-2.5 text-left"
>
<span
className={cn(
"mt-1.5 h-2 w-2 flex-shrink-0 rounded-full transition-colors",
item.read ? "bg-line-strong" : "bg-accent"
)}
aria-hidden="true"
/>
<div className="min-w-0 flex-1">
<div className="flex flex-wrap items-center gap-2">
<span
className={cn(
"text-xs font-medium",
item.read ? "text-fg-secondary" : "text-fg"
)}
>
{item.title}
</span>
<span className="rounded border border-line bg-well px-1.5 py-0.5 text-[10px] text-fg-muted">
{item.category}
</span>
</div>
<p className="mt-1 text-[11px] leading-relaxed text-fg-secondary">
{item.description}
</p>
<div className="mt-1.5 font-mono-num text-[10px] text-fg-muted">
{item.time} · {item.read ? "已读" : "未读"}
</div>
</div>
</button>
 
<button
type="button"
onClick={() => dismissItem(item.id)}
aria-label={`移除通知:${item.title}`}
className="flex h-6 w-6 flex-shrink-0 cursor-pointer items-center justify-center rounded-pill text-fg-muted opacity-70 transition-colors hover:bg-well hover:text-fg hover:opacity-100"
>
<X className="h-3.5 w-3.5" />
</button>
</motion.div>
))}
</div>
)}
</AnimatePresence>
</div>
</div>
);
}