RangeSliders allow users to quickly select a subset range. They should be used when the upper and lower bounds to the range are invariable.
Value
Use the value or defaultValue prop to set the RangeSlider's value. The value is an object with start and end properties. The onChange event is called as the user drags, and onChangeEnd is called when the thumb is released.
onChange value: {"start":25,"end":75}
onChangeEnd value: {"start":25,"end":75}import {RangeSlider} from '@react-spectrum/s2/RangeSlider';
import {style} from '@react-spectrum/s2/style' with {type: 'macro'};
import {useState} from 'react';
function Example() {
let [currentValue, setCurrentValue] = useState({start: 25, end: 75});
let [finalValue, setFinalValue] = useState(currentValue);
return (
<>
<RangeSlider
label="Cookies to buy"
value={currentValue}
onChange={setCurrentValue}
onChangeEnd={setFinalValue} />
<pre className={style({font: 'body'})}>
onChange value: {JSON.stringify(currentValue)}{'\n'}
onChangeEnd value: {JSON.stringify(finalValue)}
</pre>
</>
);
}
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.
Editable
Place a <SliderInput /> inside <RangeSlider> when users should be able to precisely input start and end values within a text field alongside the slider.
API
<RangeSlider>
<SliderInput />
</RangeSlider>
RangeSlider
| Name | Type | Default |
|---|---|---|
children | ReactNode | Default: — |
An optional | ||
size | 'S'
| 'M'
| 'L'
| 'XL' | Default: 'M'
|
The size of the Slider. | ||
isEmphasized | boolean | Default: — |
Whether the Slider should be displayed with an emphasized style. | ||
trackStyle | 'thin' | 'thick' | Default: 'thin'
|
The style of the Slider's track. | ||
thumbStyle | 'default' | 'precise' | Default: 'default'
|
The style of the Slider's thumb. | ||
isDisabled | boolean | Default: — |
Whether the whole Slider is disabled. | ||
styles | StylesProp | Default: — |
Spectrum-defined styles, returned by the | ||
value | RangeValue | Default: — |
The current value (controlled). | ||
defaultValue | RangeValue | 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. | ||
label | ReactNode | Default: — |
The content to display as the label. | ||
labelPosition | LabelPosition | Default: 'top'
|
The label's overall position relative to the element it is labeling. | ||
labelAlign | Alignment | Default: 'start'
|
The label's horizontal alignment relative to the element it is labeling. | ||
contextualHelp | ReactNode | Default: — |
A ContextualHelp element to place next to the label. | ||
minValue | number | Default: 0
|
The slider's minimum value. | ||
maxValue | number | Default: 100
|
The slider's maximum value. | ||
step | number | Default: 1
|
The slider's step value. | ||
startName | string | Default: — |
The name of the start input element, used when submitting an HTML form. See MDN. | ||
endName | string | Default: — |
The name of the end input element, used when submitting an HTML form. See MDN. | ||
form | string | Default: — |
The | ||
id | string | Default: — |
The element's unique identifier. See MDN. | ||
aria-label | string | Default: — |
Defines a string value that labels the current element. | ||
aria-labelledby | string | Default: — |
Identifies the element (or elements) that labels the current element. | ||
aria-describedby | string | Default: — |
Identifies the element (or elements) that describes the object. | ||
aria-details | string | Default: — |
Identifies the element (or elements) that provide a detailed, extended description for the object. | ||
UNSAFE_className | UnsafeClassName | Default: — |
Sets the CSS className
for the element. Only use as a last resort. Use the | ||
UNSAFE_style | CSSProperties | Default: — |
Sets inline style for the
element. Only use as a last resort. Use the | ||
slot | string | null | Default: — |
A slot name for the component. Slots allow the component to receive props from a parent
component. An explicit | ||
SliderInput
SliderInput renders an editable text field for a Slider or RangeSlider, allowing users to precisely input numeric values alongside dragging the slider thumbs.
| Name | Type | |
|---|---|---|
styles | StylesProp | |
Spectrum-defined styles, returned by the | ||
id | string | |
The element's unique identifier. See MDN. | ||
UNSAFE_className | UnsafeClassName | |
Sets the CSS className
for the element. Only use as a last resort. Use the | ||
UNSAFE_style | CSSProperties | |
Sets inline style for the
element. Only use as a last resort. Use the | ||