Slider

A number or range on a track, set by drag, keyboard or agent.

Source
$npx @dinachi/cli@latest add slider
Volume
50
defaultValue
step
SliderValue
disabled

Usage

import {
  Slider,
  SliderControl,
  SliderLabel,
  SliderRange,
  SliderThumb,
  SliderTrack,
} from "@/components/ui/slider"

export function VolumeSlider() {
  return (
    <Slider defaultValue={50}>
      <SliderLabel>Volume</SliderLabel>
      <SliderControl>
        <SliderTrack>
          <SliderRange />
          <SliderThumb />
        </SliderTrack>
      </SliderControl>
    </Slider>
  )
}

Examples

Volume
One thumb with a label.
import { Slider, SliderLabel, SliderControl, SliderTrack, SliderRange, SliderThumb } from '@/components/ui/slider';

export function Example() {
  return (
    <Slider defaultValue={50} className="w-full max-w-sm">
      <SliderLabel>Volume</SliderLabel>
      <SliderControl>
        <SliderTrack>
          <SliderRange />
          <SliderThumb />
        </SliderTrack>
      </SliderControl>
    </Slider>
  );
}

Notes

Single value or range

A number gives one thumb and an array gives a range. In a range, onValueChange receives an array, and each SliderThumb needs its index.

Naming a range

Without a SliderLabel, the agent lists a range under its first thumb's aria-label. Add a SliderLabel or an aria-label on Slider to name the whole control.

API

valuenumber | number[]
Controlled value. An array gives one thumb per entry.
defaultValuenumber | number[] = min
Starting value when uncontrolled.
onValueChange
agent
(value, details) => void
Runs on every move, for people and agents. details.cancel() refuses the change.
onValueCommitted
agent
(value, details) => void
Runs when a drag ends, and once after an agent's change.
min
agent
number = 0
The lowest value. Agents are refused below it.
max
agent
number = 100
The highest value. Agents are refused above it.
step
agent
number = 1
The increment. Agent values snap to it.
largeStepnumber = 10
The increment for Page Up, Page Down and Shift with an arrow.
minStepsBetweenValuesnumber = 0
Keeps range thumbs this many steps apart.
orientation"horizontal" | "vertical" = "horizontal"
The direction of the track.
disabled
agent
boolean = false
Blocks people and agents.
formatIntl.NumberFormatOptions
How SliderValue and the announced text format the number.
namestring
Submits the value with a form.

Agent

Inside an AgentProvider, it appears in agent.snapshot() as slider.

Reads

label

The SliderLabel or the slider's aria-label, else the first thumb's aria-label.

value

The number, or [low, high] for a range.

min / max / step

The bounds and the increment.

disabled

True when the slider is blocked.

Actions

setValue(60)

Snaps to the step. Rejects values out of range, values that are not numbers, and a range sent out of order or with the wrong count.

setValue([20, 80])

Sets both ends of a range at once.

An agent's change runs through onValueChange like anyone's. isAgentChange(details) is true for it; details.cancel() refuses it and the agent is told the app refused. A disabled slider refuses on its own.

await agent.act({
  steps: [{ ref: "Volume", action: "setValue", args: [60] }],
})

Accessibility

  • Each thumb holds a range input that announces its value and bounds.
  • Name it with a SliderLabel or an aria-label per thumb. The agent reads the same name.
  • In a range, label each thumb, for example "Minimum price" and "Maximum price".
  • Reduced motion turns off the thumb's grow on drag.