API: components
Slider
A number, or a range of numbers, chosen by dragging a thumb, pressing the track or with the keyboard, following the APG's slider patterns, in each framework.
Choose a number within bounds, or a range with several thumbs (WAI-ARIA APG
slider and
multi-thumb slider). It works in a
native <form>: each thumb submits its value. A Label names it and a Value shows the value,
formatted with Intl.NumberFormat when you pass format.
Behaviour
- Value: a number, or an array of numbers for several thumbs (sorted); the type of
defaultValue(orvalue) decides. Values snap tomin + n × stepand stay in[min, max]; thumbs never cross, and stayminStepsBetweenValues × stepapart. - Pointer: a press on the Control moves the nearest thumb there (reason
track-press), focuses it and starts a drag; the drag follows the pointer outside the Control until it is released (reasondrag,data-draggingmeanwhile).touch-action: nonekeeps a touch drag from scrolling. - Commit:
onValueCommitonce a drag ends, or after a key changed the value. - Label: names the Root and every Thumb that has no
aria-labeloraria-labelledbyof its own; a press on it focuses the first Thumb. Inside a Field, the Field's Label names the slider instead: use one or the other. - Value: an
outputwhoseforlists the Thumbs' ids. It shows the formatted values, joined with" – "for several thumbs, or the content you give it.aria-live="off": a drag would announce every step. - Format: with
format(Intl.NumberFormatOptions, inlocale), the Value and each Thumb'saria-valuetextuse the formatted value;getAriaValueTextwins over it. Withoutformat, the Value shows the plain numbers and the Thumbs have noaria-valuetextof their own. Withoutlocale, the runtime's default: a page rendered on a server passeslocale, so the server formats as the browser does. - RTL: horizontal sliders run right to left (
minat the right, the arrows swap), in the direction of a DirectionProvider, else the computed one. - Forms: one hidden input per thumb submits
name=value, in order. Inside a Field, itsvalidatereceives the number (or numbers); it cannot block a native submission (the inputs aretype="hidden"), a Form does.
Anatomy
| part | element | data-primitives-part | role |
|---|---|---|---|
| Root | div | slider-root | the group; holds the state |
| Label | div | slider-label | names the Root and its Thumbs |
| Value | output | slider-value | shows the formatted value(s) |
| Control | div | slider-control | the interactive area |
| Track | div | slider-track | the rail |
| Indicator | div | slider-indicator | the filled part: from min to the value, or between two thumbs |
| Thumb | div | slider-thumb | a slider; one per value |
| (hidden input) | input type="hidden" | slider-input | one per value: what the form submits |
Usage
import { Slider } from "@fragiola/primitives-react/slider";
<Slider.Root
defaultValue={[20, 80]}
minStepsBetweenValues={10}
name="price"
format={{ style: "currency", currency: "EUR", maximumFractionDigits: 0 }}
locale="de-DE"
>
<Slider.Label>Price</Slider.Label>
<Slider.Value />
<Slider.Control>
<Slider.Track>
<Slider.Indicator />
</Slider.Track>
<Slider.Thumb aria-label="Minimum price" />
<Slider.Thumb aria-label="Maximum price" />
</Slider.Control>
</Slider.Root>;Props
Every part also takes what the primitive contract gives:
render (Vue: as and the render slot), a class and a style, a ref, and any attribute, which
it forwards. The names below are React's; Vue binds them the same way (v-model:value,
@value-commit), and Angular as inputs and outputs ([(value)], (valueCommit)).
Root
| prop | type | default | description |
|---|---|---|---|
value | number | number[] | the value, controlled | |
defaultValue | number | number[] | min | the initial value, uncontrolled |
onValueChange | (value, details: { reason }) => void | every change (keyboard, track-press, drag) | |
onValueCommit | (value, details: { reason }) => void | once a drag ends, or after a key | |
min | number | 0 | the lower bound |
max | number | 100 | the upper bound |
step | number | 1 | the step of the arrows and the snapping |
largeStep | number | 10 | the step of Page Up, Page Down and Shift+Arrow |
minStepsBetweenValues | number | 0 | the smallest distance between two thumbs, in steps |
orientation | "horizontal" | "vertical" | "horizontal" | the axis |
disabled | boolean | false | ignores the pointer and the keys |
name | string | the form name of the hidden inputs (a Field's wins) | |
format | Intl.NumberFormatOptions | how the Value and each Thumb's aria-valuetext write the values | |
locale | string | the runtime's | the locale of format (a BCP 47 tag) |
Label
| prop | type | default | description |
|---|---|---|---|
id | string | generated | the id the Root's and the Thumbs' aria-labelledby point at |
Value
No props of its own. Its children replace the formatted values: a function
(formattedValues, values) => ReactNode in React, the default scoped slot
{ formattedValues, values } in Vue, the directive's formattedValues() and values() signals
in Angular.
Control
No props of its own.
Track
No props of its own.
Indicator
No props of its own.
Thumb
| prop | type | default | description |
|---|---|---|---|
index | number | its order | the index of its value |
getAriaValueText | (value: number, index: number) => string | the aria-valuetext; it wins over format | |
disabled | boolean | false | this thumb only |
id | string | generated | the thumb's id, in the Value's for |
A thumb's accessible name comes from the consumer (aria-label, aria-labelledby), or the
Label.
State and part attributes
| part | attributes |
|---|---|
| Root | role="group", aria-labelledby (the Label, while one is rendered), data-orientation, data-disabled, data-dragging; in a Field, the field's states |
| Label | id, data-orientation, data-disabled |
| Value | for (the Thumbs' ids, in value order), aria-live="off", data-orientation, data-disabled |
| Control, Track | data-orientation, data-disabled, data-dragging |
| Indicator | data-orientation; structural inline style (inset-inline-start and inline-size in %, or the block ones) |
| Thumb | role="slider", id, tabindex="0", aria-valuenow, aria-valuemin, aria-valuemax (the bounds it can reach), aria-valuetext, aria-labelledby (the Label, without a name of its own), aria-orientation, aria-disabled, data-orientation, data-index, data-dragging, data-disabled; structural inline style placing it |
Keyboard
| key | action |
|---|---|
| ArrowRight, ArrowUp | + step (ArrowLeft in RTL) |
| ArrowLeft, ArrowDown | − step (ArrowRight in RTL) |
| Page Up / Page Down, Shift+Arrow | ± largeStep |
| Home / End | the lowest / highest value the thumb can reach |
| Tab | the next thumb, in value order |
Exports
| entry | exports |
|---|---|
@fragiola/primitives-react/slider | ChangeDetails, ChangeReason, RenderProp, Slider, SliderLabelProps, SliderRootProps, SliderState, SliderThumbProps, SliderThumbState, SliderValueProps |
@fragiola/primitives-vue/slider | ChangeDetails, ChangeReason, RenderSlotProps, Slider, SliderLabelProps, SliderRootEmits, SliderRootProps, SliderState, SliderThumbProps, SliderThumbState, SliderValueSlotProps, SliderValueSlots |
@fragiola/primitives-angular/slider | ChangeDetails, ChangeReason, SliderControl, SliderIndicator, SliderLabel, SliderRoot, SliderRootValue, SliderState, SliderThumb, SliderThumbState, SliderTrack, SliderValue, ValueChangeEvent |