useSlider

Provides the behavior and accessibility implementation for a slider component representing one or more values.

60
Example
Slider.tsx
Slider.css
Form.css
import {Slider} from './Slider';

<Slider label="Opacity" defaultValue={60} />

API

Shows a slider with labels pointing to its parts including the label, group, track, thumb, and output elements.OutputLabelLabelLabel24TrackThumbGroup
<Slider>
  <Label />
  <SliderOutput />
  <SliderTrack>
    <SliderFill />
    <SliderThumb />
    <SliderThumb>
      <Label />
    </SliderThumb>
  </SliderTrack>
</Slider>
useSliderState<T extends numbernumber[]>(props: <T>): useSlider<T extends numbernumber[]>( props: <T>, state: , trackRef: <Elementnull> ): useSliderThumb(opts: , state: ):

SliderState

Properties

NameType
valuesnumber[]

Values managed by the slider by thumb index.

defaultValuesnumber[]

The default values for each thumb.

focusedThumbnumberundefined

Currently-focused thumb index.

pageSizenumber

The page size for the slider, used to do a bigger step.

orientation

The orientation of the slider.

isDisabledboolean

Whether the slider is disabled.

Methods

getThumbValue(index: number): number
Get the value for the specified thumb.
setThumbValue(index: number, value: number): void
Sets the value for the specified thumb. The actual value set will be clamped and rounded according to min/max/step.
setThumbPercent(index: number, percent: number): void
Sets value for the specified thumb by percent offset (between 0 and 1).
isThumbDragging(index: number): boolean
Whether the specific thumb is being dragged.
setThumbDragging(index: number, dragging: boolean): void
Set is dragging on the specified thumb.
setFocusedThumb(index: numberundefined): void
Set focused true on specified thumb. This will remove focus from any thumb that had it before.
getThumbPercent(index: number): number
Returns the specified thumb's value as a percentage from 0 to 1.
getValuePercent(value: number): number
Returns the value as a percent between the min and max of the slider.
getThumbValueLabel(index: number): string
Returns the string label for the specified thumb's value, per props.formatOptions.
getFormattedValue(value?: numbernumber[]): string
Returns the string label for the value, per props.formatOptions.
getThumbMinValue(index: number): number
Returns the min allowed value for the specified thumb.
getThumbMaxValue(index: number): number
Returns the max allowed value for the specified thumb.
getPercentValue(percent: number): number
Converts a percent along track (between 0 and 1) to the corresponding value.
isThumbEditable(index: number): boolean
Returns if the specified thumb is editable.
setThumbEditable(index: number, editable: boolean): void
Set the specified thumb's editable state.
incrementThumb(index: number, stepSize?: number): void
Increments the value of the thumb by the step or page amount.
decrementThumb(index: number, stepSize?: number): void
Decrements the value of the thumb by the step or page amount.

AriaSliderProps

NameTypeDefault
orientationDefault: 'horizontal'

The orientation of the Slider.

isDisabledbooleanDefault:

Whether the whole Slider is disabled.

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.

SliderAria

NameType
labelPropsLabelHTMLAttributes<HTMLLabelElement>

Props for the label element.

groupPropsDOMAttributes

Props for the root element of the slider component; groups slider inputs.

trackPropsDOMAttributes

Props for the track element.

outputPropsOutputHTMLAttributes<HTMLOutputElement>

Props for the output element, displaying the value of the slider thumbs.

AriaSliderThumbOptions

NameTypeDefault
trackRef<Elementnull>Default:

A ref to the track element.

inputRef<HTMLInputElementnull>Default:

A ref to the thumb input element.

isDisabledbooleanDefault:

Whether the Thumb is disabled.

indexnumberDefault: 0

Index of the thumb within the slider.

SliderThumbAria

NameType
thumbPropsDOMAttributes

Props for the root thumb element; handles the dragging motion.

inputPropsInputHTMLAttributes<HTMLInputElement>

Props for the visually hidden range input element.

labelPropsLabelHTMLAttributes<HTMLLabelElement>

Props for the label element for this thumb (optional).

isDraggingboolean

Whether this thumb is currently being dragged.

isFocusedboolean

Whether the thumb is currently focused.

isDisabledboolean

Whether the thumb is disabled.