Overview
A slider is a native <input type="range"> in a form field. The field holds a label, a bar with a thumb, and an <output> that shows the value. The bar fills from the start up to the thumb. A range slider has two thumbs, and the bar fills between them.
Pick another component when:
- the user must type an exact number: a number field in Forms
- the user picks one of a few fixed values: Radio buttons
- the setting is on or off: Switch
Markup
Variants
Currency makes two changes on a .nds-slider-value: write both. A range slider has two values, so write them on each one.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Single (default) | — | — | One thumb for one value |
| Structure | Range | canon #slider-range |
— | Two thumbs for a lowest and a highest value, such as a price range. Give each input an aria-label |
| Size | SM (default) | — | — | 12px thumb on a 4px bar. Most forms |
| Size | MD | .nds-md |
.nds-slider-container |
16px thumb on an 8px bar. Touch-first screens |
| State | None (default) | — | — | The user can change the value |
| State | Disabled | [data-state~="disabled"] |
.nds-slider-container |
The user cannot change the value now |
| State | Read-only | [data-state~="readonly"] |
.nds-slider-container |
The user sees the value but cannot change it |
| Stacked | Stacked (hint: The value sits above a full-width bar) | .nds-stacked |
.nds-slider-container |
The value moves above the bar, and the bar takes the full width. Use it for a wide value, such as a currency |
| Currency | Currency | [data-currency="SAR"] |
.nds-slider-value |
A currency symbol on the value |
| Loading | Loading | .nds-loading |
.nds-slider-container |
A skeleton while the script loads the bounds or the value |
| Field states | Label, info, feedback | — | — | Shared by every form field. See Forms |
Behavior
Range
The nds-slider-range class on the container turns on two thumbs. Each thumb is its own input, so the user can tab to it and move it with the keys. The thumbs cannot cross: a thumb dragged past the other stops at its value.
Stacked
By default the value sits beside the bar, and the bar takes the rest of the row. A wide value leaves a short bar on a phone. The nds-stacked class moves the value above the bar, so the bar takes the full width.
Number Format
The nds-number-format class on the value adds thousands separators. Without it, the value shows as the input holds it, so a year reads 2024. The data-currency attribute adds a currency symbol, with the class. The slider formats the value again on every move. The Numbers page lists the currencies.
Read-only
data-state="readonly" on the container blocks the mouse and the step keys. The thumb can still get the focus, so a screen reader can read the value. Browsers ignore readonly on a range input, so the slider script blocks the keys itself.
Loading
The nds-loading class shows the bar and the value as a skeleton, and hides the thumb. Add it while your script loads the bounds or the value, and remove it once they are set. From a script, NDS.state.add(container, 'loading') also adds the class.
Built-in Features
-
Auto-initialization
Every
nds-slider-containeron the page starts on its own, and so does a slider added later. The first slider on a page that had none needsNDS.init.mount()on the new content. -
Keyboard Control
The arrow keys move the thumb one step. Home and End move it to the lowest and highest value. Page Up and Page Down move it in larger steps. The focus ring shows on keyboard focus only.
-
Touch Target
Each thumb takes a click or a touch in a 24px circle, the WCAG 2.5.8 minimum, while the dot you see stays 12px, or 16px with MD. The two thumbs of a range slider have one each.
-
Steady Bar
The value keeps the width of the widest value the slider can show. The bar does not move as the number of digits changes.
-
Skeleton State
Until the slider script starts, the bar and the value show as a skeleton and the thumb is hidden. The bar never shows a fill that does not match the value.
-
State Sync
disabledorreadonlyon the inputs and the samedata-statetoken on the container stay in sync, in both directions. Write one: the forms script sets the other.
Best Practices
- Use a slider when the user picks an approximate value, such as a budget or a distance.
- Use a range slider to filter a list by a lowest and a highest value, such as a price or an age.
- Keep the
<output>beside every input. The user reads the exact value there. - Set
stepto the unit the value snaps to, such as 500 for a budget. Without it, the value moves in steps of 1. - Give each input of a range slider an
aria-label, such as “Lowest price” and “Highest price”. The one label does not name the two thumbs. - Use MD on touch-first screens. Its 16px dot is easier to see under a finger. Both sizes take a touch in a 24px circle.
- Use Stacked with Currency, so a wide value does not shorten the bar.
- To set the value from a script, call
NDS.Slider.setValue(). After you setinput.valueyourself, fire aninputevent on the input. The bar and the value then update. - After you change
minormaxfrom a script, callNDS.Slider.refresh(container). The value width is measured again for the new bounds. - For the label, info text and feedback, see Forms. They work the same on every field.
API
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
min, max, step, value |
.nds-slider |
Native range attributes: the bounds, the step and the start value. Without them, the bounds are 0 and 100 and the step is 1 |
data-currency |
.nds-slider-value |
The currency symbol, with nds-number-format. See Numbers |
data-state |
.nds-slider-container |
disabled or readonly. The forms script copies it to every input in the container |
CSS Custom Properties
The script writes --slider-fill-start and --slider-fill-end on .nds-slider-track on every move. Do not set them.
Tokens
Set a token at :root, or on a wrapper to reach every slider inside it. Give a token with a Dark mode value a dark override too: see Tokens. The hovered thumb color also shows on keyboard focus.
Source: the slider group in _sass/tokens/_components.scss.
| Token | Preview | Value |
|---|---|---|
--slider-track-bg | var(--background-neutral-light)Dark mode var(--colors-alpha-white-10) | |
--slider-track-fill | var(--background-primary)Dark mode var(--colors-primary-500) | |
--slider-thumb-bg | var(--background-primary)Dark mode var(--colors-primary-500) | |
--slider-thumb-bg-hovered | var(--controls-primary-hovered)Dark mode var(--colors-primary-400) | |
--slider-thumb-border | var(--colors-alpha-black-10) |
It also reads these shared tokens. A change to one also changes other components.
Source: the var() reads in _sass/components/_slider.scss.
| Token | Preview | Value |
|---|---|---|
--text-primary-paragraphRead by color | var(--colors-neutral-700)Dark mode var(--colors-neutral-300) | |
--focus-outlineRead by outline | var(--colors-base-black)Dark mode var(--colors-base-white) | |
--focus-innerRead by box-shadow | var(--colors-base-white)Dark mode var(--colors-base-black) |
JavaScript
The NDS.Slider script loads after the page shows. Until it loads, each call loads it and returns a promise of the result, so await a call whose result you use.
| Method | Effect |
|---|---|
NDS.Slider.init() |
Starts every slider on the page. It runs once at page load, and a slider added later starts on its own. The first slider on a page that had none needs NDS.init.mount() on the new content |
NDS.Slider.refresh(container) |
Measures one container again, or every container with no argument |
NDS.Slider.create(container) |
Starts one container and returns it |
NDS.Slider.destroy(container) |
Stops one container, or every container with no argument |
NDS.Slider.getValue(el) |
Single slider. Returns its value as a number, or '' for a range or when the slider is not found. el is the container, an input in it, its id or a selector |
NDS.Slider.setValue(el, n) |
Single slider. Moves it as a user does and returns true. It returns false, and changes nothing, when n is not a finite number or is outside the input’s min and max |
NDS.Slider.getValues(el) |
Range slider (.nds-slider-range). Returns [min, max] as numbers, or [] for a single slider or when the slider is not found |
NDS.Slider.setValues(el, [min, max]) |
Range slider. Moves both thumbs and returns true. It returns false, and changes nothing, when a number is bad or min is above max |
NDS.Slider.clear(el) |
Moves a single slider to its min, and a range to its whole span |
The slider fires no event of its own. Listen for the native input event on the input.
The full API is in the banner of _js/nds-slider.js.