Skip to content
Atoms

Slider

Single value with a bubble, ticks and keys.

Primitive

Variants

3 more takes on the slider, each a single file you install on its own by its id.

aui-061Value bubble

A value bubble that tilts as you drag.

$ pnpm dlx asta-ui@latest add slider-aui-061

Or with the shadcn CLI: pnpm dlx shadcn@latest add https://ui.astabyte.com/r/slider-aui-061.json

aui-062Stepped

Snaps to steps and rolls the price.

$ pnpm dlx asta-ui@latest add slider-aui-062

Or with the shadcn CLI: pnpm dlx shadcn@latest add https://ui.astabyte.com/r/slider-aui-062.json

aui-063Volume

A volume slider in a mini player.

$ pnpm dlx asta-ui@latest add slider-aui-063

Or with the shadcn CLI: pnpm dlx shadcn@latest add https://ui.astabyte.com/r/slider-aui-063.json

Installation

Primitive
  1. Run the CLI

    Run pnpm dlx asta-ui@latest init once, then this. The file lands in components/astabyte/, installs radix-ui@base-ui/react and the colour tokens go into your CSS. This is the Radix build. Drop --radix for Base UI.This is the Base UI build, the default. Want Radix? Add --radix.

    $ pnpm dlx asta-ui@latest add slider --radix

    Or with the shadcn CLI: pnpm dlx shadcn@latest add https://ui.astabyte.com/r/base/slider.json

  2. Or install the package

    Don’t want the file? Import from asta-uiasta-ui/base and update with pnpm update asta-ui. Add the stylesheet once. More.

    $ pnpm add asta-ui radix-ui @base-ui/react
    import { Slider } from "asta-ui";
    import { Slider } from "asta-ui/base";
  3. Or copy it by hand

    Install radix-ui@base-ui/react, then copy this file. Keep the paths, or fix the @/ imports. Then add the tokens from globals.css.

    "use client";
    
    import { Slider as S } from "radix-ui";
    import { useState } from "react";
    
    const cx = (...c: (string | false | null | undefined)[]) => c.filter(Boolean).join(" ");
    
    export interface SliderProps {
      value?: number;
      defaultValue?: number;
      onValueChange?: (v: number) => void;
      /** Fires once when the user lets go (pointer up or a key press). */
      onValueCommit?: (v: number) => void;
      min?: number;
      max?: number;
      step?: number;
      /** Draw a tick for every step (true) or this many evenly spaced ticks. */
      ticks?: boolean | number;
      /** Format the value in the bubble and for screen readers. */
      format?: (v: number) => string;
      disabled?: boolean;
      /** Form field name. */
      name?: string;
      "aria-label"?: string;
      "aria-labelledby"?: string;
      className?: string;
    }
    
    // The thumb is 20px wide and Radix keeps it inside the track, so its centre runs from 10px to width-10px.
    const HALF = 10;
    
    /**
     * A range slider on Radix Slider (pointer, touch, arrows, Page Up/Down, Home/End, ARIA, form value) with a
     * value bubble that pops up while you drag, hover or focus it. Clicks and key presses glide; drags follow
     * the pointer exactly.
     */
    export function Slider({
      value,
      defaultValue = 50,
      onValueChange,
      onValueCommit,
      min = 0,
      max = 100,
      step = 1,
      ticks,
      format = (v) => String(v),
      disabled,
      name,
      className,
      ...aria
    }: SliderProps) {
      const [inner, setInner] = useState(defaultValue);
      const [drag, setDrag] = useState(false);
      const v = value ?? inner;
      const k = (v - min) / (max - min);
      const marks = ticks === true ? Math.round((max - min) / step) + 1 : typeof ticks === "number" ? ticks : 0;
      // Only glide when the value jumps (a click on the track or a key), not while dragging. perf-ok, a short glide on a jump (click or key), off while dragging; the box is a few px.
      const glide = !drag && "[&>span:has(>[role=slider])]:transition-[left] [&>span:has(>[role=slider])]:duration-200 [&>span:has(>[role=slider])]:ease-out";
    
      return (
        <div className={cx("relative w-full select-none pt-7", className)}>
          <S.Root
            value={[v]}
            onValueChange={([n]) => {
              if (value === undefined) setInner(n);
              onValueChange?.(n);
            }}
            onValueCommit={([n]) => onValueCommit?.(n)}
            onPointerDown={() => setDrag(true)}
            onPointerUp={() => setDrag(false)}
            onLostPointerCapture={() => setDrag(false)}
            min={min}
            max={max}
            step={step}
            disabled={disabled}
            name={name}
            className={cx("group/sl relative flex h-6 cursor-pointer touch-none items-center data-[disabled]:pointer-events-none data-[disabled]:opacity-45", glide, "motion-reduce:[&>span]:!transition-none")}
          >
            <S.Track className="relative h-1.5 grow overflow-hidden rounded-full bg-[color-mix(in_srgb,var(--color-ink)_12%,transparent)]">
              {/* perf-ok: a short glide on a jump (click or key), off while dragging; the box is a few px. */}
              <S.Range className={cx("absolute h-full rounded-full bg-brand", !drag && "transition-[right] duration-200 ease-out motion-reduce:transition-none")} />
            </S.Track>
            {marks > 1 && (
              <div aria-hidden className="pointer-events-none absolute top-1/2 -translate-y-1/2" style={{ left: HALF, right: HALF }}>
                {Array.from({ length: marks }, (_, i) => {
                  const at = i / (marks - 1);
                  return (
                    <span
                      key={i}
                      className={cx("absolute top-1/2 h-1 w-1 -translate-x-1/2 -translate-y-1/2 rounded-full transition-colors", at <= k ? "bg-on-brand/80" : "bg-[color-mix(in_srgb,var(--color-ink)_28%,transparent)]")}
                      style={{ left: `${at * 100}%` }}
                    />
                  );
                })}
              </div>
            )}
            <S.Thumb
              {...aria}
              aria-valuetext={format(v)}
              className={cx(
                "group/th relative block h-5 w-5 rounded-full bg-white outline-none",
                "shadow-[0_0_0_1px_color-mix(in_srgb,var(--color-ink)_14%,transparent),0_1px_3px_rgb(0_0_0/0.2),0_4px_10px_-2px_rgb(0_0_0/0.2)]",
                "transition-transform duration-300 ease-[cubic-bezier(.34,1.56,.64,1)] focus-visible:ring-4 focus-visible:ring-brand/30",
                drag ? "scale-110" : "group-hover/sl:scale-105",
              )}
            >
              <span
                aria-hidden
                className={cx(
                  "pointer-events-none absolute bottom-[26px] left-1/2 origin-bottom -translate-x-1/2 whitespace-nowrap rounded-lg bg-ink px-2 py-1 text-[12px] font-medium tabular-nums text-paper",
                  "shadow-[0_4px_12px_-4px_rgb(0_0_0/0.4)] transition-[scale,opacity] duration-300 ease-[cubic-bezier(.34,1.56,.64,1)]",
                  drag ? "scale-100 opacity-100" : "scale-75 opacity-0 group-hover/sl:scale-100 group-hover/sl:opacity-100 group-focus-visible/th:scale-100 group-focus-visible/th:opacity-100",
                )}
              >
                {format(v)}
                <span className="absolute left-1/2 top-full -translate-x-1/2 border-x-[5px] border-t-[5px] border-x-transparent border-t-ink" />
              </span>
            </S.Thumb>
          </S.Root>
        </div>
      );
    }
    
    export default Slider;
    
    "use client";
    
    import { Slider as S } from "@base-ui/react/slider";
    import { useState } from "react";
    
    const cx = (...c: (string | false | null | undefined)[]) => c.filter(Boolean).join(" ");
    
    export interface SliderProps {
      value?: number;
      defaultValue?: number;
      onValueChange?: (v: number) => void;
      /** Fires once when the user lets go (pointer up or a key press). */
      onValueCommit?: (v: number) => void;
      min?: number;
      max?: number;
      step?: number;
      /** Draw a tick for every step (true) or this many evenly spaced ticks. */
      ticks?: boolean | number;
      /** Format the value in the bubble and for screen readers. */
      format?: (v: number) => string;
      disabled?: boolean;
      /** Form field name. */
      name?: string;
      "aria-label"?: string;
      "aria-labelledby"?: string;
      className?: string;
    }
    
    // The thumb is 20px wide and `thumbAlignment="edge"` keeps it inside the track, so its centre runs from 10px to width-10px.
    const HALF = 10;
    
    /**
     * A range slider on Base UI Slider (pointer, touch, arrows, Page Up/Down, Home/End, ARIA, form value) with a
     * value bubble that pops up while you drag, hover or focus it. Clicks and key presses glide; drags follow
     * the pointer exactly.
     */
    export function Slider({
      value,
      defaultValue = 50,
      onValueChange,
      onValueCommit,
      min = 0,
      max = 100,
      step = 1,
      ticks,
      format = (v) => String(v),
      disabled,
      name,
      className,
      ...aria
    }: SliderProps) {
      const [inner, setInner] = useState(defaultValue);
      const v = value ?? inner;
      const k = (v - min) / (max - min);
      const marks = ticks === true ? Math.round((max - min) / step) + 1 : typeof ticks === "number" ? ticks : 0;
      // Only glide when the value jumps (a click on the track or a key), not while dragging (Base UI sets data-dragging). perf-ok, a short glide on a jump (click or key), off while dragging; the box is a few px.
      const glide = "transition-[left,inset-inline-start] duration-200 ease-out group-data-[dragging]/sl:transition-none motion-reduce:!transition-none";
    
      return (
        <div className={cx("relative w-full select-none pt-7", className)}>
          <S.Root
            value={v}
            onValueChange={(n) => {
              const next = Array.isArray(n) ? n[0] : n;
              if (value === undefined) setInner(next);
              onValueChange?.(next);
            }}
            onValueCommitted={(n) => onValueCommit?.(Array.isArray(n) ? n[0] : n)}
            min={min}
            max={max}
            step={step}
            disabled={disabled}
            name={name}
            thumbAlignment="edge"
            className="group/sl data-[disabled]:pointer-events-none data-[disabled]:opacity-45"
          >
            <S.Control className="relative flex h-6 cursor-pointer touch-none items-center">
              <S.Track className="relative h-1.5 w-full rounded-full bg-[color-mix(in_srgb,var(--color-ink)_12%,transparent)]">
                {/* perf-ok: a short glide on a jump (click or key), off while dragging; the box is a few px. */}
                <S.Indicator className="rounded-full bg-brand transition-[width] duration-200 ease-out group-data-[dragging]/sl:transition-none motion-reduce:!transition-none" />
                {marks > 1 && (
                  <div aria-hidden className="pointer-events-none absolute top-1/2 -translate-y-1/2" style={{ left: HALF, right: HALF }}>
                    {Array.from({ length: marks }, (_, i) => {
                      const at = i / (marks - 1);
                      return (
                        <span
                          key={i}
                          className={cx("absolute top-1/2 h-1 w-1 -translate-x-1/2 -translate-y-1/2 rounded-full transition-colors", at <= k ? "bg-on-brand/80" : "bg-[color-mix(in_srgb,var(--color-ink)_28%,transparent)]")}
                          style={{ left: `${at * 100}%` }}
                        />
                      );
                    })}
                  </div>
                )}
                <S.Thumb
                  aria-label={aria["aria-label"]}
                  aria-labelledby={aria["aria-labelledby"]}
                  aria-valuetext={format(v)}
                  className={cx("group/th h-5 w-5 outline-none", glide)}
                >
                  <span
                    className={cx(
                      "block h-5 w-5 rounded-full bg-white",
                      "shadow-[0_0_0_1px_color-mix(in_srgb,var(--color-ink)_14%,transparent),0_1px_3px_rgb(0_0_0/0.2),0_4px_10px_-2px_rgb(0_0_0/0.2)]",
                      "transition-[transform,box-shadow] duration-300 ease-[cubic-bezier(.34,1.56,.64,1)] group-hover/sl:scale-105 group-data-[dragging]/sl:scale-110",
                      "group-has-[:focus-visible]/th:shadow-[0_0_0_4px_color-mix(in_srgb,var(--color-brand)_30%,transparent),0_1px_3px_rgb(0_0_0/0.2)]",
                    )}
                  />
                  <span
                    aria-hidden
                    className={cx(
                      "pointer-events-none absolute bottom-[26px] left-1/2 origin-bottom -translate-x-1/2 whitespace-nowrap rounded-lg bg-ink px-2 py-1 text-[12px] font-medium tabular-nums text-paper",
                      "shadow-[0_4px_12px_-4px_rgb(0_0_0/0.4)] transition-[scale,opacity] duration-300 ease-[cubic-bezier(.34,1.56,.64,1)]",
                      "scale-75 opacity-0 group-hover/sl:scale-100 group-hover/sl:opacity-100 group-data-[dragging]/sl:scale-100 group-data-[dragging]/sl:opacity-100 group-has-[:focus-visible]/th:scale-100 group-has-[:focus-visible]/th:opacity-100",
                    )}
                  >
                    {format(v)}
                    <span className="absolute left-1/2 top-full -translate-x-1/2 border-x-[5px] border-t-[5px] border-x-transparent border-t-ink" />
                  </span>
                </S.Thumb>
              </S.Track>
            </S.Control>
          </S.Root>
        </div>
      );
    }
    
    export default Slider;
    
  4. Or ask your AI agent

    Paste this into Claude Code, Cursor, Codex, v0 or Lovable. Fill in the last line. More in Prompts.

    Add the Astabyte UI Slider component to my project.
    
    Single value with a bubble, ticks and keys.
    It includes: single, steps, ticks, keys.
    
    Install it (Radix build):
    ```sh
    pnpm dlx asta-ui@latest add slider --radix
    ```
    Or with the shadcn CLI:
    ```sh
    pnpm dlx shadcn@latest add https://ui.astabyte.com/r/slider.json
    ```
    If the project isn't set up yet, run `pnpm dlx asta-ui@latest init --radix` first: it runs `shadcn init` when needed and adds the @astabyte registry (Next.js, Vite, React Router, Astro, TanStack Start; Tailwind CSS v4).
    
    Import it like this:
    ```tsx
    import { Slider } from "@/components/astabyte/slider";
    ```
    
    Usage example and props: https://ui.astabyte.com/slider
    Full catalogue for context: https://ui.astabyte.com/llms.txt
    
    Rules:
    - Keep the colour tokens (paper, paper-2, ink, ink-2, mute, brand). Don't restyle with hard-coded colours; rebrand by changing the token values in the CSS.
    - Keep the motion as it is: transform and opacity only, and the prefers-reduced-motion fallback.
    - Keep keyboard, focus and ARIA behaviour (interactive pieces are built on Radix primitives from the radix-ui package).
    - Stay on Radix: install other Astabyte UI components with `--radix` so the project uses one primitive library.
    - Use fake data and fake brand names in examples.
    
    Use it to: [describe where it goes and what it should do]
    Add the Astabyte UI Slider component to my project.
    
    Single value with a bubble, ticks and keys.
    It includes: single, steps, ticks, keys.
    
    Install it (Base UI build):
    ```sh
    pnpm dlx asta-ui@latest add slider
    ```
    Or with the shadcn CLI:
    ```sh
    pnpm dlx shadcn@latest add https://ui.astabyte.com/r/base/slider.json
    ```
    If the project isn't set up yet, run `pnpm dlx asta-ui@latest init` first: it runs `shadcn init` when needed and adds the @astabyte registry (Next.js, Vite, React Router, Astro, TanStack Start; Tailwind CSS v4).
    
    Import it like this:
    ```tsx
    import { Slider } from "@/components/astabyte/slider";
    ```
    
    Usage example and props: https://ui.astabyte.com/slider
    Full catalogue for context: https://ui.astabyte.com/llms.txt
    
    Rules:
    - Keep the colour tokens (paper, paper-2, ink, ink-2, mute, brand). Don't restyle with hard-coded colours; rebrand by changing the token values in the CSS.
    - Keep the motion as it is: transform and opacity only, and the prefers-reduced-motion fallback.
    - Keep keyboard, focus and ARIA behaviour (interactive pieces are built on Base UI primitives from the @base-ui/react package).
    - Stay on Base UI: install other Astabyte UI components without `--radix` so the project uses one primitive library.
    - Use fake data and fake brand names in examples.
    
    Use it to: [describe where it goes and what it should do]
  5. Or use plain HTML + CSS

    Not on React? Use it from Django, Rails, Laravel, Go html/template, htmx or a static page. Load asta.css and asta.js once (the head.html tab), then paste the markup. The ui.astabyte.com/cdn link is temporary: pin a version from jsDelivr for production. More in HTML + CSS.

    <div class="asta-slider" style="max-width:360px">
      <input type="range" class="asta-slider-input" name="volume" min="0" max="100" step="25" value="50" aria-label="Volume">
      <div class="asta-slider-ticks" aria-hidden="true">
        <span class="asta-slider-tick" style="left:0%"></span><span class="asta-slider-tick" style="left:25%"></span><span class="asta-slider-tick" style="left:50%"></span><span class="asta-slider-tick" style="left:75%"></span><span class="asta-slider-tick" style="left:100%"></span>
      </div>
    </div>

