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.

30
Β 
trackStyleΒ 
thumbStyleΒ 
sizeΒ 
labelPositionΒ 
formatOptionsΒ 
contextualHelpΒ 
isEmphasizedΒ 
isDisabledΒ 
import {Slider} from '@react-spectrum/s2/Slider';

<Slider label="Cookies" defaultValue={30} />

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.

25
onChange value: 25
onChangeEnd value: 25
import {Slider} from '@react-spectrum/s2/Slider';
import {style} from '@react-spectrum/s2/style' with {type: 'macro'};
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 className={style({font: 'body'})}>
        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.

50
Β 
Β 
Β 
Β 
<Slider
  label="Amount"
  maxValue={150}
  defaultValue={50}
  step={5}
  fillOffset={75} />

Editable

Place a <SliderInput /> inside <Slider> when users should be able to precisely input a value within a text field alongside the slider.

sizeΒ 
labelPositionΒ 
isEmphasizedΒ 
isDisabledΒ 
import {Slider, SliderInput} from '@react-spectrum/s2/Slider';

<Slider
  label="Opacity"
  defaultValue={0.5}
  maxValue={1}
  step={0.01}
  formatOptions={{style: 'percent'}}
>
  <SliderInput />
</Slider>

API

<Slider>
  <SliderInput />
</Slider>

Slider

NameTypeDefault
fillOffsetnumberDefault: β€”

The offset from which to start the fill.

childrenReactNodeDefault: β€”

An optional <SliderInput /> to allow editing the slider value within a text field.

size'S' |Β 'M' |Β 'L' |Β 'XL'Default: 'M'

The size of the Slider.

isEmphasizedbooleanDefault: β€”

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.

isDisabledbooleanDefault: β€”

Whether the whole Slider is disabled.

stylesDefault: β€”

Spectrum-defined styles, returned by the style() macro.

valueTDefault: β€”

The current value (controlled).

defaultValueTDefault: β€”

The default value (uncontrolled).

onChange(value: T) => voidDefault: β€”

Handler that is called when the value changes.

onChangeEnd(value: T) => voidDefault: β€”

Fired when the slider stops moving, due to being let go.

formatOptionsIntl.NumberFormatOptionsDefault: β€”

The display format of the value label.

SliderInput

SliderInput renders an editable text field for a Slider or RangeSlider, allowing users to precisely input numeric values alongside dragging the slider thumbs.

NameType
styles

Spectrum-defined styles, returned by the style() macro.