Form
Slider
A single-thumb slider built on the native range input.
- Element
<slab-slider>- Built on
<input type=range>- As Markdown
- slider.md
Example
Here's a styled version, <slab-styled-slider>. It's not
in the package — it's an example of what you can build with the
headless element. Copy it and make it yours at the
bottom of this page. Switch to "As it ships" to see the same markup
with the headless element and no CSS at all. That's your starting
point.
<slab-styled-slider label="Slump" max="7" min="2" step="0.5" value="4">
<span slot="description">Inches. A wetter mix is easier to place and weaker once it cures.</span>
</slab-styled-slider>
<slab-slider label="Slump" max="7" min="2" step="0.5" value="4">
<span slot="description">Inches. A wetter mix is easier to place and weaker once it cures.</span>
</slab-slider>
Usage
pnpm add slab-elements lit
<script type="module">
import 'slab-elements/slider.js';
</script>
<slab-slider label="Volume" name="volume" min="0" max="100" value="50"></slab-slider>
Keyboard
| Key | What it does |
|---|---|
| Left, Down, Right, Up | Steps the value down or up, as on a range input. |
| Page Up, Page Down | Steps it by more, where the browser does. |
| Home, End | Moves to the minimum or maximum. |
Behavior
A single-thumb slider with no visual styles, around a native range input, so it is dragged, stepped with the keyboard, and read the way one is.
The label and the input are both in the shadow root, so a plain for reference names it without crossing out of one. The host delegates focus, so clicking anywhere on it, or a <label for> pointing at it, focuses the input.
The value is the native input's. min, max and step are passed through to it, and value is read back from it, so a value outside the range or off a step is clamped and snapped the way a range input does it. With no value, it sits halfway between min and max, as a range input does.
Style it through its parts and through :state(disabled). Where the label sits relative to the input is layout, so it's left to the styles around it.
API
<slab-slider>
| Name | Type | Default | Description |
|---|---|---|---|
label
Required
|
string |
||
disabled
|
boolean |
false |
|
hide-label
|
boolean |
false |
|
max
|
number |
100 when unset, as on a range input. | |
min
|
number |
0 when unset, as on a range input. | |
name
|
string |
'' |
|
step
|
number | 'any' |
A number, or "any" to allow any value. Left unset, the input's own default of 1 applies. | |
value
|
string |
'' |
Like a native input, the attribute is the default value and the property is the current one. The property is always the value the input settled on, so it's never outside the range or off a step. |
version
|
string |
| Name | Type | Description |
|---|---|---|
form
Read-only
|
HTMLFormElement | null |
|
validity
Read-only
|
ValidityState |
| Signature | Description |
|---|---|
checkValidity(): boolean
|
|
click(): void
|
|
focus(options?: FocusOptions): void
|
|
reportValidity(): boolean
|
| Name | Type | Description |
|---|---|---|
input
|
Event |
|
change
|
Event |
| Name | Type | Description |
|---|---|---|
description
|
Element | string |
Text about the slider, such as a hint |
| Name | Description |
|---|---|
description
|
Text about the slider, such as a hint |
input
|
|
label
|
The label |
Copy and own it
<slab-styled-slider> isn't in the package. It's an example —
copy it into your project and change whatever you want. Under the
hood, it's just the headless element, subclassed, with
styles set. That's it. Keyboard, focus, ARIA, forms, and
events all come from the headless element, and the styles only reach
in through ::part() and :state(), same as
yours would.
To use it, copy these files, swap the tag in
@customElement() for one of your own, and import it.
tokens.styles.ts holds the colors and fonts every styled
example shares, so you only need it once.
slider.ts
import { type PropertyValues } from 'lit';
import { customElement } from 'lit/decorators.js';
import Slider from 'slab-elements/slider.js';
import styles from './slider.styles.js';
import tokens from './tokens.styles.js';
declare global {
interface HTMLElementTagNameMap {
'slab-styled-slider': StyledSlider;
}
}
/**
* Slider, styled: an ink track filled with ember up to a tall, brick-shaped
* thumb.
*
* Only Firefox can style the part of a range input's track before its thumb.
* Everywhere else the fill is a gradient, so how far along the value is goes
* to the input as `--fill`.
*
* It's set after each update, for `value`, `min` and `max` set from outside,
* and on "input", for dragging. Dragging doesn't update the Slider: `value`
* is read from the input, so it has already changed by the time Slider sets
* it, and Lit sees nothing new.
*/
@customElement('slab-styled-slider')
export default class StyledSlider extends Slider {
static override styles = [tokens, styles];
override updated(changes: PropertyValues) {
super.updated(changes);
this.#updateFill();
}
constructor() {
super();
this.addEventListener('input', this.#updateFill);
}
#updateFill = () => {
const min = this.min ?? 0;
const max = this.max ?? 100;
const fill =
max > min ? ((Number(this.value) - min) / (max - min)) * 100 : 0;
this.shadowRoot
?.querySelector<HTMLElement>("[part~='input']")
?.style.setProperty('--fill', `${fill}%`);
};
}
slider.styles.ts
import { css } from 'lit';
/*
The track is 0.875rem with its border, and the thumb 1.875rem. Chromium
and WebKit put the thumb at the top of the track's content box rather than
centering it, so it's pulled up by the difference.
*/
export default css`
:host {
color: var(--ink);
display: grid;
font-family: var(--font-body);
gap: 0.5rem;
}
[part~='label'] {
font-family: var(--font-display);
font-size: 1.0625rem;
font-weight: 600;
}
[part~='input'] {
appearance: none;
background: transparent;
block-size: 1.875rem;
cursor: pointer;
inline-size: 100%;
margin: 0;
&::-webkit-slider-runnable-track {
background: linear-gradient(
to right,
var(--ember) var(--fill, 50%),
var(--paper) var(--fill, 50%)
);
block-size: 0.875rem;
border: var(--border);
box-sizing: border-box;
}
&::-moz-range-track {
background-color: var(--paper);
block-size: 0.875rem;
border: var(--border);
box-sizing: border-box;
}
&::-moz-range-progress {
background-color: var(--ember);
block-size: 0.875rem;
border: var(--border);
border-inline-end: 0;
box-sizing: border-box;
}
&::-webkit-slider-thumb {
appearance: none;
background-color: var(--ink);
block-size: 1.875rem;
border: 2px solid var(--paper);
border-radius: 0;
box-shadow: 0 0 0 2px var(--ink);
inline-size: 1rem;
margin-block-start: calc((0.875rem - 4px - 1.875rem) / 2);
}
&::-moz-range-thumb {
background-color: var(--ink);
block-size: 1.875rem;
border: 2px solid var(--paper);
border-radius: 0;
box-shadow: 0 0 0 2px var(--ink);
box-sizing: border-box;
inline-size: 1rem;
}
&:focus-visible {
outline: none;
}
&:focus-visible::-webkit-slider-thumb {
outline: 3px solid var(--ember);
outline-offset: 3px;
}
&:focus-visible::-moz-range-thumb {
outline: 3px solid var(--ember);
outline-offset: 3px;
}
&:disabled {
cursor: not-allowed;
opacity: 0.5;
}
}
[part~='description'] {
color: var(--ash);
display: block;
font-size: 0.875rem;
}
`;
tokens.styles.ts
import { css } from 'lit';
/*
The styled components' palette and type, taken from the logo: ink for the
courses, ember for the base, paper behind them.
Each is a short alias for a "--slab-styled-*" property, with the default as
its fallback. Retheming is "--slab-styled-ember" and the rest on ":root",
which every shadow root inherits. Were the defaults declared under those
names here instead, each host's own declaration would shadow the page's.
Each default is "light-dark()", so the components follow the page's
"color-scheme": "dark" for dark, "light" for light, and "light dark" to
follow the operating system. Ink and paper trade places in the dark, so
ink is always the text and paper always what's behind it. Contrast is given
as light, then dark.
*/
export default css`
:host {
/* Secondary text. 6.8:1 and 6.9:1 on paper. */
--ash: var(--slab-styled-ash, light-dark(#5e5a55, #a8a19a));
--border: 2px solid var(--ink);
/* Behind disabled and read-only controls, and hovered rows. */
--chalk: var(--slab-styled-chalk, light-dark(#f4f1ec, #2b2926));
/*
The logo's orange, brighter in the dark. 4.25:1 and 5.2:1 on paper is
enough for borders, fills and large text, but not always for small text,
which takes "--ember-deep".
*/
--ember: var(--slab-styled-ember, light-dark(#d9492a, #ec5f3b));
/*
Orange for small text on paper, and for paper text on it. 5.7:1 and
7.4:1.
*/
--ember-deep: var(--slab-styled-ember-deep, light-dark(#b3381a, #ff8566));
--font-body: var(
--slab-styled-font-body,
'IBM Plex Sans',
system-ui,
sans-serif
);
--font-display: var(
--slab-styled-font-display,
'Zilla Slab',
rockwell,
'Roboto Slab',
serif
);
--font-mono: var(
--slab-styled-font-mono,
'IBM Plex Mono',
ui-monospace,
monospace
);
--ink: var(--slab-styled-ink, light-dark(#1c1c1c, #f4f1ec));
--paper: var(--slab-styled-paper, light-dark(#fff, #1a1918));
/* The hard, offset shadow a raised surface casts. */
--shadow: 4px 4px 0 var(--ink);
}
`;