"use client"; import { AnimatePresence, type AnimationPlaybackControls, animate, type HTMLMotionProps, motion, useMotionTemplate, useMotionValue, useReducedMotion, useSpring, useTransform, } from "motion/react"; import { useEffect, useRef, useState } from "react"; import { TextMorph } from "@/components/motion/text-morph"; import { EASE_IN, EASE_IN_OUT, EASE_OUT, SPRING_GLIDE, SPRING_PRESS, SPRING_SWAP } from "@/lib/ease"; import { useMessages } from "@/lib/i18n"; import { useSound } from "@/lib/sound"; import { successSound } from "@/lib/sounds/success"; import { tapSound } from "@/lib/sounds/tap"; import { cn } from "@/lib/utils"; export const downloadButtonMessages = { download: "Download", downloading: "Downloading", downloaded: "Downloaded", progress: "Download progress", }; export type DownloadButtonMessages = typeof downloadButtonMessages; type Phase = "idle" | "downloading" | "done"; export interface DownloadButtonProps extends Omit, "children" | "onChange"> { /** * Runs the download. Call `report` with progress from 0 to 1 to move the * fill; return a promise to finish when it settles. Without it the fill * runs on its own, as a demo. */ onDownload?: (report: (progress: number) => void) => unknown; /** Override any built-in string, e.g. for translation. */ labels?: Partial; /** Play a tap on start and a success chime when done. Off by default; a `SoundProvider` can mute it app-wide. */ sound?: boolean; } // The pill, and the round badge it folds into when the download is done. const WIDE = 192; const ROUND = 40; // The badge holds this long before it opens back into the button. const DONE_MS = 1300; // Without onDownload, the fill runs over this long. const DEMO_SECONDS = 2.4; // One stripe period, in px: the stripe layer slides this far per loop. const STRIPE = 16; const EXIT = { duration: 0.14, ease: EASE_OUT } as const; const FADE = { duration: 0.16, ease: EASE_OUT } as const; // The check pops in a beat after the pill has folded. const DRAW = { duration: 0.32, ease: EASE_OUT, delay: 0.22 } as const; const POP = { ...SPRING_PRESS, delay: 0.18 } as const; // While it works, an arrow keeps dropping into the tray. const DROP = { duration: 0.9, ease: EASE_IN, repeat: Number.POSITIVE_INFINITY } as const; // The stripes drift evenly; easing would make them pulse. const DRIFT = { duration: 0.7, ease: "linear", repeat: Number.POSITIVE_INFINITY } as const; /** * A download where the whole button is the progress bar. Colour sweeps * across the pill with a bright leading edge and drifting stripes, and the * label and icon turn white exactly where the fill passes. When it is done * the pill folds into a round badge where a check pops in, then opens back * into the button as the colour drains away. */ export function DownloadButton({ onDownload, labels, sound = false, className, onClick, ...rest }: DownloadButtonProps) { const t = useMessages("DownloadButton", downloadButtonMessages, labels); const playStart = useSound(tapSound, { enabled: sound }); const playDone = useSound(successSound, { enabled: sound }); const reduce = Boolean(useReducedMotion()); const [phase, setPhase] = useState("idle"); const [percent, setPercent] = useState(0); // Progress drives one value; a critically damped spring smooths uneven // reports into a steady sweep, and the clip and the edge both read it. const target = useMotionValue(0); const smooth = useSpring(target, SPRING_GLIDE); const progress = reduce ? target : smooth; const right = useTransform(progress, (value) => `${(1 - value) * 100}%`); const clip = useMotionTemplate`inset(0 ${right} 0 0)`; // The leading edge rides the fill: a full-width layer whose right edge // sits at the progress, moved by transform only. const edgeX = useTransform(progress, (value) => `${(value - 1) * 100}%`); const fillOpacity = useMotionValue(0); const drainRef = useRef(null); const report = (value: number) => { const clamped = Math.min(1, Math.max(0, value)); target.set(clamped); setPercent(Math.round(clamped * 100)); }; const reset = () => { target.jump(0); smooth.jump(0); setPercent(0); }; // Done: hold the badge, then open back into the button as the colour // drains, and reset the fill under it once it is gone. useEffect(() => { if (phase !== "done") return; const id = setTimeout(() => { setPhase("idle"); const drain = animate(fillOpacity, 0, { duration: 0.3, ease: EASE_OUT }); drainRef.current = drain; drain.then(() => { if (drainRef.current !== drain) return; drainRef.current = null; target.jump(0); smooth.jump(0); setPercent(0); }); }, DONE_MS); return () => clearTimeout(id); }, [phase, fillOpacity, target, smooth]); const start = async () => { // A press during the drain stops it, so it cannot pull the new fill to 0. drainRef.current?.stop(); drainRef.current = null; reset(); fillOpacity.set(1); setPhase("downloading"); try { if (onDownload) await onDownload(report); else await animate(0, 1, { duration: DEMO_SECONDS, ease: EASE_IN_OUT, onUpdate: report }); } catch { fillOpacity.set(0); reset(); setPhase("idle"); return; } report(1); playDone(); setPhase("done"); }; const instant = { duration: 0 }; const done = phase === "done"; const label = t(phase === "idle" ? "download" : phase === "downloading" ? "downloading" : "downloaded"); // The content is drawn twice, identically: in ink on the button, and in // white on the fill, clipped to the progress. Where the fill has reached, // you see the white copy. const content = (onFill: boolean) => ( {label} {phase === "downloading" ? ( // A fixed slot, so the label never shifts as the number grows. {percent}% ) : null} ); return ( <> { onClick?.(event); if (event.defaultPrevented || phase !== "idle") return; playStart(); void start(); }} className={cn( "relative isolate inline-flex h-10 items-center justify-center overflow-hidden rounded-full border border-border bg-background text-sm font-medium text-foreground transition-colors duration-200", "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring/50", "disabled:pointer-events-none disabled:opacity-50", phase === "idle" && "hover:bg-muted/60", className, )} {...rest} > {content(false)} {/* The fill, clipped to the progress, with the white copy of the content on it. */} {done ? t("downloaded") : ""} ); }