Slider
A number picked along a range, where the position says as much as the number, such as a model's temperature. For a value nudged a step at a time, use a Stepper.
import { Slider } from '@adecore/ui';import { Slider } from '@adecore/ui';
import { useState } from 'react';
export default function SliderDemo() {
const [agents, setAgents] = useState(8);
const [temperature, setTemperature] = useState(0.7);
const [volume, setVolume] = useState(40);
const [sent, setSent] = useState(40);
return (
<div className="flex w-72 flex-col gap-4">
<Slider label="Parallel agents" value={agents} onValueChange={setAgents} min={1} max={16} />
<Slider label="Temperature" value={temperature} onValueChange={setTemperature} min={0} max={1} step={0.05} decimals={2} />
<Slider label="Volume" value={volume} onValueChange={setVolume} onValueCommitted={setSent} min={0} max={100} unit="%" heading={false} />
<span className="text-xs text-text-muted">Sent to the speaker: {sent}%</span>
<Slider label="Context window" value={200} onValueChange={() => {}} min={50} max={1000} step={50} unit="k" disabled />
</div>
);
}The label above the track is the accessible name; the value sits on the right in tabular figures. The arrows move a step, Page Up and Page Down a tenth of the range, Home and End to either end.
The value is controlled. It stays within min and max, snaps to a step from min, and is rounded so 0.2 plus 0.1 lands on 0.3. It shows as many decimals as step, unless you pass decimals. The unit follows the number on screen and for screen readers.
On release
onValueChange follows the pointer, so a drag calls it many times. Work that only needs the last value, such as a command to a device, goes in onValueCommitted. It runs when a drag ends and after each key press, with the value rounded the same way.
<Slider label="Volume" value={volume} onValueChange={setVolume} onValueCommitted={(next) => speaker.setVolume(next)} min={0} max={100} unit="%" heading={false} />Without the heading
heading={false} leaves out the label and value above the track, for a slider beside an icon and a reading of its own, or in a row of a list. The label then names the thumb, and screen readers still hear the value with its unit.
Props
| Prop | Type | Default | |
|---|---|---|---|
value | number | Required. | |
onValueChange | (value: number) => void | Required. Called on every step and while dragging. | |
onValueCommitted | (value: number) => void | Called when a drag ends and after a key press. | |
min | number | Required. | |
max | number | Required. | |
step | number | 1 | |
label | string | Required. | |
heading | boolean | true | False leaves out the label and value above the track. |
decimals | number | the places of step | |
unit | string | Printed right after the number, such as px or %. | |
disabled | boolean | ||
className | string | On the root, which fills the width it is given. | |
ref | Ref<HTMLDivElement> |
SliderProps is an exported type.