Slider
Sliders allow users to quickly select a value within a range. They should be used when the upper and lower bounds to the range are invariable.
trackStyle
thumbStyle
size
labelPosition
formatOptions
The display format of the value label.
formatOptions
contextualHelp
isEmphasized
isDisabled
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 '@react-spectrum/s2';
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>
</>
);
}
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.
API
Name | Type | Default |
---|---|---|
fillOffset | number | Default: — |
The offset from which to start the fill. | ||
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 style() macro. | ||
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. | ||