Slider
A number, or a range of two, picked by moving a thumb along a track.
Installation
pnpm dlx @human-kit/design add slidernpx @human-kit/design add slideryarn dlx @human-kit/design add sliderbunx @human-kit/design add sliderThe command copies the files to src/lib/components/ui/slider. It also
installs the packages that the files import.
Usage
<script lang="ts">
import * as Slider from '$lib/components/ui/slider';
let weight = $state(72);
</script>
<Slider.Root label="Weight" bind:value={weight} min={30} max={150} /> Examples
Steps
step sets the values the thumb lands on. With a few of them, marks draws a dot at each, so the scale reads before it is touched.
Range
Give value an array and the slider draws one thumb per number. minStepsBetweenThumbs keeps them apart, and thumbLabels names each one for a screen reader.
Guidelines
- Use a slider for a value where about is good enough, and where seeing it move helps: a weight, a range of ages, a volume.
- For an exact number, use a number field. A slider over a wide range is hard to stop on one value.
- Keep
showValue: the number at the end of the label is what tells the user where the thumb is. - Format the value with
formatOptions, as a unit or a percent, so it reads as what it is. - Use
onChangeEnd, notonChange, for work that must not run on each move of a drag, as a request to the server.
Accessibility
- The root is a
role="group"named bylabel, or byaria-labelwhen there is none. - Each thumb holds a native
<input type="range">witharia-valuenowandaria-valuetext. A range of two thumbs names them "Minimum" and "Maximum" unlessthumbLabelssays otherwise. - The arrows step,
PageUp,PageDownandShiftwith an arrow move a large step, andHomeandEndgo to the ends. The horizontal arrows follow the direction of the text. - A press on the track moves the thumb there and focuses it without a ring. A key after it shows the ring.
descriptiondescribes the slider througharia-describedby.invalidputsaria-invalidon the thumbs.
API reference
| Prop | Type | Default |
|---|---|---|
value | number | number[] One number for one thumb, an ascending array for a range. Bindable. | — |
defaultValue | number | number[] The value at the start, when you give no | — |
min | number The lowest value. | 0 |
max | number The highest value. | 100 |
step | number The distance between two values. | 1 |
minStepsBetweenThumbs | number The steps two thumbs of a range keep between them. | 0 |
marks | boolean Draws a dot at each step, up to fifty of them. | false |
label | string The name over the track. Without it, give | — |
description | string A line under the track. | — |
showValue | boolean Shows the value at the end of the line of the label. | true |
formatOptions | Intl.NumberFormatOptions The format of the value shown and read aloud. | — |
thumbLabels | string[] One name per thumb, for a range: | — |
orientation | 'horizontal' | 'vertical' The direction of the track. A vertical one is at least 96px tall and grows into a parent with a height. | 'horizontal' |
onChange | (value: number | number[], details: SliderChangeDetails) => void Called on each change, also on each move of a drag. | — |
onChangeEnd | (value: number | number[], details: SliderChangeDetails) => void Called when a drag or a key press ends. | — |
disabled | boolean Turns the slider off and takes the thumbs out of the tab order. | false |
readonly | boolean Keeps the thumbs in the tab order, but the value does not change. | false |
invalid | boolean Marks the value as wrong: the fill and the label turn red. | false |
element | HTMLDivElement | null The rendered element. Bindable. | — |
Plus every attribute of a <div>, and id, name, form, controlledValue, largeStep, getValueText and aria-labelledby of the primitive.