Slider API
导入
import Slider from '@mui/material/Slider';
// 或
import { Slider } from '@mui/material';
组件名称
在主题中,名称“MuiSlider
”可用于提供默认属性(props),或者样式覆盖属性
SliderUnstyled 组件的属性也是可用的。
名称 | 类型 | 默认值 | 描述 |
---|---|---|---|
aria-label | string | The label of the slider. | |
aria-labelledby | string | The id of the element containing a label for the slider. | |
aria-valuetext | string | A string value that provides a user-friendly name for the current value of the slider. | |
classes | object | Override or extend the styles applied to the component. See CSS API below for more details. | |
color | 'primary' | 'secondary' | string | 'primary' | The color of the component. It supports both default and custom theme colors, which can be added as shown in the palette customization guide. |
components | { Input?: elementType, 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. |
componentsProps | { input?: func | object, mark?: func | object, markLabel?: func | object, rail?: func | object, root?: func | object, thumb?: func | object, track?: func | object, valueLabel?: func | { children?: element, className?: string, open?: bool, style?: object, value?: number, valueLabelDisplay?: 'auto' | 'off' | 'on' } } | {} | The props used for each slot inside the Slider. |
defaultValue | Array<number> | number | The default input value. Use when the component is not controlled. | |
disabled | bool | false | 如果被设置为 true ,那么该组件将会被禁用。 |
disableSwap | bool | false | If true , the active thumb doesn't swap when moving pointer over a thumb while dragging another thumb. |
getAriaLabel | func | Accepts a function which returns a string value that provides a user-friendly name for the thumb labels of the slider. This is important for screen reader users. Signature: function(index: number) => string index: The thumb label's index to format. | |
getAriaValueText | func | Accepts a function which returns a string value that provides a user-friendly name for the current value of the slider. This is important for screen reader users. Signature: function(value: number, index: number) => string value: The thumb label's value to format. index: The thumb label's index to format. | |
isRtl | bool | false | Indicates whether the theme context has rtl direction. It is set automatically. |
marks | Array<{ label?: node, value: number }> | bool | false | Marks 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. |
max | number | 100 | The maximum allowed value of the slider. Should not be equal to min. |
min | number | 0 | The minimum allowed value of the slider. Should not be equal to max. |
name | string | Name attribute of the hidden input element. | |
onChange | func | Callback function that is fired when the slider's value changed. Signature: function(event: object, value: number | number[], activeThumb: number) => void event: The event source of the callback. You can pull out the new value by accessing event.target.value (any). Warning: This is a generic event not a change event.value: The new value. activeThumb: Index of the currently moved thumb. | |
onChangeCommitted | func | Callback 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 divider orientation. |
scale | func | (x) => x | A transformation function, to change the scale of the slider. |
size | 'small' | 'medium' | string | 'medium' | The size of the slider. |
slotProps | { input?: func | object, mark?: func | object, markLabel?: func | object, rail?: func | object, root?: func | object, thumb?: func | object, track?: func | object, valueLabel?: func | { children?: element, className?: string, open?: bool, style?: object, value?: number, valueLabelDisplay?: 'auto' | 'off' | 'on' } } | {} | |
slots | { input?: elementType, mark?: elementType, markLabel?: elementType, rail?: elementType, root?: elementType, thumb?: elementType, track?: elementType, valueLabel?: elementType } | {} | |
step | number | 1 | The 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. |
sx | Array<func | object | bool> | func | object | The system prop that allows defining system overrides as well as additional CSS styles. See the `sx` page for more details. | |
tabIndex | number | Tab index attribute of the hidden input element. | |
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. |
value | Array<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. |
valueLabelFormat | func | string | (x) => x | The 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 |
ref
则会被传递到根元素中。继承
尽管上文没有明文记录, SliderUnstyled 组件 的属性(props) 在组件Slider上同样是可用的。 你可以利用这一点来 指向嵌套组件CSS
规则名称 | 全局类名 | 描述 |
---|---|---|
root | .MuiSlider-root | Styles applied to the root element. |
marked | .MuiSlider-marked | Class name applied to the root element if marks is provided with at least one label. |
vertical | .MuiSlider-vertical | Class name applied to the root element if orientation="vertical" . |
disabled | .Mui-disabled | Pseudo-class applied to the root and thumb element if disabled={true} . |
dragging | .MuiSlider-dragging | Pseudo-class applied to the root if a thumb is being dragged. |
rail | .MuiSlider-rail | Class name applied to the rail element. |
track | .MuiSlider-track | Class name applied to the track element. |
trackFalse | .MuiSlider-trackFalse | Class name applied to the root element if track={false} . |
trackInverted | .MuiSlider-trackInverted | Class name applied to the root element if track="inverted" . |
thumb | .MuiSlider-thumb | Class name applied to the thumb element. |
active | .Mui-active | Pseudo-class applied to the thumb element if it's active. |
focusVisible | .Mui-focusVisible | Pseudo-class applied to the thumb element if keyboard focused. |
valueLabel | .MuiSlider-valueLabel | Class name applied to the thumb label element. |
valueLabelOpen | .MuiSlider-valueLabelOpen | Class name applied to the thumb label element if it's open. |
valueLabelCircle | .MuiSlider-valueLabelCircle | Class name applied to the thumb label's circle element. |
valueLabelLabel | .MuiSlider-valueLabelLabel | Class name applied to the thumb label's label element. |
mark | .MuiSlider-mark | Class name applied to the mark element. |
markActive | .MuiSlider-markActive | Class name applied to the mark element if active (depending on the value). |
markLabel | .MuiSlider-markLabel | Class name applied to the mark label element. |
markLabelActive | .MuiSlider-markLabelActive | Class name applied to the mark label element if active (depending on the value). |
colorPrimary | .MuiSlider-colorPrimary | Class name applied to the root element if color="primary" . |
colorSecondary | .MuiSlider-colorSecondary | Class name applied to the root element if color="secondary" . |
sizeSmall | .MuiSlider-sizeSmall | Class name applied to the root element if size="small" . |
thumbColorPrimary | .MuiSlider-thumbColorPrimary | Class name applied to the thumb element if color="primary" . |
thumbColorSecondary | .MuiSlider-thumbColorSecondary | Class name applied to the thumb element if color="secondary" . |
thumbSizeSmall | .MuiSlider-thumbSizeSmall | Class name applied to the thumb element if size="small" . |
您可以使用组件自定义选项对组件进行个性化:
- 具备 全局类名.
- 在自定义主体中,设定一个规则名称作为组件的
styleOverrides
属性的一部分。