Skip to the content
human-kit design
14 — Form

Slider

A number, or a range of two, picked by moving a thumb along a track.

Weight 72 kg

Installation

pnpm dlx @human-kit/design add slider
npx @human-kit/design add slider
yarn dlx @human-kit/design add slider
bunx @human-kit/design add slider

The 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.

Pain 3

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.

Age 25–40

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, not onChange, 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 by label, or by aria-label when there is none.
  • Each thumb holds a native <input type="range"> with aria-valuenow and aria-valuetext. A range of two thumbs names them "Minimum" and "Maximum" unless thumbLabels says otherwise.
  • The arrows step, PageUp, PageDown and Shift with an arrow move a large step, and Home and End go 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.
  • description describes the slider through aria-describedby. invalid puts aria-invalid on the thumbs.

API reference

PropTypeDefault
valuenumber | number[]

One number for one thumb, an ascending array for a range. Bindable.

—
defaultValuenumber | number[]

The value at the start, when you give no value. The default is min.

—
minnumber

The lowest value.

0
maxnumber

The highest value.

100
stepnumber

The distance between two values.

1
minStepsBetweenThumbsnumber

The steps two thumbs of a range keep between them.

0
marksboolean

Draws a dot at each step, up to fifty of them.

false
labelstring

The name over the track. Without it, give aria-label.

—
descriptionstring

A line under the track.

—
showValueboolean

Shows the value at the end of the line of the label.

true
formatOptionsIntl.NumberFormatOptions

The format of the value shown and read aloud.

—
thumbLabelsstring[]

One name per thumb, for a range: ["Youngest", "Oldest"].

—
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.

—
disabledboolean

Turns the slider off and takes the thumbs out of the tab order.

false
readonlyboolean

Keeps the thumbs in the tab order, but the value does not change.

false
invalidboolean

Marks the value as wrong: the fill and the label turn red.

false
elementHTMLDivElement | null

The rendered element. Bindable.

—

Plus every attribute of a <div>, and id, name, form, controlledValue, largeStep, getValueText and aria-labelledby of the primitive.