Components
Slider
An easily stylable range input.
Usage guidelines#
- Form controls must have an accessible name. Prefer
<Slider.Label>, or provide anaria-labelon each<Slider.Thumb>when no visible label is rendered. See Labeling a slider.
Anatomy#
Import the component and assemble its parts:
import { Slider } from '@egoist/solid-base/slider';
<Slider.Root>
<Slider.Label />
<Slider.Value />
<Slider.Control>
<Slider.Track>
<Slider.Indicator />
<Slider.Thumb />
</Slider.Track>
</Slider.Control>
</Slider.Root>;Examples#
Labeling a slider#
A single-thumb slider without a visible label (such as a volume control) can be labeled using
aria-label on <Slider.Thumb>:
<Slider.Root>
<Slider.Control>
<Slider.Track>
<Slider.Indicator />
<Slider.Thumb aria-label="Volume" />
</Slider.Track>
</Slider.Control>
</Slider.Root>A visible label can be created using <Slider.Label>:
<Slider.Root>
<Slider.Label>Volume</Slider.Label>
<Slider.Control>
<Slider.Track>
<Slider.Indicator />
<Slider.Thumb />
</Slider.Track>
</Slider.Control>
</Slider.Root>For a multi-thumb range slider with a visible label, add aria-label on each <Slider.Thumb> to
distinguish them:
<Slider.Root defaultValue={[25, 75]}>
<Slider.Label>Price range</Slider.Label>
<Slider.Control>
<Slider.Track>
<Slider.Indicator />
<Slider.Thumb index={0} aria-label="Minimum price" />
<Slider.Thumb index={1} aria-label="Maximum price" />
</Slider.Track>
</Slider.Control>
</Slider.Root>Range slider#
To create a range slider:
- Pass an array of values and place a
<Slider.Thumb>for each value in the array - Additionally for server-side rendering, specify a numeric
indexfor each thumb that corresponds to the index of its value in the value array
Thumbs can be configured to behave differently when they collide during pointer interactions
using the thumbCollisionBehavior prop on <Slider.Root>.
Steps#
Use the step prop to snap the value to multiples of a given increment, and largeStep to
control the increment when using Page Up/Page Down or Shift + Arrow Up/Arrow Down.
Marks#
Render your own tick marks, positioning each mark along the track as a percentage of the value range.
Marks are independent of the value's granularity—pair them with the step prop to snap
the thumb to each mark.
Thumb alignment#
Set thumbAlignment="edge" to inset the thumb such that its edge aligns with the edge of the
control when the value is at min or max, without overflowing the control like the default
"center" alignment.
A client-only alternative thumbAlignment="edge-client-only" can be used to reduce bundle size
but only renders after hydration.
Vertical#
Set orientation="vertical" on <Slider.Root> to build a vertical slider.
Controlled#
Pass value and update it from onValueChange, which fires on every change while dragging. Use
onValueCommitted to react only when the interaction ends (pointer up or a key press):
const [volume, setVolume] = createSignal(25);
<Slider.Root value={volume()} onValueChange={(value) => setVolume(value)}>
{/* ... */}
</Slider.Root>;API reference#
<Slider.Root>
Groups all parts of the slider. Renders a <div> element.
defaultValueValue—The uncontrolled value of the slider when it's initially rendered.
To render a controlled slider, use the value prop instead.
disabledbooleanfalseWhether the slider should ignore user interaction.
formatIntl.NumberFormatOptions—Options to format the value.
localeIntl.LocalesArgument—The locale used by Intl.NumberFormat when formatting the value. Defaults to the user's runtime locale. When server rendering, en-US is used until hydration.
maxnumber100The maximum allowed value of the slider. Should not be equal to min.
minnumber0The minimum allowed value of the slider. Should not be equal to max.
minStepsBetweenValuesnumber0The minimum steps between values in a range slider.
namestring—Identifies the field when a form is submitted.
formstring—Identifies the form that owns the slider inputs. Useful when the slider is rendered outside the form.
orientationOrientation'horizontal'The component orientation.
stepnumber1The granularity with which the slider can step through values. (A "discrete" slider.) The min prop serves as the origin for the valid values. We recommend (max - min) to be evenly divisible by the step.
largeStepnumber10The granularity with which the slider can step through values when using Page Up/Page Down or Shift + Arrow Up/Arrow Down.
thumbAlignment'center' | 'edge' | 'edge-client-only''center'How the thumb(s) are aligned relative to Slider.Control when the value is at min or max: - center: The center of the thumb is aligned with the control edge - edge: The thumb is inset within the control such that its edge is aligned with the control edge - edge-client-only: Same as edge but renders after hydration on the client, reducing bundle size in return
thumbCollisionBehavior'push' | 'swap' | 'none''push'Controls how thumbs behave when they collide during pointer interactions.
- 'push' (default): Thumbs push each other without restoring their previous positions when dragged back. - 'swap': Thumbs swap places when dragged past each other. - 'none': Thumbs cannot move past each other; excess movement is ignored.
valueValue—The value of the slider. For range sliders, provide an array with one value per thumb.
onValueChange| (( value: Value extends number ? number : Value, eventDetails: SliderRoot.ChangeEventDetails, ) => void)—Callback function that is fired when the slider's value changed. Receives the new value as the first argument; the originating event is available as eventDetails.event. The value is also reflected on eventDetails.event.target.value for form integration.
The eventDetails.reason indicates what triggered the change:
- 'input-change' when the hidden range input emits a change event (for example, via form integration) - 'track-press' when the control track is pressed - 'drag' while dragging a thumb - 'keyboard' for keyboard input - 'none' when the change is triggered without a specific interaction
onValueCommitted| (( value: Value extends number ? number : Value, eventDetails: SliderRoot.CommitEventDetails, ) => void)—Callback function that is fired when a value change is committed. Does not fire if the value did not change, or if the change was canceled. **Warning**: This is a generic event, not a change event.
The eventDetails.reason indicates what triggered the commit:
- 'drag' while dragging a thumb - 'track-press' when the control track is pressed - 'keyboard' for keyboard input - 'input-change' when the hidden range input emits a change event (for example, via form integration) - 'none' when the commit occurs without a specific interaction
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-draggingPresent while the user is dragging.
data-orientationIndicates the orientation of the slider.
data-disabledPresent when the slider is disabled.
data-validPresent when the slider is in a valid state (when wrapped in Field.Root).
data-invalidPresent when the slider is in an invalid state (when wrapped in Field.Root).
data-touchedPresent when the slider has been touched (when wrapped in Field.Root).
data-dirtyPresent when the slider's value has changed (when wrapped in Field.Root).
data-focusedPresent when the slider is focused (when wrapped in Field.Root).
<Slider.Label>
An accessible label that is automatically associated with the slider thumbs. Renders a <div> element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<Slider.Value>
Displays the current value of the slider as text. Renders an <output> element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-draggingPresent while the user is dragging.
data-orientationIndicates the orientation of the slider.
data-disabledPresent when the slider is disabled.
data-validPresent when the slider is in a valid state (when wrapped in Field.Root).
data-invalidPresent when the slider is in an invalid state (when wrapped in Field.Root).
data-touchedPresent when the slider has been touched (when wrapped in Field.Root).
data-dirtyPresent when the slider's value has changed (when wrapped in Field.Root).
data-focusedPresent when the slider is focused (when wrapped in Field.Root).
<Slider.Control>
The clickable, interactive part of the slider. Renders a <div> element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-draggingPresent while the user is dragging.
data-orientationIndicates the orientation of the slider.
data-disabledPresent when the slider is disabled.
data-validPresent when the slider is in a valid state (when wrapped in Field.Root).
data-invalidPresent when the slider is in an invalid state (when wrapped in Field.Root).
data-touchedPresent when the slider has been touched (when wrapped in Field.Root).
data-dirtyPresent when the slider's value has changed (when wrapped in Field.Root).
data-focusedPresent when the slider is focused (when wrapped in Field.Root).
<Slider.Track>
Contains the slider indicator and represents the entire range of the slider. Renders a <div> element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-draggingPresent while the user is dragging.
data-orientationIndicates the orientation of the slider.
data-disabledPresent when the slider is disabled.
data-validPresent when the slider is in a valid state (when wrapped in Field.Root).
data-invalidPresent when the slider is in an invalid state (when wrapped in Field.Root).
data-touchedPresent when the slider has been touched (when wrapped in Field.Root).
data-dirtyPresent when the slider's value has changed (when wrapped in Field.Root).
data-focusedPresent when the slider is focused (when wrapped in Field.Root).
<Slider.Thumb>
The draggable part of the slider at the tip of the indicator. Renders a <div> element and a nested <input type="range">.
disabledbooleanfalseWhether the thumb should ignore user interaction.
aria-valuetextstring—A string value forwarded to the aria-valuetext attribute of the input. Ignored when getAriaValueText is provided.
getAriaLabel((index: number) => string) | null—A function which returns a string value for the aria-label attribute of the input.
getAriaValueText| ((formattedValue: string, value: number, index: number) => string) | null—A function which returns a string value for the aria-valuetext attribute of the input. This is important for screen reader users.
indexnumber—The index of the thumb which corresponds to the index of its value in the value or defaultValue array. This prop is required to support server-side rendering for range sliders with multiple thumbs.
inputRefRefProp<HTMLInputElement>—A ref to access the nested input element.
onBlurJSX.EventHandlerUnion<HTMLInputElement, FocusEvent>—A blur handler forwarded to the input.
onFocusJSX.EventHandlerUnion<HTMLInputElement, FocusEvent>—A focus handler forwarded to the input.
onKeyDownJSX.EventHandlerUnion<HTMLInputElement, KeyboardEvent>—A keydown handler forwarded to the input.
tabindexnumber—Optional tab index attribute forwarded to the input.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-indexIndicates the index of the thumb in range sliders.
data-draggingPresent while the user is dragging.
data-orientationIndicates the orientation of the slider.
data-disabledPresent when the thumb or the slider is disabled.
data-validPresent when the slider is in a valid state (when wrapped in Field.Root).
data-invalidPresent when the slider is in an invalid state (when wrapped in Field.Root).
data-touchedPresent when the slider has been touched (when wrapped in Field.Root).
data-dirtyPresent when the slider's value has changed (when wrapped in Field.Root).
data-focusedPresent when the slider is focused (when wrapped in Field.Root).
<Slider.Indicator>
Visualizes the current value of the slider. Renders a <div> element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-draggingPresent while the user is dragging.
data-orientationIndicates the orientation of the slider.
data-disabledPresent when the slider is disabled.
data-validPresent when the slider is in a valid state (when wrapped in Field.Root).
data-invalidPresent when the slider is in an invalid state (when wrapped in Field.Root).
data-touchedPresent when the slider has been touched (when wrapped in Field.Root).
data-dirtyPresent when the slider's value has changed (when wrapped in Field.Root).
data-focusedPresent when the slider is focused (when wrapped in Field.Root).