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

版本时间轴

FreeRegistry
components/linyi/timeline.tsx
motion
"use client";
 
import * as React from "react";
import { AnimatePresence, motion } from "motion/react";
import { cn } from "@/lib/cn";
import { EASE_OUT, SPRING_PROGRESS, SPRING_SNAPPY } from "@/lib/motion";
 
export interface TimelineItem {
version: string;
title: string;
date?: string;
summary?: string;
}
 
export interface TimelineProps {
items: TimelineItem[];
activeIndex?: number;
defaultActiveIndex?: number;
onActiveIndexChange?: (index: number) => void;
compact?: boolean;
ariaLabel?: string;
className?: string;
}
 
export function Timeline({
items,
activeIndex,
defaultActiveIndex = 0,
onActiveIndexChange,
compact = false,
ariaLabel = "版本时间轴",
className,
}: TimelineProps) {
const safeDefault =
items.length > 0 ? Math.min(Math.max(defaultActiveIndex, 0), items.length - 1) : 0;
const [internalIndex, setInternalIndex] = React.useState(safeDefault);
const nodeRefs = React.useRef<(HTMLButtonElement | null)[]>([]);
const uid = React.useId();
 
const isControlled = activeIndex !== undefined;
const currentIndex =
items.length > 0
? Math.min(Math.max(isControlled ? activeIndex : internalIndex, 0), items.length - 1)
: 0;
 
const progress = items.length > 1 ? (currentIndex / (items.length - 1)) * 100 : 100;
const currentItem = items[currentIndex];
 
const selectIndex = (next: number, moveFocus = false) => {
if (!items.length) return;
const clamped = ((next % items.length) + items.length) % items.length;
if (!isControlled) {
setInternalIndex(clamped);
}
onActiveIndexChange?.(clamped);
if (moveFocus) {
nodeRefs.current[clamped]?.focus();
}
};
 
const handleKeyDown = (e: React.KeyboardEvent<HTMLButtonElement>, idx: number) => {
if (!items.length) return;
if (e.key === "ArrowRight") {
e.preventDefault();
selectIndex(idx + 1, true);
} else if (e.key === "ArrowLeft") {
e.preventDefault();
selectIndex(idx - 1, true);
} else if (e.key === "Home") {
e.preventDefault();
selectIndex(0, true);
} else if (e.key === "End") {
e.preventDefault();
selectIndex(items.length - 1, true);
}
};
 
return (
<div className={cn("w-full space-y-4", className)}>
<div className="relative px-1">
<div
role="progressbar"
aria-valuemin={0}
aria-valuemax={100}
aria-valuenow={Math.round(progress)}
aria-label={`${ariaLabel}进度`}
className="absolute left-5 right-5 top-1/2 -translate-y-1/2 h-[2px] rounded-pill bg-line overflow-hidden"
>
<motion.div
initial={false}
animate={{ scaleX: progress / 100 }}
transition={SPRING_PROGRESS}
className="h-full w-full origin-left rounded-pill bg-accent"
/>
</div>
 
<div role="tablist" aria-label={ariaLabel} className="relative flex items-center justify-between">
{items.map((item, i) => {
const active = i === currentIndex;
const reached = i <= currentIndex;
return (
<button
key={`${item.version}-${i}`}
ref={(el) => {
nodeRefs.current[i] = el;
}}
type="button"
role="tab"
aria-selected={active}
tabIndex={active ? 0 : -1}
onClick={() => selectIndex(i)}
onKeyDown={(e) => handleKeyDown(e, i)}
className={cn(
"relative z-10 w-9 h-9 rounded-pill font-mono-num text-[11px] flex items-center justify-center cursor-pointer transition-colors outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-2 focus-visible:ring-offset-canvas",
active
? "bg-accent text-white border border-transparent"
: reached
? "bg-surface text-fg border border-line-strong"
: "bg-surface text-fg-muted border border-line-strong"
)}
>
{active && (
<motion.span
layoutId={`tl-ring-${uid}`}
transition={SPRING_SNAPPY}
className="absolute -inset-1 rounded-pill pointer-events-none border-[1.5px] border-accent-border"
/>
)}
<span>{item.version}</span>
</button>
);
})}
</div>
</div>
 
{currentItem &&
(compact ? (
<div className="h-5 flex items-center justify-center overflow-hidden">
<AnimatePresence mode="wait" initial={false}>
<motion.div
key={currentIndex}
initial={{ opacity: 0, y: 6 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -6 }}
transition={EASE_OUT}
className="text-xs font-medium text-fg-secondary truncate"
>
v{currentItem.version} · {currentItem.title}
</motion.div>
</AnimatePresence>
</div>
) : (
<div className="rounded-well border border-line bg-surface px-4 py-3 min-h-[76px] overflow-hidden">
<AnimatePresence mode="wait" initial={false}>
<motion.div
key={currentIndex}
initial={{ opacity: 0, y: 6 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -6 }}
transition={EASE_OUT}
className="space-y-1"
>
<div className="flex items-center justify-between gap-2">
<div className="flex items-center gap-2 min-w-0">
<span className="font-mono-num text-[10px] px-1.5 py-0.5 rounded-badge bg-accent-soft text-accent border border-accent-border">
v{currentItem.version}
</span>
<span className="text-xs font-semibold text-fg truncate">
{currentItem.title}
</span>
</div>
{currentItem.date && (
<span className="font-mono-num text-[10px] text-fg-muted flex-shrink-0">
{currentItem.date}
</span>
)}
</div>
{currentItem.summary && (
<p className="text-xs text-fg-secondary leading-relaxed">
{currentItem.summary}
</p>
)}
</motion.div>
</AnimatePresence>
</div>
))}
</div>
);
}