Range slider
A slider with two handles for a lowest and a highest value, such as a budget, with the range written out above it. It is two real range inputs over one track, so the keyboard works as usual.
Budget
Options
Code
import { useId, useState } from "react";
import "./range-slider.css";
/* A slider with two handles. value is [lowest, highest].
<RangeSlider label="Budget" min={0} max={10000} step={500} prefix="$" value={budget} onChange={setBudget} /> */
export function RangeSlider({ label, min = 0, max = 100, step = 1, prefix = "", value, onChange }) {
const id = useId();
const [low, high] = value;
const share = (amount) => (amount - min) / (max - min);
const text = (amount) => prefix + amount.toLocaleString("en-US");
/* The handles stop one step short of each other. */
const setLow = (amount) => onChange([Math.min(amount, high - step), high]);
const setHigh = (amount) => onChange([low, Math.max(amount, low + step)]);
return (
<div
className="dh-range"
role="group"
aria-labelledby={id}
style={{ "--lo": share(low), "--hi": share(high) }}
>
<p>
<span id={id}>{label}</span>
<output>
{text(low)} to {text(high)}
</output>
</p>
<div>
<input
type="range"
min={min}
max={max}
step={step}
value={low}
aria-label={"Lowest " + label.toLowerCase()}
aria-valuetext={text(low)}
onChange={(event) => setLow(Number(event.target.value))}
/>
<input
type="range"
min={min}
max={max}
step={step}
value={high}
aria-label={"Highest " + label.toLowerCase()}
aria-valuetext={text(high)}
onChange={(event) => setHigh(Number(event.target.value))}
/>
</div>
<p aria-hidden="true">
<small>{text(min)}</small>
<small>{text((min + max) / 2)}</small>
<small>{text(max)}</small>
</p>
</div>
);
}
export function Example() {
const [budget, setBudget] = useState([2000, 8000]);
return <RangeSlider label="Budget" min={0} max={10000} step={500} prefix="$" value={budget} onChange={setBudget} />;
}/* A slider with two handles for a lowest and a highest value, in a small card.
It is two real range inputs laid over one ruled track; --lo and --hi are
where the handles sit, from 0 to 1. */
.dh-range {
--accent: var(--dh-accent, #2563ff);
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
--thumb: 1.25em;
width: 21em;
max-width: 100%;
padding: 0.95em 1.1em 0.85em;
border-radius: 0.9em;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
color: var(--dh-fg, #171717);
font-size: 1em;
line-height: 1.5;
text-align: left;
}
.dh-range p {
display: flex;
align-items: center;
justify-content: space-between;
margin: 0;
}
.dh-range p span {
font-weight: 500;
letter-spacing: -0.015em;
}
.dh-range output {
padding: 0.1em 0.5em;
border-radius: 0.4em;
background: color-mix(in srgb, var(--accent) 12%, transparent);
color: color-mix(in srgb, var(--accent) 80%, var(--dh-fg, #171717));
font-size: 0.85em;
font-variant-numeric: tabular-nums;
}
/* The track, with the part between the handles filled. */
.dh-range div {
position: relative;
height: var(--thumb);
margin: 1em 0 0.35em;
}
.dh-range div::before {
content: "";
position: absolute;
inset: calc(50% - 0.2em) 0 auto;
height: 0.4em;
border-radius: 1em;
background: linear-gradient(
90deg,
var(--dh-well, #ededeb) calc(var(--lo) * (100% - var(--thumb)) + var(--thumb) / 2),
var(--accent) 0,
color-mix(in srgb, var(--accent) 84%, #fff) calc(var(--hi) * (100% - var(--thumb)) + var(--thumb) / 2),
var(--dh-well, #ededeb) 0
);
box-shadow: inset 0 0 0 1px var(--line);
}
.dh-range input {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
margin: 0;
background: none;
-webkit-appearance: none;
appearance: none;
pointer-events: none;
outline: none;
}
.dh-range input::-webkit-slider-runnable-track {
height: 100%;
background: none;
}
.dh-range input::-moz-range-track {
height: 100%;
background: none;
}
.dh-range input::-webkit-slider-thumb {
-webkit-appearance: none;
width: var(--thumb);
height: var(--thumb);
border: 0;
border-radius: 50%;
background: #fff;
box-shadow:
0 0 0 1px rgb(0 0 0 / 0.1),
0 1px 2px rgb(0 0 0 / 0.25),
inset 0 0 0 0.3em #fff,
inset 0 0 0 1em var(--accent);
cursor: grab;
pointer-events: auto;
transition: scale 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-range input::-moz-range-thumb {
width: var(--thumb);
height: var(--thumb);
border: 0;
border-radius: 50%;
background: #fff;
box-shadow:
0 0 0 1px rgb(0 0 0 / 0.1),
0 1px 2px rgb(0 0 0 / 0.25),
inset 0 0 0 0.3em #fff,
inset 0 0 0 1em var(--accent);
cursor: grab;
pointer-events: auto;
}
.dh-range input:active::-webkit-slider-thumb {
scale: 0.92;
cursor: grabbing;
}
.dh-range input:focus-visible::-webkit-slider-thumb {
outline: 2px solid var(--dh-faint, #8a8a8a);
outline-offset: 2px;
}
.dh-range input:focus-visible::-moz-range-thumb {
outline: 2px solid var(--dh-faint, #8a8a8a);
outline-offset: 2px;
}
/* A ruler under the track, with the ends and the middle written out. */
.dh-range p:last-child {
padding-top: 0.55em;
background: repeating-linear-gradient(90deg, var(--line) 0 1px, transparent 1px calc((100% - var(--thumb) - 1px) / 20)) calc(var(--thumb) / 2) 0 / calc(100% - var(--thumb)) 0.3em no-repeat;
}
.dh-range small {
color: var(--dh-faint, #8a8a8a);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 0.72em;
font-variant-numeric: tabular-nums;
}
@media (pointer: coarse) {
.dh-range {
--thumb: 1.75em;
}
}
@media (prefers-reduced-motion: reduce) {
.dh-range input::-webkit-slider-thumb {
transition: none;
}
}<div class="dh-range" role="group" aria-labelledby="dh-range-label" data-prefix="$" style="--lo: 0.2; --hi: 0.8">
<p><span id="dh-range-label">Budget</span><output>$2,000 to $8,000</output></p>
<div>
<input type="range" min="0" max="10000" step="500" value="2000" aria-label="Lowest budget" />
<input type="range" min="0" max="10000" step="500" value="8000" aria-label="Highest budget" />
</div>
<p aria-hidden="true"><small>$0</small><small>$5,000</small><small>$10,000</small></p>
</div>/* A slider with two handles for a lowest and a highest value, in a small card.
It is two real range inputs laid over one ruled track; --lo and --hi are
where the handles sit, from 0 to 1. */
.dh-range {
--accent: var(--dh-accent, #2563ff);
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
--thumb: 1.25em;
width: 21em;
max-width: 100%;
padding: 0.95em 1.1em 0.85em;
border-radius: 0.9em;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
color: var(--dh-fg, #171717);
font-size: 1em;
line-height: 1.5;
text-align: left;
}
.dh-range p {
display: flex;
align-items: center;
justify-content: space-between;
margin: 0;
}
.dh-range p span {
font-weight: 500;
letter-spacing: -0.015em;
}
.dh-range output {
padding: 0.1em 0.5em;
border-radius: 0.4em;
background: color-mix(in srgb, var(--accent) 12%, transparent);
color: color-mix(in srgb, var(--accent) 80%, var(--dh-fg, #171717));
font-size: 0.85em;
font-variant-numeric: tabular-nums;
}
/* The track, with the part between the handles filled. */
.dh-range div {
position: relative;
height: var(--thumb);
margin: 1em 0 0.35em;
}
.dh-range div::before {
content: "";
position: absolute;
inset: calc(50% - 0.2em) 0 auto;
height: 0.4em;
border-radius: 1em;
background: linear-gradient(
90deg,
var(--dh-well, #ededeb) calc(var(--lo) * (100% - var(--thumb)) + var(--thumb) / 2),
var(--accent) 0,
color-mix(in srgb, var(--accent) 84%, #fff) calc(var(--hi) * (100% - var(--thumb)) + var(--thumb) / 2),
var(--dh-well, #ededeb) 0
);
box-shadow: inset 0 0 0 1px var(--line);
}
.dh-range input {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
margin: 0;
background: none;
-webkit-appearance: none;
appearance: none;
pointer-events: none;
outline: none;
}
.dh-range input::-webkit-slider-runnable-track {
height: 100%;
background: none;
}
.dh-range input::-moz-range-track {
height: 100%;
background: none;
}
.dh-range input::-webkit-slider-thumb {
-webkit-appearance: none;
width: var(--thumb);
height: var(--thumb);
border: 0;
border-radius: 50%;
background: #fff;
box-shadow:
0 0 0 1px rgb(0 0 0 / 0.1),
0 1px 2px rgb(0 0 0 / 0.25),
inset 0 0 0 0.3em #fff,
inset 0 0 0 1em var(--accent);
cursor: grab;
pointer-events: auto;
transition: scale 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-range input::-moz-range-thumb {
width: var(--thumb);
height: var(--thumb);
border: 0;
border-radius: 50%;
background: #fff;
box-shadow:
0 0 0 1px rgb(0 0 0 / 0.1),
0 1px 2px rgb(0 0 0 / 0.25),
inset 0 0 0 0.3em #fff,
inset 0 0 0 1em var(--accent);
cursor: grab;
pointer-events: auto;
}
.dh-range input:active::-webkit-slider-thumb {
scale: 0.92;
cursor: grabbing;
}
.dh-range input:focus-visible::-webkit-slider-thumb {
outline: 2px solid var(--dh-faint, #8a8a8a);
outline-offset: 2px;
}
.dh-range input:focus-visible::-moz-range-thumb {
outline: 2px solid var(--dh-faint, #8a8a8a);
outline-offset: 2px;
}
/* A ruler under the track, with the ends and the middle written out. */
.dh-range p:last-child {
padding-top: 0.55em;
background: repeating-linear-gradient(90deg, var(--line) 0 1px, transparent 1px calc((100% - var(--thumb) - 1px) / 20)) calc(var(--thumb) / 2) 0 / calc(100% - var(--thumb)) 0.3em no-repeat;
}
.dh-range small {
color: var(--dh-faint, #8a8a8a);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 0.72em;
font-variant-numeric: tabular-nums;
}
@media (pointer: coarse) {
.dh-range {
--thumb: 1.75em;
}
}
@media (prefers-reduced-motion: reduce) {
.dh-range input::-webkit-slider-thumb {
transition: none;
}
}/* Keeps the two handles from crossing, fills the track between them and
writes the range out in words. */
document.querySelectorAll(".dh-range").forEach((range) => {
const [low, high] = range.querySelectorAll("input");
const output = range.querySelector("output");
const prefix = range.dataset.prefix || "";
const min = Number(low.min);
const max = Number(low.max);
const gap = Number(low.step) || 1;
const money = (value) => prefix + Number(value).toLocaleString("en-US");
const paint = (moved) => {
if (Number(high.value) - Number(low.value) < gap) {
if (moved === low) low.value = Number(high.value) - gap;
else high.value = Number(low.value) + gap;
}
range.style.setProperty("--lo", (low.value - min) / (max - min));
range.style.setProperty("--hi", (high.value - min) / (max - min));
output.textContent = money(low.value) + " to " + money(high.value);
low.setAttribute("aria-valuetext", money(low.value));
high.setAttribute("aria-valuetext", money(high.value));
};
low.addEventListener("input", () => paint(low));
high.addEventListener("input", () => paint(high));
paint();
});Range slider is not in the Figma file yet.
Use the React or HTML code for now.