Slider
<pc-slider> Sliders allow the user to select a single value within a given range.
<pc-slider
label="Number of users"
hint="Limit six per team"
name="value"
value="3"
min="0"
max="6"
with-markers
has-tooltip
>
<span slot="reference">Less</span>
<span slot="reference">More</span>
</pc-slider> This component works well with standard <form> elements. Please refer to the form controls page to learn more about form submission and client‐side validation.
Use the label attribute to give the slider an accessible label. For labels that contain HTML, use the label slot instead.
Add descriptive hint to a slider with the hint attribute. For hints that contain HTML, use the hint slot instead.
Use the has-tooltip attribute to display a tooltip with the current value when the slider is focused or being dragged.
Use the min and max attributes to define the slider’s range, and the step attribute to control the increment between values.
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.
Use the reference slot to add contextual labels below the slider. References are automatically spaced using justify-content: space-between, making them easy to align with the start, center and end positions.
If you want to show a reference next to a specific marker, you can add position: absolute to it and set the inset-block-start, inset-block-end, inset-inline-start or inset-inline-end property to a percentage that corresponds to the marker’s position.
Customise 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. You can use the Intl.NumberFormat API to format numbers in many ways.
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.
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("pc-slider[range]");
console.log(`Minimum value: ${slider.minValue}, maximum value: ${slider.maxValue}`);
slider.minValue = 30;
slider.maxValue = 70; Set the orientation attribute to vertical to create a vertical slider. Vertical sliders automatically centre themselves and fill the available vertical space.
Range sliders can also be vertical.
Control the slider’s size using the size attribute.
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.
Use the disabled attribute to disable a slider.
| Name | Description | Reflects | Default |
|---|---|---|---|
validationTarget | Overrides the validation target to point to the focusable element. |
| ‐ |
label | The slider’s label. If you need to provide HTML in the label, use the label slot instead.Type: string |
| "" |
hint | The slider’s hint. If you need to display HTML, use the hint slot instead.Type: string |
| "" |
name | The name of the slider, submitted as a name/value pair with form data. Type: string |
| ‐ |
minValuemin-value | The minimum value of a range selection. This is only used when the range attribute is set.Type: number |
| 0 |
maxValuemax-value | The maximum value of a range selection. This is only used when the range attribute is set.Type: number |
| 50 |
defaultValuevalue | The default value of the slider. Primarily used for resetting the slider. Type: number |
| ‐ |
value | The current value of the slider, submitted as a name/value pair with form data. Type: number |
| ‐ |
range | Converts the slider to a range slider with two thumbs. Type: boolean |
| false |
isRange | Get if this is a range slider. Type: boolean |
| ‐ |
disabled | Disables the slider. Type: boolean |
| false |
readonly | Makes the slider a read‐only field. Type: boolean |
| false |
orientation | The orientation of the slider. Type: "horizontal" | "vertical" |
| "horizontal" |
size | The slider’s size. Type: "small" | "medium" | "large" |
| "medium" |
indicatorOffsetindicator-offset | The starting value from which to draw the slider’s fill, which is based on its current value. Type: number | undefined |
| ‐ |
min | The minimum value allowed. Type: number |
| 0 |
max | The maximum value allowed. Type: number |
| 100 |
step | The granularity the value must adhere to when incrementing and decrementing the value. Type: number |
| 1 |
required | Indicates if the slider must be interacted with or not. Type: boolean |
| false |
autofocus | Tells the browser to focus the slider when the page loads or a dialog is shown. Type: boolean |
| ‐ |
tooltipDistancetooltip-distance | The distance of the tooltip from the slider’s thumb. Type: number |
| 8 |
tooltipPlacementtooltip-placement | The placement of the tooltip in reference to the slider’s thumb. Type: "top" | "right" | "bottom" | "left" |
| "top" |
withMarkerswith-markers | Adds markers at each step along the slider. Type: boolean |
| false |
hasTooltiphas-tooltip | Adds a tooltip above the thumb when the control has focus or is dragged. Type: boolean |
| false |
valueFormatter | A custom formatting function to apply to the value. This will be shown in the tooltip and announced by screen readers and is property only. Type: (value: number) => string | undefined |
| ‐ |
updateComplete | A read‐only promise that resolves when the component has finished updating. | ‐ |
Learn more about customising animations.
| Name | Description |
|---|---|
label | The slider label. Alternatively, you can use the label attribute. |
hint | Text that describes how to use the input. Alternatively, you can use the hint attribute. |
reference | One or more reference labels to show visually below the slider. |
Learn more about using slots.
| Name | Description | Arguments |
|---|---|---|
focus() | Focuses the slider. | ‐ |
blur() | Unfocuses the slider (i.e., blurs it). | ‐ |
stepDown() | Decreases the slider’s value by step. This is a programmatic change, so change and input events will not be emitted when this is called. | ‐ |
stepUp() | Increases the slider’s value by step. This is a programmatic change, so change and input events will not be emitted when this is called. | ‐ |
Learn more about methods.
| Name | Description | Event detail |
|---|---|---|
change | Emitted when an alteration to the control’s value is committed by the user. | Event |
focus | Emitted when the control gains focus. | FocusEvent |
blur | Emitted when the control loses focus. | FocusEvent |
input | Emitted when the control receives input. | InputEvent |
pc-invalid | Emitted when the slider has been checked for validity and its constraints aren’t satisfied. | ‐ |
Learn more about events.
| Name | Description | Default |
|---|---|---|
--track-size | The height or width of the slider's track. | 0.75em |
--marker-width | The width of each individual marker. | 0.1875em |
--marker-height | The height of each individual marker. | 0.1875em |
--thumb-width | The width of the thumb. | 1.25em |
--thumb-height | The height of the thumb. | 1.25em |
Learn more about customising custom properties.
| Name | Description |
|---|---|
label | The element that contains the slider’s label. |
hint | The element that contains the slider’s hint. |
slider | The focusable element with role="slider". Contains the track and reference slot. |
track | The slider’s track. |
indicator | The colored indicator that shows from the start of the slider to the current value. |
markers | The container that holds all the markers when with-markers is used. |
marker | The individual markers that are shown when with-markers is used. |
references | The container that holds references that get slotted in. |
thumb | The slider’s thumb. |
thumb-min | The thumb representing the minimum value in a range slider. |
thumb-max | The thumb representing the maximum value in a range slider. |
tooltip | The tooltip, a <pc-tooltip> element. |
tooltip-tooltip | The tooltip’s tooltip part. |
tooltip-content | The tooltip’s content part. |
tooltip-arrow | The tooltip’s arrow part. |
Learn more about customising CSS parts.
If you’re using the autoloader or the standard loader, you can skip this section. But if you’re cherry picking, you can use any of the following snippets to import this component.
To manually import this component from the CDN, copy this code snippet and paste it in your HTML.
<script type="module" src="https://cdn.jsdelivr.net/npm/placer-toolkit@1.0.0-alpha.4/cdn/components/slider/slider.js"></script> To manually import this component from the CDN, copy this code snippet and paste it in your JavaScript file.
import "https://cdn.jsdelivr.net/npm/placer-toolkit@1.0.0-alpha.4/cdn/components/slider/slider.js"; To manually import this component from npm, copy this code snippet and paste it in your JavaScript file.
import "placer-toolkit/dist/components/slider/slider.js"; This component automatically imports these components: