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

状态徽章

FreeRegistry
components/linyi/animated-badge.tsx
motion
"use client";
 
import * as React from "react";
import { motion } from "motion/react";
import { cn } from "@/lib/cn";
import { SPRING_SNAPPY } from "@/lib/motion";
 
export type AnimatedBadgeStatus = "live" | "ready" | "warning" | "paused";
 
const STATUS_STYLES: Record<
AnimatedBadgeStatus,
{ dot: string; ring: string; border: string; meta: string }
> = {
live: {
dot: "bg-accent",
ring: "bg-accent",
border: "border-line-strong",
meta: "text-fg-secondary",
},
ready: {
dot: "bg-success",
ring: "bg-success",
border: "border-line-strong",
meta: "text-success",
},
warning: {
dot: "bg-warning",
ring: "bg-warning",
border: "border-warning-border",
meta: "text-warning",
},
paused: {
dot: "bg-fg-muted",
ring: "bg-fg-muted",
border: "border-line",
meta: "text-fg-muted",
},
};
 
export interface AnimatedBadgeProps {
status?: AnimatedBadgeStatus;
pulse?: boolean;
label: string;
meta?: string;
onClick?: () => void;
className?: string;
}
 
export function AnimatedBadge({
status = "live",
pulse,
label,
meta,
onClick,
className,
}: AnimatedBadgeProps) {
const tone = STATUS_STYLES[status];
const shouldPulse = pulse ?? status !== "paused";
 
const content = (
<>
<span className="relative flex h-2 w-2 flex-shrink-0" aria-hidden="true">
{shouldPulse && (
<span
className={cn(
"animate-ping absolute inline-flex h-full w-full rounded-full opacity-70",
tone.ring
)}
/>
)}
<span
className={cn("relative inline-flex h-2 w-2 rounded-full", tone.dot)}
/>
</span>
<span className="font-medium text-fg">{label}</span>
{meta ? (
<>
<span className="text-fg-muted/60" aria-hidden="true">
·
</span>
<span className={cn("font-mono-num", tone.meta)}>{meta}</span>
</>
) : null}
</>
);
 
const sharedClass = cn(
"inline-flex min-h-[34px] items-center gap-2 rounded-pill border bg-surface px-3.5 py-1.5 text-xs shadow-panel transition-colors",
tone.border,
className
);
 
if (onClick) {
return (
<motion.button
type="button"
whileTap={{ scale: 0.96 }}
transition={SPRING_SNAPPY}
onClick={onClick}
className={cn(
sharedClass,
"cursor-pointer hover:border-line-strong hover:bg-surface-2"
)}
>
{content}
</motion.button>
);
}
 
return <span className={sharedClass}>{content}</span>;
}