滑块 · Slider
滑块用于表单输入场景。下面的官方示例展示原生用法与状态,属性名保持上游 API。
安装与前置条件
npm install @astryxdesign/core@0.7.1 @astryxdesign/theme-neutral@0.7.1 @stylexjs/stylex@0.19.0
需要 React 19+,按顺序导入基础 CSS 和主题,并设置 Theme Provider。参见开始使用。
示例与使用方式
- 滑块 · 综合展示 1:首先体验这个示例,再查看代码理解组件的组合方式。
- 滑块 · 交互示例 2:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
- 滑块 · 交互示例 3:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
- 滑块 · 交互示例 4:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
- 滑块 · 状态 5:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
API 与上游详细参考
优先使用 0.7.1 官方中文说明;上游尚未提供中文的条目保留英文。属性名、类型及默认值保持原生 API,不把示例状态名称当成组件属性。
Slider
A draggable control for selecting a numeric value or range within defined bounds. Supports single value and range selection, tick marks, custom value formatting, and vertical orientation. Use it when users need to explore a continuous range, such as volume, price, or percentage.
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| label * | string | 标签文本(始终渲染以确保无障碍可访问性)。 | |
| value * | number | [number, number] | 当前值——number 用于单滑块模式,[number, number] 用于范围模式。 | |
| onChange | (value: number) => void | (value: [number, number]) => void | 拖拽过程中值变更时触发的回调。 | |
| onChangeEnd | (value: number) => void | (value: [number, number]) => void | 拖拽结束时触发的回调。 | |
| min | number | 0 | 最小值。 |
| max | number | 100 | 最大值。 |
| step | number | 1 | 步进增量。 |
| orientation | 'horizontal' | 'vertical' | 'horizontal' | 滑块的方向。 |
| formatValue | (value: number) => string | 自定义值格式化函数,用于显示和 aria-valuetext。 | |
| valueDisplay | 'tooltip' | 'text' | 'none' | 'tooltip' | 当前值的显示方式。 |
| marks | Array<{ value: number; label?: string }> | 在指定位置的刻度标记,带可选标签。未填充区域的标记使用轨道颜色;填充区域内的标记使用强调色。 | |
| minStepsBetweenThumbs | number | 0 | 范围模式下滑块之间的最小步数;防止滑块重叠。 |
| isDisabled | boolean | false | 是否禁用滑块。 |
| htmlName | string | 用于表单提交的 HTML name 属性。渲染携带当前值的隐藏输入(范围模式下为两个条目)。 | |
| disabledMessage | string | Explains why the slider is disabled. With isDisabled, shows a tooltip on hover/keyboard focus and keeps the thumb focusable via aria-disabled (value changes stay blocked). Use this instead of wrapping a disabled Slider in Tooltip. Disabled controls swallow the hover events an external Tooltip needs. | |
| isOptional | boolean | false | 字段是否为可选。 |
| isRequired | boolean | false | 字段是否为必填。 |
| isLabelHidden | boolean | false | 是否在视觉上隐藏标签。 |
| description | string | 标签下方渲染的描述文本。 | |
| status | {type: 'warning' | 'error' | 'success', message?: string} | 验证反馈的状态指示器对象({ type, message })。 | |
| labelTooltip | string | 标签旁信息图标的提示文本。 | |
| width | SizeValue | Width of the field (number = pixels, string used as-is, e.g. "100%"). Sizes the whole field (label, control, and status) so they stay aligned. | |
| containerXstyle | StyleXStyles | 字段容器(标签、控件和状态整体)的 StyleX 样式。在 InputGroup 内(请改为设置 InputGroup 的样式)或 horizontal-labels FormLayout 中无效。不接受根级 xstyle、className 和 style。 | |
| containerClassName | string | 字段容器的类名,作用目标同 containerXstyle。 | |
| containerStyle | CSSProperties | 字段容器的内联样式,作用目标同 containerXstyle。 | |
| controlXstyle | StyleXStyles | 可视控件框的 StyleX 样式:绘制边框和背景并包含图标与尾部控件的元素,而非原生输入元素。 | |
| controlClassName | string | 可视控件框的类名。 | |
| controlStyle | CSSProperties | 可视控件框的内联样式。 |
使用建议
- Always provide a label, even if visually hidden, so the slider is accessible to screen readers.
- Format values with meaningful units like "$50" or "75%" instead of raw numbers.
- 避免:Use for precise numeric entry; pair with a text input or use NumberInput instead.
- 避免:Set a step size so large that only a few positions are possible; use SegmentedControl or radio buttons instead.
- 避免:Wrap a disabled slider in Tooltip to explain why it is disabled; disabled controls swallow the hover events the wrapper needs. Use the disabledMessage prop instead.
主题 API
{
"targets": [
{
"className": "astryx-slider",
"visualProps": [
"orientation"
],
"states": [
"disabled"
]
},
{
"className": "astryx-slider-control",
"visualProps": [
"orientation"
],
"states": [
"disabled"
]
},
{
"className": "astryx-slider-track",
"visualProps": [
"orientation"
]
},
{
"className": "astryx-slider-thumb",
"visualProps": [
"orientation"
],
"states": [
"disabled"
]
}
]
}
无障碍
- Thumb: The thumb must have at least 3:1 contrast with the track and the surface behind it. Pointer down is the whole drag: measure the thumb with the pressed overlay applied. — 3:1
组件结构
- Label: Text identifying the numeric setting controlled by the slider.
- Description: Helper text between the label and the slider control.
- Slider: Control row containing the track, thumb or thumbs, and optional text value.
- Interactive control: Pointer and keyboard interaction surface containing the rail, fill, marks, and thumbs.
- Track: Background rail representing the available numeric range.
- Filled range: Accent segment from the minimum to a single value, or between two range values.
- Tick mark: Position marker supplied through the marks collection.
- Mark label: Optional text displayed beside a tick mark.
- Thumb: Draggable value indicator; range mode renders a minimum and maximum thumb.
- Value display: Formatted current value shown beside the slider when valueDisplay is text.
- Value tooltip: Formatted current value shown in a tooltip when valueDisplay is tooltip.
- Status message: Error, warning, or success message below the slider.