components/linyi/stepper.tsx
motionlucide-react
"use client"; import * as React from "react";import { AnimatePresence, motion } from "motion/react";import { Minus, Plus } from "lucide-react";import { cn } from "@/lib/cn";import { SPRING_SNAPPY } from "@/lib/motion"; export interface StepperProps { value?: number; defaultValue?: number; onChange?: (value: number) => void; min?: number; max?: number; step?: number; unit?: string; label?: string; disabled?: boolean; showBoundaryHint?: boolean; className?: string;} export function Stepper({ value, defaultValue = 4, onChange, min = 0, max = 10, step = 1, unit = "", label = "数值步进", disabled = false, showBoundaryHint = true, className,}: StepperProps) { const clamp = (n: number) => Math.min(max, Math.max(min, n)); const [internalVal, setInternalVal] = React.useState(() => clamp(defaultValue)); const [direction, setDirection] = React.useState(1); const [boundaryNudge, setBoundaryNudge] = React.useState(0); const [boundaryMessage, setBoundaryMessage] = React.useState<string | null>(null); const hintTimerRef = React.useRef<number | null>(null); const currentVal = clamp(value ?? internalVal); const atMin = currentVal <= min; const atMax = currentVal >= max; React.useEffect(() => { return () => { if (hintTimerRef.current) window.clearTimeout(hintTimerRef.current); }; }, []); const flashBoundary = (msg: string) => { setBoundaryNudge((n) => n + 1); setBoundaryMessage(msg); if (hintTimerRef.current) window.clearTimeout(hintTimerRef.current); hintTimerRef.current = window.setTimeout(() => setBoundaryMessage(null), 1500); }; const applyValue = (nextRaw: number) => { if (disabled) return; const clamped = clamp(nextRaw); if (clamped === currentVal) { if (nextRaw < min) flashBoundary(`已达下限 ${min}${unit}`); else if (nextRaw > max) flashBoundary(`已达上限 ${max}${unit}`); return; } setDirection(clamped > currentVal ? 1 : -1); if (value === undefined) { setInternalVal(clamped); } onChange?.(clamped); if (clamped <= min) { flashBoundary(`已达下限 ${min}${unit}`); } else if (clamped >= max) { flashBoundary(`已达上限 ${max}${unit}`); } else { setBoundaryMessage(null); } }; const handleKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => { if (disabled) return; if (e.key === "ArrowUp" || e.key === "ArrowRight") { e.preventDefault(); applyValue(currentVal + step); } else if (e.key === "ArrowDown" || e.key === "ArrowLeft") { e.preventDefault(); applyValue(currentVal - step); } else if (e.key === "Home") { e.preventDefault(); applyValue(min); } else if (e.key === "End") { e.preventDefault(); applyValue(max); } }; return ( <div className={cn("inline-flex flex-col items-center gap-1.5", className)}> <motion.div key={boundaryNudge > 0 ? `nudge-${boundaryNudge}` : "idle"} animate={boundaryNudge > 0 ? { x: [0, -3, 3, -1.5, 0] } : { x: 0 }} transition={{ type: "tween", duration: 0.24, ease: "easeOut" }} role="spinbutton" tabIndex={disabled ? -1 : 0} aria-label={label} aria-valuenow={currentVal} aria-valuemin={min} aria-valuemax={max} aria-disabled={disabled || undefined} onKeyDown={handleKeyDown} className={cn( "inline-flex items-center gap-3 rounded-pill border border-line bg-surface p-1.5 shadow-panel focus-visible:outline-2 focus-visible:outline-accent focus-visible:outline-offset-2", disabled && "opacity-50" )} > <motion.button type="button" whileTap={disabled || atMin ? undefined : { scale: 0.9 }} transition={SPRING_SNAPPY} disabled={disabled || atMin} onClick={() => applyValue(currentVal - step)} aria-label="减少" className="ui-btn ui-btn-default h-9 w-9" > <Minus className="h-3.5 w-3.5" /> </motion.button> <div className="relative flex h-6 min-w-[2.5rem] items-center justify-center overflow-hidden px-1"> <AnimatePresence mode="popLayout" initial={false}> <motion.span key={currentVal} initial={{ y: direction > 0 ? 14 : -14, opacity: 0 }} animate={{ y: 0, opacity: 1 }} exit={{ y: direction > 0 ? -14 : 14, opacity: 0 }} transition={SPRING_SNAPPY} className="absolute font-mono-num text-sm font-semibold text-fg" > {currentVal} {unit} </motion.span> </AnimatePresence> </div> <motion.button type="button" whileTap={disabled || atMax ? undefined : { scale: 0.9 }} transition={SPRING_SNAPPY} disabled={disabled || atMax} onClick={() => applyValue(currentVal + step)} aria-label="增加" className="ui-btn ui-btn-solid h-9 w-9" > <Plus className="h-3.5 w-3.5" /> </motion.button> </motion.div> {showBoundaryHint && ( <div className="h-4 flex items-center justify-center"> <AnimatePresence mode="wait"> {boundaryMessage ? ( <motion.span key={boundaryMessage} initial={{ opacity: 0, y: 2 }} animate={{ opacity: 1, y: 0 }} exit={{ opacity: 0, y: -2 }} transition={{ duration: 0.16 }} role="status" aria-live="polite" className="font-mono-num text-[10px] text-accent" > {boundaryMessage} </motion.span> ) : ( <motion.span key="range" initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }} transition={{ duration: 0.14 }} className="font-mono-num text-[10px] text-fg-muted" > {min} – {max} {unit} </motion.span> )} </AnimatePresence> </div> )} </div> );}