Slab
Slab

Form

Slider

A single-thumb slider built on the native range input.

Element
<slab-slider>
Built on
<input type=range>

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.

Styled example As it ships
Inches. A wetter mix is easier to place and weaker once it cures.
HTML
<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>
Inches. A wetter mix is easier to place and weaker once it cures.
HTML
<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

Install
pnpm add slab-elements lit
ReactLitVueSvelteEmberHTML
import 'slab-elements/slider.js';

export function MySlider() {
  return (
    <slab-slider label="Volume" name="volume" min={0} max={100} value="50"></slab-slider>
  );
}

MySlider.jsx React 19 or later, which sets props on custom elements as properties.

import 'slab-elements/slider.js';
import { html, LitElement } from 'lit';
import { customElement } from 'lit/decorators.js';

@customElement('my-slider')
export class MySlider extends LitElement {
  render() {
    return html`
      <slab-slider label="Volume" name="volume" min="0" max="100" value="50"></slab-slider>
    `;
  }
}

my-slider.ts Slab is built on Lit, so its elements are at home in Lit's templates.

<script setup>
import 'slab-elements/slider.js';
</script>

<template>
  <slab-slider label="Volume" name="volume" :min="0" :max="100" value="50"></slab-slider>
</template>

MySlider.vue Let Vue's compiler know slab- tags are custom elements with isCustomElement.

<script>
  import 'slab-elements/slider.js';
</script>

<slab-slider label="Volume" name="volume" min={0} max={100} value="50"></slab-slider>

MySlider.svelte Svelte binds properties and events to custom elements like any other element.

import 'slab-elements/slider.js';

export const MySlider = <template>
  <slab-slider label="Volume" name="volume" min="0" max="100" value="50"></slab-slider>
</template>;

MySlider.gjs Ember sets attributes, and a dynamic value like {{this.max}} sets the property when the element has one. {{on}} listens for any event by its name.

<script type="module">
  import 'slab-elements/slider.js';
</script>

<slab-slider label="Volume" name="volume" min="0" max="100" value="50"></slab-slider>

index.html Any bundler (or an import map) takes care of the imports.

Screenreaders read the value as a bare number. When it means more than that, set formatValue to a function of the value, such as (value) => `$${value}`, and they’ll read what it returns instead.

Its value is always in range, so it’s never invalid on its own. For a rule of your own, such as a minimum, call setCustomValidity() with a message. It shows in ::part(validity-message), and the host matches :state(error), once validity is reported, as on Text Field: when focus leaves, when its form is submitted, or on reportValidity().

stepUp() and stepDown() step the value by step from code, as a range input’s do, such as for buttons beside the slider. They’re kept to min and max, dispatch no “input” or “change”, and throw when step is “any”.

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.

Screenreaders read the value as a number. Set formatValue to a function of it that says what it means, such as "$40" or "3 of 5 GB", which becomes the input's aria-valuetext.

Its value is always in range, so it's never invalid on its own. For a rule of your own, such as a minimum, call setCustomValidity() or setValidity(). The validity message shows in its description, as on Text Field, once validity is reported: when focus leaves it, its form is submitted, or reportValidity() is called.

Style it through its parts and through :state(disabled) and :state(error). Where the label sits relative to the input is layout, so it's left to the styles around it.

API

<slab-slider>

