Components

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

Do
  • Label every slider, even when the label is visually hidden.
  • Show the value with units, like “$50” or “75%”, using format and SliderValue.
  • Pair it with a number input when people sometimes need an exact value.
Don’t
  • Use a slider for precise entry, like a price to the cent.
  • Set a step so 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.

PropTypeDefault
defaultValue
Initial value when uncontrolled. An array gives one thumb per value.
number | number[]—
min
Lowest value.
number0
max
Highest value.
number100
step
Values snap to multiples of this, counted from min.
number1
minStepsBetweenThumbs
Steps the thumbs of a range must keep between them.
number0
format
How the value is written in SliderValue and for screen readers.
Intl.NumberFormatOptions | ((value: number) => string)—
orientation
Direction of the track.
'horizontal' | 'vertical''horizontal'
disabled
Ignores input and dims the slider.
booleanfalse
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

SliderLabel

A <label> in the first row. Names the thumb, or the group of a range.

PropTypeDefault
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

SliderValue

An <output> at the end of the first row showing the formatted value.

PropTypeDefault
children
Write the value yourself instead of the default.
(state: { text: string; value: number | number[] }) => ReactNode—
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

SliderControl

The pressable row with the track and indicator. Holds the thumbs and marks.

PropTypeDefault
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

SliderThumb

The draggable knob, with a hidden range input inside.

PropTypeDefault
index
Which value in the array this thumb moves. Needed for ranges.
number—
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

SliderMark

A <span> dot on the track, with an optional label below it.

PropTypeDefault
value
Where the mark sits, in the slider’s units.
number—
children
Label drawn under the dot.
ReactNode—
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

SliderHistogram

A <div> of bars sitting on the track. Place it before SliderControl. Horizontal only.

PropTypeDefault
data
Count per bucket, splitting min to max into equal steps.
number[]—
sx
StyleX styles merged after the built-in ones.
StyleXStyles—