Slider
Slider component
Source
LLM docs
Docs
PackageIcon
Usage
Controlled
Uncontrolled
Slider can be used in uncontrolled forms the same way as a native input element.
Set the name attribute to include the slider value in the FormData object on form submission.
To set the initial value in uncontrolled mode, use the defaultValue prop.
Example usage of uncontrolled Slider with FormData:
Disabled
onChangeEnd
onChangeEnd callback is called when the user stops dragging the slider or when the value is changed with the keyboard.
You can use it as a debounced callback to avoid too frequent updates.
onChange value: 50
onChangeEnd value: 50
Control label
To change label behavior and appearance, set the following props:
label– formatter function, accepts value as an argument, set null to disable label, defaults tof => flabelAlwaysOn– if true, the label will always be displayed; by default the label is visible only when the user is dragginglabelTransitionProps– props passed down to the Transition component, can be used to customize label animation
No label
Formatted label
Label always visible
Custom label transition
Min, max and step
Decimal step
Step matched with marks
Domain
By default, the min and max props define both the visual range (track display) and
the selectable range (possible values). The domain prop allows you to independently
control the selectable range. This is useful when you want to display a wider track
(for context) but restrict the actual selection to a subset.
In the example below, the track displays from 0 to 100 (min/max), but the thumb
can only be dragged between 20 and 80 (domain):
Decimal values
To use Slider with decimal values, set min, max and step props:
Marks
Add any number of marks to the slider by setting the marks prop to an array of objects:
Note that the mark value is relative to the slider value, not width:
Restrict selection to marks
Set the restrictToMarks prop to restrict the slider value to marks only. Note that in
this case the step prop is ignored:
Thumb size
Thumb children
Scale
You can use the scale prop to represent the value on a different scale.
In the following demo, the value x represents the value 2^x. Increasing x by one increases the represented value by 2 to the power of x.
Inverted
You can invert the track with the inverted prop:
Styles API
Slider supports the Styles API; you can add styles to any inner element of the component with the classNames prop. Follow the Styles API documentation to learn more.
Component Styles API
Hover over selectors to highlight corresponding elements
Example of using Styles API to change Slider styles:
Vertical slider
Set orientation="vertical" to render the slider vertically. In vertical orientation,
the minimum value is at the bottom and the maximum value is at the top.
Hidden marks
Hidden marks allow you to snap to specific values without displaying them visually on the track.
This is useful when you want to create a "sticky" snapping behavior to certain values that
you don't want to show to the user. Use this feature together with restrictToMarks prop:
Hidden marks allow you to snap to specific values without displaying them visually. Current value: 50
Build custom slider
If the Slider component does not meet your requirements, you can build a custom slider with the use-move hook:
Accessibility
Slider component is accessible by default:
- Thumbs are focusable
- When the user uses the mouse to interact with the slider, focus is moved to the slider track; when the user presses arrows, focus is moved to the thumb
- The value can be changed with arrows with step increment/decrement
To label the component for screen readers, add labels to thumbs: