Slider
A control that lets users set a value or a range of values by dragging a thumb along a track.
A slider lets users set a number by dragging a thumb along a track, and the filled part of the track shows what they have set. Users also change the value with a press on the track or with the arrow keys. The slider takes its values as an array with one entry per thumb, also when it has a single thumb. It has no label of its own, so the examples name it with a FieldTitle that aria-labelledby refers to.
Usage
Examples
Range
Filters often ask for a span instead of a single figure, such as the products that sell between two and five cases a week. When defaultValue holds two entries, the slider offers two thumbs, and users set the lower and the upper limit of the span on one track.
Multiple Thumbs
A single scale can carry several thresholds, for example the stock levels at which an item counts as low, sufficient or overstocked. Each further entry in defaultValue adds a thumb, so users place every threshold on the same scale. During a drag, one thumb pushes the next one along, and the keyboard stops a thumb at its neighbor, so the thresholds always keep their order.
Minimum and Maximum
Many values only make sense within natural limits, such as the shelf life of a product in days or the capacity of a shelf in cases. Through min and max the track covers just that span, so users cannot set a value outside it, and the full length of the track serves the values that matter.
Step
Goods often move in fixed packing units, such as a case of twelve bottles. Set to the size of such a unit, step makes the thumb snap to whole units counted from min, and each press of an arrow key moves it by one unit. Users therefore never set a quantity that would leave a case half full.
Minimum Distance Between Thumbs
A span that shrinks to a single value loses its meaning, for example a price band whose lower and upper limit coincide. With minStepsBetweenValues, neighboring thumbs keep at least that many steps between them, whether users drag a thumb or move it with the keyboard, so the span never narrows below a useful width.
Center Alignment
Where a slider lines up with a scale or a chart axis beside it, every thumb should point at the exact place of its value, the ends included. The value "center" of thumbAlignment places the center of a thumb on the end of the track at min and max, so its position matches the scale along the whole length.
Vertical
In a narrow side panel or next to a column of figures, height is often easier to spare than width. With orientation set to "vertical", users set the value along the height, with min at the bottom, so a drag upward raises it. Its height comes from the element around it, and it never falls below 10rem.
Controlled
Sometimes the application needs the values while users are still deciding, for example to update a forecast during a drag or to set the slider from a preset. Through value and onValueChange it holds the values itself, so other parts of the interface can display or change them. That matters all the more since the slider shows no number of its own.
15% off the shelf price.
Disabled
A value can stay fixed for a while, for example while a central rule governs it or until users have chosen the store it applies to. Once disabled is set, the slider accepts no interaction, exposes its state to assistive technology and is left out when the form is submitted, while users still see the value it holds.
API Reference
| Prop | value |
|---|---|
| Type | number | readonly number[] |
| Default | – |
| Description | Sets the values of the slider and makes it controlled. Pass an array with one entry per thumb, also for a single thumb. With a plain number, the slider renders two thumbs for the one value. Update it in onValueChange. |
| Prop | defaultValue |
|---|---|
| Type | number | readonly number[] |
| Default | – |
| Description | Sets the values at first render. The slider then tracks its values itself. Pass an array with one entry per thumb, also for a single thumb. Without value and defaultValue, the slider renders two thumbs at min. |
| Prop | onValueChange |
|---|---|
| Type | (value: number | readonly number[], eventDetails: SliderRootChangeEventDetails) => void |
| Default | – |
| Description | Runs when users change a value, also on every move during a drag. It receives the values that follow: an array for several thumbs, and a number or an array with one entry for a single thumb. |
| Prop | onValueCommitted |
|---|---|
| Type | (value: number | readonly number[], eventDetails: SliderRootCommitEventDetails) => void |
| Default | – |
| Description | Runs when users have finished a change: when they release the pointer after a drag or a press on the track, and after every change with the keyboard. It doesn't run when the values stayed the same. It receives the values in the same form as onValueChange. |
| Prop | min |
|---|---|
| Type | number |
| Default | 0 |
| Description | Sets the lowest value that users can set. It must be lower than max. |
| Prop | max |
|---|---|
| Type | number |
| Default | 100 |
| Description | Sets the highest value that users can set. It must be higher than min. |
| Prop | step |
|---|---|
| Type | number |
| Default | 1 |
| Description | Sets the distance between two values that users can set. The steps count from min, and decimal numbers work as well. Choose a step that divides the distance between min and max evenly. |
| Prop | largeStep |
|---|---|
| Type | number |
| Default | 10 |
| Description | Sets how far a thumb moves when users press Page Up or Page Down, or an arrow key together with Shift. |
| Prop | minStepsBetweenValues |
|---|---|
| Type | number |
| Default | 0 |
| Description | Sets how many steps two neighboring thumbs keep between them. It has an effect only on a slider with several thumbs. |
| Prop | thumbCollisionBehavior |
|---|---|
| Type | "push" | "swap" | "none" |
| Default | "push" |
| Description | Sets what happens when users drag one thumb into another. With "push", the dragged thumb pushes the other one along, and the pushed thumb stays where it ends up. With "swap", the dragged thumb moves past the other one. With "none", the dragged thumb stops at the other one. With the keyboard, a thumb always stops at its neighbor. |
| Prop | thumbAlignment |
|---|---|
| Type | "center" | "edge" | "edge-client-only" |
| Default | "edge" |
| Description | Sets where a thumb sits when its value is min or max. With "edge", the thumb stays inside the track, and its edge meets the end of the track. With "center", the center of the thumb sits on the end of the track, so that half of the thumb reaches beyond it. "edge-client-only" places the thumb like "edge", but only after hydration on the client, which reduces the bundle size. |
| Prop | orientation |
|---|---|
| Type | "horizontal" | "vertical" |
| Default | "horizontal" |
| Description | Sets the direction of the slider. A horizontal slider fills the width of its container. A vertical slider has min at the bottom, fills the height of its container and is at least 10rem tall. |
| Prop | disabled |
|---|---|
| Type | boolean |
| Default | false |
| Description | Prevents users from changing the values and exposes the disabled state to assistive technology. A disabled slider is not submitted with the form. |
| Prop | format |
|---|---|
| Type | Intl.NumberFormatOptions |
| Default | – |
| Description | Sets how the slider formats the value that it announces to assistive technology, for example as a currency. The slider itself shows no number. |
| Prop | locale |
|---|---|
| Type | Intl.LocalesArgument |
| Default | – |
| Description | Sets the locale that format uses. Without it, the slider uses the locale of the runtime. |
| Prop | name |
|---|---|
| Type | string |
| Default | – |
| Description | Identifies the slider when the form is submitted. Every thumb submits its value under this name. |
| Prop | form |
|---|---|
| Type | string |
| Default | – |
| Description | Sets the ID of the form that the slider belongs to. Set it when the slider is rendered outside of that form. |
| Prop | aria-labelledby |
|---|---|
| Type | string |
| Default | – |
| Description | Sets the ID of the element that names the slider. The slider passes it on to every thumb, so that assistive technology announces the name together with the value. |
| Prop | render |
|---|---|
| Type | ReactElement | render function |
| Default | – |
| Description | Replaces the native element or composes the slider with another component. |