RangeInput

A slider: one value on a range, or two or more thumbs for a range of values.

Demos

For usage examples, visit the component demo page: RangeInput

Import

import { RangeInput } from '@soroush.tech/design-system/RangeInput'

Props

PropTypeDefaultDescription
valuenumber | number[]-One thumb for a number, one per entry for an array - an empty one is a thumb at min. Controlled with onChange.
defaultValuenumber | number[]minThe value at first, when uncontrolled.
onChange(event, value, activeThumb: number) => void-Fired as the value changes. event.target is { value, name }, as a form library reads it.
onChangeCommitted(event, value) => void-Fired when a change is done: on release, and after every key.
min / maxnumber0 / 100The range.
stepnumber | null1The step. null limits the value to marks.
shiftStepnumber10PageUp, PageDown and Shift with an arrow.
marksboolean | { value, label? }[]falsetrue marks every step; or the marks, each with the label it shows under it.
orientation'horizontal' | 'vertical''horizontal'A vertical slider is 12rem tall unless given a height.
track'normal' | false | 'inverted''normal'The line between the thumbs - 'inverted' fills the rest, false draws none.
scale(value: number) => numberitselfThe value as it is shown and announced - not what is stored.
valueLabel'off' | 'auto' | 'on''off'When a thumb shows its value: never, while hovered or focused, or always.
formatValueLabel(value: number, index: number) => ReactNodethe scaled valueWhat the label says.
getAriaLabel(index: number) => string-Names each thumb. A range needs it unless a FormLabel names them; a single thumb may take aria-label.
getAriaValueText(value: number, index: number) => string-The value's text for each thumb, where the number alone isn't clear.
shouldSwapThumbsbooleantrueA thumb passes its neighbour, taking its place. Without it, it stops at its neighbour.
sizekeyof Theme['sizes']its FormControl's, 'md'The thumb's size (theme.icon) and the rail's thickness.
colorkeyof Theme['palette']its FormControl's, 'primary'The track's and the thumbs' color.
disabledbooleanits FormControl'sDimmed; the inputs are off and the pointer does nothing.
errorbooleanits FormControl'sThe track and thumbs take the error color, and the inputs are aria-invalid.
namestring-What a form submits each thumb's value as - one entry for each thumb.
idstringits FormControl'sOn the first thumb's input, which a FormLabel targets.
aria-label, aria-labelledby, ...On each thumb's input.

It takes the styled-system space props, width, height, minWidth, maxWidth, minHeight, maxHeight, className and data-testid too.

Keys

KeyDoes
ArrowUp, ArrowRightUp by step (with Shift, by shiftStep). ArrowRight goes down right to left.
ArrowDown, ArrowLeftDown by step (with Shift, by shiftStep). ArrowLeft goes up right to left.
PageUp, PageDownUp and down by shiftStep.
Home, EndTo min and max.

With step={null} the arrows and Page keys go to the next mark the way they point and stop at the last; Home and End go to the first and last mark. Each key tells onChangeCommitted, even at an end.

The pointer

A press sets the value under it - on the nearest thumb, the later one on a tie - and a drag follows the pointer, held by pointer capture even past the slider's box; touch drags a thumb, not the page. onChange is told only when the value changes, onChangeCommitted once on release. A release that never came - another element took it - ends the drag on the next move.

In a range, a thumb dragged past its neighbour swaps places with it, the values staying sorted (shouldSwapThumbs={false} stops it at its neighbour instead).

In a FormControl

Its FormLabel targets the first thumb and names every thumb (aria-labelledby), its FormHelperText describes them, and the field's size, color, error and disabled apply, an own prop winning.

<FormControl>
  <FormLabel>Volume</FormLabel>
  <RangeInput defaultValue={30} />
  <FormHelperText>Loud or quiet.</FormHelperText>
</FormControl>

A slider always has a value, so it takes no required.

Customization

theme.components.RangeInput styles the root, the rail, the track, each thumb, the marks and their markLabels, the valueLabel and each input. Its defaultProps set size, color, orientation, track, step, shiftStep, valueLabel and shouldSwapThumbs.

Source code

If this page does not answer your question, have a look at the implementation of the component for more detail.