跳至正文
获取 Pro
动效组件M.16

头像叠组

FreeRegistry
components/linyi/avatar-group.tsx
motion
"use client";
 
import * as React from "react";
import { AnimatePresence, motion } from "motion/react";
import { cn } from "@/lib/cn";
import { SPRING_BOUNCY, SPRING_SNAPPY } from "@/lib/motion";
 
export interface AvatarItem {
initials: string;
name: string;
role?: string;
accent?: boolean;
}
 
export interface AvatarGroupProps {
items: AvatarItem[];
max?: number;
size?: "sm" | "md";
onSelect?: (item: AvatarItem, index: number) => void;
className?: string;
}
 
export function AvatarGroup({
items,
max,
size = "md",
onSelect,
className,
}: AvatarGroupProps) {
const [activeKey, setActiveKey] = React.useState<string | null>(null);
 
const visibleItems = max && max > 0 ? items.slice(0, max) : items;
const overflowItems = max && max > 0 && items.length > max ? items.slice(max) : [];
const isSpread = activeKey !== null;
 
const sizeClass = size === "sm" ? "h-8 w-8 text-[10px]" : "h-10 w-10 text-[11px]";
const collapsedMargin = size === "sm" ? -8 : -10;
const expandedMargin = size === "sm" ? 4 : 6;
const tooltipY = size === "sm" ? -24 : -28;
 
return (
<div
className={cn("inline-flex items-center pt-5", className)}
onMouseLeave={() => setActiveKey(null)}
onBlur={(e) => {
if (!e.currentTarget.contains(e.relatedTarget as Node | null)) {
setActiveKey(null);
}
}}
>
{visibleItems.map((item, idx) => {
const key = `${item.initials}-${item.name}-${idx}`;
const isActive = activeKey === key;
 
return (
<motion.button
key={key}
type="button"
aria-label={item.role ? `${item.name},${item.role}` : item.name}
onMouseEnter={() => setActiveKey(key)}
onFocus={() => setActiveKey(key)}
onClick={() => {
setActiveKey(isActive ? null : key);
onSelect?.(item, idx);
}}
animate={{
marginLeft: idx === 0 ? 0 : isSpread ? expandedMargin : collapsedMargin,
y: isActive ? -5 : 0,
scale: isActive ? 1.08 : 1,
}}
transition={SPRING_BOUNCY}
className={cn(
"relative flex items-center justify-center rounded-pill border-2 border-well font-mono-num font-medium cursor-pointer select-none",
sizeClass,
item.accent
? "bg-accent text-white"
: "bg-surface text-fg"
)}
style={{ zIndex: isActive ? 20 : visibleItems.length - idx + 2 }}
>
<span>{item.initials}</span>
 
<AnimatePresence>
{isActive && (
<motion.span
initial={{ opacity: 0, y: 4, scale: 0.92 }}
animate={{ opacity: 1, y: tooltipY, scale: 1 }}
exit={{ opacity: 0, y: 2, scale: 0.92 }}
transition={SPRING_SNAPPY}
role="tooltip"
className="pointer-events-none absolute left-1/2 -translate-x-1/2 whitespace-nowrap rounded-pill bg-inverse px-2.5 py-0.5 text-[10px] font-sans font-medium text-fg-inverse shadow-popover"
>
{item.name}
{item.role ? (
<span className="ml-1 opacity-70">· {item.role}</span>
) : null}
</motion.span>
)}
</AnimatePresence>
</motion.button>
);
})}
 
{overflowItems.length > 0 && (
<motion.button
type="button"
aria-label={`其余 ${overflowItems.length} 位成员:${overflowItems.map((m) => m.name).join("、")}`}
onMouseEnter={() => setActiveKey("overflow")}
onFocus={() => setActiveKey("overflow")}
onClick={() =>
setActiveKey((prev) => (prev === "overflow" ? null : "overflow"))
}
animate={{
marginLeft: isSpread ? expandedMargin : collapsedMargin,
y: activeKey === "overflow" ? -4 : 0,
scale: activeKey === "overflow" ? 1.06 : 1,
}}
transition={SPRING_BOUNCY}
className={cn(
"relative flex items-center justify-center rounded-pill border-2 border-well bg-surface-2 font-mono-num font-medium text-fg-secondary cursor-pointer select-none",
sizeClass
)}
style={{ zIndex: activeKey === "overflow" ? 20 : 1 }}
>
<span>+{overflowItems.length}</span>
 
<AnimatePresence>
{activeKey === "overflow" && (
<motion.span
initial={{ opacity: 0, y: 4, scale: 0.92 }}
animate={{ opacity: 1, y: tooltipY, scale: 1 }}
exit={{ opacity: 0, y: 2, scale: 0.92 }}
transition={SPRING_SNAPPY}
role="tooltip"
className="pointer-events-none absolute left-1/2 -translate-x-1/2 whitespace-nowrap rounded-pill bg-inverse px-2.5 py-0.5 text-[10px] font-sans font-medium text-fg-inverse shadow-popover"
>
{overflowItems.map((m) => m.name).join(" · ")}
</motion.span>
)}
</AnimatePresence>
</motion.button>
)}
</div>
);
}