Slider
A slider picks a number, or a range between two numbers, from a scale. A number box beside the track lets users type the exact value.
Installation
npm gives you every component from the package. shadcn copies the source into your app instead: a copy pinned to this version that you can change. The shadcn commands need the @kanok-ui registry in your components.json once, see Installation.
Use it
The web component fires input while the value moves (dragging, keys, typing) and change when it is committed: the pointer released, a key handled, the number box left. In React those are onInput / onChange on UiSlider. The Tailwind Slider calls onChange(value) on every change and onRelease(value) on commits.
Scale and step
min and max set the ends (0 and 100 by default) and step the increment (1). Arrow keys move one step; with Shift they move step-multiplier steps (4 by default). min-label and max-label add a unit after the end numbers.
Format the labels
For more control, the formatLabel property (a function (value, label) => string) formats the end labels and the value screen readers hear. It is a property on the web component and a prop in both React versions. With hide-text-input, the tooltip on the thumb uses it too, so money reads as money.
Two handles: a range
Set value-upper and the slider picks a range from value to value-upper. The two thumbs never pass each other, a press on the track moves the nearer one, and each has its own number box: the lower one before the track, the upper one after.
Search from
The form gets both values: under name-upper for the upper one, or both under name when you leave name-upper out. The thumbs are named "Lower bound" and "Upper bound" after the slider's label; input-label and upper-input-label rename the number boxes.
The number box
The box mirrors the value, and typing changes it:
- A number in range applies at once, snapped to the nearest step.
- On blur or Enter, a number outside
min/maxis clamped to the nearest end. Screen readers hear that it was corrected (corrected-textchanges the words;{value}is the typed number). - A box that holds no number (cleared, say) shows the error "Enter a number" and blocks submit until it is fixed.
hide-text-input removes the box and shows the value in a tooltip on the thumb instead:
Errors and warnings
invalid with invalid-text shows an error from your own validation and blocks submit; warn with warn-text shows a warning that doesn't. The slider has no helper line.
How errors show across fields: Forms and validation.
Read-only and disabled
readonly keeps the value and submits it; the thumb can't be moved and the number box can't be edited. A read-only thumb leaves the Tab order unless the box is hidden, so users still reach the value. disabled greys everything out and nothing is submitted.
The web component and its React wrapper spell it readonly; React + Tailwind uses readOnly.
Show the value as it moves
Use input to update what depends on the value while the thumb moves, and change to save once it settles. In React + Tailwind those are onChange and onRelease.
On a ฿1,200,000 vehicle: . Saved:
save stands for your own code.
In a form
With a name, the slider submits its value with the form; a range also submits name-upper. Form reset puts the thumbs back where they started.
Submitted:
Skeleton
While the page loads its data, show <ui-slider-skeleton> in the slider's place, as Carbon's SliderSkeleton. two-handles (twoHandles in React + Tailwind) draws a range and hide-label leaves out the label bar. It has its own import, @purpurabog/ui/slider-skeleton, and is hidden from screen readers.
Keyboard
On a thumb:
| Key | Does |
|---|---|
| ← / ↓ | One step down |
| → / ↑ | One step up |
| Shift + arrow | step-multiplier steps (4 by default) |
| Home / End | To min / max (in a range, as far as the other thumb allows) |
In the number box, Enter commits the typed number.
Accessibility
- Each thumb is a
role="slider"witharia-valuemin,aria-valuemax,aria-valuenowandaria-valuetext, named by the label. In a range each thumb's bounds stop at the other thumb. - A click on the label focuses the thumb, as a label focuses a native range input.
- The number box is a native number input with its own name ("Slider number input", or "Lower bound" / "Upper bound";
input-labelandupper-input-labelchange them). - An error sets
aria-invalidand is linked witharia-describedby. A clamped number is announced in a polite live region.
Unstyled React parts with the same behaviour: Headless Slider.
API
Generated from the package. In React, pass a property as a prop of the same name; events are props too (the React column).
<ui-slider> · React UiSlider
Attributes
| Attribute | Property | Type | Default | Description |
|---|---|---|---|---|
label |
label |
string |
'' |
|
hide-label |
hideLabel |
boolean |
false |
|
value |
value |
number |
0 |
|
value-upper |
valueUpper |
number | null |
null |
A second thumb: the slider picks the range value…value-upper (Carbon unstable_valueUpper). |
name-upper |
nameUpper |
string |
'' |
The name the upper value is submitted under (default: name, so the form gets both under one name). |
upper-input-label |
upperInputLabel |
string |
'' |
Accessible name of the upper number box (Carbon unstable_ariaLabelInputUpper; default "Upper bound"). |
min |
min |
number |
0 |
|
max |
max |
number |
100 |
|
step |
step |
number |
1 |
|
step-multiplier |
stepMultiplier |
number |
4 |
Shift + arrow moves by step × step-multiplier. |
min-label |
minLabel |
string |
'' |
Appended to the min / max numbers, e.g. "%" (Carbon minLabel / maxLabel). |
max-label |
maxLabel |
string |
'' |
|
input-label |
inputLabel |
string |
'' |
Accessible name of the number input (Carbon ariaLabelInput). |
hide-text-input |
hideTextInput |
boolean |
false |
|
name |
name |
string |
'' |
|
disabled |
disabled |
boolean |
false |
|
readonly |
readonly |
boolean |
false |
|
required |
required |
boolean |
false |
|
invalid |
invalid |
boolean |
false |
|
invalid-text |
invalidText |
string |
'' |
|
warn |
warn |
boolean |
false |
|
warn-text |
warnText |
string |
'' |
|
corrected-text |
correctedText |
string |
'' |
Announcement after an out-of-range number is clamped; {value} is the typed number (Carbon auto-correct message). |
Properties
| Property | Type | Description |
|---|---|---|
formatLabel |
SliderFormatLabel |
Formats min / max labels and the value tooltip (Carbon formatLabel). |
willValidate |
(read-only) Whether the field takes part in constraint validation (not while disabled), as natively. | |
labels |
(read-only) The <label>s that point at this field. |
|
isRange |
(read-only) Two thumbs (value-upper set). |
Methods
| Method | Description |
|---|---|
setCustomValidity(message) |
Like <input>.setCustomValidity(): a non-empty message makes the field invalid (and is shown as its error once touched) until cleared with ''. |
focus(options) |
Focus the handle. |
Events
| Event | React | Description |
|---|---|---|
input |
onInput |
|
change |
onChange |
Slots
| Slot | Description |
|---|---|
label |
Rich label (overrides label) |
invalid-text |
Rich error text |
warn-text |
Rich warning text |
CSS parts
| Part | Description |
|---|---|
label |
The label |
base |
Row with labels, track and input |
min-label |
Minimum label |
max-label |
Maximum label |
track |
The 2px track |
filled-track |
The filled part |
thumb |
The handle (role=slider) |
thumb-lower |
With value-upper: the lower handle (also thumb) |
thumb-upper |
With value-upper: the upper handle (also thumb) |
input |
The number input |
input-lower |
With value-upper: the lower bound's input (also input) |
input-upper |
With value-upper: the upper bound's input (also input) |
error-text |
Error text below |
invalid-text |
Same line as error-text (named after the invalid-text attribute / slot) |
warning-text |
Warning text below |
warn-text |
Same line as warning-text (named after the warn-text attribute / slot) |
CSS custom properties
| Custom property | Default | Description |
|---|---|---|
--ui-field-border |
/ --ui-field-radius / --ui-field-background - The number box, as the other fields | |
--ui-slider-fill |
The thumb, the filled track and the range handles (default --ui-layer-selected-inverse; Kanok: --ui-interactive) |
|
--ui-slider-handle-hover |
A range handle under the pointer (default --ui-ink-muted) |
|
--ui-slider-tick |
display of the mark at the middle of the track (default inline-block; Kanok: none) |
|
--ui-slider-range-handle-width |
/ --ui-slider-range-handle-height / --ui-slider-range-lower-offset / --ui-slider-range-upper-offset - A range's handle box and where it sits on its value (default 16 × 24px, beside the value: Carbon's flags) | |
--ui-slider-range-thumb |
/ --ui-slider-range-thumb-hover / --ui-slider-range-thumb-inset / --ui-slider-range-thumb-radius / --ui-slider-range-focus-ring - A range thumb drawn in the handle (default none: the flag icons only; Kanok: a round dot) | |
--ui-slider-range-gap |
/ --ui-slider-range-icon-opacity - The gap where a flag meets the track and the flags' opacity (default the page colour, 1) |