免费拖拽
区间滑块
数值滑块(价格、评分、音量、额度都适用):传一个数字是单滑块,传 [低, 高] 就是区间。
适合使用的场景
- 商品列表的价格区间筛选
- 最低评分、预算或距离筛选
注意事项
- 耗时的搜索请放在 onValueCommit,而不是 onValueChange
- 只有几个固定值时,用 sliding-tabs 的筛选标签在手机上更好点
交给你的 agent 安装
一次复制就包含安装命令、使用时机、props 与设计规则。粘贴到 Claude Code、Cursor 或 Codex 即可。
用 CLI 安装
使用 shadcn CLI。源码会复制到你的项目中,每一行都由你掌控。
$ npx shadcn@latest add https://sunui.synvize.com/r/range-slider.json或手动安装
- 1. 安装依赖。
$ npm install clsx tailwind-merge framer-motion - 2. 如果还没有,在
lib/utils.ts添加cn辅助函数。没有用 shadcn/ui?再加上 sunui-theme.css 提供颜色变量(做法)。然后复制组件代码。range-slider.tsx"use client"; import { motion, useReducedMotion } from "framer-motion"; import { useCallback, useId, useRef, useState, type KeyboardEvent, type PointerEvent as ReactPointerEvent, type ReactNode } from "react"; import { cn } from "@/lib/utils"; /** Brand accent: follows the theme's --primary by default; set --sunui-spot to use your own brand colour. Used only for selected state, deals and current progress */ const spotBg = "bg-[color:var(--sunui-spot,var(--primary))]"; const spotBorder = "border-[color:var(--sunui-spot,var(--primary))]"; const springs = { snappy: { type: "spring", stiffness: 520, damping: 34 }, } as const; type RangeValue = number | [number, number]; export interface RangeSliderProps<V extends RangeValue = RangeValue> { /** A number means a single value; [low, high] means a range */ value?: V; defaultValue?: V; /** Called continuously while dragging */ onValueChange?: (value: V) => void; /** Called once after release or a keyboard action; good for submitting a filter */ onValueCommit?: (value: V) => void; min?: number; max?: number; step?: number; /** Minimum gap between the two handles in range mode */ minGap?: number; /** Format of the displayed value, e.g. (n) => `$${n}` */ format?: (n: number) => ReactNode; /** Ticks: a number means that many equal divisions, an array gives explicit positions */ ticks?: number | number[]; /** Show the value above the handle while dragging */ showTooltip?: boolean; label?: ReactNode; /** Form field name; a range submits name-min and name-max */ name?: string; /** aria-label of the handles; in range mode in the order [low, high] */ thumbLabels?: [string, string] | string; disabled?: boolean; className?: string; } const clamp = (n: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, n)); export function RangeSlider<V extends RangeValue>({ value: valueProp, defaultValue, onValueChange, onValueCommit, min = 0, max = 100, step = 1, minGap = 0, format = (n) => n, ticks, showTooltip = true, label, name, thumbLabels, disabled = false, className, }: RangeSliderProps<V>) { const reduce = useReducedMotion(); const labelId = useId(); const trackRef = useRef<HTMLDivElement>(null); const [inner, setInner] = useState<RangeValue>(defaultValue ?? valueProp ?? min); const current = (valueProp ?? inner) as RangeValue; const range = Array.isArray(current); const values: number[] = range ? [...current] : [current]; const [dragging, setDragging] = useState<number | null>(null); const [focused, setFocused] = useState<number | null>(null); const latest = useRef(values); latest.current = values; const pct = (n: number) => ((n - min) / (max - min)) * 100; const snap = (n: number) => clamp(Math.round((n - min) / step) * step + min, min, max); const decimals = (String(step).split(".")[1] ?? "").length; const emit = useCallback( (next: number[], commit = false) => { const out = (range ? [next[0], next[1]] : next[0]) as V; if (valueProp === undefined) setInner(out); onValueChange?.(out); if (commit) onValueCommit?.(out); }, [range, valueProp, onValueChange, onValueCommit], ); const setThumb = (index: number, raw: number, commit = false) => { const next = [...latest.current]; let v = Number(snap(raw).toFixed(decimals)); if (range) v = index === 0 ? Math.min(v, next[1] - minGap) : Math.max(v, next[0] + minGap); if (next[index] === v && !commit) return; next[index] = v; latest.current = next; emit(next, commit); }; const fromPointer = (clientX: number) => { const rect = trackRef.current!.getBoundingClientRect(); return min + clamp((clientX - rect.left) / rect.width, 0, 1) * (max - min); }; const onTrackDown = (e: ReactPointerEvent<HTMLDivElement>) => { if (disabled || e.button !== 0) return; const raw = fromPointer(e.clientX); const index = !range ? 0 : raw <= values[0] ? 0 : raw >= values[1] ? 1 : raw - values[0] < values[1] - raw ? 0 : 1; setDragging(index); setThumb(index, raw); (e.currentTarget as HTMLElement).setPointerCapture(e.pointerId); trackRef.current?.querySelectorAll<HTMLElement>("[role=slider]")[index]?.focus(); }; const onTrackMove = (e: ReactPointerEvent<HTMLDivElement>) => { if (dragging === null) return; setThumb(dragging, fromPointer(e.clientX)); }; const onTrackUp = () => { if (dragging === null) return; emit(latest.current, true); setDragging(null); }; const onKeyDown = (e: KeyboardEvent<HTMLDivElement>, index: number) => { const big = Math.max(step, (max - min) / 10); const map: Record<string, number> = { ArrowRight: step, ArrowUp: step, ArrowLeft: -step, ArrowDown: -step, PageUp: big, PageDown: -big }; let target: number | undefined; if (e.key in map) target = values[index] + map[e.key]; if (e.key === "Home") target = min; if (e.key === "End") target = max; if (target === undefined) return; e.preventDefault(); setThumb(index, target, true); }; const tickList = typeof ticks === "number" ? Array.from({ length: ticks + 1 }, (_, i) => min + ((max - min) * i) / ticks) : ticks ?? []; const lo = range ? pct(values[0]) : 0; const hi = pct(range ? values[1] : values[0]); const t = reduce || dragging !== null ? { duration: 0 } : springs.snappy; const labels = typeof thumbLabels === "string" ? [thumbLabels, thumbLabels] : thumbLabels ?? ["Minimum", "Maximum"]; return ( <div className={cn("w-full select-none", disabled && "pointer-events-none opacity-50", className)}> {label && ( <div className="mb-3 flex items-baseline justify-between gap-3 text-sm"> <span id={labelId} className="font-medium"> {label} </span> <span className="tabular-nums text-muted-foreground"> {format(values[0])} {range && <> – {format(values[1])}</>} </span> </div> )} <div ref={trackRef} className="relative h-6 cursor-pointer touch-none" onPointerDown={onTrackDown} onPointerMove={onTrackMove} onPointerUp={onTrackUp} onPointerCancel={onTrackUp} > <div className="absolute inset-x-0 top-1/2 h-1 -translate-y-1/2 rounded-full bg-foreground/10" /> <motion.div className={cn("absolute top-1/2 h-1 -translate-y-1/2 rounded-full", spotBg)} initial={false} animate={{ left: `${lo}%`, width: `${hi - lo}%` }} transition={t} /> {tickList.map((tick) => ( <span key={tick} aria-hidden="true" className={cn("absolute top-1/2 size-1 -translate-x-1/2 -translate-y-1/2 rounded-full", pct(tick) >= lo && pct(tick) <= hi ? "bg-card" : "bg-foreground/30")} style={{ left: `${pct(tick)}%` }} /> ))} {values.map((v, i) => ( <motion.div key={i} role="slider" tabIndex={disabled ? -1 : 0} aria-label={range ? labels[i] : typeof thumbLabels === "string" ? thumbLabels : undefined} aria-labelledby={!range && !thumbLabels && label ? labelId : undefined} aria-valuemin={range && i === 1 ? values[0] + minGap : min} aria-valuemax={range && i === 0 ? values[1] - minGap : max} aria-valuenow={v} aria-valuetext={typeof format(v) === "string" || typeof format(v) === "number" ? String(format(v)) : undefined} aria-disabled={disabled || undefined} onKeyDown={(e) => onKeyDown(e, i)} onFocus={() => setFocused(i)} onBlur={() => setFocused(null)} className={cn("absolute top-1/2 z-10 -ml-2.5 -mt-2.5 size-5 rounded-full border-[1.5px] bg-card outline-offset-2 focus-visible:outline-2", spotBorder)} initial={false} animate={{ left: `${pct(v)}%`, scale: dragging === i && !reduce ? 1.15 : 1 }} transition={t} style={{ zIndex: dragging === i || focused === i ? 20 : 10 }} > {showTooltip && (dragging === i || focused === i) && ( <span className="pointer-events-none absolute bottom-full left-1/2 mb-2 -translate-x-1/2 whitespace-nowrap rounded-sm border border-foreground/10 bg-card px-1.5 py-0.5 text-xs tabular-nums text-foreground shadow-[0_1px_2px_rgb(0_0_0/0.05)] dark:shadow-[inset_0_1px_0_rgb(255_255_255/0.06)]"> {format(v)} </span> )} </motion.div> ))} </div> {name && ( <> <input type="hidden" name={range ? `${name}-min` : name} value={values[0]} /> {range && <input type="hidden" name={`${name}-max`} value={values[1]} />} </> )} </div> ); }
相似区块
- Pro
- Pro
- Pro
- Pro
- Pro





