<cs-slider>
StableFormsSince 0.1

Sliders let users choose a numeric value within a defined range by dragging a thumb along a track.

Less More
Less More

This component works with standard <form> elements. See form controls for form submission and client-side validation.

Examples

Label

Use the label attribute to give the slider an accessible label. For labels that contain HTML, use the label slot instead.

Hint

Add descriptive hint to a slider with the hint attribute. For hints that contain HTML, use the hint slot instead.

Min, Max & Step

Use the min and max attributes to define the slider’s range, and the step attribute to control the increment between values.

Showing a Tooltip

Use the with-tooltip attribute to display a tooltip with the current value when the slider is focused or being dragged.

Showing Markers

Use the with-markers attribute to display visual indicators at each step increment. This works best with sliders that have a smaller range of values.

Adding References

Use the reference slot to add contextual labels below the slider. References are automatically spaced using space-between, making them easy to align with the start, center, and end positions.

Slow Medium Fast

Show a reference next to a specific marker.
Add position: absolute to the reference and set left, right, top, or bottom to a percentage that matches the marker’s position.

Range Selection

Use the range attribute to enable dual-thumb selection for choosing a range of values. Set the initial thumb positions with the min-value and max-value attributes.

$0 $50 $100

For range sliders, the minValue and maxValue properties represent the current positions of the thumbs. When the form is submitted, both values will be included as separate entries with the same name.

const slider = document.querySelector('cs-slider[range]');

// Get the current values
console.log(`Min value: ${slider.minValue}, Max value: ${slider.maxValue}`);

// Set the values programmatically
slider.minValue = 30;
slider.maxValue = 70;

Vertical Sliders

Set the orientation attribute to vertical to create a vertical slider. Vertical sliders automatically center themselves and fill the available vertical space.

Range sliders can also be vertical.

Size

Control the slider’s size with the size attribute. Valid options are xs, s, m, l, and xl.

Indicator Offset

By default, the filled indicator extends from the minimum value to the current position. Use the indicator-offset attribute to change the starting point of this visual indicator.

Easy Moderate Difficult

Disabled

Use the disabled attribute to disable a slider.

Readonly

Use the readonly attribute to show a value that users can’t change by dragging. Unlike disabled, a readonly slider stays focusable and its value is still submitted with the form.

Formatting the Value

Customize how values are displayed in tooltips and announced to screen readers using the valueFormatter property. Set it to a function that accepts a number and returns a formatted string. The Intl.NumberFormat API is particularly useful for this.



Reacting to Input

The slider emits an input event as the user drags, so you can drive live UI from its value in real time. Here, moving the slider resizes the preview text.

The quick brown fox jumps over the lazy dog.

Filtering with a Range

A range slider’s two thumbs make it a natural filter control. Here, dragging the thumbs hides list items whose price falls outside the selected range.

  • Sticker pack — $15
  • T-shirt — $30
  • Hoodie — $55
  • Backpack — $80
  • Jacket — $120

API

Importing

If you're using the autoloader or a hosted project, components load on demand — no manual import needed. To cherry-pick this component, use one of the following snippets.

npm Self-Hosted React
import '@cruglobal/cornerstone-components/components/slider/slider.js';
import './cornerstone/components/slider/slider.js';
import CsSlider from '@cruglobal/cornerstone-components/react/slider/index.js';

Slots

Learn more about using slots.

NameDescription
hintText that describes how to use the input. Alternatively, you can use the hint attribute. instead.
labelThe slider label. Alternatively, you can use the label attribute.
referenceOne or more reference labels to show visually below the slider.

Attributes & Properties

Learn more about attributes and properties.

