Skip to content
v2.5.0
English
Русский
Documentation

Slider

Slider is a compound component for selecting one or several numbers from a bounded range.

Root Props

PropTypeDefault
valueV—
Controlled value: a number for one thumb, or an array with one entry per SliderThumb by its index.
defaultValueV—
Initial value for uncontrolled usage: a number for one thumb, or an array with one entry per SliderThumb by its index. Defaults to the middle of the range, like <input type="range">.
onChangeValue(value: NoInfer<V>) => void—
Called when a thumb changes, with a number or an array, in the shape of value or defaultValue.
minnumber0
The minimum allowed value. Defaults to 0.
maxnumber100
The maximum allowed value. Defaults to 100.
stepnumber | 'any'1
The granularity of the values, a number or 'any'. Defaults to 1.
minStepsBetweenThumbsnumber0
The minimum distance between neighbouring thumbs, in steps. Defaults to 0. Has no effect with step='any', which has no step to count; use a small numeric step such as 0.01 to keep a gap.
orientationSliderOrientation'horizontal'
'horizontal' (default) or 'vertical', which grows from the bottom.
sizeSliderSize'md'
Visual size of the slider.
variantSliderVariant'primary'
Visual styling variant.
disabledboolean—
Disables all thumbs.
namestring—
Form field name of the thumbs, unless a thumb sets its own.
refRef<HTMLDivElement>—
Ref to the root element.

Also accepts all <div> attributes.

Child Parts

  • SliderTrack: the rail, a div.
  • SliderRange: the filled part of the rail, a div inside SliderTrack.
  • SliderThumb: one handle, a div with display: contents that groups its input and indicator.
    • index: its entry of the root value, 0 by default
    • sets the position variables; CSS variables in its style reach both parts
  • SliderThumbInput: the native <input type="range"> inside SliderThumb. All its props (name, disabled, list, aria-*, onChange, ref…) go to the input; min, max, step and value come from the root.
  • SliderThumbIndicator: the visual handle inside SliderThumb, a div with any content. It is aria-hidden and lets the pointer through to the input.

SliderTrack

PropTypeDefault
refRef<HTMLDivElement>—
—

Also accepts all <div> attributes.

SliderRange

PropTypeDefault
refRef<HTMLDivElement>—
—

Also accepts all <div> attributes.

SliderThumb

PropTypeDefault
indexnumber0
Position of the thumb among the thumbs of the Slider, its entry of the root value. Defaults to 0.
refRef<HTMLDivElement>—
Ref to the thumb element.

Also accepts all <div> attributes.

SliderThumbInput

PropTypeDefault
refRef<HTMLInputElement>—
Ref to the native <input type="range">.

Also accepts all <input> attributes.

SliderThumbIndicator

PropTypeDefault
refRef<HTMLDivElement>—
Ref to the indicator element.

Also accepts all <div> attributes.

Hooks

Advanced hooks for custom parts inside Slider. Use them only when the exported parts are not enough.

useSliderContext

Root state, available anywhere inside Slider:

FieldTypeNotes
minnumberRoot min.
maxnumberRoot max.
stepnumber | 'any'Root step.
gapnumberMinimum distance between neighbouring thumbs: minStepsBetweenThumbs * step, 0 with step='any'.
orientationSliderOrientationRoot orientation.
disabledboolean | undefinedRoot disabled.
namestring | undefinedRoot name.
valuesnumber[]Current values of all thumbs by index, snapped to the step grid and sorted, an array even for one thumb.
changeThumb(index, value)(index: number, value: number) => voidSets the value of the thumb at index, kept between its neighbours; reports through onChangeValue.

useSliderThumbContext

The surrounding SliderThumb:

FieldTypeNotes
indexnumberindex of the thumb.
lownumberLowest value the thumb may take: the previous thumb plus gap, or min.
highnumberHighest value the thumb may take: the next thumb minus gap, or max.

A value bubble that also works without value on the root:

usesliderthumbcontext.tsx · 55 lines
import {
  Slider,
  SliderRange,
  SliderThumb,
  SliderThumbIndicator,
  SliderThumbInput,
  SliderTrack,
  Text,
  useSliderContext,
  useSliderThumbContext,
} from '@vega-ui/react';

Types

  • SliderProps
  • SliderTrackProps
  • SliderRangeProps
  • SliderThumbProps
  • SliderThumbInputProps
  • SliderThumbIndicatorProps
  • SliderSize
  • SliderVariant
  • SliderOrientation

State Model

Slider owns the values of its thumbs. The shape follows value or defaultValue: a number for one thumb, an array with one entry per SliderThumb by its index for several. onChangeValue receives the same shape.

  • Uncontrolled: defaultValue sets the start; a reset of the form of the thumbs restores it, including a form linked with the form attribute. Without value and defaultValue the slider has one thumb in the middle of the range and reports numbers, like <input type="range">.
  • Controlled: value with onChangeValue. A change the parent does not apply is rolled back, like a controlled input.
  • Each thumb is a controlled native input: pointer, keyboard, focus, forms and assistive technologies come from the browser, and onChange on a thumb fires for every change.
  • Values off the step grid or outside min/max are snapped like <input type="range"> does, without calling onChangeValue.
  • The shape is fixed by the first value: switching between a number and an array on the fly is not supported.
controlled-brightness.tsx · 44 lines
import { useState } from 'react';
import {
  Label,
  Slider,
  SliderRange,
  SliderThumb,
  SliderThumbIndicator,
  SliderThumbInput,
  SliderTrack,
  Text,
} from '@vega-ui/react';
controlled-range.tsx · 56 lines
import { useState } from 'react';
import {
  Fieldset,
  FieldsetLegend,
  Slider,
  SliderRange,
  SliderThumb,
  SliderThumbIndicator,
  SliderThumbInput,
  SliderTrack,
  Text,
} from '@vega-ui/react';

Thumb Constraints

  • Each thumb input gets its own native min and max: the neighbours' values, minStepsBetweenThumbs * step away, or the root min and max at the ends. The browser keeps the thumb between them for dragging and keys, and aria-valuemin/aria-valuemax announce the real limits.
  • With step='any' there is no step to count, so minStepsBetweenThumbs has no effect and thumbs may meet; use a small numeric step such as 0.01 to keep a gap.
  • Values given out of order are sorted and kept minStepsBetweenThumbs steps apart, without calling onChangeValue, so the thumbs, the inputs and the submitted values agree.

Behavior

  • Like <input type="range">, steps count from min and the value stays within min and the last step that fits in max.
  • The inputs of neighbouring thumbs are cut at the midpoint between their values, so a click on the track moves the nearest thumb natively. Stacked thumbs are split at their centre: the left half moves the lower one, the right half the upper one.
  • A disabled thumb keeps its half of the track: a click there does nothing.
  • On touch screens a horizontal slider leaves gestures to the browser like <input type="range">; a vertical one sets touch-action: pan-x pinch-zoom, so a vertical swipe moves the thumb instead of scrolling the page.
  • A <datalist> connected through list snaps the pointer natively; its tick marks are not visible, draw your own.