Slab

Form

Range Slider

A two-thumb slider for picking a range, with a thumb at each end that can meet but not pass the other.

Element
<slab-range-slider>
Built on
[role=slider]
As Markdown
range-slider.md

Example

Here's a styled version, <slab-styled-range-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.

Styled example As it ships
Inches. Most slabs pour between 3 and 5.
HTML
<slab-styled-range-slider label="Slump" max="7" min="2" step="0.5" start-value="3" end-value="5">
  <span slot="description">Inches. Most slabs pour between 3 and 5.</span>
</slab-styled-range-slider>
Inches. Most slabs pour between 3 and 5.
HTML
<slab-range-slider label="Slump" max="7" min="2" step="0.5" start-value="3" end-value="5">
  <span slot="description">Inches. Most slabs pour between 3 and 5.</span>
</slab-range-slider>

Usage

Install
pnpm add slab-elements lit
HTML
<script type="module">
  import 'slab-elements/range-slider.js';
</script>

<slab-range-slider label="Price" name="price" min="0" max="500" start-value="100" end-value="300"></slab-range-slider>

Each thumb is named by label and then its own label, “Minimum” and “Maximum” unless you set start-label and end-label, so a screenreader reads “Price Minimum”. The form gets two entries under name, the start and then the end. Give the track a size, since the thumbs and the range between them are placed along it.

Keyboard

Key What it does
Left, Down, Right, Up Steps the focused thumb down or up by step. Left and Right move it the way the arrow points.
Page Up, Page Down Steps it by a tenth of the range.
Home, End Moves it as far as it can go: to min or max, or to the other thumb.

Behavior

A two-thumb slider with no visual styles, for picking a range, such as a price from one amount to another. It follows the APG multi-thumb slider pattern: each thumb is a [role=slider], and each one's minimum or maximum is the other thumb, so the two can meet but not pass each other.

A range input has only one thumb, so the thumbs are elements of their own rather than two range inputs on top of each other, which can't report each other as their bounds and fight over the pointer where they overlap.

start-value and end-value are the range, and they're always between min and max, on a step from min, and in order. Unset, they're min and max, so the whole range is picked. Like a native input's value, their attributes are the defaults and their properties the current values.

The group is named by label, and each thumb by it and its own label, so a screenreader reads "Price Minimum" and "Price Maximum". The arrow keys move a thumb by step, Page Up and Page Down by a tenth of the range, and Home and End as far as it can go. Drag a thumb, or press the track to bring the nearer thumb there.

Placing the thumbs and the range between them along the track is what a slider does, so that's functional and done here: each thumb is centered on its value, and the range spans from one to the other. Give the track a size, and style the rest through its parts and through :state(disabled) and :state(dragging).

API

<slab-range-slider>

Attributes
Name Type Default Description
label Required string Names the group. Each thumb is named by it and its own label.
disabled boolean false
end-label string Names the end thumb, after label. "Maximum" when unset.
end-value number undefined Like a native input's value, the attribute is the default and the property is the current value. Between start-value and max, on a step. max when unset.
hide-label boolean false
max number 100
min number 0
name string '' Submitted twice, with the start value and then the end value.
start-label string Names the start thumb, after label. "Minimum" when unset.
start-value number undefined Like a native input's value, the attribute is the default and the property is the current value. Between min and end-value, on a step. min when unset.
step number 1
version string
Properties
Name Type Description
form Read-only HTMLFormElement | null
validity Read-only ValidityState
Methods
Signature Description
checkValidity(): boolean
focus(options?: FocusOptions): void
reportValidity(): boolean
Events
Name Type Description
input Event
change Event
Slots
Name Type Description
description Element | string Text about the slider, such as a hint
CSS parts
Name Description
container The label, the track and the description
description Text about the slider, such as a hint
end The end thumb
label The label
range The part of the track between the thumbs
start The start thumb
track The thumbs and the range between them

Copy and own it

<slab-styled-range-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.

range-slider.ts
import { customElement } from 'lit/decorators.js';
import RangeSlider from 'slab-elements/range-slider.js';
import styles from './range-slider.styles.js';
import tokens from './tokens.styles.js';

declare global {
  interface HTMLElementTagNameMap {
    'slab-styled-range-slider': StyledRangeSlider;
  }
}

/**
 * Range Slider, styled: Slider's look, with an ember range between two
 * brick-shaped thumbs.
 */
@customElement('slab-styled-range-slider')
export default class StyledRangeSlider extends RangeSlider {
  static override styles = [tokens, styles];
}
range-slider.styles.ts
import { css } from 'lit';

/*
  The thumbs are centered on their values, so at either end of the track half
  of one hangs off it. The track is inset by half a thumb so they stay inside
  the host.
*/
export default css`
  :host {
    color: var(--ink);
    display: grid;
    font-family: var(--font-body);
  }

  [part~='container'] {
    display: grid;
    gap: 0.5rem;
  }

  [part~='label'] {
    font-family: var(--font-display);
    font-size: 1.0625rem;
    font-weight: 600;
  }

  [part~='track'] {
    background-color: var(--paper);
    block-size: 0.875rem;
    border: var(--border);
    box-sizing: border-box;
    cursor: pointer;
    margin-block: 0.5rem;
    margin-inline: 0.5rem;
  }

  [part~='range'] {
    background-color: var(--ember);
  }

  [part~='start'],
  [part~='end'] {
    background-color: var(--ink);
    block-size: 1.875rem;
    border: 2px solid var(--paper);
    box-shadow: 0 0 0 2px var(--ink);
    box-sizing: border-box;
    inline-size: 1rem;

    &:focus-visible {
      outline: 3px solid var(--ember);
      outline-offset: 3px;
    }
  }

  [part~='description'] {
    color: var(--ash);
    display: block;
    font-size: 0.875rem;
  }

  :host(:state(disabled)) [part~='track'] {
    cursor: not-allowed;
    opacity: 0.5;
  }
`;
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);
  }
`;