InputsReact
An easily stylable range input.
Rendered by React 19.3.1
Loading React…Installation
npm i @hamur/reactNot on npm yet. The package goes out under the chosen name.
Anatomy
Import the component and assemble its parts:
<Slider.Root>
<Slider.Label />
<Slider.Value />
<Slider.Control>
<Slider.Track>
<Slider.Indicator />
<Slider.Thumb />
</Slider.Track>
</Slider.Control>
</Slider.Root>Examples
Range Slider
Two thumbs set a range; thumbCollisionBehavior decides what happens when they meet.
Steps
step snaps the value; largeStep sets the jump of PageUp, PageDown and Shift with the arrows.
Marks
Tick marks placed along the track, paired with step.
Edge Alignment
thumbAlignment keeps the thumb inside the control at min and max.
Vertical
orientation makes a vertical slider.
API reference
28 props across 7 parts, in React syntax. className, style, render are listed once, under Shared props.
ControlSlider.Control
The clickable, interactive part of the slider. Renders a <div>.
| Data attribute | Description |
|---|---|
data-dragging | Present while the user is dragging. |
data-orientation | Indicates the orientation of the slider. |
data-disabled | Present when the slider is disabled. |
data-valid | Present when the slider is in a valid state (when wrapped in Field.Root). |
data-invalid | Present when the slider is in an invalid state (when wrapped in Field.Root). |
data-touched | Present when the slider has been touched (when wrapped in Field.Root). |
data-dirty | Present when the slider's value has changed (when wrapped in Field.Root). |
data-focused | Present when the slider is focused (when wrapped in Field.Root). |
IndicatorSlider.Indicator
Visualizes the current value of the slider. Renders a <div>.
| Data attribute | Description |
|---|---|
data-dragging | Present while the user is dragging. |
data-orientation | Indicates the orientation of the slider. |
data-disabled | Present when the slider is disabled. |
data-valid | Present when the slider is in a valid state (when wrapped in Field.Root). |
data-invalid | Present when the slider is in an invalid state (when wrapped in Field.Root). |
data-touched | Present when the slider has been touched (when wrapped in Field.Root). |
data-dirty | Present when the slider's value has changed (when wrapped in Field.Root). |
data-focused | Present when the slider is focused (when wrapped in Field.Root). |
LabelSlider.Label
An accessible label that is automatically associated with the slider thumbs. Renders a <div>.
RootSlider.Root
Groups all parts of the slider. Renders a <div>.
| Prop | Type | Default |
|---|---|---|
nameIdentifies the field when a form is submitted. | string | — |
defaultValueThe uncontrolled value of the slider when it's initially rendered. | Value | — |
valueThe value of the slider. | Value | — |
onValueChangeCallback function that is fired when the slider's value changed. | ((value: Value extends number ? number : Value, eventDetails: SliderRootChangeEventDetails) => void) | — |
onValueCommittedCallback function that is fired when a value change is committed. | ((value: Value extends number ? number : Value, eventDetails: SliderRootCommitEventDetails) => void) | — |
formIdentifies the form that owns the slider inputs. | string | — |
localeThe locale used by Intl.NumberFormat when formatting the value. | Intl.LocalesArgument | — |
thumbAlignmentHow the thumb(s) are aligned relative to Slider.Control when the value is at min or max: | "center" | "edge" | "edge-client-only" | 'center' |
thumbCollisionBehaviorControls how thumbs behave when they collide during pointer interactions. | "none" | "push" | "swap" | 'push' |
stepThe granularity with which the slider can step through values. | number | 1 |
largeStepThe granularity with which the slider can step through values when using Page Up/Page Down or Shift + Arrow Up/Arrow Down. | number | 10 |
minStepsBetweenValuesThe minimum steps between values in a range slider. | number | 0 |
minThe minimum allowed value of the slider. | number | 0 |
maxThe maximum allowed value of the slider. | number | 100 |
formatOptions to format the value. | Intl.NumberFormatOptions | — |
disabledWhether the slider should ignore user interaction. | boolean | false |
orientationThe component orientation. | Orientation | 'horizontal' |
| Data attribute | Description |
|---|---|
data-dragging | Present while the user is dragging. |
data-orientation | Indicates the orientation of the slider. |
data-disabled | Present when the slider is disabled. |
data-valid | Present when the slider is in a valid state (when wrapped in Field.Root). |
data-invalid | Present when the slider is in an invalid state (when wrapped in Field.Root). |
data-touched | Present when the slider has been touched (when wrapped in Field.Root). |
data-dirty | Present when the slider's value has changed (when wrapped in Field.Root). |
data-focused | Present when the slider is focused (when wrapped in Field.Root). |
ThumbSlider.Thumb
The draggable part of the slider at the tip of the indicator. and a nested <input type="range">. Renders a <div>.
| Prop | Type | Default |
|---|---|---|
aria-valuetextA string value forwarded to the [aria-valuetext](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Attributes/aria-valuetext) attribute of the input. | string | — |
getAriaLabelA function which returns a string value for the [aria-label](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Attributes/aria-label) attribute of the input. | ((index: number) => string) | null | — |
getAriaValueTextA function which returns a string value for the [aria-valuetext](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Attributes/aria-valuetext) attribute of the input. | ((formattedValue: string, value: number, index: number) => string) | null | — |
indexThe index of the thumb which corresponds to the index of its value in the | number | — |
onBlurA blur handler forwarded to the input. | React.FocusEventHandler<HTMLInputElement> | — |
onFocusA focus handler forwarded to the input. | React.FocusEventHandler<HTMLInputElement> | — |
onKeyDownA keydown handler forwarded to the input. | React.KeyboardEventHandler<HTMLInputElement> | — |
tabIndexOptional tab index attribute forwarded to the input. | number | — |
disabledWhether the thumb should ignore user interaction. | boolean | false |
inputRefA ref to access the nested input element. | React.Ref<HTMLInputElement> | — |
| Data attribute | Description |
|---|---|
data-index | Indicates the index of the thumb in range sliders. |
data-dragging | Present while the user is dragging. |
data-orientation | Indicates the orientation of the slider. |
data-disabled | Present when the thumb or the slider is disabled. |
data-valid | Present when the slider is in a valid state (when wrapped in Field.Root). |
data-invalid | Present when the slider is in an invalid state (when wrapped in Field.Root). |
data-touched | Present when the slider has been touched (when wrapped in Field.Root). |
data-dirty | Present when the slider's value has changed (when wrapped in Field.Root). |
data-focused | Present when the slider is focused (when wrapped in Field.Root). |
TrackSlider.Track
Contains the slider indicator and represents the entire range of the slider. Renders a <div>.
| Data attribute | Description |
|---|---|
data-dragging | Present while the user is dragging. |
data-orientation | Indicates the orientation of the slider. |
data-disabled | Present when the slider is disabled. |
data-valid | Present when the slider is in a valid state (when wrapped in Field.Root). |
data-invalid | Present when the slider is in an invalid state (when wrapped in Field.Root). |
data-touched | Present when the slider has been touched (when wrapped in Field.Root). |
data-dirty | Present when the slider's value has changed (when wrapped in Field.Root). |
data-focused | Present when the slider is focused (when wrapped in Field.Root). |
ValueSlider.Value
Displays the current value of the slider as text. Renders an <output>.
| Prop | Type | Default |
|---|---|---|
children | ((formattedValues: readonly string[], values: readonly number[]) => React.ReactNode) | null | — |
| Data attribute | Description |
|---|---|
data-dragging | Present while the user is dragging. |
data-orientation | Indicates the orientation of the slider. |
data-disabled | Present when the slider is disabled. |
data-valid | Present when the slider is in a valid state (when wrapped in Field.Root). |
data-invalid | Present when the slider is in an invalid state (when wrapped in Field.Root). |
data-touched | Present when the slider has been touched (when wrapped in Field.Root). |
data-dirty | Present when the slider's value has changed (when wrapped in Field.Root). |
data-focused | Present when the slider is focused (when wrapped in Field.Root). |
Shared props
Every part that renders an element takes these.
| Prop | Type |
|---|---|
classNameCSS class applied to the element, or a function that
returns a class based on the component's state. | string | ((state: State) => string | undefined) |
styleStyle applied to the element, or a function that
returns a style object based on the component's state. | React.CSSProperties | ((state: State) => React.CSSProperties | undefined) |
renderAllows you to replace the component's HTML element
with a different tag, or compose it with another component. | React.ReactElement<unknown, string | React.JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, State> |
Keyboard and accessibility
Keyboard
| Keys | Does | Part |
|---|---|---|
| ArrowRightArrowUp | Increases the value by step (ArrowLeft in right-to-left) | Thumb |
| ArrowLeftArrowDown | Decreases the value by step (ArrowRight in right-to-left) | Thumb |
| ShiftArrowUpShiftArrowRight | Increases the value by largeStep, stopping at max | Thumb |
| ShiftArrowDownShiftArrowLeft | Decreases the value by largeStep, stopping at min | Thumb |
| PageUp | Increases the value by largeStep | Thumb |
| PageDown | Decreases the value by largeStep | Thumb |
| Home | Sets the value to min | Thumb |
| End | Sets the value to max | Thumb |
Accessibility
| Part | Sets | So that |
|---|---|---|
| Thumb | a nested <input type="range"> with aria-valuenow, aria-orientation | each thumb is read as a slider with its value |
| Thumb | aria-valuetext from getAriaValueText or format | the value is read as written, such as "40 °C" |
| Thumb | aria-labelledby to Label, or the name from getAriaLabel | each thumb has a name, also in a range slider |
| Root | role="group", aria-labelledby to Label | the thumbs are read as one control |
| Value | renders an <output> | the value text is tied to the slider without being announced on every change |
Focus order
- Each Thumb is one tab stop.
- Pressing the Control focuses the thumb it moves; clicking Label focuses the thumb of a single-value slider.