跳转到内容

Slider API

API documentation for the React Slider component. Learn about the available props, and the CSS API.

Import

import Slider from '@material-ui/core/Slider';
// or
import { Slider } from '@material-ui/core';
You can learn about the difference by reading this guide on minimizing bundle size.

Component name

The name MuiSlider can be used when providing default props or style overrides in the theme.

Props

NameTypeDefaultDescription
aria-labelstringThe label of the slider.
aria-labelledbystringThe id of the element containing a label for the slider.
aria-valuetextstringA string value that provides a user-friendly name for the current value of the slider.
classesobject{}Override or extend the styles applied to the component. See CSS API below for more details.
color'primary'
| 'secondary'
'primary'The color of the component. It supports those theme colors that make sense for this component.
components{ Mark?: elementType, MarkLabel?: elementType, Rail?: elementType, Root?: elementType, Thumb?: elementType, Track?: elementType, ValueLabel?: elementType }{}The components used for each slot inside the Slider. Either a string to use a HTML element or a component.
componentsPropsobject{}The props used for each slot inside the Slider.
defaultValueArray<number>
| number
The default element value. Use when the component is not controlled.
disabledboolfalseIf true, the slider is disabled.
getAriaLabelfuncAccepts a function which returns a string value that provides a user-friendly name for the thumb labels of the slider.

Signature:
function(index: number) => string
index: The thumb label's index to format.
getAriaValueTextfuncAccepts a function which returns a string value that provides a user-friendly name for the current value of the slider.

Signature:
function(value: number, index: number) => string
value: The thumb label's value to format.
index: The thumb label's index to format.
isRtlboolfalseIndicates whether the theme context has rtl direction. It is set automatically.
marksArray<{ label?: node, value: number }>
| bool
falseMarks indicate predetermined values to which the user can move the slider. If true the marks are spaced according the value of the step prop. If an array, it should contain objects with value and an optional label keys.
maxnumber100The maximum allowed value of the slider. Should not be equal to min.
minnumber0The minimum allowed value of the slider. Should not be equal to max.
namestringName attribute of the hidden input element.
onChangefuncCallback function that is fired when the slider's value changed.

Signature:
function(event: object, value: number \| number[]) => void
event: The event source of the callback. Warning: This is a generic event not a change event.
value: The new value.
onChangeCommittedfuncCallback function that is fired when the mouseup is triggered.

Signature:
function(event: object, value: number \| number[]) => void
event: The event source of the callback. Warning: This is a generic event not a change event.
value: The new value.
orientation'horizontal'
| 'vertical'
'horizontal'The slider orientation.
scalefunc(x) => xA transformation function, to change the scale of the slider.
stepnumber1The granularity with which the slider can step through values. (A "discrete" slider.) The min prop serves as the origin for the valid values. We recommend (max - min) to be evenly divisible by the step.
When step is null, the thumb can only be slid onto marks provided with the marks prop.
sxobjectThe system prop that allows defining system overrides as well as additional CSS styles.
track'inverted'
| 'normal'
| false
'normal'The track presentation:
- normal the track will render a bar representing the slider value. - inverted the track will render a bar representing the remaining slider value. - false the track will render without a bar.
valueArray<number>
| number
The value of the slider. For ranged sliders, provide an array with two values.
valueLabelDisplay'auto'
| 'off'
| 'on'
'off'Controls when the value label is displayed:
- auto the value label will display when the thumb is hovered or focused. - on will display persistently. - off will never display.
valueLabelFormatfunc
| string
(x) => xThe format function the value label's value.
When a function is provided, it should have the following signature:
- {number} value The value label's value to format - {number} index The value label's index to format
componentelementTypeThe component used for the root node. Either a string to use a HTML element or a component.

The ref is forwarded to the root element.
Any other props supplied will be provided to the root element (SliderUnstyled).

Inheritance

While not explicitly documented above, the props of the SliderUnstyled component are also available on Slider. You can take advantage of this to target nested components.

CSS

Rule nameGlobal classDescription
root.MuiSlider-rootClass name applied to the root element.
marked.MuiSlider-markedClass name applied to the root element if marks is provided with at least one label.
vertical.MuiSlider-verticalClass name applied to the root element if orientation="vertical".
disabled.Mui-disabledPseudo-class applied to the root and thumb element if disabled={true}.
rail.MuiSlider-railClass name applied to the rail element.
track.MuiSlider-trackClass name applied to the track element.
trackFalse.MuiSlider-trackFalseClass name applied to the track element if track={false}.
trackInverted.MuiSlider-trackInvertedClass name applied to the track element if track="inverted".
thumb.MuiSlider-thumbClass name applied to the thumb element.
active.MuiSlider-activePseudo-class applied to the thumb element if it's active.
focusVisible.Mui-focusVisiblePseudo-class applied to the thumb element if keyboard focused.
valueLabel.MuiSlider-valueLabelClass name applied to the thumb label element.
mark.MuiSlider-markClass name applied to the mark element.
markActive.MuiSlider-markActiveClass name applied to the mark element if active (depending on the value).
markLabel.MuiSlider-markLabelClass name applied to the mark label element.
markLabelActive.MuiSlider-markLabelActiveClass name applied to the mark label element if active (depending on the value).
colorPrimary.MuiSlider-colorPrimaryClass name applied to the root element if color="primary".
colorSecondary.MuiSlider-colorSecondaryClass name applied to the root element if color="secondary".
thumbColorPrimary.MuiSlider-thumbColorPrimaryClass name applied to the thumb element if color="primary".
thumbColorSecondary.MuiSlider-thumbColorSecondaryClass name applied to the thumb element if color="secondary".

You can override the style of the component using one of these customization options:

Demos