Skip to content

Slider

Picks a number, or a range of numbers, by dragging a thumb along a track.

Installation

pnpm dlx shadcn@latest add @silkware/slider

To copy the component by hand, install its dependencies:

pnpm add react-aria-components cn

Then copy the source into components/ui/slider.tsx:

components/ui/slider.tsx
"use client"
import { cn } from "cn"import {  SliderFill,  Slider as SliderPrimitive,  SliderThumb,  SliderTrack,  type SliderProps as SliderPrimitiveProps,} from "@animate-ui/react/primitives/react-aria"
type SliderValue = number | number[]type SliderProps<T extends SliderValue = SliderValue> = Omit<  SliderPrimitiveProps<T>,  "className"> & {  className?: string}
function Slider<T extends SliderValue = SliderValue>({  className,  ...props}: SliderProps<T>) {  return (    <SliderPrimitive      className={cn(        "group relative flex w-full touch-none items-center select-none data-disabled:opacity-50 data-vertical:h-full data-vertical:min-h-40 data-vertical:w-auto data-vertical:flex-col",        className      )}      data-slot="slider"      {...props}    >      {({ state }) => {        return (          <>            <SliderTrack              data-slot="slider-track"              className="relative grow overflow-hidden rounded-full bg-muted select-none data-horizontal:h-1.5 data-horizontal:w-full data-vertical:h-full data-vertical:w-1.5"            >              <SliderFill                data-slot="slider-range"                className="absolute bg-primary select-none data-horizontal:h-full data-vertical:w-full"              />            </SliderTrack>            {state.values.map((_, index) => (              <SliderThumb                data-slot="slider-thumb"                key={index}                index={index}                aria-labelledby={props["aria-labelledby"]}                className="block size-4 shrink-0 rounded-full border border-primary bg-white shadow-sm ring-ring/50 transition-[color,box-shadow] select-none group-data-horizontal:top-[50%] group-data-vertical:left-[50%] hover:ring-4 focus-visible:ring-4 focus-visible:outline-hidden disabled:pointer-events-none disabled:opacity-50"              />            ))}          </>        )      }}    </SliderPrimitive>  )}
export { Slider }

Usage

import { Slider } from "@/components/ui/slider"
<Slider aria-label="Production line speed" defaultValue={60} />

The range runs from minValue to maxValue, which default to 0 and 100, in steps of step, which defaults to 1. A single number gives one thumb, and an array gives one thumb for each number. onChange receives a number or an array in the same shape, on every move, and onChangeEnd receives it once when the drag ends.

The slider renders no label or value text. Name it with aria-label, or with aria-labelledby that points to a FieldTitle or another element, and show the value yourself where people need to read it.

Examples

Range

An array of two numbers gives a range with two thumbs. A thumb cannot pass the other. More numbers give more thumbs.

Controlled

Hold the value in state with value and onChange, and show it beside the slider. The value is always an array here, so cast the argument of onChange.

Order value

From 2000 to 8000 EUR.

Format

formatOptions takes Intl.NumberFormat options. It sets how the value is read out by assistive technology, for example as a percentage. Keep the visible text in the same format.

Volume discount

15% off orders over 500 units.

Vertical

orientation="vertical" stands the slider upright. Give it a height with a class.

Disabled

isDisabled dims the slider and ignores input. Say why in the label or the description.

Accessibility

KeyAction
RightIncreases the value by one step.
UpIncreases the value by one step.
LeftDecreases the value by one step.
DownDecreases the value by one step.
Page UpIncreases the value by a larger step.
Page DownDecreases the value by a larger step.
HomeSets the value to the minimum.
EndSets the value to the maximum.
TabMoves focus to the next thumb, then to the next control.

React Aria provides:

  • A group for the slider and a native range input behind each thumb, with the slider role and the current, minimum and maximum values.
  • The keyboard behaviour above, and dragging with mouse, touch and pen, including a press on the track.
  • Right-to-left support, which reverses the direction of the arrow keys.
  • Spoken values that follow formatOptions.

You provide:

  • A name for the slider through aria-label or aria-labelledby. The component passes aria-labelledby on to each thumb, so the thumbs are named either way. The thumbs of a range share that name and differ by their values; name the range in the label, such as "Order value range".
  • The current value as text near the slider, because the track alone does not show a number.
  • A step that matches the precision the task needs.

Guidelines

When to use

  • To choose a value in a bounded range where a rough position is enough: a discount, a speed, a safety margin.
  • To choose a range, such as a minimum and a maximum.

When not to use

  • For an exact number. Use Input with type="number".
  • For a very wide range, or one without limits. Use an input.
  • For a choice from a few labelled options. Use Radio Group or Toggle Group.
  • To show progress. Use Progress.

Content

  • Name what the slider sets and its unit: "Safety stock, as days of demand".
  • State the limits in the description when they are not obvious.
  • Show the current value next to the slider, in the same unit and format.
  • Pick a step that people can hit without precision: 5 or 10 rather than 1 on a wide range.

Precision

Safety stock, as days of demand

Between 5 and 30 days.

Do

Use a slider for a bounded range where a rough value is enough.

Quantity to order

Between 0 and 100,000 units.

Don’t

Don’t use a slider for an exact quantity over a wide range. A drag cannot hit it. Use an input.

API reference

Slider

Renders the React Aria Slider with a track, a filled range and one thumb for each value. It accepts every React Aria Slider prop, except that className is a string.

PropTypeDefaultDescription
value?number | number[]NoneThe value, for a controlled slider. An array gives one thumb for each number.
defaultValue?number | number[]NoneThe value at the start, for an uncontrolled slider.
onChange?(value: number | number[]) => voidNoneCalled on every change while the thumb moves.
onChangeEnd?(value: number | number[]) => voidNoneCalled when the person stops dragging, and after each key press.
minValue?number0The smallest value.
maxValue?number100The largest value.
step?number1The size of one step.
orientation?"horizontal" | "vertical""horizontal"The direction of the track.
formatOptions?Intl.NumberFormatOptionsNoneHow the value is formatted when it is read out.
isDisabled?booleanfalseDims the slider and ignores input.
aria-label?stringNoneThe name of the slider when there is no visible label.
aria-labelledby?stringNoneThe id of the element that names the slider.
className?stringNoneClasses merged after the base classes.