Price range slider
A two-thumb range slider over a little histogram of prices; bars outside the range fade and the labels follow the thumbs.
"use client";
import { useMemo, useState, type CSSProperties } from "react";
import "./range-slider.css";
type RangeSliderProps = {
min?: number;
max?: number;
step?: number;
theme?: "light" | "dark";
};
/**
* RangeSlider
* - Two native <input type="range"> stacked on one track: keyboard, touch and screen readers work for free.
* - Only the thumbs receive pointer events, so either thumb can be grabbed.
* - The thumbs can never cross (each one is clamped by the other).
*/
export function RangeSlider({
min = 0,
max = 500,
step = 5,
theme = "light",
}: RangeSliderProps) {
const [lo, setLo] = useState(80);
const [hi, setHi] = useState(320);
const pct = (v: number) => ((v - min) / (max - min)) * 100;
// Fake product prices for the histogram. Swap in your own counts.
const bars = useMemo(
() =>
Array.from(
{ length: 28 },
(_, i) =>
12 +
Math.round(
70 * Math.exp(-(((i - 9) / 6) ** 2)) +
30 * Math.exp(-(((i - 19) / 3) ** 2)),
),
),
[],
);
const inRange = (i: number) => {
const v = min + ((i + 0.5) / bars.length) * (max - min);
return v >= lo && v <= hi;
};
const count = bars.reduce((sum, b, i) => sum + (inRange(i) ? b : 0), 0);
return (
<div
className="gu-range"
data-theme={theme}
style={{ "--lo": `${pct(lo)}%`, "--hi": `${pct(hi)}%` } as CSSProperties}
>
<div className="gu-range__head">
<strong>Price</strong>
<span>{count} products</span>
</div>
<div className="gu-range__hist" aria-hidden="true">
{bars.map((b, i) => (
<span
key={i}
style={{ height: `${b}%` }}
data-in={inRange(i) ? "" : undefined}
/>
))}
</div>
<div className="gu-range__track">
<div className="gu-range__fill" />
<input
type="range"
min={min}
max={max}
step={step}
value={lo}
aria-label="Minimum price"
aria-valuetext={`$${lo}`}
onChange={(e) => setLo(Math.min(Number(e.target.value), hi - step))}
/>
<input
type="range"
min={min}
max={max}
step={step}
value={hi}
aria-label="Maximum price"
aria-valuetext={`$${hi}`}
onChange={(e) => setHi(Math.max(Number(e.target.value), lo + step))}
/>
<output className="gu-range__bubble" style={{ left: "var(--lo)" }}>
${lo}
</output>
<output className="gu-range__bubble" style={{ left: "var(--hi)" }}>
${hi}
{hi === max ? "+" : ""}
</output>
</div>
<div className="gu-range__fields">
<label>
<span>Min</span>
<input
type="number"
min={min}
max={hi - step}
step={step}
value={lo}
onChange={(e) =>
setLo(
Math.max(min, Math.min(Number(e.target.value) || 0, hi - step)),
)
}
/>
</label>
<i aria-hidden="true" />
<label>
<span>Max</span>
<input
type="number"
min={lo + step}
max={max}
step={step}
value={hi}
onChange={(e) =>
setHi(
Math.min(max, Math.max(Number(e.target.value) || 0, lo + step)),
)
}
/>
</label>
</div>
</div>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: Price range slider
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
A shop-filter price range: a two-thumb slider sitting under a small histogram of product prices. Bars inside the range turn orange, the count updates, and price bubbles follow the thumbs. Min/Max number fields stay in sync.
## Layout and look
- Card 420px, radius 22px. Header "Price" (bold) and "{n} products" (muted, tabular numbers).
- Histogram: 28 thin bars, 72px tall, faint gray; in-range bars orange #ff5a1f.
- Track: 4px line, orange fill between the thumbs. Thumbs 28px Ink circles with a 4px card-colored ring and soft shadow; grow 15% while dragging.
- Bubbles under each thumb: small Ink pills with "$80" / "$320" (a "+" at max).
- Two bordered number fields with a short dash between.
## Behavior and motion
1. Two native range inputs stacked on one track (appearance none, pointer-events only on thumbs) so either thumb can be dragged.
2. Clamp so min stays at least one step below max.
3. CSS variables --lo and --hi (percentages) position the fill and bubbles.
4. Bars fade color in 250ms.
## Touch
Native range dragging; typed fields as a fallback.
## Accessibility
- Each range has aria-label ("Minimum price", "Maximum price") and aria-valuetext with the currency.
- Focus ring on the thumb (#2b35ff, dark #d7ff3a). Histogram is aria-hidden.
- prefers-reduced-motion: no transitions.
## Props
min, max, step, theme. Replace the bars array with real counts.
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### RangeSlider.tsx
```tsx
"use client";
import { useMemo, useState, type CSSProperties } from "react";
import "./range-slider.css";
type RangeSliderProps = {
min?: number;
max?: number;
step?: number;
theme?: "light" | "dark";
};
/**
* RangeSlider
* - Two native <input type="range"> stacked on one track: keyboard, touch and screen readers work for free.
* - Only the thumbs receive pointer events, so either thumb can be grabbed.
* - The thumbs can never cross (each one is clamped by the other).
*/
export function RangeSlider({
min = 0,
max = 500,
step = 5,
theme = "light",
}: RangeSliderProps) {
const [lo, setLo] = useState(80);
const [hi, setHi] = useState(320);
const pct = (v: number) => ((v - min) / (max - min)) * 100;
// Fake product prices for the histogram. Swap in your own counts.
const bars = useMemo(
() =>
Array.from(
{ length: 28 },
(_, i) =>
12 +
Math.round(
70 * Math.exp(-(((i - 9) / 6) ** 2)) +
30 * Math.exp(-(((i - 19) / 3) ** 2)),
),
),
[],
);
const inRange = (i: number) => {
const v = min + ((i + 0.5) / bars.length) * (max - min);
return v >= lo && v <= hi;
};
const count = bars.reduce((sum, b, i) => sum + (inRange(i) ? b : 0), 0);
return (
<div
className="gu-range"
data-theme={theme}
style={{ "--lo": `${pct(lo)}%`, "--hi": `${pct(hi)}%` } as CSSProperties}
>
<div className="gu-range__head">
<strong>Price</strong>
<span>{count} products</span>
</div>
<div className="gu-range__hist" aria-hidden="true">
{bars.map((b, i) => (
<span
key={i}
style={{ height: `${b}%` }}
data-in={inRange(i) ? "" : undefined}
/>
))}
</div>
<div className="gu-range__track">
<div className="gu-range__fill" />
<input
type="range"
min={min}
max={max}
step={step}
value={lo}
aria-label="Minimum price"
aria-valuetext={`$${lo}`}
onChange={(e) => setLo(Math.min(Number(e.target.value), hi - step))}
/>
<input
type="range"
min={min}
max={max}
step={step}
value={hi}
aria-label="Maximum price"
aria-valuetext={`$${hi}`}
onChange={(e) => setHi(Math.max(Number(e.target.value), lo + step))}
/>
<output className="gu-range__bubble" style={{ left: "var(--lo)" }}>
${lo}
</output>
<output className="gu-range__bubble" style={{ left: "var(--hi)" }}>
${hi}
{hi === max ? "+" : ""}
</output>
</div>
<div className="gu-range__fields">
<label>
<span>Min</span>
<input
type="number"
min={min}
max={hi - step}
step={step}
value={lo}
onChange={(e) =>
setLo(
Math.max(min, Math.min(Number(e.target.value) || 0, hi - step)),
)
}
/>
</label>
<i aria-hidden="true" />
<label>
<span>Max</span>
<input
type="number"
min={lo + step}
max={max}
step={step}
value={hi}
onChange={(e) =>
setHi(
Math.min(max, Math.max(Number(e.target.value) || 0, lo + step)),
)
}
/>
</label>
</div>
</div>
);
}
```
### range-slider.css
```css
/* RangeSlider: change these colors to match your brand. */
.gu-range {
--card: #fbf8f2;
--text: #0e0e10;
--muted: #57534b;
--line: rgb(14 14 16 / 0.16);
--bar: rgb(14 14 16 / 0.14);
--accent: #ff5a1f;
--thumb: #0e0e10;
--focus: #2b35ff;
display: grid;
gap: 14px;
width: min(100%, 420px);
padding: 24px;
border: 1px solid var(--line);
border-radius: 22px;
background: var(--card);
color: var(--text);
font-family: system-ui, sans-serif;
}
.gu-range[data-theme="dark"] {
--card: #18181b;
--text: #f3eee3;
--muted: #aea89c;
--line: rgb(243 238 227 / 0.16);
--bar: rgb(243 238 227 / 0.14);
--thumb: #f3eee3;
--focus: #d7ff3a;
}
.gu-range__head {
display: flex;
justify-content: space-between;
align-items: baseline;
}
.gu-range__head strong {
font-size: 1.15rem;
font-weight: 900;
}
.gu-range__head span {
color: var(--muted);
font-size: 0.85rem;
font-variant-numeric: tabular-nums;
}
.gu-range__hist {
display: flex;
align-items: flex-end;
gap: 2px;
height: 72px;
margin: 8px 14px 0;
}
.gu-range__hist span {
flex: 1;
border-radius: 3px 3px 0 0;
background: var(--bar);
transition: background-color 0.25s;
}
.gu-range__hist span[data-in] {
background: var(--accent);
}
.gu-range__track {
position: relative;
height: 28px;
margin: 0 14px 30px;
}
.gu-range__track::before,
.gu-range__fill {
content: "";
position: absolute;
top: 12px;
height: 4px;
border-radius: 4px;
}
.gu-range__track::before {
left: 0;
right: 0;
background: var(--bar);
}
.gu-range__fill {
left: var(--lo);
right: calc(100% - var(--hi));
background: var(--accent);
}
/* Stack both native ranges on top of each other; only thumbs catch the pointer. */
.gu-range__track input {
position: absolute;
inset: 0 -14px;
width: calc(100% + 28px);
height: 28px;
margin: 0;
background: none;
pointer-events: none;
appearance: none;
-webkit-appearance: none;
}
.gu-range__track input::-webkit-slider-runnable-track {
height: 28px;
background: none;
}
.gu-range__track input::-moz-range-track {
background: none;
}
.gu-range__track input::-webkit-slider-thumb {
width: 28px;
height: 28px;
border: 4px solid var(--card);
border-radius: 50%;
background: var(--thumb);
box-shadow:
0 0 0 1.5px var(--thumb),
0 6px 14px -4px rgb(0 0 0 / 0.4);
cursor: grab;
pointer-events: auto;
-webkit-appearance: none;
transition: transform 0.2s;
}
.gu-range__track input::-moz-range-thumb {
width: 20px;
height: 20px;
border: 4px solid var(--card);
border-radius: 50%;
background: var(--thumb);
box-shadow: 0 0 0 1.5px var(--thumb);
cursor: grab;
pointer-events: auto;
}
.gu-range__track input:active::-webkit-slider-thumb {
transform: scale(1.15);
cursor: grabbing;
}
.gu-range__track input:focus-visible::-webkit-slider-thumb {
box-shadow:
0 0 0 1.5px var(--thumb),
0 0 0 6px var(--focus);
}
.gu-range__track input:focus-visible::-moz-range-thumb {
box-shadow: 0 0 0 6px var(--focus);
}
.gu-range__track input:focus {
outline: none;
}
.gu-range__bubble {
position: absolute;
top: 34px;
translate: -50% 0;
padding: 2px 8px;
border-radius: 8px;
background: var(--thumb);
color: var(--card);
font-size: 0.78rem;
font-weight: 800;
font-variant-numeric: tabular-nums;
white-space: nowrap;
pointer-events: none;
}
.gu-range__fields {
display: grid;
grid-template-columns: 1fr 16px 1fr;
align-items: center;
gap: 8px;
}
.gu-range__fields i {
height: 1.5px;
background: var(--line);
}
.gu-range__fields label {
display: grid;
gap: 2px;
padding: 8px 12px;
border: 1.5px solid var(--line);
border-radius: 12px;
}
.gu-range__fields label:focus-within {
border-color: var(--focus);
}
.gu-range__fields span {
color: var(--muted);
font-size: 0.72rem;
font-weight: 700;
}
.gu-range__fields input {
width: 100%;
border: 0;
outline: none;
background: none;
color: inherit;
font:
800 1rem system-ui,
sans-serif;
font-variant-numeric: tabular-nums;
}
@media (prefers-reduced-motion: reduce) {
.gu-range__hist span,
.gu-range__track input::-webkit-slider-thumb {
transition: none;
}
}
```
You might also want
Next piece: Pricing comparison tableDate range picker
Components, advanced
Floating label form
Components, beginner
Multi-step form
Components, advanced