Drags out a continuous value within a range: volume, recording playback position. Use onChange while dragging; actions like seek that should happen only on release go in onChangeEnd.
Status: stable · Components: Slider
Import
Section titled “Import”import { Slider } from 'nacre'import { Text, View, signal } from 'nsh'import { Slider, TEXT_SIZE } from 'nacre'
export function SliderUsage() { const progress = signal(40) return ( <View class="flex-row flex-wrap items-center gap-3"> <View class="w-[260px] max-w-full"> <Slider label="Progress" value={progress()} max={100} onChange={(value) => progress.set(value)} /> </View> <Text class={`${TEXT_SIZE.small} text-muted-foreground tabular-nums`}>{String(progress())}</Text> </View> )}Examples
Section titled “Examples”Apply on release
Section titled “Apply on release”Dragging only updates the displayed time; the playback position jumps on release (onChangeEnd), avoiding repeated seeks mid-drag.
import { Text, View, signal } from 'nsh'import { Slider, TEXT_SIZE } from 'nacre'
const DURATION = 185
const clock = (seconds: number) => `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, '0')}`
export function SliderSeek() { const dragging = signal(12) const playing = signal(12) return ( <View class="w-[300px] max-w-full gap-1"> <Slider label="Call recording" value={dragging()} max={DURATION} onChange={(value) => dragging.set(value)} onChangeEnd={(value) => playing.set(value)} /> <View class="flex-row justify-between"> <Text class={`${TEXT_SIZE.caption} text-muted-foreground tabular-nums`}>{`${clock(dragging())} / ${clock(DURATION)}`}</Text> <Text class={`${TEXT_SIZE.caption} text-muted-foreground tabular-nums`}>{`$Playback position ${clock(playing())}`}</Text> </View> </View> )}min / max / step set the range and step; each key press moves one step.
Phone: min / max / step set the range and step; dragging snaps to each step.
import { Text, View, signal } from 'nsh'import { Slider, TEXT_SIZE } from 'nacre'
export function SliderStep() { const volume = signal(60) return ( <View class="flex-row flex-wrap items-center gap-3"> <View class="w-[200px] max-w-full"> <Slider label="Alert volume" value={volume()} min={0} max={100} step={10} onChange={(value) => volume.set(value)} /> </View> <Text class={`${TEXT_SIZE.small} text-muted-foreground tabular-nums`}>{`${volume()}%`}</Text> </View> )}Disabled
Section titled “Disabled”Ignores dragging and the keyboard, and drops to 40% opacity.
Phone: It ignores dragging and drops to 40% opacity.
import { View } from 'nsh'import { Slider } from 'nacre'
export function SliderDisabled() { return ( <View class="w-[260px] max-w-full"> <Slider label="Disabled" value={30} max={100} disabled /> </View> )}Keyboard
Section titled “Keyboard”| Key | Action |
|---|---|
| Tab | Focus the slider |
| ← → / ↑ ↓ | Decrease / increase by one step |
| Home / End | Jump to the minimum / maximum |
Accessibility
Section titled “Accessibility”- label is required and is the slider’s accessible name; screen readers read it as an adjustable control and announce the current value.
- Show the current value as text next to it, not just through the thumb position.
Do / Don’t
Section titled “Do / Don’t”Do
- Use onChangeEnd for seeking
- Show the current value next to it
Don’t
- Values that need precise input (use a text field)
- Only a few discrete steps (use a segmented control)
Property descriptions follow the Chinese source comments.
Slider
Section titled “Slider”| Prop | Type | Default | Description |
|---|---|---|---|
value * |
number |
— | |
min |
number |
0 |
|
max * |
number |
— | |
step |
number |
1 |
|
label * |
string |
— | |
disabled |
boolean |
— | |
testID |
string |
— | |
class |
string |
'w-full' |
|
onChange |
(value: number) => void |
— | |
onChangeEnd |
(value: number) => void |
— | 拖完松手(seek 这类只在结束时做的) |
* required