Slider
Slider.Root is a native <input type="range">. The browser handles arrow keys, Page Up and Down, Home and End, and announces the value. A single slider draws its thumb from slider.fill. Chrome 135 and later, and Safari, draw the filled track from that thumb. Chrome before 135 leaves the track as ButtonFace. Firefox paints the filled track with its own progress.
On this page
import { Field, Label, Slider, Stack, Text } from '@tounsoo/todae';
import { useId, useState } from 'react';
export default function SliderVolume() {
const id = useId();
const [value, setValue] = useState(40);
return (
<Stack.Root>
<Field.Group>
<Label.Root htmlFor={id}>Volume</Label.Root>
<Slider.Root
id={id}
min={0}
max={100}
step={5}
value={value}
onChange={(event) => setValue(Number(event.target.value))}
/>
</Field.Group>
<Text.Root>{value}%</Text.Root>
</Stack.Root>
);
}
40%
Server Components can import it from @tounsoo/todae/slider. Inside a Fieldset, Slider.Root from @tounsoo/todae takes the Fieldset's disabled and invalid, and its own aria-invalid wins. @tounsoo/todae/slider reads no Fieldset, since that needs a client component. Name it with Label and show the current value as text nearby when the exact number matters. Restyle through slider.fill and slider.ring.focus. mode="range" puts two thumbs on one track. A thumb stops when it meets the other.
Range
mode="range" places two native range inputs on one track. startLabel and endLabel name the two thumbs. startValueText and endValueText are what a screen reader says for each thumb instead of the number. value holds the start and the end. onValueChange receives the next pair. The filled part runs from the start thumb to the end thumb. name is the two field names. Omit name and neither input submits.
import { Slider, Stack, Text } from '@tounsoo/todae';
import { useState } from 'react';
export default function SliderRange() {
const [value, setValue] = useState<[number, number]>([20, 80]);
return (
<Stack.Root>
<Slider.Root
mode="range"
min={0}
max={100}
startLabel="Minimum"
endLabel="Maximum"
value={value}
onValueChange={setValue}
/>
<Text.Root>
{value[0]} to {value[1]}
</Text.Root>
</Stack.Root>
);
}
20 to 80
Accessible composition
Todae handles: Styles. One thumb is a native <input type="range"> with the slider role, its value, minimum and maximum, and the keys above. mode="range" also stops a thumb where it meets the other, fills the track between them, and moves the nearer thumb when the empty track is pressed. A single slider draws its thumb from slider.fill. Chrome 135 and later, and Safari, draw the filled track from that thumb. Chrome before 135 leaves the track as ButtonFace. Firefox paints the filled track with its own progress. The focus outline comes from slider.ring.focus while the slider is focus-visible.
You supply: A name. For one thumb, a Label with htmlFor or aria-label. For two, startLabel and endLabel. aria-valuetext on one thumb, or startValueText and endValueText on a range, when the number alone does not mean much, such as "40%" or "Medium". The current value as visible text when the exact number matters, and aria-describedby for any hint.
Watch for: A div slider built by hand needs its role, value attributes and keys added one by one; inside a Field, see useField. An outline: none of yours removes the focus ring.
When to use
When: Picking a value in a range where the rough position matters more than the exact number, such as volume or zoom.
When not: An exact number → Field.NumberInput. A few named choices → Radio. Showing progress the user does not control → Progress.
Props
| Name | Type | Description |
|---|---|---|
mode | 'single' | 'range' | One thumb, or "range" for two thumbs on one track. A thumb stops when it meets the other. Default: 'single' |
min | number | string | The lowest value. Default: 0 |
max | number | string | The highest value. Default: 100 |
step | number | string | How far one arrow key moves the value. Default: 1 |
value | number| string| readonly [number, number] | The value, with onChange. With mode="range", the start and the end, with onValueChange. |
defaultValue | number| string| readonly [number, number] | The starting value, when the slider keeps it. With mode="range", the starting pair. |
onValueChange | (value: [number, number]) => void | With mode="range", called with the next start and end after a gesture. |
startLabel | string | Required when mode is "range". The accessible name of the start thumb. |
endLabel | string | Required when mode is "range". The accessible name of the end thumb. |
name | string | readonly [string, string] | The field name. With mode="range", the start field name and the end field name. Omit it and neither range input submits. |
startValueText | string | With mode="range", what a screen reader says for the start thumb instead of the number. |
endValueText | string | With mode="range", what a screen reader says for the end thumb instead of the number. |
aria-valuetext | string | On one thumb, what a screen reader says instead of the number, such as "40%" or "Medium". On a range, use startValueText and endValueText. |