Slider
A value or a range picked on a scale, with the keyboard or by dragging.
A slider picks a value where the position on a scale matters more than the exact number, such as a share or a range of ages. With two values it picks a range. When the exact number matters, pair it with text that shows the value, or use a NumberField.
40% of the contributions
From 25 to 65 years
// apps/docs/examples/ui/slider.tsx'use client';
import {useState} from 'react';
import { Field, FieldDescription, FieldLabel,} from '@workspace/active-ui/components/primitives/field';import {Slider} from '@workspace/active-ui/components/primitives/slider';
/** * A value picked on a scale: one thumb, a range with two thumbs and a step, * and a disabled slider. The value is an array, one number per thumb; the * current value is shown as text, because a slider alone does not tell the * reader the exact number. */export default function SliderExample() { const [share, setShare] = useState([40]); const [range, setRange] = useState([25, 65]); return ( <div className="grid w-full max-w-md gap-8"> <Field> <FieldLabel id="slider-share-label">Equity share</FieldLabel> <Slider value={share} onValueChange={(value) => setShare(value as number[])} aria-labelledby="slider-share-label" /> <FieldDescription>{share[0]}% of the contributions</FieldDescription> </Field> <Field> <FieldLabel id="slider-age-label">Age range</FieldLabel> <Slider value={range} min={18} max={67} step={1} onValueChange={(value) => setRange(value as number[])} aria-labelledby="slider-age-label" /> <FieldDescription> From {range[0]} to {range[1]} years </FieldDescription> </Field> {/* The label keeps full contrast: it names a control that is not associated with it by `for`, so it is not exempt as disabled text. */} <Field> <FieldLabel id="slider-fixed-label">Employer share (fixed)</FieldLabel> <Slider defaultValue={[50]} disabled aria-labelledby="slider-fixed-label" /> </Field> </div> );}Usage#
import {Slider} from '@workspace/active-ui/components/primitives/slider';
<Slider value={share} onValueChange={(value) => setShare(value as number[])} aria-labelledby="share-label"/>;Values and thumbs#
One thumb per value. A number (
40) or an array of one ([40]) gives one thumb; an array of two ([25, 65]) a range.onreports the value in the shape you passed.Value Change Without
valueordefaultit renders two thumbs, atValue minandmax: a range.min(default0),max(default100) andstep(default1) set the scale.onreports while the reader drags;Value Change ononce, when they let go. Use the second for work that should not run on every step, such as a request.Value Committed
What your application provides#
A label:
aria-pointing at visible text, orlabelledby aria-. Both reach the input of every thumb; a range has one name for both thumbs.label The value as text, when the reader needs the number. The slider announces it to assistive technology (
aria-), but shows no number itself.valuenow locale, andformatfor a value that is more than a plain number (a percentage, a currency). Base UI formats the announced value withIntl.; withoutNumber Format localeit uses the browser's language, not the one your application presents.
Accessibility and keyboard#
Each thumb is a tab stop. The arrow keys move it by
step;Page UpandPage Downby a larger step (large, defaultStep 10);HomeandEndtominandmax.Each thumb is a
role="slider"input with its minimum, maximum and value.When two thumbs meet while dragging, the one you drag pushes the other along (
thumb, defaultCollision Behavior push;swapandnoneare the alternatives).
API#
Import from @workspace/
All props come from the Base UI part this component is built on.
Props
- Type
number | readonly number[]
The value of the slider. For range sliders, provide an array with one value per thumb.
- Type
number | readonly number[]
The uncontrolled value of the slider when it's initially rendered.
To render a controlled slider, use the value prop instead.
- Type
boolean- Default
false
Whether the slider should ignore user interaction.
- Type
string
Identifies the form that owns the slider inputs. Useful when the slider is rendered outside the form.
- Type
Intl.Number Format Options
Options to format the value.
- Type
number- Default
10
The granularity with which the slider can step through values when using Page Up/Page Down or Shift + Arrow Up/Arrow Down.
- Type
Intl.Locales Argument
The locale used by Intl. when formatting the value. Defaults to the user's runtime locale.
- Type
number- Default
100
The maximum allowed value of the slider. Should not be equal to min.
- Type
number- Default
0
The minimum allowed value of the slider. Should not be equal to max.
- Type
number- Default
0
The minimum steps between values in a range slider.
- Type
string
Identifies the field when a form is submitted.
- Type
Orientation- Default
'horizontal'
The component orientation.
- Type
number- Default
1
The granularity with which the slider can step through values. (A "discrete" slider.) The min prop serves as the origin for the valid values. We recommend (max - min) to be evenly divisible by the step.
- Type
"center""edge""edge-client- only" - Default
'center'
How the thumb(s) are aligned relative to Slider. when the value is at min or max:
center: The center of the thumb is aligned with the control edgeedge: The thumb is inset within the control such that its edge is aligned with the control edgeedge-: Same asclient- only edgebut renders after React hydration on the client, reducing bundle size in return
- Type
"none""push""swap"- Default
'push'
Controls how thumbs behave when they collide during pointer interactions.
'push'(default): Thumbs push each other without restoring their previous positions when dragged back.'swap': Thumbs swap places when dragged past each other.'none': Thumbs cannot move past each other; excess movement is ignored.
- Type
ComponentRender Fn<โฆ> ReactElement<โฆ>
Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a React or a function that returns the element to render.
Events
Called by the component when something happens.
- Type
(value, eventDetails) => void
Callback function that is fired when the slider's value changed. Receives the new value as the first argument; the originating event is available as event. The value is also reflected on event for form integration.
The event indicates what triggered the change:
'input-when the hidden range input emits a change event (for example, via form integration)change' 'track-when the control track is pressedpress' 'drag'while dragging a thumb'keyboard'for keyboard input'none'when the change is triggered without a specific interaction
- Type
(value, eventDetails) => void
Callback function that is fired when a value change is committed. Does not fire if the value did not change, or if the change was canceled. Warning: This is a generic event, not a change event.
The event indicates what triggered the commit:
'drag'while dragging a thumb'track-when the control track is pressedpress' 'keyboard'for keyboard input'input-when the hidden range input emits a change event (for example, via form integration)change' 'none'when the commit occurs without a specific interaction
Other props, such as the standard attributes of the underlying element, className and ref, are passed through.
This page adapts the Slider documentation of shadcn/ui (MIT licence) to this package; see Third-party notices.