A slider allows a user to select one or more values within a range.
Vanilla CSS theme
--tint CSS variable used by the Vanilla CSS examples.formatOptions
Value
Use the value or defaultValue prop to set the slider's value. The onChange event is called as the user drags, and onChangeEnd is called when the thumb is released.
onChange value: 25 onChangeEnd value: 25
import {Slider} from './Slider';
import {useState} from 'react';
function Example() {
let [currentValue, setCurrentValue] = useState(25);
let [finalValue, setFinalValue] = useState(currentValue);
return (
<>
<Slider
label="Cookies to buy"
value={currentValue}
onChange={setCurrentValue}
onChangeEnd={setFinalValue} />
<pre style={{fontSize: 12}}>
onChange value: {currentValue}{'\n'}
onChangeEnd value: {finalValue}
</pre>
</>
);
}
Multi-thumb
Set the value or defaultValue to an array of numbers to render multiple thumbs. Each thumb should have an aria-label to describe it for assistive technologies (provided via thumbLabels here).
import {Slider} from './Slider';
<Slider
label="Range"
defaultValue={[30, 60]}
thumbLabels={['start', 'end']} />
Value scale
By default, slider values are percentages between 0 and 100. Use the minValue, maxValue, and step props to set the allowed values. Steps are calculated starting from the minimum. For example, if minValue={2}, and step={3}, the valid step values would be 2, 5, 8, 11, etc.
Examples
API
<Slider>
<Label />
<SliderOutput />
<SliderTrack>
<SliderThumb />
<SliderThumb>
<Label />
</SliderThumb>
</SliderTrack>
</Slider>
Slider
A slider allows a user to select one or more values within a range.
| Name | Type | Default |
|---|---|---|
orientation | Orientation | Default: 'horizontal'
|
| The orientation of the Slider. | ||
isDisabled | boolean | Default: — |
| Whether the whole Slider is disabled. | ||
children | ChildrenOrFunction | Default: — |
| The children of the component. A function may be provided to alter the children based on component state. | ||
value | T | Default: — |
| The current value (controlled). | ||
defaultValue | T | Default: — |
| The default value (uncontrolled). | ||
onChange | | Default: — |
| Handler that is called when the value changes. | ||
onChangeEnd | | Default: — |
| Fired when the slider stops moving, due to being let go. | ||
formatOptions | Intl.NumberFormatOptions | Default: — |
| The display format of the value label. | ||
Default className: react-aria-Slider
| Render Prop | CSS Selector |
|---|---|
orientation | CSS Selector: [data-orientation="horizontal | vertical"]
|
| The orientation of the slider. | |
isDisabled | CSS Selector: [data-disabled]
|
| Whether the slider is disabled. | |
state | CSS Selector: — |
| State of the slider. | |
SliderOutput
A slider output displays the current value of a slider as text.
| Name | Type | |
|---|---|---|
children | ChildrenOrFunction | |
| The children of the component. A function may be provided to alter the children based on component state. | ||
Default className: react-aria-SliderOutput
| Render Prop | CSS Selector |
|---|---|
orientation | CSS Selector: [data-orientation="horizontal | vertical"]
|
| The orientation of the slider. | |
isDisabled | CSS Selector: [data-disabled]
|
| Whether the slider is disabled. | |
state | CSS Selector: — |
| State of the slider. | |
SliderTrack
A slider track is a container for one or more slider thumbs.
| Name | Type | |
|---|---|---|
children | ChildrenOrFunction | |
| The children of the component. A function may be provided to alter the children based on component state. | ||
Default className: react-aria-SliderTrack
| Render Prop | CSS Selector |
|---|---|
isHovered | CSS Selector: [data-hovered]
|
| Whether the slider track is currently hovered with a mouse. | |
orientation | CSS Selector: [data-orientation="horizontal | vertical"]
|
| The orientation of the slider. | |
isDisabled | CSS Selector: [data-disabled]
|
| Whether the slider is disabled. | |
state | CSS Selector: — |
| State of the slider. | |
SliderThumb
A slider thumb represents an individual value that the user can adjust within a slider track.
| Name | Type | Default |
|---|---|---|
inputRef | RefObject | Default: — |
| A ref for the HTML input element. | ||
isDisabled | boolean | Default: — |
| Whether the Thumb is disabled. | ||
index | number | Default: 0
|
| Index of the thumb within the slider. | ||
children | ChildrenOrFunction | Default: — |
| The children of the component. A function may be provided to alter the children based on component state. | ||
Default className: react-aria-SliderThumb
| Render Prop | CSS Selector |
|---|---|
state | CSS Selector: — |
| State of the slider. | |
isDragging | CSS Selector: [data-dragging]
|
| Whether this thumb is currently being dragged. | |
isHovered | CSS Selector: [data-hovered]
|
| Whether the thumb is currently hovered with a mouse. | |
isFocused | CSS Selector: [data-focused]
|
| Whether the thumb is currently focused. | |
isFocusVisible | CSS Selector: [data-focus-visible]
|
| Whether the thumb is keyboard focused. | |
isDisabled | CSS Selector: [data-disabled]
|
| Whether the thumb is disabled. | |