Components / Controls
Slider
Picks a number by dragging a thumb along a track, or a range with two thumbs. It works with the keyboard, pointer and touch, and submits with native forms.
When to useLink to "When to use"
- A value where the rough position matters more than the exact number: volume, opacity, playback speed.
- A range: a price band, a time window.
Use something else when:
- the exact number matters or the range is large: a TextField with
type="number" - there are only a few named choices: a SegmentedControl or RadioGroup
- it shows progress rather than taking input: a ProgressBar
UsageLink to "Usage"
import { Slider } from "@arshad-shah/cynosure";
<Slider label="Volume" value={volume} onChange={setVolume} showValue />
<Slider label="Price" defaultValue={[20, 60]} name="price" onCommit={search} />onChange follows every movement while dragging; onCommit fires once the change is finished, so expensive work (a search, a save) can wait for it.
RangeLink to "Range"
Pass [from, to] for two thumbs. Each thumb is limited by the other, so they can meet but never cross. Each thumb has its own name ("Price, minimum" and "Price, maximum"), and the pair is a group named by the label.
More thumbs, and your own trackLink to "More thumbs, and your own track"
Give three or more numbers for as many thumbs, each held between its neighbours: gradient stops, or the cut points of a timeline. trackContent draws your own track (a gradient, a histogram) under the thumbs, in place of the accent fill; it is as tall as a thumb.
<Slider
label="Gradient"
value={stops}
onChange={setStops}
trackContent={<span style={{ background: `linear-gradient(to right, ${colours})` }} />}
/>Marks and value textLink to "Marks and value text"
marks draws ticks along the track, with optional text under it. labels.value turns a number into words, used for showValue and read by screen readers.
PropsLink to "Props"
| Prop | What it does | Type | Default |
|---|---|---|---|
labelRequired | Accessible name, shown above the slider unless labelHidden. | string | |
labelHidden | Visually hide the label (it still names the slider). | boolean | |
value | A number, [from, to] for a range, or more numbers for more thumbs (each stays between its neighbours). | SliderValue | |
defaultValue | Starting value when uncontrolled. Default: min. | SliderValue | `min` |
onChange | Every change while dragging or pressing keys. | ((value: SliderValue) => void) | |
onCommit | When a change is finished: pointer released, or after each key press. | ((value: SliderValue) => void) | |
min | Lowest value. Default 0. | number | 0 |
max | Highest value. Default 100. | number | 100 |
step | Values snap to this step, and the arrow keys move by it. Default 1. | number | 1 |
largeStep | Page Up and Page Down step (default a tenth of the range). | number | |
orientation | vertical runs bottom to top. Default "horizontal". | "horizontal" | "vertical" | "horizontal" |
marks | Ticks on the track, with optional text under them. | SliderMark[] | |
trackContent | Your own drawing of the track (a gradient, a histogram, a waveform), filling it under the thumbs. The accent fill is not drawn then. | ReactNode | |
showValue | Show the current value (or range) at the end of the label row. | boolean | |
name | Form field name; a range submits two values under it. | string | |
disabled | Not focusable, cannot be changed and not submitted. | boolean | |
size | Track and thumb size. Default "md". | Size | "md" |
labels | Replace the built-in English text: the thumb names and the value text. | Partial<SliderLabels> | |
className | Added to the root element, for layout. | string | |
style | Layout only. Restyle through --cy-slider-* tokens. | CSSProperties |
Generated from the type definitions in @arshad-shah/cynosure 0.5.0.
Behaviour and accessibilityLink to "Behaviour and accessibility"
| Input | Result |
|---|---|
| Right arrow / Up arrow | Up by step (in right-to-left text, Right arrow lowers it) |
| Left arrow / Down arrow | Down by step |
| Page Up / Page Down | By largeStep (a tenth of the range by default) |
| Home / End | To min / max |
| Press or drag on the track | Moves the nearest thumb and keeps dragging |
- Roles: each thumb is
role="slider"witharia-valuemin,aria-valuemax,aria-valuenow,aria-valuetextandaria-orientation. A range is agroup. - Forms: with
name, the value is submitted (two values for a range) and a form reset restores the default. Disabled sliders are not submitted. - Disabled: thumbs leave the Tab order; the track fades but the label stays readable.
- Direction: horizontal sliders start at the inline start (right in right-to-left text); vertical ones start at the bottom.
TouchLink to "Touch"
The thumb keeps its size, but its hit area is 44px on coarse pointers. Horizontal drags belong to the slider, so the page does not scroll sideways, while a vertical swipe over it still scrolls the page.
ThemingLink to "Theming"
Tokens: --cy-slider-track-{sm,md,lg}, --cy-slider-thumb-{sm,md,lg}, --cy-slider-track (the empty bar colour) and --cy-slider-length (vertical length).
createTheme({ components: { slider: { thumbMd: "18px", trackMd: "6px" } } });RelatedLink to "Related"
These examples run Cynosure 0.5.0 (commit 50ce30b), the version on npm.