Slider

lightning-slider

An input range slider for specifying a value between two specified numbers.

For Use In

Lightning Experience, Experience Builder Sites, Salesforce Mobile App, Lightning Out (Beta), Standalone Lightning App, Mobile Offline

A lightning-slider component is a horizontal or vertical slider for specifying a value between two specified numbers. For example, this slider can be used to capture user input about order quantity or when you want to use an input field of type="range". To orient the slider vertically, set type="vertical". Older browsers that don’t support the slider fallback and treat it as type="text".

Here’s an example of a slider with a step increment of 10.

1<template>
2    <lightning-slider
3        label="Volume"
4        step="10"
5        value={myValue}
6        onchange={handleChange}
7    >
8    </lightning-slider>
9    <div class="slds-m-vertical_medium">
10        <p>
11            The value of the slider is:
12            <span class="slds-text-heading_small">{myValue}</span>
13        </p>
14    </div>
15</template>

Handle the change event and get the slider value by using the event.detail property.

1import { LightningElement } from "lwc";
2
3export default class SliderExample extends LightningElement {
4  myValue = 20; //initial value
5
6  handleChange(event) {
7    this.myValue = event.detail.value;
8  }
9}

Design 

lightning-slider implements the slider blueprint in the Salesforce Lightning Design System (SLDS). The slider adapts to SLDS 1 or SLDS 2 styling based on the org’s theme or the container app that you use.

SLDS 1SLDS 2
DesignSliderSlider
For Use InLightning Experience, Experience Builder sites, Salesforce mobile app, Lightning Out (Beta), Standalone Lightning app, Mobile OfflineLightning Experience

Input Validation 

Client-side input validation is available for this component. Note that a disabled slider is always valid.

To programmatically show error messages on invalid fields, use the reportValidity() method. For custom validity error messages, specify the message using setCustomValidity() and reportValidity().

For more information, see the lightning-input documentation.

Component Styling 

Use a combination of variants, sizes, positioning type, and utility classes to customize the slider.

Variants 

Specify the variant attribute with one of these values.

  • standard is the default variant, which includes the text label before the range values and slider
  • label-hidden hides the text label from view

Sizes 

Specify the size attribute with one of these values.

  • x-small takes up a width of 192px
  • small takes up a width of 240px
  • medium takes up a width of 320px
  • largetakes up a width of 400px

The default slider size takes up the width of the view port.

Positioning Type 

Specify the type attribute with one of these attributes.

  • horizontal is the default position type, which places the slider horizontally across the view port
  • vertical places the slider vertically with a height of 192px

Utility Classes 

To apply additional styling, use the SLDS utility classes with the class attribute.

This example adds padding above the slider using an SLDS class.

1<div class="slds-p-top_large">
2    <lightning-slider label="Volume" step="10" value="20"> </lightning-slider>
3</div>

Styling Hooks 

Component styling hooks provide CSS custom properties that use the --slds-c-* prefix and they change styling for specific elements or properties of a component. Component styling hooks are supported for SLDS 1 only. See the SLDS 1 component blueprints for available component styling hooks.

For more information, see Style Components Using Lightning Design System Styling Hooks in the Lightning Web Components Developer Guide.

Usage Considerations 

Use the change event to detect changes to the slider value instead of the blur event. Browsers such as Safari don’t give focus to the slider so the blur event doesn’t fire.

By default, the min and max values are 0 and 100, but you can specify your own values. If you specify your own step increment value, you can drag the slider based on the step increment only. If you set the value lower than the min value, then the value is set to the min value. Similarly, setting the value higher than the max value results in the value being set to the max value.

For precise numerical values, we recommend using the lightning-input component of type="number" instead.

Attributes 

NameDescriptionTypeDefaultRequired
disabledIf present, the slider is disabled and users cannot interact with it.booleanfalse
labelText label to describe the slider. Provide your own label to describe the slider.string
maxThe maximum value of the input range. The default is 100.number100
message-when-bad-inputError message to be displayed when a bad input is detected.string
message-when-pattern-mismatchError message to be displayed when a pattern mismatch is detected.string
message-when-range-overflowError message to be displayed when a range overflow is detected.string
message-when-range-underflowError message to be displayed when a range underflow is detected.string
message-when-step-mismatchError message to be displayed when a step mismatch is detected.string
message-when-too-longError message to be displayed when the value is too long.string
message-when-type-mismatchError message to be displayed when a type mismatch is detected.string
message-when-value-missingError message to be displayed when the value is missing.string
minThe minimum value of the input range. The default is 0.number0
sizeThe size of the slider. The default is an empty string, which sets the slider to the width of the viewport. Accepted values are x-small, small, medium, and large.string
stepThe step increment value of the input range. Example steps include 0.1, 1, or 10. The default is 1.number1
typeThe type determines the orientation of the slider. Accepted values are vertical and horizontal. The default is horizontal.stringhorizontal
validityRepresents the validity states of the slider input, with respect to constraint validation.object
valueThe numerical value of the slider. The default is 0.number0
variantThe variant changes the appearance of the slider. Accepted variants include standard and label-hidden. The default is standard.stringstandard

Methods 

NameDescriptionArgument NameArgument TypeArgument Description
blurRemoves keyboard focus from the input element.
checkValidityReturns the valid attribute value (Boolean) on the ValidityState object.
focusSets focus on the input element.
reportValidityDisplays the error messages and returns false if the input is invalid. If the input is valid, reportValidity() clears displayed error messages and returns true.
setCustomValiditySets a custom error message to be displayed when the slider value is submitted.messagestringThe string that describes the error. If message is an empty string, the error message is reset.
showHelpMessageIfInvalidDisplays error messages on invalid fields. An invalid field fails at least one constraint validation and returns false when checkValidity() is called.