Slider
A range input with a live readout, or a range with two thumbs.
Demo
Usage
import { Slider } from '@tuttn/slant'
// Controlled: pass value and onValueChange, which gets a number.
const [speed, setSpeed] = useState(1.4)
<Slider value={speed} onValueChange={setSpeed} min={0.5} max={3} step={0.1}
label="Agent speed" format={value => `${value.toFixed(1)}×`} />
// Uncontrolled: leave value out; it starts from defaultValue (or min).
// The ref and attributes such as name reach the range input itself.
const ref = useRef<HTMLInputElement>(null)
<Slider ref={ref} name="budget" defaultValue={40} step={5} label="Monthly budget" />
// A range: a [low, high] pair gives two thumbs, and onValueChange gets a pair.
// The thumbs stop minDistance apart; a form gets both under the name.
const [hours, setHours] = useState<readonly [number, number]>([9, 17])
<Slider value={hours} onValueChange={setHours} min={0} max={24} minDistance={1}
label="Working hours" name="hours" />
// Inset: a tall track with the name and value written inside it.
<Slider variant="inset" defaultValue={60} step={5} label="Review volume"
format={value => `${value}%`} />Props
| Prop | Type | Default | Description |
|---|---|---|---|
value | V | — | The current value: a number, or a `[low, high]` pair for a range with two thumbs. Pass it to control the slider; leave it out to let the slider keep its own. |
defaultValue | V | min | The value an uncontrolled slider starts at. A pair makes it a range. |
onValueChange | (value: V) => void | — | Called with the new value, in the shape it was given (a number or a pair), while a thumb moves. |
min | number | 0 | The lowest value. |
max | number | 100 | The highest value. |
step | number | 1 | The increment the thumbs and the arrow keys move by. |
label | string | — | The accessible name. The default variant shows it in the head row (a range names its group with it); the inset variant writes it inside the track. |
format | (value: number) => string | — | Formats the readout, and each thumb's default `aria-valuetext`; the raw value otherwise. |
id | string | — | The range input's id (a range's lower thumb), so an external label can reach it; generated when omitted. A range's upper thumb takes `<id>-upper`. |
disabled | boolean | — | Dims the slider and ignores input. |
minDistance | number | 0 | A range's smallest gap between its two thumbs: neither can pass the other, and they stop this far apart. |
thumbLabels | readonly [string, string] | messages.rangeMin(label) and messages.rangeMax(label) | A range's thumb names, lower then upper. |
variant | 'default' | 'inset' | 'default' | `default` is a thin track under a head row with the name and the value. `inset` is a tall track with the name and the value written inside it, over the fill. A range always draws the default track. |
…rest | InputHTMLAttributes<HTMLInputElement> | — | The ref and every other attribute (aria-*, handlers, data-*) reach the <input type="range">, a range’s lower thumb; className and style go on the wrapper. name and form go on both of a range’s thumbs. |
Each thumb is a native range input, so the arrow keys move it by step, Page Up and Page Down by larger steps, and Home and End to the ends; under <Slant dir="rtl"> the track and its fill run from the right. With format, each input’s aria-valuetext is the formatted value as well, so a screen reader hears “$40” rather than “40”. Pass your own aria-valuetext to override it.
A range is a group named by label, holding two sliders named “label, minimum” and “label, maximum” (thumbLabels renames them; the defaults come from the root’s messages.rangeMin and rangeMax). Neither thumb passes the other. A form submits both values under one name, low then high, so FormData.getAll(name) reads the pair.
The inset variant draws the name and the value inside the track, in --sl-text over the empty part and in --sl-accent-text over the fill. That text is hidden from assistive technology: the range input carries the same name and value. Inset takes a single thumb; a range always draws the default track.