@biem/ui

Slider

A control that lets users set a value or a range of values by dragging a thumb along a track.

A slider lets users set a number by dragging a thumb along a track, and the filled part of the track shows what they have set. Users also change the value with a press on the track or with the arrow keys. The slider takes its values as an array with one entry per thumb, also when it has a single thumb. It has no label of its own, so the examples name it with a FieldTitle that aria-labelledby refers to.

Reorder level

Usage

slider-default.tsx
import { Field, FieldTitle } from "@biem/ui/components/ui/field";
import { Slider } from "@biem/ui/components/ui/slider";

export function SliderDefault() {
	return (
		<div className="w-full max-w-sm">
			<Field>
				<FieldTitle id="reorder-level">Reorder level</FieldTitle>
				<Slider aria-labelledby="reorder-level" defaultValue={[40]}/>
			</Field>
		</div>
	);
}

Examples

Range

Filters often ask for a span instead of a single figure, such as the products that sell between two and five cases a week. When defaultValue holds two entries, the slider offers two thumbs, and users set the lower and the upper limit of the span on one track.

Price range

Multiple Thumbs

A single scale can carry several thresholds, for example the stock levels at which an item counts as low, sufficient or overstocked. Each further entry in defaultValue adds a thumb, so users place every threshold on the same scale. During a drag, one thumb pushes the next one along, and the keyboard stops a thumb at its neighbor, so the thresholds always keep their order.

Discount tiers

Minimum and Maximum

Many values only make sense within natural limits, such as the shelf life of a product in days or the capacity of a shelf in cases. Through min and max the track covers just that span, so users cannot set a value outside it, and the full length of the track serves the values that matter.

Order quantity

Step

Goods often move in fixed packing units, such as a case of twelve bottles. Set to the size of such a unit, step makes the thumb snap to whole units counted from min, and each press of an arrow key moves it by one unit. Users therefore never set a quantity that would leave a case half full.

Transfer quantity

Minimum Distance Between Thumbs

A span that shrinks to a single value loses its meaning, for example a price band whose lower and upper limit coincide. With minStepsBetweenValues, neighboring thumbs keep at least that many steps between them, whether users drag a thumb or move it with the keyboard, so the span never narrows below a useful width.

Stock range

Center Alignment

Where a slider lines up with a scale or a chart axis beside it, every thumb should point at the exact place of its value, the ends included. The value "center" of thumbAlignment places the center of a thumb on the end of the track at min and max, so its position matches the scale along the whole length.

Sales range

Vertical

In a narrow side panel or next to a column of figures, height is often easier to spare than width. With orientation set to "vertical", users set the value along the height, with min at the bottom, so a drag upward raises it. Its height comes from the element around it, and it never falls below 10rem.

Target fill level

Controlled

Sometimes the application needs the values while users are still deciding, for example to update a forecast during a drag or to set the slider from a preset. Through value and onValueChange it holds the values itself, so other parts of the interface can display or change them. That matters all the more since the slider shows no number of its own.

Discount

15% off the shelf price.

Disabled

A value can stay fixed for a while, for example while a central rule governs it or until users have chosen the store it applies to. Once disabled is set, the slider accepts no interaction, exposes its state to assistive technology and is left out when the form is submitted, while users still see the value it holds.

Minimum stock

API Reference

Propvalue
Typenumber | readonly number[]
Default–
DescriptionSets the values of the slider and makes it controlled. Pass an array with one entry per thumb, also for a single thumb. With a plain number, the slider renders two thumbs for the one value. Update it in onValueChange.
PropdefaultValue
Typenumber | readonly number[]
Default–
DescriptionSets the values at first render. The slider then tracks its values itself. Pass an array with one entry per thumb, also for a single thumb. Without value and defaultValue, the slider renders two thumbs at min.
ProponValueChange
Type(value: number | readonly number[], eventDetails: SliderRootChangeEventDetails) => void
Default–
DescriptionRuns when users change a value, also on every move during a drag. It receives the values that follow: an array for several thumbs, and a number or an array with one entry for a single thumb.
ProponValueCommitted
Type(value: number | readonly number[], eventDetails: SliderRootCommitEventDetails) => void
Default–
DescriptionRuns when users have finished a change: when they release the pointer after a drag or a press on the track, and after every change with the keyboard. It doesn't run when the values stayed the same. It receives the values in the same form as onValueChange.
Propmin
Typenumber
Default0
DescriptionSets the lowest value that users can set. It must be lower than max.
Propmax
Typenumber
Default100
DescriptionSets the highest value that users can set. It must be higher than min.
Propstep
Typenumber
Default1
DescriptionSets the distance between two values that users can set. The steps count from min, and decimal numbers work as well. Choose a step that divides the distance between min and max evenly.
ProplargeStep
Typenumber
Default10
DescriptionSets how far a thumb moves when users press Page Up or Page Down, or an arrow key together with Shift.
PropminStepsBetweenValues
Typenumber
Default0
DescriptionSets how many steps two neighboring thumbs keep between them. It has an effect only on a slider with several thumbs.
PropthumbCollisionBehavior
Type"push" | "swap" | "none"
Default"push"
DescriptionSets what happens when users drag one thumb into another. With "push", the dragged thumb pushes the other one along, and the pushed thumb stays where it ends up. With "swap", the dragged thumb moves past the other one. With "none", the dragged thumb stops at the other one. With the keyboard, a thumb always stops at its neighbor.
PropthumbAlignment
Type"center" | "edge" | "edge-client-only"
Default"edge"
DescriptionSets where a thumb sits when its value is min or max. With "edge", the thumb stays inside the track, and its edge meets the end of the track. With "center", the center of the thumb sits on the end of the track, so that half of the thumb reaches beyond it. "edge-client-only" places the thumb like "edge", but only after hydration on the client, which reduces the bundle size.
Proporientation
Type"horizontal" | "vertical"
Default"horizontal"
DescriptionSets the direction of the slider. A horizontal slider fills the width of its container. A vertical slider has min at the bottom, fills the height of its container and is at least 10rem tall.
Propdisabled
Typeboolean
Defaultfalse
DescriptionPrevents users from changing the values and exposes the disabled state to assistive technology. A disabled slider is not submitted with the form.
Propformat
TypeIntl.NumberFormatOptions
Default–
DescriptionSets how the slider formats the value that it announces to assistive technology, for example as a currency. The slider itself shows no number.
Proplocale
TypeIntl.LocalesArgument
Default–
DescriptionSets the locale that format uses. Without it, the slider uses the locale of the runtime.
Propname
Typestring
Default–
DescriptionIdentifies the slider when the form is submitted. Every thumb submits its value under this name.
Propform
Typestring
Default–
DescriptionSets the ID of the form that the slider belongs to. Set it when the slider is rendered outside of that form.
Proparia-labelledby
Typestring
Default–
DescriptionSets the ID of the element that names the slider. The slider passes it on to every thumb, so that assistive technology announces the name together with the value.
Proprender
TypeReactElement | render function
Default–
DescriptionReplaces the native element or composes the slider with another component.