Usage

The demo above, as code.

"use client";

import { useState } from "react";
import { Slider } from "@/components/astabyte/slider";

export default function SliderDemo() {
  const [budget, setBudget] = useState(24);
  const [weeks, setWeeks] = useState(6);

  return (
    <div className="flex min-h-[420px] w-full items-center justify-center p-6 sm:p-10">
      <div className="flex w-full max-w-md flex-col gap-8">
        <div>
          <div className="flex items-baseline justify-between">
            <span id="budget" className="text-[13px] font-medium text-ink">
              Budget
            </span>
            <span className="text-[13px] tabular-nums text-mute">${budget}k</span>
          </div>
          <Slider aria-labelledby="budget" value={budget} onValueChange={setBudget} min={5} max={100} format={(v) => `$${v}k`} />
        </div>
        <div>
          <div className="flex items-baseline justify-between">
            <span id="weeks" className="text-[13px] font-medium text-ink">
              Timeline
            </span>
            <span className="text-[13px] tabular-nums text-mute">{weeks} weeks</span>
          </div>
          <Slider aria-labelledby="weeks" value={weeks} onValueChange={setWeeks} min={2} max={12} step={2} ticks format={(v) => `${v} wk`} />
        </div>
        <p className="text-[12.5px] text-mute">Drag, click the track, or focus a thumb and use the arrow keys, Page Up/Down, Home and End.</p>
      </div>
    </div>
  );
}