Slider
A control element that allows for a range of selections.
Anatomy
<Slider.Root>
<Slider.Label />
<Slider.ValueText />
<Slider.Control>
<Slider.Track>
<Slider.Range />
</Slider.Track>
<Slider.Thumb>
<Slider.HiddenInput />
</Slider.Thumb>
</Slider.Control>
<Slider.MarkerGroup>
<Slider.Marker />
</Slider.MarkerGroup>
</Slider.Root>
Examples
Range
Render multiple Slider.Thumb parts for a range slider.
Marks
Add marks with Slider.MarkerGroup and Slider.Marker. Each marker's value sets its position on the track.
Min and Max
Set min and max to change the bounds. They default to 0 and 100. This example accepts -10 to 10.
Granularity
step sets the granularity. It defaults to 1, so values are integers. This example sets step to 0.01 for a value
between 5 and 10 with two decimal places.
Change Events
onValueChange fires as the value changes. onValueChangeEnd fires when the interaction ends.
Orientation
Set orientation="vertical" for a vertical slider. The up and down arrow keys then change the value.
Give the vertical slider a height in your styles.
Origin
The origin defaults to the start of the track. Set origin to center to start from the middle.
Root Provider
Use RootProvider with the useSlider hook to access the state and methods from outside the component.
Dragging Indicator
Place Slider.DraggingIndicator inside Slider.Thumb to show an indicator while dragging.
With Tooltip
Render the thumb as a tooltip trigger to show the current value on hover, focus and drag.
With Number Input
Sync a range slider with number inputs so either one can set the value.
Thumb Overlap
Set minStepsBetweenThumbs to keep a minimum gap between range thumbs, as in a price filter.
Thumb Collision
thumbCollisionBehavior sets what happens when thumbs collide while dragging: push (default), swap or none.
API Reference
Props
Root
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
defaultValue | number[]The initial value of the slider when rendered. Use when you don't need to control the value of the slider. | |
disabled | booleanWhether the slider is disabled | |
form | stringThe associate form of the underlying input element. | |
getAriaLabel | (details: ThumbLabelDetails) => stringFunction that returns the accessible name of each thumb, e.g. "Minimum price" | |
getAriaLabelledBy | (details: ThumbLabelDetails) => stringFunction that returns the `id` of the element that labels each thumb. Defaults to the label part when no `getAriaLabel` is provided. | |
getAriaValueText | (details: ValueTextDetails) => stringFunction that returns a human readable value for the slider thumb | |
id | stringThe unique identifier of the machine. | |
ids | ElementIdsThe ids of the elements in the slider. Useful for composition. | |
invalid | booleanWhether the slider is invalid | |
largeStep | 10 * step | numberThe step value of the slider when the `Shift` key is held, or the `PageUp`/`PageDown` keys are used. |
max | 100 | numberThe maximum value of the slider |
min | 0 | numberThe minimum value of the slider |
minStepsBetweenThumbs | 0 | numberThe minimum permitted steps between multiple thumbs. `minStepsBetweenThumbs` * `step` should reflect the gap between the thumbs. - `step: 1` and `minStepsBetweenThumbs: 10` => gap is `10` - `step: 10` and `minStepsBetweenThumbs: 2` => gap is `20` |
name | stringThe name associated with each slider thumb (when used in a form) | |
onFocusChange | (details: FocusChangeDetails) => voidFunction invoked when the slider's focused index changes | |
onValueChange | (details: ValueChangeDetails) => voidFunction invoked when the value of the slider changes | |
onValueChangeEnd | (details: ValueChangeDetails) => voidFunction invoked when the slider value change is done | |
orientation | 'horizontal' | 'horizontal' | 'vertical'The orientation of the slider |
origin | 'start' | SliderOriginThe origin of the slider range. The track is filled from the origin to the thumb. - `"start"` / `"end"`: fill from `min` / `max` - `"center"`: fill from the midpoint of `min`/`max` - a `number`: fill from that value instead, for when the neutral point isn't the midpoint |
readOnly | booleanWhether the slider is read-only | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<SliderRootState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. | |
step | 1 | numberThe step value of the slider |
thumbAlignment | 'contain' | 'center' | 'contain'The alignment of the slider thumb relative to the track - `center`: the thumb will extend beyond the bounds of the slider track. - `contain`: the thumb will be contained within the bounds of the track. |
thumbCollisionBehavior | 'none' | 'none' | 'push' | 'swap'Controls how thumbs behave when they collide during pointer interactions. - `none` (default): Thumbs cannot move past each other; excess movement is ignored. - `push`: Thumbs push each other without restoring their previous positions when dragged back. - `swap`: Thumbs swap places when dragged past each other. |
thumbSize | number | { width: number; height: number }The slider thumbs dimensions. A number sets both the width and height. | |
value | number[]The controlled value of the slider |
| Attribute | Description |
|---|---|
[data-slider-root] | <uid> |
[data-disabled] | Present when disabled |
[data-orientation] | The orientation of the slider |
[data-dragging] | Present when in the dragging state |
[data-invalid] | Present when invalid |
[data-focus] | Present when focused |
| CSS Variable | Description |
|---|---|
--slider-thumb-width | The thumb width of the slider |
--slider-thumb-height | The thumb height of the slider |
--slider-thumb-transform | The thumb transform of the slider |
--slider-range-start | The range start of the slider |
--slider-range-end | The range end of the slider |
--translate-x | The horizontal translation value |
--translate-y | The vertical translation value |
Control
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-slider-control] | <uid> |
[data-disabled] | Present when disabled |
[data-orientation] | The orientation of the control |
[data-dragging] | Present when in the dragging state |
[data-invalid] | Present when invalid |
[data-focus] | Present when focused |
DraggingIndicator
Defines the <span> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-slider-dragging-indicator] | <uid> |
[data-orientation] | The orientation of the draggingindicator |
[data-state] | "open" | "closed" |
HiddenInput
Defines the <input> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
Label
Defines the <label> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-slider-label] | <uid> |
[data-disabled] | Present when disabled |
[data-orientation] | The orientation of the label |
[data-dragging] | Present when in the dragging state |
[data-invalid] | Present when invalid |
[data-focus] | Present when focused |
MarkerGroup
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-slider-marker-group] | <uid> |
[data-orientation] | The orientation of the markergroup |
Marker
Defines the <span> element.
| Attribute | Default | Type |
|---|---|---|
value | number | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<SliderMarkerState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-slider-marker] | <uid> |
[data-orientation] | The orientation of the marker |
[data-value] | The value of the item |
[data-disabled] | Present when disabled |
[data-state] | "under-value" | "over-value" | "at-value" |
[data-in-range] | Present when is within the range |
Range
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-slider-range] | <uid> |
[data-disabled] | Present when disabled |
[data-orientation] | The orientation of the range |
[data-dragging] | Present when in the dragging state |
[data-invalid] | Present when invalid |
[data-focus] | Present when focused |
RootProvider
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
value | UseSliderReturn | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<SliderRootProviderState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
Thumb
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
index | number | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
disabled | boolean | |
name | string | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<SliderThumbState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-slider-thumb] | <uid> |
[data-index] | The index of the item |
[data-name] | |
[data-disabled] | Present when disabled |
[data-invalid] | Present when invalid |
[data-orientation] | The orientation of the thumb |
[data-focus] | Present when focused |
[data-dragging] | Present when in the dragging state |
Track
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-slider-track] | <uid> |
[data-disabled] | Present when disabled |
[data-orientation] | The orientation of the track |
[data-dragging] | Present when in the dragging state |
[data-invalid] | Present when invalid |
[data-focus] | Present when focused |
ValueText
Defines the <span> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-slider-value-text] | <uid> |
[data-disabled] | Present when disabled |
[data-orientation] | The orientation of the valuetext |
[data-dragging] | Present when in the dragging state |
[data-invalid] | Present when invalid |
[data-focus] | Present when focused |
Context
API
| Property | Type |
|---|---|
value | number[]The value of the slider. |
dragging | booleanWhether the slider is being dragged. |
focused | booleanWhether the slider is focused. |
setValue | (value: number[]) => voidFunction to set the value of the slider. |
getThumbValue | (index: number) => numberReturns the value of the thumb at the given index. |
setThumbValue | (index: number, value: number) => voidSets the value of the thumb at the given index. |
getValuePercent | (value: number) => numberReturns the percent of the thumb at the given index. |
getPercentValue | (percent: number) => numberReturns the value of the thumb at the given percent. |
getThumbPercent | (index: number) => numberReturns the percent of the thumb at the given index. |
setThumbPercent | (index: number, percent: number) => voidSets the percent of the thumb at the given index. |
getThumbMin | (index: number) => numberReturns the min value of the thumb at the given index. |
getThumbMax | (index: number) => numberReturns the max value of the thumb at the given index. |
increment | (index: number) => voidFunction to increment the value of the slider at the given index. |
decrement | (index: number) => voidFunction to decrement the value of the slider at the given index. |
focus | VoidFunctionFunction to focus the slider. This focuses the first thumb. |
getRootState | () => RootStateReturns the state of the slider |
getThumbState | (props: ThumbProps) => ThumbStateReturns the state of a specific thumb |
getMarkerState | (props: MarkerProps) => MarkerStateReturns the state of a specific marker |
Accessibility
Complies with the Slider WAI-ARIA design pattern.
Keyboard Support
| Key | Description |
|---|---|
ArrowRight | Increments the slider based on defined step |
ArrowLeft | Decrements the slider based on defined step |
ArrowUp | Increases the value by the step amount. |
ArrowDown | Decreases the value by the step amount. |
PageUp | Increases the value by the largeStep amount. |
PageDown | Decreases the value by the largeStep amount. |
Shift + ArrowUp | Increases the value by the largeStep amount. |
Shift + ArrowDown | Decreases the value by the largeStep amount. |
Home | Sets the value to its minimum. |
End | Sets the value to its maximum. |