Back to Base UIYou are on Slider.

Slider

A range slider whose thumb stretches in the direction of travel.

Available inAvailable in React, Vue, Svelte, Solid, JavaScript· All five frameworks

Libraries

Base UI

Motion

Spring + short tween

Reduced motion

Fade in place
slider.tsx
Play with the real component, then read the code it ships with.All components
01

Installation

Installing for React·components/prequelui/slider.tsx

Add the component

npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/react/slider.json
npmthe default on every machine

First component? Set up your project first

Frameworks

The live preview is React. Each tab below is the file that ships for that framework, with the same durations, springs and reduced-motion rule.

5 runtimes
slider.tsx
"use client";

import * as React from "react";
import { motion, useReducedMotion } from "motion/react";
import { cn } from "@/lib/utils";

export type SliderProps = {
  value?: number;
  defaultValue?: number;
  min?: number;
  max?: number;
  step?: number;
  label?: string;
  showValue?: boolean;
  onValueChange?: (value: number) => void;
  className?: string;
};

const slide = { type: "spring", stiffness: 400, damping: 32 } as const;
const stretch = 19 / 16;

export function Slider({
  value,
  defaultValue = 42,
  min = 0,
  max = 100,
  step = 1,
  label = "Volume",
  showValue = true,
  onValueChange,
  className,
}: SliderProps) {
  const [internal, setInternal] = React.useState(defaultValue);
  const current = value ?? internal;
  const [dragging, setDragging] = React.useState(false);
  const reduce = useReducedMotion();
  const track = React.useRef<HTMLDivElement>(null);
  const safeMax = max > min ? max : min + 1;
  const percent = Math.min(100, Math.max(0, ((current - min) / (safeMax - min)) * 100));

  const commit = (next: number) => {
    const snapped = Math.round((next - min) / step) * step + min;
    const clamped = Math.min(safeMax, Math.max(min, snapped));
    if (value === undefined) setInternal(clamped);
    onValueChange?.(clamped);
  };

  const fromClientX = (clientX: number) => {
    const rect = track.current?.getBoundingClientRect();
    if (!rect || rect.width === 0) return;
    const ratio = Math.min(1, Math.max(0, (clientX - rect.left) / rect.width));
    commit(min + ratio * (safeMax - min));
  };

  const onPointerDown = (event: React.PointerEvent) => {
    event.currentTarget.setPointerCapture(event.pointerId);
    setDragging(true);
    fromClientX(event.clientX);
  };
  const onPointerMove = (event: React.PointerEvent) => {
    if (!dragging) return;
    fromClientX(event.clientX);
  };
  const stop = () => setDragging(false);

  const onKey = (event: React.KeyboardEvent) => {
    const delta =
      event.key === "ArrowRight" || event.key === "ArrowUp"
        ? step
        : event.key === "ArrowLeft" || event.key === "ArrowDown"
          ? -step
          : event.key === "Home"
            ? min - current
            : event.key === "End"
              ? safeMax - current
              : 0;
    if (!delta) return;
    event.preventDefault();
    commit(current + delta);
  };

  return (
    <div className={cn("w-full max-w-[360px]", className)}>
      <div className="mb-2 flex items-center justify-between text-[13px]">
        <span className="text-muted-foreground">{label}</span>
        {showValue && <span className="tabular-nums">{current}</span>}
      </div>
      <div
        ref={track}
        role="slider"
        tabIndex={0}
        aria-valuemin={min}
        aria-valuemax={safeMax}
        aria-valuenow={current}
        aria-label={label}
        onPointerDown={onPointerDown}
        onPointerMove={onPointerMove}
        onPointerUp={stop}
        onPointerCancel={stop}
        onKeyDown={onKey}
        className="relative flex h-6 w-full touch-none select-none items-center outline-none"
      >
        <div className="h-1.5 w-full overflow-hidden rounded-full bg-accent">
          <motion.div
            initial={false}
            animate={{ width: `${percent}%` }}
            transition={reduce ? { duration: 0 } : slide}
            className="h-full rounded-full bg-foreground"
          />
        </div>
        <motion.span
          aria-hidden="true"
          initial={false}
          animate={{ left: `${percent}%`, width: dragging ? 12 * stretch : 12 }}
          transition={reduce ? { duration: 0 } : slide}
          style={{ marginLeft: -6 }}
          className={cn(
            "absolute top-1/2 block h-3 -translate-y-1/2 rounded-full border bg-background shadow-sm",
            dragging && "border-foreground/40",
          )}
        />
      </div>
    </div>
  );
}

Install

npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/react/slider.json
npmthe default on every machine

Dependencies

motion lucide-react

The registry keeps the component source, its motion tokens and the reduced-motion branch together, so the install never pulls a different animation from the preview.

02

Usage

Import the component and compose it exactly like the primitive it wraps. Motion is enabled by default; the tempo prop scales every duration and softens every spring.

slider.tsx
import { Slider } from "@/components/prequelui/slider";

export default function Demo() {
  return (
    <Slider>
      {/* compose the parts exactly like the underlying primitive */}
    </Slider>
  );
}
import { Slider } from "@/components/prequelui/slider";

export function Variant() {
  return (
    <Slider tempo={1.2}>
      …
    </Slider>
  );
}
03

Animation

This primitive keeps the API and semantics of the underlying component and adds motion at the surface edges. Entrances are slower and more settled than exits, so closing never feels like waiting.

  • Springs for position, scale and size; tweens for colour and opacity.
  • The exit is shorter than the entrance, and keeps the current position as its starting point.
  • All measurements are taken again when content or font metrics change.
  • With reduced motion, the surface fades in place and never travels.
spring 300 / 30exit 150 msblur 4 pxheight 450 ms
04

API Reference

Every part takes exactly the props of the Slider primitive. The animated surface adds only motion props, so the accessibility tree and keyboard behaviour stay the same.

Slider

PropTypeDefault
childrenReactNode—
disabledbooleanfalse
transitionTransitionspring.press
classNamestring—

Next component

Switch

A switch whose thumb stretches 3/16 and lands softly.