components/linyi/hold-to-confirm.tsx
lucide-reactmotion
"use client"; import * as React from "react";import { AnimatePresence, motion, useMotionValue, useSpring, useTransform,} from "motion/react";import { Check, Trash2, ShieldCheck } from "lucide-react";import { cn } from "@/lib/cn";import { SPRING_PROGRESS, SPRING_SNAPPY } from "@/lib/motion";import { playSound } from "@/lib/sound"; export interface HoldToConfirmProps { label?: string; holdingLabel?: string; confirmedLabel?: string; holdMs?: number; tone?: "danger" | "solid"; onConfirm?: () => void; className?: string;} export function HoldToConfirm({ label = "长按删除工作区", holdingLabel = "继续按住确认...", confirmedLabel = "已确认执行", holdMs = 1100, tone = "danger", onConfirm, className,}: HoldToConfirmProps) { const [holding, setHolding] = React.useState(false); const [confirmed, setConfirmed] = React.useState(false); 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)` ); const rafRef = React.useRef<number | null>(null); const resetRef = React.useRef<ReturnType<typeof setTimeout> | null>(null); const startTimeRef = React.useRef(0); const clearRaf = () => { if (rafRef.current) { cancelAnimationFrame(rafRef.current); rafRef.current = null; } }; React.useEffect(() => { return () => { clearRaf(); if (resetRef.current) clearTimeout(resetRef.current); }; }, []); const startHold = () => { if (confirmed || holding) return; if (resetRef.current) clearTimeout(resetRef.current); playSound("tap", { pitch: 0.9, volume: 0.7 }); setHolding(true); startTimeRef.current = performance.now(); const step = (now: number) => { const ratio = Math.min(1, (now - startTimeRef.current) / Math.max(300, holdMs)); progress.set(ratio); if (ratio < 1) { rafRef.current = requestAnimationFrame(step); } else { clearRaf(); setHolding(false); setConfirmed(true); playSound("success"); onConfirm?.(); resetRef.current = setTimeout(() => { setConfirmed(false); progress.set(0); }, 1800); } }; rafRef.current = requestAnimationFrame(step); }; const cancelHold = () => { if (!holding) return; clearRaf(); setHolding(false); progress.set(0); }; const isDanger = tone === "danger"; const fillClass = confirmed ? "bg-success" : isDanger ? "bg-accent" : "bg-inverse"; const Icon = isDanger ? Trash2 : ShieldCheck; return ( <motion.button type="button" onPointerDown={startHold} onPointerUp={cancelHold} onPointerLeave={cancelHold} onPointerCancel={cancelHold} onBlur={cancelHold} onKeyDown={(e) => { if ((e.key === " " || e.key === "Enter") && !e.repeat) { e.preventDefault(); startHold(); } else if (e.key === "Escape") { cancelHold(); } }} onKeyUp={(e) => { if (e.key === " " || e.key === "Enter") { cancelHold(); } }} animate={{ scale: holding ? 0.97 : 1 }} transition={SPRING_SNAPPY} aria-pressed={holding} aria-busy={holding} aria-live="polite" className={cn( "relative isolate inline-flex h-10 min-w-[164px] items-center justify-center gap-2 overflow-hidden rounded-full px-4 text-xs font-medium select-none touch-none cursor-pointer outline-none focus-visible:ring-2 focus-visible:ring-accent focus-visible:ring-offset-2 focus-visible:ring-offset-canvas", isDanger ? "border border-accent-border bg-accent-soft text-accent" : "border border-line-strong bg-surface text-fg", className )} > {/* Spring-driven clipPath fill */} <motion.span aria-hidden="true" style={{ clipPath }} className={cn("pointer-events-none absolute inset-0 z-0 transition-colors duration-200", fillClass)} /> {/* Base label */} <span className="relative z-10 inline-flex items-center gap-2"> <AnimatePresence mode="popLayout" initial={false}> {confirmed ? ( <motion.span key="done-icon" initial={{ opacity: 0, scale: 0.7 }} animate={{ opacity: 1, scale: 1 }} exit={{ opacity: 0, scale: 0.7 }} transition={SPRING_SNAPPY} className="inline-flex text-white" > <Check className="h-3.5 w-3.5" /> </motion.span> ) : ( <motion.span key="idle-icon" initial={{ opacity: 0, scale: 0.8 }} animate={{ opacity: 1, scale: 1 }} exit={{ opacity: 0, scale: 0.8 }} transition={SPRING_SNAPPY} className="inline-flex" > <Icon className="h-3.5 w-3.5" /> </motion.span> )} </AnimatePresence> <span className={cn("whitespace-nowrap", confirmed && "text-white")}> {confirmed ? confirmedLabel : holding ? holdingLabel : label} </span> </span> {/* Inverted text clipped identically to the progress fill */} {!confirmed && ( <motion.span aria-hidden="true" style={{ clipPath }} className={cn( "pointer-events-none absolute inset-0 z-20 inline-flex items-center justify-center gap-2 px-4 text-xs font-medium whitespace-nowrap", isDanger ? "text-white" : "text-fg-inverse" )} > <Icon className="h-3.5 w-3.5" /> <span>{holding ? holdingLabel : label}</span> </motion.span> )} </motion.button> );}