Skip to content

Fields

Slider

A native range control for a value along a track.

When to use it: Use it when a range is easy to understand by position.

On this page

Example

Volume: 40%

Slider.tsxtsx
import { useState } from "react";
import { Label, Slider } from "@comp0/react";

export function Example() {
  const [value, setValue] = useState(40);

  return (
    <div className="flex max-w-xs flex-col gap-2">
      <Label
        className="text-base font-medium text-zinc-900 sm:text-sm dark:text-zinc-100"
        htmlFor="volume"
      >
        Volume
      </Label>
      <Slider
        aria-label="Volume"
        className="w-full accent-teal-600 dark:accent-teal-400"
        id="volume"
        name="volume"
        value={value}
        onChange={setValue}
      />
      <p className="text-base text-zinc-600 tabular-nums sm:text-sm dark:text-zinc-400">
        Volume: {value}%
      </p>
    </div>
  );
}

Anatomy

Dashed frames are invisible state providers; shaded shapes own real DOM. Numbered pins match the list below.

A wireframe sketch of the assembled component. Each numbered marker matches a part in the list that follows.

  1. Slider

    Native range input. Owns a DOM element.

Step by step

  1. 1

    Add the main part

    Add Slider with a visible label.

  2. 2

    Add the supporting parts

    Set min, max, and step to make the range honest.

  3. 3

    Make the behavior clear

    Name it if the chosen value belongs in a form.

    Exampletsx
    <Slider name="volume" defaultValue={30} min={0} max={100} />

Keyboard

Increases the value.
Decreases the value.
Home
Moves to minimum.
End
Moves to maximum.

Forms and accessibility

Submits as a named native range input.

Accessibility checklist

  • Give the range a visible name.
  • Make minimum, maximum, and current meaning understandable.
  • Prefer NumberField when exact typing matters more than quick adjustment.

API reference

Importtsx
import { Slider } from "@comp0/react";

Slider

DOM element

Native range input.

PropTypeDescription
valuenumberControlled position.
defaultValuenumberInitial position.
onChange(value: number) => voidReceives the next position.
min / max / stepnumberRange bounds; default 0, 100, and 1.
namestringSubmission name for the value.
disabledbooleanDisables the slider.

Style hooks

Attributes that appear while a state is true. Target them with Tailwind data variants such as data-open:bg-zinc-100, or with any CSS selector.

[data-disabled]

on Slider

The slider is disabled.

Keep exploring