RangeControl 是 WordPress 组件库中的一个滑块控件,用于从一系列增量值中选择单个值。它适用于调整音量、不透明度或文本大小等设置,提供连续或离散的交互方式。
import { useState } from 'react';
import { RangeControl } from '@wordpress/components';
const MyRangeControl = () => {
const [ columns, setColumns ] = useState( 2 );
return (
<RangeControl
__next40pxDefaultSize
label="Columns"
value={ columns }
onChange={ ( value ) => setColumns( value ) }
min={ 2 }
max={ 10 }
/>
);
};RangeControls are used to make selections from a range of incremental values.

A RangeControl can contain the following elements:
Continuous sliders allow users to select a value along a subjective range. They do not display the selected numeric value. Use them when displaying/editing the numeric value is not important, like volume.
Discrete sliders can be adjusted to a specific value by referencing its value entry field. Use them when it’s important to display/edit the numeric value, like text size.
Possible selections may be organized through the use of tick marks, which a thumb will snap to (or to which an input will round up or down).
RangeControls reflect a range of values along a track, from which users may select a single value. They are ideal for adjusting settings such as volume, opacity, or text size.
RangeControls can have icons on both ends of the track that reflect a range of values.
Changes made with RangeControls are immediate, allowing a user to make adjustments until finding their preference. They shouldn’t be paired with settings that have delays in providing feedback.

Don’t
Don’t use RangeControls if the effect isn’t immediate.
RangeControls reflect the current state of the settings they control.

A RangeControl with an editable numeric value
Editable numeric values: Editable numeric values allow users to set the exact value of a RangeControl. After setting a value, the thumb position is immediately updated to match the new value.

Don’t
RangeControls should only be used for choosing selections from a range of values (e.g., don’t use a RangeControl if there are only 2 values).

Don’t
RangeControls should provide the full range of choices available for the user to select from (e.g., don’t disable only part of a RangeControl).
Render a RangeControl to make a selection from a range of incremental values.
import { useState } from 'react';
import { RangeControl } from '@wordpress/components';
const MyRangeControl = () => {
const [ columns, setColumns ] = useState( 2 );
return (
<RangeControl
__next40pxDefaultSize
label="Columns"
value={ columns }
onChange={ ( value ) => setColumns( value ) }
min={ 2 }
max={ 10 }
/>
);
};
The set of props accepted by the component will be specified below.
Props not included in this set will be applied to the input elements.
afterIcon: string|Function|Component|nullIf this property is added, an Icon component will be rendered after the slider with the icon equal to afterIcon.
For more information on IconType see the Icon component.
allowReset: booleanIf this property is true, a button to reset the slider is rendered.
falsebeforeIcon: string|Function|Component|nullIf this property is added, an Icon component will be rendered before the slider with the icon equal to beforeIcon.
For more information on IconType see the Icon component.
color: CSSProperties['color']CSS color string for the RangeControl wrapper.
currentInput: numberThe current input to use as a fallback if value is currently undefined.
disabled: booleanDisables the input, preventing new values from being applied.
help: string|ElementIf this property is added, a help text will be generated using help property as the content.
hideLabelFromVision: booleanProvides control over whether the label will only be visible to screen readers.
icon: stringAn icon to be shown above the slider next to its container title.
initialPosition: numberThe slider starting position, used when no value is passed. The initialPosition will be clamped between the provided min and max prop values.
isShiftStepEnabled: booleanPassed as a prop to the NumberControl component and is only applicable if withInputField is true. If true, while the number input has focus, pressing UP or DOWN along with the SHIFT key will change the value by the shiftStep value.
label: stringIf this property is added, a label will be generated using label property as the content.
marks: Array|booleanRenders a visual representation of step ticks. Custom mark indicators can be provided by an Array.
Example:
const marks = [
{
value: 0,
label: '0',
},
{
value: 1,
label: '1',
},
{
value: 8,
label: '8',
},
{
value: 10,
label: '10',
},
];
const MyRangeControl() {
return ( <RangeControl marks={ marks } min={ 0 } max={ 10 } step={ 1 } /> )
}
onBlur: FocusEventHandler< HTMLInputElement >Callback for when RangeControl input loses focus.
onChange: ( value?: number ) => voidA function that receives the new value. The value will be less than max and more than min unless a reset (enabled by allowReset) has occurred. In which case the value will be either that of resetFallbackValue if it has been specified or otherwise undefined.
onFocus: FocusEventHandler< HTMLInputElement >Callback for when RangeControl input gains focus.
onMouseLeave: MouseEventHandler< HTMLInputElement >Callback for when mouse exits the RangeControl.
onMouseMove: MouseEventHandler< HTMLInputElement >Callback for when mouse moves within the RangeControl.
min: numberThe minimum value allowed.
max: numberThe maximum value allowed.
railColor: CSSProperties[ 'color' ]CSS color string to customize the rail element’s background.
renderTooltipContent: ( value ) => valueA way to customize the rendered UI of the value. Example:
const customTooltipContent = value => `${value}%`
const MyRangeControl() {
return (<RangeControl renderTooltipContent={ customTooltipContent } />)
}
resetFallbackValue: numberThe value to revert to if the Reset button is clicked (enabled by allowReset)
separatorType: 'none' | 'fullWidth' | 'topFullWidth'Define if separator line under/above control row should be disabled or full width. By default it is placed below excluding underline the control icon.
shiftStep: numberPassed as a prop to the NumberControl component and is only applicable if withInputField and isShiftStepEnabled are both true and while the number input has focus. Acts as a multiplier of step.
showTooltip: booleanForcing the Tooltip UI to show or hide. This is overridden to false when step is set to the special string value any.
step: number | 'any'The minimum amount by which value changes. It is also a factor in validation as value must be a multiple of step (offset by min) to be valid. Accepts the special string value any that voids the validation constraint and overrides both withInputField and showTooltip props to false.
trackColor: CSSProperties[ 'color' ]CSS color string to customize the track element’s background.
type: stringDefine if the value selection should present a stepper control or a slider control in the bottom sheet on mobile. To use the stepper set the type value as stepper. Defaults to slider if no option is provided.
value: numberThe current value of the range slider.
withInputField: booleanDetermines if the input number field will render next to the RangeControl. This is overridden to false when step is set to the special string value any.
__next40pxDefaultSize: booleanStart opting into the larger default height that will become the default size in a future version.
falseTextControl component.