Specification
- Catalogue
- No. 42 of 110
- Section
- Forms and inputs
- Interaction
- Drag, Type
- Element
- Input, Counter
- Preview
- Live component
- Imports
- react 19.3.1, @number-flow/react 0.6.2
- Added
Notes
Slide to the number of windows and the price works itself out, with a box for typing an exact number.
A buyer can explore what changes the price before they ask. The value sits on the thumb, the sum is written out in words, and the total rolls.
Native range input with a value bubble on the thumb. Number box for exact entry, checked on leaving the field. Total rolls with NumberFlow; the calculation is shown in words.
The names, figures and pictures in the demo are sample content; a real build uses the client's own.
Code
React and plain CSS"use client";
import { useId, useState, type CSSProperties } from "react";
import { Num, radioArrows } from "@/components/pieces/shared";
import type { LivePreviewProps } from "@/components/live/types";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import type { Tour } from "@/components/pieces/stage/tour";
import "./range-slider.css";
const PER_WINDOW = 2.5;
const MIN_PRICE = 15;
const FREQ = [
{ id: "4", label: "Every 4 weeks", extra: 0 },
{ id: "8", label: "Every 8 weeks", extra: 3 },
{ id: "once", label: "One-off", extra: 10 },
];
const MIN_W = 4;
const MAX_W = 40;
function RangeSliderDemo() {
const [windows, setWindows] = useState(14);
const [text, setText] = useState("14");
const [freq, setFreq] = useState(0);
const uid = useId();
const total = Math.max(MIN_PRICE, windows * PER_WINDOW) + FREQ[freq]!.extra;
const p = (windows - MIN_W) / (MAX_W - MIN_W);
const set = (n: number) => {
const v = Math.min(MAX_W, Math.max(MIN_W, Math.round(n)));
setWindows(v);
setText(String(v));
};
return (
<div className="dm">
<div className="rs">
<div className="rs-head">
<label htmlFor={`${uid}-r`}>Windows</label>
<input
className="rs-num"
type="number"
inputMode="numeric"
min={MIN_W}
max={MAX_W}
value={text}
aria-label="Number of windows"
onChange={(e) => {
setText(e.target.value);
const n = Number(e.target.value);
if (e.target.value !== "" && Number.isFinite(n) && n >= MIN_W && n <= MAX_W) setWindows(Math.round(n));
}}
onBlur={() => set(Number(text) || MIN_W)}
/>
</div>
<div className="rs-track" style={{ "--p": p } as CSSProperties}>
<span className="rs-bubble" aria-hidden="true">
{windows}
</span>
<input id={`${uid}-r`} type="range" min={MIN_W} max={MAX_W} value={windows} onChange={(e) => set(Number(e.target.value))} aria-valuetext={`${windows} windows`} />
</div>
<div className="rs-freq" role="radiogroup" aria-label="How often" onKeyDown={radioArrows}>
{FREQ.map((f, i) => (
<button key={f.id} type="button" role="radio" aria-checked={freq === i} onClick={() => setFreq(i)}>
{f.label}
</button>
))}
</div>
<div className="rs-total">
<span>
{windows} windows at £{PER_WINDOW.toFixed(2)}
{FREQ[freq]!.extra ? `, plus £${FREQ[freq]!.extra} ${freq === 2 ? "one-off visit" : "for the longer gap"}` : ""}
</span>
<strong>
<Num value={total} format={{ style: "currency", currency: "GBP" }} />
</strong>
</div>
</div>
</div>
);
}
// What a hovered card shows: the piece used the way a visitor would.
const TOUR: Tour = [
{ input: 'input[type="range"]', value: "24", wait: 900 },
{ input: 'input[type="range"]', value: "8", wait: 900 },
{ click: '[role="radio"]', nth: 1, wait: 1100 },
{ click: '[role="radio"]', nth: 0, wait: 600 },
];
export default function RangeSliderEntry(props: LivePreviewProps) {
return (
<PieceStage {...props} label="Estimate slider" sample tour={TOUR}>
<RangeSliderDemo />
</PieceStage>
);
}