PropertyAttributeDescriptionTypeDefaultReflects
autofocusautofocusTells the browser to focus the slider when the page loads or a dialog is shown.boolean
defaultValuevalueThe default value of the form control. Primarily used for resetting the form control.numberYes
disableddisabledDisables the slider.booleanfalse
formBy default, form controls are associated with the nearest containing <form> element. This attribute allows you to place the form control outside of a form and associate it with the form that has this id. The form must be in the same document or shadow root for this to work.HTMLFormElement | null
hinthintThe slider hint. If you need to display HTML, use the hint slot instead.string''
indicatorOffsetindicator-offsetThe starting value from which to draw the slider's fill, which is based on its current value.number
isRangeGet if this is a range sliderboolean
labellabelThe slider's label. If you need to provide HTML in the label, use the label slot instead.string''
maxmaxThe maximum value allowed.number100
maxValuemax-valueThe maximum value of a range selection. Used only when range attribute is set.number50
minminThe minimum value allowed.number0
minValuemin-valueThe minimum value of a range selection. Used only when range attribute is set.number0
namenameThe name of the slider. This will be submitted with the form as a name/value pair.string | nullnullYes
orientationorientationThe orientation of the slider.'horizontal' | 'vertical''horizontal'Yes
rangerangeConverts the slider to a range slider with two thumbs.booleanfalseYes
readonlyreadonlyMakes the slider a read-only field.booleanfalseYes
sizesizeThe slider's size.'xs' | 's' | 'm' | 'l' | 'xl''m'Yes
ssrHintssr-hintOnly required for SSR. Set to true if you're slotting in a hint element so the server-rendered markup includes the hint before the component hydrates on the client.booleanfalse
ssrLabelssr-labelOnly required for SSR. Set to true if you're slotting in a label element so the server-rendered markup includes the label before the component hydrates on the client.booleanfalse
ssrReferencessr-referenceOnly required for SSR. Set to true if you're slotting in a reference element.booleanfalse
stepstepThe granularity the value must adhere to when incrementing and decrementing.number1
tooltipDistancetooltip-distanceThe distance of the tooltip from the slider's thumb.number8
tooltipPlacementtooltip-placementThe placement of the tooltip in reference to the slider's thumb.'top' | 'right' | 'bottom' | 'left''top'Yes
validationTargetOverride validation target to point to the focusable elementundefined | HTMLElement
validatorsValidators are static because they have observedAttributes, essentially attributes to "watch" for changes. Whenever these attributes change, we want to be notified and update the validator.Validator[][]
valueThe current value of the slider, submitted as a name/value pair with form data.number
valueFormatterA custom formatting function to apply to the value. This will be shown in the tooltip and announced by screen readers. Must be set with JavaScript. Property only.(value: number) => string
withMarkerswith-markersDraws markers at each step along the slider.booleanfalse
withTooltipwith-tooltipDraws a tooltip above the thumb when the control has focus or is dragged.booleanfalse

Methods

Learn more about methods.

NameDescriptionArguments
blur()Removes focus from the slider.
focus()Sets focus to the slider.
formStateRestoreCallback()Called when the browser is trying to restore element’s state to state in which case reason is "restore", or when the browser is trying to fulfill autofill on behalf of user in which case reason is "autocomplete". In the case of "restore", state is a string, File, or FormData object previously set as the second argument to setFormValue.state: string | File | FormData | null, reason: 'autocomplete' | 'restore'
resetValidity()Reset validity is a way of removing manual custom errors and native validation.
setCustomValidity()Do not use this when creating a "Validator". This is intended for end users of components. We track manually defined custom errors so we don't clear them on accident in our validators.message: string
stepDown()Decreases the slider's value by step. This is a programmatic change, so input and change events will not be emitted when this is called.
stepUp()Increases the slider's value by step. This is a programmatic change, so input and change events will not be emitted when this is called.

Events

Learn more about events.

NameDescription
blurEmitted when the control loses focus.
changeEmitted when an alteration to the control's value is committed by the user.
cs-invalidEmitted when the form control has been checked for validity and its constraints aren't satisfied.
focusEmitted when the control gains focus.
inputEmitted when the control receives input.

CSS Custom Properties

Learn more about CSS custom properties.

NameDescriptionDefault
--marker-heightThe height of each individual marker.0.1875em
--marker-widthThe width of each individual marker.0.1875em
--thumb-heightThe height of the thumb.1.25em
--thumb-widthThe width of the thumb.1.25em
--track-sizeThe height or width of the slider's track.0.75em

Custom States

Learn more about custom states.

NameDescriptionCSS selector
disabledApplied when the slider is disabled.:state(disabled)
draggingApplied when the slider is being dragged.:state(dragging)
focusedApplied when the slider has focus.:state(focused)
user-invalidApplied when the slider is invalid and the user has sufficiently interacted with it.:state(user-invalid)
user-validApplied when the slider is valid and the user has sufficiently interacted with it.:state(user-valid)

CSS Parts

Learn more about CSS parts.

NameDescriptionCSS selector
hintThe element that contains the slider's description.::part(hint)
indicatorThe colored indicator that shows from the start of the slider to the current value.::part(indicator)
labelThe element that contains the sliders's label.::part(label)
markerThe individual markers that are shown when with-markers is used.::part(marker)
markersThe container that holds all the markers when with-markers is used.::part(markers)
referencesThe container that holds references that get slotted in.::part(references)
sliderThe focusable element with role="slider". Contains the track and reference slot.::part(slider)
thumbThe slider's thumb.::part(thumb)
thumb-maxThe max value thumb in a range slider.::part(thumb-max)
thumb-minThe min value thumb in a range slider.::part(thumb-min)
tooltipThe tooltip, a <cs-tooltip> element.::part(tooltip)
tooltip__arrowThe tooltip's arrow part.::part(tooltip__arrow)
tooltip__bodyThe tooltip's body part.::part(tooltip__body)
tooltip__tooltipThe tooltip's tooltip part.::part(tooltip__tooltip)
trackThe slider's track.::part(track)

Dependencies

This component automatically imports the following elements. Sub-dependencies, if any exist, are included in this list.