Slider
一个适用于 ReactLynx 的 Slider 组件包,优先提供可组合的基础能力,同时支持单值滑块和双拇指区间滑块。你可以查看源码来了解它的运行原理,也欢迎大家提 PR 来丰富其能力。
基础用法
使用基础原语组合不同样式、禁用状态和从右到左的滑块。
示例
受控模式
从外部控制滑块值,跳转到预设值,并按固定步长吸附。
动态宽度
在滑块容器宽度变化时正确重新测量轨道。
Facade
将滑块原语封装为可复用组件,并支持替换拇指内容。
价格区间
构建带实时值、提交值和预设选项的步进双拇指区间滑块。
进度
省略拇指,将滑块原语复用为只读进度指示器。
形状
通过定制轨道、指示器和拇指样式构建不同视觉形态。
与 ScrollView 组合
在横向与纵向嵌套滚动区域中保持滑块的水平拖动手势。
结构
单值滑块使用一个拇指。
区间滑块使用两个带索引的拇指。
组件 API
SliderRoot
Root primitive props for a single-value or range slider. The generic
selects the value shape and defaults to number for existing single-value
usage. A range slider must provide its tuple through either value or
defaultValue so the runtime can determine the shape.
Range-thumb collision behavior is intentionally non-crossing: thumbs may
meet, but the active thumb stops at the other value without swapping or
pushing the other thumb.
SliderTrack 内包含 SliderIndicator 以及可选的 SliderThumb。0 开始;区间滑块必须提供二元组。true 时,滑块范围从右向左增长。SliderRef.updateValue。在受控模式下,请通过此回调让外部 value 属性与渲染值保持同步。[0, 1]。设置后值会吸附到最近的 step 倍数。[0, 1] 内的数值或有序的下限/上限二元组。请勿与 defaultValue 同时使用。基于状态的样式与渲染
该组件通过以下一种或两种方式暴露运行时状态。下表仅展示每个状态实际支持的方式:
- UI variant class:支持时,对应组件节点会动态加上
ui-<state>形式的类名,可用.ui-<state> { ... }选择器定制样式; - Render-prop 字段:支持时,当
children传入函数,状态会以children({ <state>: boolean, ... })的形式作为参数传入,便于在运行时做条件渲染。
UI variants applied by SliderRoot.
| 类名 | 说明 |
|---|---|
| ui-active | 从指针或触摸交互开始到结束期间生效。 |
| ui-disabled | 滑块被禁用时生效。 |
SliderTrack
Track primitive props.
SliderTrack establishes the measurement/layout coordinate space and renders the base rail.
SliderIndicator 以及可选的 SliderThumb。UI variants applied by SliderTrack.
| 类名 | 说明 |
|---|---|
| ui-active | 从指针或触摸交互开始到结束期间生效。 |
| ui-disabled | 滑块被禁用时生效。 |
SliderIndicator
Indicator primitive props.
SliderIndicator is a pure visual layer. It fills from zero to the value in single-value mode and spans the selected interval in range mode.
UI variants applied by SliderIndicator.
| 类名 | 说明 |
|---|---|
| ui-active | 从指针或触摸交互开始到结束期间生效。 |
| ui-disabled | 滑块被禁用时生效。 |
SliderThumb
Thumb primitive props.
SliderThumb is positioned by the current ratio inside SliderTrack and does not own interaction logic.
0;区间滑块使用 0 表示下限拇指,使用 1 表示上限拇指。UI variants applied by SliderThumb.
| 类名 | 说明 |
|---|---|
| ui-active | 从指针或触摸交互开始到结束期间,仅在当前选中的拇指上生效。 |
| ui-disabled | 滑块被禁用时生效。 |
SliderRef
Imperative methods exposed by SliderRoot for the selected value shape.
The generic defaults to number for compatibility with existing
single-value refs; use SliderRef<SliderRangeValue> for a range slider.
[0, 1]。辅助类型
SliderRangeValue
[0, 1] 范围。
拖拽时,下限和上限拇指可以相遇,但不会交叉、交换身份或推动彼此。SliderValue
SliderRoot 接受的值:单个标准化数值,或有序的下限/上限二元组。SliderThumbIndex
0;区间滑块使用索引 0 表示下限、索引 1 表示上限。SliderValueChangeSource
external:通过命令式 API 调用更新。
- drag:通过指针/触摸拖拽更新。SliderUpdateValueOptions
Options used by SliderRef.updateValue.