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

3D 书架

FreeRegistry
components/linyi/bookshelf.tsx
motion
"use client";
 
import * as React from "react";
import { AnimatePresence, motion } from "motion/react";
import { cn } from "@/lib/cn";
import { EASE_OUT, SPRING_SMOOTH } from "@/lib/motion";
import { playSound } from "@/lib/sound";
 
export interface BookshelfItem {
id: string;
spine: string;
title: string;
subtitle?: string;
year?: string;
pages?: number;
color: string;
fg: string;
}
 
export interface BookshelfProps {
books: BookshelfItem[];
activeIndex?: number;
defaultActiveIndex?: number;
onActiveIndexChange?: (index: number) => void;
compact?: boolean;
showDetails?: boolean;
ariaLabel?: string;
className?: string;
}
 
export function Bookshelf({
books,
activeIndex,
defaultActiveIndex = 0,
onActiveIndexChange,
compact = false,
showDetails = !compact,
ariaLabel = "3D 书架",
className,
}: BookshelfProps) {
const safeDefault = books.length > 0 ? Math.min(Math.max(defaultActiveIndex, 0), books.length - 1) : 0;
const [internalIndex, setInternalIndex] = React.useState(safeDefault);
const buttonRefs = React.useRef<(HTMLButtonElement | null)[]>([]);
 
const isControlled = activeIndex !== undefined;
const currentIndex = books.length > 0
? Math.min(Math.max(isControlled ? activeIndex : internalIndex, 0), books.length - 1)
: 0;
 
const selectIndex = (next: number, moveDomFocus = false) => {
if (!books.length) return;
const clamped = ((next % books.length) + books.length) % books.length;
if (clamped !== currentIndex) {
playSound("tap", { pitch: 0.92 + clamped * 0.05, volume: 0.45 });
}
if (!isControlled) {
setInternalIndex(clamped);
}
onActiveIndexChange?.(clamped);
if (moveDomFocus) {
buttonRefs.current[clamped]?.focus();
}
};
 
const handleKeyDown = (e: React.KeyboardEvent<HTMLButtonElement>, idx: number) => {
if (!books.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(books.length - 1, true);
}
};
 
const currentBook = books[currentIndex];
const expandedWidth = compact ? 74 : 96;
const collapsedWidth = compact ? 26 : 34;
 
return (
<div className={cn("flex flex-col items-center gap-4", className)}>
<div
role="tablist"
aria-label={ariaLabel}
className={cn(
"flex items-end justify-center gap-1.5 px-2",
compact ? "h-36" : "h-44"
)}
>
{books.map((book, idx) => {
const pulled = idx === currentIndex;
return (
<motion.button
key={book.id}
ref={(el) => {
buttonRefs.current[idx] = el;
}}
type="button"
role="tab"
aria-selected={pulled}
tabIndex={pulled ? 0 : -1}
onClick={() => selectIndex(idx)}
onKeyDown={(e) => handleKeyDown(e, idx)}
whileHover={{ y: pulled ? -10 : -4 }}
whileTap={{ scale: 0.97 }}
animate={{
width: pulled ? expandedWidth : collapsedWidth,
y: pulled ? -10 : 0,
rotate: pulled ? -1.5 : 0,
}}
transition={SPRING_SMOOTH}
className={cn(
"rounded-[6px] flex flex-col items-center justify-center cursor-pointer overflow-hidden relative px-1.5 select-none outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-2 focus-visible:ring-offset-canvas",
compact ? "h-28" : "h-36"
)}
style={{
background: book.color,
color: book.fg,
border: "1px solid rgba(255,255,255,0.16)",
boxShadow: pulled
? "0 14px 26px -10px rgba(0,0,0,0.42), inset 0 1px 0 rgba(255,255,255,0.28)"
: "inset 0 1px 0 rgba(255,255,255,0.14)",
}}
>
<AnimatePresence mode="wait" initial={false}>
{pulled ? (
<motion.div
key="open"
initial={{ opacity: 0, scale: 0.92 }}
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: 0.92 }}
transition={EASE_OUT}
className="flex flex-col items-center justify-between h-full py-2.5 text-center"
>
<span className="font-mono-num text-[9px] tracking-widest opacity-70">
{book.spine}
</span>
<span className={cn("font-semibold leading-tight", compact ? "text-[11px]" : "text-xs")}>
{book.title}
</span>
<span className="w-4 h-[1.5px] rounded-pill bg-current opacity-45" />
</motion.div>
) : (
<motion.span
key="spine"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: 0.14 }}
className="font-mono-num text-[9px] tracking-widest font-medium"
style={{ writingMode: "vertical-rl" }}
>
{book.spine}
</motion.span>
)}
</AnimatePresence>
</motion.button>
);
})}
</div>
 
{showDetails && currentBook && (
<div className="w-full max-w-xs rounded-well border border-line bg-surface px-3.5 py-2.5 min-h-[60px] flex items-center justify-between gap-3">
<AnimatePresence mode="wait" initial={false}>
<motion.div
key={currentBook.id}
initial={{ opacity: 0, y: 4 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -4 }}
transition={EASE_OUT}
className="min-w-0 flex-1"
>
<div className="text-xs font-medium text-fg truncate">{currentBook.title}</div>
{currentBook.subtitle && (
<div className="text-[11px] text-fg-secondary truncate mt-0.5">
{currentBook.subtitle}
</div>
)}
</motion.div>
</AnimatePresence>
<div className="font-mono-num text-[11px] text-fg-muted text-right flex-shrink-0">
<div>{String(currentIndex + 1).padStart(2, "0")} / {String(books.length).padStart(2, "0")}</div>
{currentBook.pages !== undefined && <div>{currentBook.pages}p</div>}
</div>
</div>
)}
</div>
);
}