components/linyi/sound-pad.tsx
lucide-reactmotion
"use client"; import * as React from "react";import { AnimatePresence, motion } from "motion/react";import { Bell, Droplets, Keyboard, Sparkles } from "lucide-react";import { cn } from "@/lib/cn";import { SPRING_KEYCAP } from "@/lib/motion";import { playSound, type SoundEffectName } from "@/lib/sound"; export interface SoundPadItem { id: string; label: string; en: string; sound: SoundEffectName; pitch?: number; icon: "bell" | "bubble" | "wood" | "type";} export interface SoundPadProps { items?: SoundPadItem[]; pitch?: number; compact?: boolean; className?: string;} const DEFAULT_PADS: SoundPadItem[] = [ { id: "bell", label: "风铃", en: "Bell", sound: "bell", pitch: 1, icon: "bell" }, { id: "bubble", label: "水泡", en: "Bubble", sound: "bubble", pitch: 1, icon: "bubble" }, { id: "wood", label: "木磬", en: "Wood", sound: "wood", pitch: 1, icon: "wood" }, { id: "type", label: "机械键", en: "Key", sound: "type", pitch: 1.02, icon: "type" },]; function PadIcon({ kind }: { kind: SoundPadItem["icon"] }) { if (kind === "bell") return <Bell className="h-4 w-4" />; if (kind === "bubble") return <Droplets className="h-4 w-4" />; if (kind === "wood") return <Sparkles className="h-4 w-4" />; return <Keyboard className="h-4 w-4" />;} export function SoundPad({ items = DEFAULT_PADS, pitch = 1, compact = false, className,}: SoundPadProps) { const [pulseMap, setPulseMap] = React.useState<Record<string, number>>({}); const [pressedId, setPressedId] = React.useState<string | null>(null); // 提示复位定时器:重复触发时先清除旧定时器,卸载时统一清理,避免提前复位与卸载后更新状态 const resetTimerRef = React.useRef<ReturnType<typeof setTimeout> | null>(null); React.useEffect( () => () => { if (resetTimerRef.current) clearTimeout(resetTimerRef.current); }, [] ); const trigger = (pad: SoundPadItem) => { playSound(pad.sound, { pitch: (pad.pitch ?? 1) * pitch }); setPressedId(pad.id); setPulseMap((prev) => ({ ...prev, [pad.id]: (prev[pad.id] ?? 0) + 1 })); if (resetTimerRef.current) clearTimeout(resetTimerRef.current); resetTimerRef.current = setTimeout(() => { setPressedId((cur) => (cur === pad.id ? null : cur)); }, 120); }; return ( <div className={cn( "grid grid-cols-2 gap-2.5", compact ? "w-full max-w-[232px]" : "w-full max-w-[300px]", className )} > {items.map((pad) => { const pulse = pulseMap[pad.id] ?? 0; const pressed = pressedId === pad.id; return ( <div key={pad.id} className="relative rounded-[14px] p-1" style={{ background: "var(--bg-well)", border: "1px solid var(--line-subtle)", }} > <motion.button type="button" onPointerDown={() => trigger(pad)} onKeyDown={(e) => { if (e.key === "Enter" || e.key === " ") { e.preventDefault(); trigger(pad); } }} animate={{ y: pressed ? 2.5 : 0, scale: pressed ? 0.975 : 1 }} transition={SPRING_KEYCAP} className={cn( "relative flex w-full cursor-pointer flex-col items-center justify-center gap-1.5 overflow-hidden rounded-[10px] select-none outline-none focus-visible:ring-2 focus-visible:ring-accent", compact ? "h-[68px] px-2" : "h-[86px] px-3" )} style={{ background: "var(--bg-surface)", color: pressed ? "var(--accent)" : "var(--fg-primary)", border: pressed ? "1px solid var(--accent-border)" : "1px solid var(--line-strong)", boxShadow: pressed ? "inset 0 1px 2px rgba(0,0,0,0.10)" : "0 2px 0 var(--line-subtle), var(--elevation-panel)", }} > <AnimatePresence mode="popLayout"> {pulse > 0 && ( <motion.span key={pulse} initial={{ scale: 0.45, opacity: 0.45 }} animate={{ scale: 2.1, opacity: 0 }} exit={{ opacity: 0 }} transition={{ duration: 0.42, ease: [0.16, 1, 0.3, 1] }} aria-hidden="true" className="pointer-events-none absolute h-10 w-10 rounded-full" style={{ border: "1.5px solid var(--accent)" }} /> )} </AnimatePresence> <span className="relative z-10 opacity-85"> <PadIcon kind={pad.icon} /> </span> <span className="relative z-10 flex items-center gap-1 text-[11px] font-medium leading-none"> <span>{pad.label}</span> {!compact && ( <span className="font-mono-num text-[10px] opacity-50">{pad.en}</span> )} </span> </motion.button> </div> ); })} </div> );}