Attributes
Name Type Default Description
label Required string Names it. It's the accessible name, and the visible label where there is one.
disabled boolean false Disables it, as disabled does a native control: the user can't focus or use it, and a form control's value isn't submitted.
hide-label boolean false Hides the label visually and leaves it as the accessible name, for a design that shows the label some other way.
max number 100 when unset, as on a range input.
min number 0 when unset, as on a range input.
name string '' The name its value is submitted under, as a native control's name.
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.
custom-validity string Makes it invalid with this message, as setCustomValidity() does, and shows the message at once rather than once validity is reported, for an error the page already knows about, such as one from a server. Removing it, or setting it to an empty string, makes it valid again, unless something else makes it invalid.
Properties
Name Type Description
formatValue (value: number) => string | undefined What screenreaders read for the value, such as "$40" rather than "40". The value itself when unset.
defaultValue string The value it starts with, and goes back to when its form resets. It's the value attribute, as a native input's defaultValue is, so setting it sets the attribute, and value with it.
form Read-only HTMLFormElement | null The form it belongs to, or null.
labels Read-only NodeList The <label> elements associated with it, as a native control's labels: each <label for> that names its id, and a <label> around it.
validationMessage Read-only string What's wrong, as the validity message says it: the message given to setCustomValidity() or setValidity(). Empty while it's valid.
validity Read-only ValidityState Its ValidityState, as a native control's.
valueAsNumber number value as a number, as on a native range input.
willValidate Read-only boolean Whether its validity is checked when its form submits. It isn't while it's disabled or read-only.
Methods
Signature Description
checkValidity(): boolean Whether its value is valid. Dispatches "invalid" when it isn't, as a native control's does.
click(): void Clicks it, as a native element's click() does.
focus(options?: FocusOptions): void Focuses it, or the part of it that takes focus.
reportValidity(): boolean Whether its value is valid. When it isn't, dispatches "invalid" and shows the validity message, as a native control's does.
resetValidityFeedback(): void Clears the validity feedback without changing validity.
setCustomValidity(message: string): void Makes it invalid with a message of your own, or valid again with an empty one, as a native control's does.
setValidity(flags?: ValidityStateFlags, message?: string): void Sets its validity and message, as ElementInternals.setValidity() does, for checks of your own beyond setCustomValidity().
stepDown(count?: number): void Steps value down by step times count, as a native range input's stepDown() does: kept to min and snapped to a step. Like a native input's, it dispatches no events, and throws when step is "any".
stepUp(count?: number): void Steps value up by step times count, as a native range input's stepUp() does: kept to max and snapped to a step. Like a native input's, it dispatches no events, and throws when step is "any".
Events
Name Type Description
input Event As the user changes its value, as a native control's does. Setting it from code doesn't dispatch it.
change Event When the user changes its value, as a native control's does. Setting it from code doesn't dispatch it.
invalid Event When a check finds its value invalid, as a native control's does. Cancel it to leave the validity message unshown, after a blur, a submit or reportValidity(), and focus where it is.
Slots
Name Type Description
description Element | string Text that describes it, such as a hint or what the validity message means. It's its accessible description.
validity-message Element | string Shown in place of the validity message's text while it's invalid and the message shows, for wording of your own, a translation, or a link. It's part of its accessible description, and validationMessage stays the browser's or setCustomValidity()'s.
CSS parts
Name Description
description Text about the slider, and its validity message when it has one
input The input
label The label
validity-message The validity message, which says why the value is invalid
CSS states
Name Description
:state(disabled) While it's disabled.
:state(error) While it's invalid and its validity message shows, which is once its validity is reported, as when its form is submitted.

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. 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.

ReactLitVueSvelteEmber

This one styles its native control's own pieces, like a range input's thumb or a meter's bar. Browsers only let those be styled from inside the element's shadow root, not through ::part(), so it can't be written as plain CSS. Copy the Lit files instead. They're a custom element like any other, so they work in React as they are.

Under the hood, it's just the headless element, subclassed, with styles set. That's it. 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;
  }

  [part~='validity-message'] {
    color: var(--ember-deep);
    display: block;
    font-size: 0.875rem;
    font-weight: 600;
    margin-block-start: 0.375rem;

    &::before {
      content: '■ ' / '';
    }
  }
`;
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);
  }

  /*
    Forced colors, such as Windows' contrast themes, replace every color that
    isn't a system color, so ember fills and paper marks all become the same
    background, and a checked Checkbox looks like an unchecked one. The palette
    becomes the user's system colors instead, which are kept. Ember is the
    highlight, the color of what's selected, and ember-deep, being for text and
    for paper text on it, is the text color.
  */
  @media (forced-colors: active) {
    :host {
      --ash: GrayText;
      --chalk: Canvas;
      --ember: Highlight;
      --ember-deep: CanvasText;
      --ink: CanvasText;
      --paper: Canvas;
    }
  }
`;

This one styles its native control's own pieces, like a range input's thumb or a meter's bar. Browsers only let those be styled from inside the element's shadow root, not through ::part(), so it can't be written as plain CSS. Copy the Lit files instead. They're a custom element like any other, so they work in Vue as they are.

This one styles its native control's own pieces, like a range input's thumb or a meter's bar. Browsers only let those be styled from inside the element's shadow root, not through ::part(), so it can't be written as plain CSS. Copy the Lit files instead. They're a custom element like any other, so they work in Svelte as they are.

This one styles its native control's own pieces, like a range input's thumb or a meter's bar. Browsers only let those be styled from inside the element's shadow root, not through ::part(), so it can't be written as plain CSS. Copy the Lit files instead. They're a custom element like any other, so they work in Ember as they are.