Slider
Picks a number or a range by dragging a thumb along a track. Add a histogram above a two-thumb slider for a price filter.
Usage
import {
Slider,
SliderControl,
SliderLabel,
SliderThumb,
SliderValue,
} from 'nyctoui/slider'
<Slider defaultValue={40}>
<SliderLabel>Volume</SliderLabel>
<SliderValue />
<SliderControl>
<SliderThumb />
</SliderControl>
</Slider>The label and value share the first row; the control spans the full width below them. The whole 24px row is pressable, so the 4px track is easy to hit, and it lines up with small buttons and inputs.
Good for
- A price range filter in a shop, with a histogram of prices above it.
- Volume or brightness in a media or smart home app.
- Brush size or opacity in a drawing app.
- A loan amount or a term in a finance calculator.
Examples
Single value
format takes Intl.NumberFormat options and applies to both SliderValue and what screen readers announce.
Range
Pass an array to get one thumb per value. Give each thumb its index and an aria-label, and pass a function to SliderValue to write the range your own way.
More than two thumbs
The array can hold any number of values, one thumb each. The indicator runs from the first thumb to the last, so the outer thumbs bound a span and the inner ones mark points within it, here a three-point estimate.
Thumbs push each other when they meet; set thumbCollisionBehavior to 'swap' or 'none' to change that.
Price filter
The demo at the top of the page. SliderHistogram draws one bar per bucket between min and max, sitting on the track. Bars inside the selected range turn solid as the thumbs move. The inputs below are ordinary Inputs that share the slider’s controlled value and commit on blur or Enter.
minStepsBetweenThumbs keeps the two thumbs from landing on the same price.
Marks
SliderMark puts a dot on the track, with its children as a label underneath. Dots the indicator has passed turn light. Here the slider steps through an index, and the marks label each step.
Vertical
With orientation="vertical" the control is 160px tall; set a different height through its sx. Up and down arrows move the thumb.
Disabled
Keyboard
The arrow keys move the focused thumb by step; Page Up and Page Down, or Shift with an arrow, move it by largeStep. Home and End jump to the ends.
Best practices
- Label every slider, even when the label is visually hidden.
- Show the value with units, like “$50” or “75%”, using
formatandSliderValue. - Pair it with a number input when people sometimes need an exact value.
- Use a slider for precise entry, like a price to the cent.
- Set a
stepso large that only a few positions are left. Show those as segments instead.
API reference
Slider
Wraps ui-sdk’s Slider in a Field, so the label names it, and takes its props, including value, onValueChange, onValueCommit, largeStep and name.
| Prop | Type | Default |
|---|---|---|
defaultValueInitial value when uncontrolled. An array gives one thumb per value. | number | number[] | — |
minLowest value. | number | 0 |
maxHighest value. | number | 100 |
stepValues snap to multiples of this, counted from min. | number | 1 |
minStepsBetweenThumbsSteps the thumbs of a range must keep between them. | number | 0 |
formatHow the value is written in SliderValue and for screen readers. | Intl.NumberFormatOptions | ((value: number) => string) | — |
orientationDirection of the track. | 'horizontal' | 'vertical' | 'horizontal' |
disabledIgnores input and dims the slider. | boolean | false |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
SliderLabel
A <label> in the first row. Names the thumb, or the group of a range.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
SliderValue
An <output> at the end of the first row showing the formatted value.
| Prop | Type | Default |
|---|---|---|
childrenWrite the value yourself instead of the default. | (state: { text: string; value: number | number[] }) => ReactNode | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
SliderControl
The pressable row with the track and indicator. Holds the thumbs and marks.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
SliderThumb
The draggable knob, with a hidden range input inside.
| Prop | Type | Default |
|---|---|---|
indexWhich value in the array this thumb moves. Needed for ranges. | number | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
SliderMark
A <span> dot on the track, with an optional label below it.
| Prop | Type | Default |
|---|---|---|
valueWhere the mark sits, in the slider’s units. | number | — |
childrenLabel drawn under the dot. | ReactNode | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
SliderHistogram
A <div> of bars sitting on the track. Place it before SliderControl. Horizontal only.
| Prop | Type | Default |
|---|---|---|
dataCount per bucket, splitting min to max into equal steps. | number[] | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |