Skip to content

Range slider

A two-handle range on one track with the span filled, for a filter like amount from and to.

<div class="cap-specimen-row" style="max-inline-size: 18rem">
  <div class="cap-range-slider" id="rs-live" data-cap="range-slider" data-currency="USD" data-cents data-open-top role="group" aria-labelledby="rs-live-l">
    <div class="cap-range-slider-head">
      <span class="cap-range-slider-label" id="rs-live-l">Amount</span>
      <output class="cap-range-slider-value" aria-hidden="true">$0 โ€“ $500+</output>
    </div>
    <div class="cap-range-slider-track">
      <input type="range" class="cap-range cap-range-slider-input" name="amountMin" min="0" max="50000" step="1000" value="0" aria-label="Minimum amount" />
      <input type="range" class="cap-range cap-range-slider-input" name="amountMax" min="0" max="50000" step="1000" value="50000" aria-label="Maximum amount" />
    </div>
    <div class="cap-range-slider-ends" aria-hidden="true"><span>$0</span><span>$500+</span></div>
  </div>
</div>
<p id="rs-said" aria-live="polite"></p>

Installation

// package.json: "capsomer": "github:DrDustinEdwards/capsomer#v0.152.2"
import "capsomer/tokens.css";
import "capsomer/base.css";
import "capsomer/range-slider.css";
import { enhance } from "capsomer/behavior/range-slider";

enhance();

Usage

<div class="cap-range-slider" data-cap="range-slider" data-currency="USD" data-cents data-open-top role="group" aria-labelledby="amount-l">
  <div class="cap-range-slider-head">
    <span class="cap-range-slider-label" id="amount-l">Amount</span>
    <output class="cap-range-slider-value" aria-hidden="true">$0 โ€“ $500+</output>
  </div>
  <div class="cap-range-slider-track">
    <input type="range" class="cap-range cap-range-slider-input" name="amountMin" min="0" max="50000" step="1000" value="0" aria-label="Minimum amount" />
    <input type="range" class="cap-range cap-range-slider-input" name="amountMax" min="0" max="50000" step="1000" value="50000" aria-label="Maximum amount" />
  </div>
  <div class="cap-range-slider-ends" aria-hidden="true"><span>$0</span><span>$500+</span></div>
</div>

Examples

A chosen span, the high handle under the pointer

<div class="cap-specimen-row" style="max-inline-size: 18rem">
  <div class="cap-range-slider" data-cap="range-slider" role="group" aria-labelledby="rs-span-l">
    <div class="cap-range-slider-head">
      <span class="cap-range-slider-label" id="rs-span-l">Retries</span>
      <output class="cap-range-slider-value" aria-hidden="true">2 โ€“ 6</output>
    </div>
    <div class="cap-range-slider-track">
      <input type="range" class="cap-range cap-range-slider-input" min="0" max="10" step="1" value="2" aria-label="Fewest retries" />
      <input type="range" class="cap-range cap-range-slider-input" min="0" max="10" step="1" value="6" aria-label="Most retries" />
    </div>
    <div class="cap-range-slider-ends" aria-hidden="true"><span>0</span><span>10</span></div>
  </div>
</div>

API reference

RangeSlider

PropTypeRequiredDoes
labelstringYes
minnumberYes
maxnumberYes
lownumberYesThe values in force (from the address, say). New values move the handles.
highnumberYes
stepnumberYes
names[string, string]YesThe inputs' names, so a form with no script still submits both.
handleLabels[string, string]The handles' names for assistive technology.
format(value: number, which: "low" | "high") => stringA value in words: "$120", or "$500+" at the top.
onCommit(low: number, high: number) => voidCalled a quarter second after a handle stops.
classNamestring

Functions

FunctionSignatureDoes
rangeFill(value: number, min: number, max: number): stringA value's share of the track, "0.00%" to "100.00%": the single slider's --cap-range-fill too.
clampPair(low: number, high: number, { min, max, step }: { min: number; max: number; step: number; }, moved: "low" | "high"): [number, number]The low and high values kept a step apart and inside the track.
trackPick(track: HTMLElement, clientX: number, [low, high]: [number, number], { min, max, step }: { min: number; max: number; step: number; }): { which: "low" | "high"; value: number; }A press on the track away from the thumbs (WCAG 2.5.7: a single pointer sets a handle without a drag): the value under the pointer, on a step, and the handle nearer to it, the low one on a tie below the span and the high one above it. RTL tracks run from the right.
formatValue(root: HTMLElement, value: number, max: number): stringThe words for a value: data-currency (whole units of a currency, in cents when data-cents), with "+" on the top of the track when data-open-top says the top means "and more".
paint(root: HTMLElement): voidWrites the fill and the words from the inputs' values.
enhance(root?: ParentNode): () => void

CSS variables

VariableDefault
--cap-range-hi100%
--cap-range-lo0%
--cap-range-tracktransparent

Parts and data attributes

ClassIs
.cap-range-sliderthe root
.cap-range-slider-endsa part
.cap-range-slider-heada part
.cap-range-slider-inputa part
.cap-range-slider-labela part
.cap-range-slider-tracka part
.cap-range-slider-valuea part
StateRead from
:focus-visiblethe platform

Accessibility

KeyDoes
TabMoves to the low handle, then the high one
Arrow keysMove the handle one step
Page Up, Page DownMove the handle a larger step (the browser's)
Home, EndMove the handle to its end of the track
  • A group named by its visible label; each handle has its own name ("Minimum amount") and its value in words.
  • The span over the track repeats the handles' values and is hidden from assistive technology, so they are not read twice.
  • Focus is the ring around the thumb; in forced colors an outline.