Root Props
| Prop | Type | Default |
|---|---|---|
value | V | — |
Controlled value: a number for one thumb, or an array with one entry per SliderThumb by its index. | ||
defaultValue | V | — |
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. | ||
min | number | 0 |
The minimum allowed value. Defaults to 0. | ||
max | number | 100 |
The maximum allowed value. Defaults to 100. | ||
step | number | 'any' | 1 |
The granularity of the values, a number or 'any'. Defaults to 1. | ||
minStepsBetweenThumbs | number | 0 |
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. | ||
orientation | SliderOrientation | 'horizontal' |
'horizontal' (default) or 'vertical', which grows from the bottom. | ||
size | SliderSize | 'md' |
| Visual size of the slider. | ||
variant | SliderVariant | 'primary' |
| Visual styling variant. | ||
disabled | boolean | — |
| Disables all thumbs. | ||
name | string | — |
| Form field name of the thumbs, unless a thumb sets its own. | ||
ref | Ref<HTMLDivElement> | — |
| Ref to the root element. | ||
Also accepts all <div> attributes.
Child Parts
SliderTrack: the rail, adiv.SliderRange: the filled part of the rail, adivinsideSliderTrack.SliderThumb: one handle, adivwithdisplay: contentsthat groups its input and indicator.index: its entry of the rootvalue,0by default- sets the position variables; CSS variables in its
stylereach both parts
SliderThumbInput: the native<input type="range">insideSliderThumb. All its props (name,disabled,list,aria-*,onChange,ref…) go to the input;min,max,stepandvaluecome from the root.SliderThumbIndicator: the visual handle insideSliderThumb, adivwith any content. It isaria-hiddenand lets the pointer through to the input.
SliderTrack
| Prop | Type | Default |
|---|---|---|
ref | Ref<HTMLDivElement> | — |
| — | ||
Also accepts all <div> attributes.
SliderRange
| Prop | Type | Default |
|---|---|---|
ref | Ref<HTMLDivElement> | — |
| — | ||
Also accepts all <div> attributes.
SliderThumb
| Prop | Type | Default |
|---|---|---|
index | number | 0 |
Position of the thumb among the thumbs of the Slider, its entry of the root value. Defaults to 0. | ||
ref | Ref<HTMLDivElement> | — |
| Ref to the thumb element. | ||
Also accepts all <div> attributes.
SliderThumbInput
| Prop | Type | Default |
|---|---|---|
ref | Ref<HTMLInputElement> | — |
Ref to the native <input type="range">. | ||
Also accepts all <input> attributes.
SliderThumbIndicator
| Prop | Type | Default |
|---|---|---|
ref | Ref<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:
| Field | Type | Notes |
|---|---|---|
min | number | Root min. |
max | number | Root max. |
step | number | 'any' | Root step. |
gap | number | Minimum distance between neighbouring thumbs: minStepsBetweenThumbs * step, 0 with step='any'. |
orientation | SliderOrientation | Root orientation. |
disabled | boolean | undefined | Root disabled. |
name | string | undefined | Root name. |
values | number[] | 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) => void | Sets the value of the thumb at index, kept between its neighbours; reports through onChangeValue. |
useSliderThumbContext
The surrounding SliderThumb:
| Field | Type | Notes |
|---|---|---|
index | number | index of the thumb. |
low | number | Lowest value the thumb may take: the previous thumb plus gap, or min. |
high | number | Highest 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
SliderPropsSliderTrackPropsSliderRangePropsSliderThumbPropsSliderThumbInputPropsSliderThumbIndicatorPropsSliderSizeSliderVariantSliderOrientation
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:
defaultValuesets the start; a reset of the form of the thumbs restores it, including a form linked with theformattribute. WithoutvalueanddefaultValuethe slider has one thumb in the middle of the range and reports numbers, like<input type="range">. - Controlled:
valuewithonChangeValue. 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
onChangeon a thumb fires for every change. - Values off the step grid or outside
min/maxare snapped like<input type="range">does, without callingonChangeValue. - 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
minandmax: the neighbours' values,minStepsBetweenThumbs * stepaway, or the rootminandmaxat the ends. The browser keeps the thumb between them for dragging and keys, andaria-valuemin/aria-valuemaxannounce the real limits. - With
step='any'there is no step to count, sominStepsBetweenThumbshas no effect and thumbs may meet; use a small numeric step such as0.01to keep a gap. - Values given out of order are sorted and kept
minStepsBetweenThumbssteps apart, without callingonChangeValue, so the thumbs, the inputs and the submitted values agree.
Behavior
- Like
<input type="range">, steps count fromminand the value stays withinminand the last step that fits inmax. - 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 setstouch-action: pan-x pinch-zoom, so a vertical swipe moves the thumb instead of scrolling the page. - A
<datalist>connected throughlistsnaps the pointer natively; its tick marks are not visible, draw your own.