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

音效下载按钮

FreeRegistry
components/linyi/download-button.tsx
lucide-reactmotion
"use client";
 
import * as React from "react";
import {
AnimatePresence,
motion,
useMotionValue,
useSpring,
useTransform,
} from "motion/react";
import { ArrowDownToLine, Check, Loader2 } from "lucide-react";
import { cn } from "@/lib/cn";
import { SPRING_PROGRESS, SPRING_SNAPPY } from "@/lib/motion";
import { playSound } from "@/lib/sound";
 
export type DownloadState = "idle" | "downloading" | "done";
 
export interface DownloadButtonProps {
label?: string;
downloadingLabel?: string;
doneLabel?: string;
durationMs?: number;
sound?: boolean;
onComplete?: () => void;
className?: string;
}
 
export function DownloadButton({
label = "下载组件包",
downloadingLabel = "下载中",
doneLabel = "已完成",
durationMs = 2000,
sound = true,
onComplete,
className,
}: DownloadButtonProps) {
const [state, setState] = React.useState<DownloadState>("idle");
const progress = useMotionValue(0);
const smoothProgress = useSpring(progress, SPRING_PROGRESS);
const clipPath = useTransform(
smoothProgress,
(v) => `inset(0 ${Math.max(0, (1 - v) * 100).toFixed(2)}% 0 0)`
);
// 百分比读数直接由 MotionValue 写入文本节点,避免逐帧 setState 重渲染整个按钮
const percentText = useTransform(progress, (v) => `${Math.round(v * 100)}%`);
 
const rafRef = React.useRef<number | null>(null);
const resetRef = React.useRef<ReturnType<typeof setTimeout> | null>(null);
 
React.useEffect(() => {
return () => {
if (rafRef.current) cancelAnimationFrame(rafRef.current);
if (resetRef.current) clearTimeout(resetRef.current);
};
}, []);
 
const handleClick = () => {
if (state === "downloading") return;
if (resetRef.current) clearTimeout(resetRef.current);
if (rafRef.current) cancelAnimationFrame(rafRef.current);
 
if (sound) playSound("downloadStart");
 
setState("downloading");
progress.jump(0);
 
const start = performance.now();
const tick = (now: number) => {
const elapsed = now - start;
const raw = Math.min(1, elapsed / Math.max(400, durationMs));
// Smooth ease-in-out curve for realistic transfer feel
const eased = raw < 0.5 ? 2 * raw * raw : 1 - Math.pow(-2 * raw + 2, 2) / 2;
progress.set(eased);
 
if (raw < 1) {
rafRef.current = requestAnimationFrame(tick);
} else {
setState("done");
if (sound) playSound("success");
onComplete?.();
resetRef.current = setTimeout(() => {
setState("idle");
progress.jump(0);
}, 1800);
}
};
 
rafRef.current = requestAnimationFrame(tick);
};
 
return (
<motion.button
type="button"
onClick={handleClick}
whileTap={state === "downloading" ? undefined : { scale: 0.965 }}
transition={SPRING_SNAPPY}
aria-live="polite"
className={cn(
"relative isolate inline-flex h-10 min-w-[148px] items-center justify-center gap-2 overflow-hidden rounded-full px-4 text-xs font-medium select-none cursor-pointer outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-2 focus-visible:ring-offset-canvas",
"border border-line-strong bg-surface text-fg shadow-panel transition-colors duration-150 hover:border-fg/30",
className
)}
>
{/* Spring-driven clipPath fill layer */}
<motion.span
aria-hidden="true"
style={{ clipPath }}
className={cn(
"pointer-events-none absolute inset-0 z-0 transition-colors duration-200",
state === "done" ? "bg-success" : "bg-inverse"
)}
>
{state === "downloading" && (
<span
className="absolute inset-0 opacity-25 linyi-diag-stripes"
aria-hidden="true"
/>
)}
</motion.span>
 
{/* Base label (visible on uncovered surface) */}
<span className="relative z-10 inline-flex items-center gap-2">
<AnimatePresence mode="popLayout" initial={false}>
{state === "idle" && (
<motion.span
key="icon-idle"
initial={{ opacity: 0, scale: 0.75, y: -3 }}
animate={{ opacity: 1, scale: 1, y: 0 }}
exit={{ opacity: 0, scale: 0.75, y: 3 }}
transition={SPRING_SNAPPY}
className="inline-flex"
>
<ArrowDownToLine className="h-3.5 w-3.5" />
</motion.span>
)}
{state === "downloading" && (
<motion.span
key="icon-loading"
initial={{ opacity: 0, scale: 0.75 }}
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: 0.75 }}
transition={SPRING_SNAPPY}
className="inline-flex"
>
<Loader2 className="h-3.5 w-3.5 animate-spin" />
</motion.span>
)}
{state === "done" && (
<motion.span
key="icon-done"
initial={{ opacity: 0, scale: 0.65 }}
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: 0.75 }}
transition={SPRING_SNAPPY}
className="inline-flex text-white"
>
<Check className="h-3.5 w-3.5" />
</motion.span>
)}
</AnimatePresence>
 
<AnimatePresence mode="popLayout" initial={false}>
<motion.span
key={state}
initial={{ opacity: 0, y: 5 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -5 }}
transition={SPRING_SNAPPY}
className={cn(
"inline-flex items-center gap-1.5 whitespace-nowrap",
state === "done" && "text-white"
)}
>
<span>
{state === "idle"
? label
: state === "downloading"
? downloadingLabel
: doneLabel}
</span>
{state === "downloading" && (
<motion.span className="font-mono-num text-[11px] opacity-80">{percentText}</motion.span>
)}
</motion.span>
</AnimatePresence>
</span>
 
{/* Inverted text layer clipped to exact same progress mask for razor-sharp contrast */}
{state === "downloading" && (
<motion.span
aria-hidden="true"
style={{ clipPath }}
className="pointer-events-none absolute inset-0 z-20 inline-flex items-center justify-center gap-2 px-4 text-xs font-medium text-fg-inverse"
>
<Loader2 className="h-3.5 w-3.5 animate-spin" />
<span className="inline-flex items-center gap-1.5 whitespace-nowrap">
<span>{downloadingLabel}</span>
<motion.span className="font-mono-num text-[11px] opacity-80">{percentText}</motion.span>
</span>
</motion.span>
)}
</motion.button>
);
}