SunUI
Founding$79 lifetime updates till Jan 11Claim a seat
FreeDrag

Range Slider

Slider for any numeric value or range (price, rating, volume, quota) that takes a number for one thumb or a [low, high] pair for a range: step snapping, minimum gap, ticks, a value bubble while dragging or focused, custom formatting and hidden inputs for forms.

When to use it

  • Price range filters on a product listing
  • Minimum rating, budget or distance filters

Watch out

  • Run expensive searches from onValueCommit, not onValueChange
  • For a handful of exact values, sliding-tabs chips are easier to hit on phones

Let your agent add it

One copy holds the install command, when to use it, the props and the design rules. Paste it into Claude Code, Cursor or Codex.

Install with the CLI

Uses the shadcn CLI. The source is copied into your project, so you own every line.

$ npx shadcn@latest add https://sunui.synvize.com/r/range-slider.json

Or install manually

  1. 1. Install the dependencies.
    $ npm install clsx tailwind-merge framer-motion
  2. 2. Add a cn helper at lib/utils.ts if you do not have one. Not using shadcn/ui? Also add sunui-theme.css for the colour tokens (how). Then copy the component.
    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>
      );
    }