Web · Components
Slider
Slider for picking a value or a range of values.
4 exports
Live example
Preview
Code
Events
- Events appear here when you interact with the preview.
Volume: a single value, bubble on hover
Storage: marks only (1, 2, 4, 8, 16 GB)
Balance: fill in both directions from the middle (origin)
Working hours: three thumbs, value always visible
Vertical: equalizer
Controlled: catch the value with onValueCommit
Live: [25,75] · On release: [25,75]
Import
Slider for picking a value or a range of values. Each thumb contains a real <input type="range">; so it works out of the box with labels, forms, the keyboard and screen readers (including swipe up/down in iOS VoiceOver).
- Single value or range:
defaultValue={50}is a single thumb,defaultValue={[20, 80]}a range. Thumbs don't pass each other;minStepsBetweensets the minimum gap. More than two thumbs work too ([10, 40, 70]). - Mouse and touch: Clicking the track moves the nearest thumb there; grabbing a thumb drags it without jumping. For stacked thumbs the right one is chosen by drag direction. With
draggableRangethe fill in between can be grabbed to move the whole range. - Mobile: Swiping up/down over a horizontal slider scrolls the page and doesn't change the value; the value changes only when pulled sideways or tapped. Supported devices vibrate lightly when snapping to marks. The touch area is larger than the visual.
- Keyboard: ←/→ (↑/↓) one step, with Shift or PageUp/PageDown a big step, Home/End to the ends. In right-to-left languages horizontal arrows work in reverse.
- Format: With
formatit shows like$1,250,40%,16 GB; the bubble, mark labels and screen reader use the same text. - Form: With
nameeach thumb's value joinsFormData; on form reset it returns to the initial value.
To use it with a label, value display and error message see SliderField.
Examples
SliderValue
typeValue of the slider: a number for a single thumb, a number array for a range.
SliderProps
interfaceProps of the Slider component.
Extends: Omit<ComponentProps<"div">, "defaultValue" | "onChange" | "children">
| Name | Type | Default | Description |
|---|---|---|---|
value? | V | Value in controlled use. A number draws a single thumb, an array draws a thumb for each element ([20, 80] → range selection). onValueChange returns the value in the same form it was given. | |
defaultValue? | V | min | Initial value in uncontrolled use. |
onValueChange? | (value: V) => void | Called whenever the value changes: on every step while dragging, with keys and when clicking the track. | |
onValueCommit? | (value: V) => void | Called with the final value when the user lets go (drag ends, a key is pressed). For expensive work like a server request or URL update. | |
min? | number | 0 | Minimum value. |
max? | number | 100 | Maximum value. |
step? | number | 1 | Increment of the value. |
largeStep? | number | 10 times `step` (at most a tenth of the range) | Step of Shift + arrow, PageUp and PageDown. |
minStepsBetween? | number | 0 | Minimum number of steps that must stay between thumbs in a range (2 and step={5} → at least 10 apart). |
marks? | boolean | readonly (number | SliderMark)[] | Marks on the track. true puts a dot at every step (at most 100 steps). A number array draws only dots, a { value, label } array also draws labels below. Clicking a label goes to that value. | |
snapToMarks? | boolean | false | The value can only be one of the marks (e.g. [1, 2, 4, 8, 16] GB). Marks are spread evenly on the track (even if their values aren't proportional); arrows go to the next mark. |
valueLabel? | "auto" | "always" | "never" | "auto" | Value bubble above the thumb: "auto" while dragging, on hover and on keyboard focus; "always" always; "never" never. |
format? | Intl.NumberFormatOptions | ((value: number) => ReactNode) | How the value is written: Intl.NumberFormat options ({ style: "currency", currency: "TRY" }) or a function ((v) => \${v} GB\``). Used in the bubble, labels and screen reader. | |
locale? | string | {@link UIProvider}'s `locale | Language of numbers (BCP 47). |
orientation? | "horizontal" | "vertical" | "horizontal" | Direction. Vertically it increases from bottom to top. |
origin? | number | min | Value where the fill starts for a single thumb. In ranges like -50 … 50, passing 0 makes the fill grow from the middle in both directions. |
draggableRange? | boolean | false | In a range the fill between two thumbs can be grabbed and dragged; the whole range moves while keeping its width. |
tone? | Tone | "primary" | Color. |
size? | Size | "md" | Thickness and thumb size. |
name? | string | readonly string[] | Name submitted with the form. In a range each thumb is submitted with the same name (formData.getAll("price") → ["100", "500"]) or a name per thumb is given (["minPrice", "maxPrice"]). | |
form? | string | Id of the form it belongs to (if used outside the form). | |
disabled? | boolean | Disabled. | |
readOnly? | boolean | The value is visible but can't be changed; it is submitted with the form. | |
thumbLabels? | readonly string[] | Thumb names for screen readers. If omitted, a range uses "Başlangıç" / "Bitiş" (UIMessages). |
SliderMark
typeA mark on the slider: a value and (optional) label.
| Name | Type | Description |
|---|---|---|
value | number | Value of the mark. |
label? | Label | Label written under the mark (beside it when vertical). Only a dot is drawn when omitted. |