Range slider
Two native sliders for the ends of an interval, with an overview track and labelled numeric readouts.
pnpm dlx shadcn@latest add @haze/range-slider---
import { cn } from "../../../lib/haze/utils";
import Slider from "./slider.astro";
interface Props {
id: string;
label?: string;
lowerLabel?: string;
upperLabel?: string;
lowerName?: string;
upperName?: string;
lower?: number;
upper?: number;
min?: number;
max?: number;
step?: number;
unit?: string;
disabled?: boolean;
class?: string;
}
const {
id, label = "Budget range", lowerLabel = "Minimum", upperLabel = "Maximum",
lowerName = `${id}-lower`, upperName = `${id}-upper`, min = 0, max = 100, lower, upper, step = 1,
unit = "", disabled, class: className,
} = Astro.props;
const floor = Number.isFinite(min) ? min : 0;
const ceiling = Number.isFinite(max) && Number.isFinite(max - floor) ? Math.max(floor, max) : Math.max(floor, 100);
const increment = Number.isFinite(step) && step > 0 ? step : 1;
const extent = ceiling - floor;
const clamp = (value: number | undefined, fallback: number) =>
value !== undefined && Number.isFinite(value) ? Math.min(ceiling, Math.max(floor, value)) : fallback;
const start = clamp(lower, floor + extent * 0.25);
const end = Math.max(start, clamp(upper, floor + extent * 0.75));
const readout = "hidden text-muted-foreground tabular-nums [[data-haze-range-slider][data-enhanced]_&]:inline";
---
<fieldset data-haze-range-slider data-range-unit={unit} disabled={disabled} class={cn("min-w-0 text-foreground", className)}>
<legend class="mb-3 text-[0.8125rem]/5 font-medium">{label}</legend>
<div class="grid gap-3 rounded-xl border border-border bg-card p-4 text-card-foreground">
<div aria-hidden="true" class="relative mb-1 hidden h-1.5 rounded-full bg-input [[data-haze-range-slider][data-enhanced]_&]:block forced-colors:border forced-colors:border-current">
<div class="absolute inset-y-0 left-(--range-start) w-[calc(var(--range-end)_-_var(--range-start))] rounded-full bg-primary forced-colors:bg-[Highlight]"></div>
</div>
<noscript><p class="text-[0.8125rem]/5 text-muted-foreground">Range {floor}–{ceiling}{unit && ` ${unit}`}.</p></noscript>
<div class="grid min-w-0 gap-0.5">
<div class="flex min-w-0 flex-wrap items-baseline justify-between gap-x-2 text-[0.8125rem]/5">
<label for={`${id}-lower`} class="font-medium">{lowerLabel}</label>
<span data-lower-value aria-hidden="true" class={readout}></span>
</div>
<Slider id={`${id}-lower`} name={lowerName} min={floor} max={ceiling} step={increment} value={start} data-range-lower dir="ltr" />
</div>
<div class="grid min-w-0 gap-0.5">
<div class="flex min-w-0 flex-wrap items-baseline justify-between gap-x-2 text-[0.8125rem]/5">
<label for={`${id}-upper`} class="font-medium">{upperLabel}</label>
<span data-upper-value aria-hidden="true" class={readout}></span>
</div>
<Slider id={`${id}-upper`} name={upperName} min={floor} max={ceiling} step={increment} value={end} data-range-upper dir="ltr" />
</div>
</div>
</fieldset>
<script>
function enhance(root: HTMLElement) {
if (root.hasAttribute("data-enhanced")) return;
const lower = root.querySelector<HTMLInputElement>("[data-range-lower]");
const upper = root.querySelector<HTMLInputElement>("[data-range-upper]");
const lowText = root.querySelector<HTMLElement>("[data-lower-value]");
const highText = root.querySelector<HTMLElement>("[data-upper-value]");
if (!lower || !upper || !lowText || !highText) return;
const unit = root.dataset.rangeUnit;
const sync = (changed: EventTarget | null = lower) => {
if (lower.valueAsNumber > upper.valueAsNumber) {
if (changed === upper) upper.value = lower.value;
else lower.value = upper.value;
}
const min = Number(lower.min);
const width = Number(lower.max) - min;
const share = (input: HTMLInputElement) => width > 0 ? (input.valueAsNumber - min) / width * 100 : 0;
root.style.setProperty("--range-start", `${share(lower)}%`);
root.style.setProperty("--range-end", `${share(upper)}%`);
for (const [input, text] of [[lower, lowText], [upper, highText]] as const) {
const value = unit ? `${input.value} ${unit}` : input.value;
if (unit) input.setAttribute("aria-valuetext", value);
text.textContent = value;
}
};
root.addEventListener("input", (event) => sync(event.target), { capture: true });
root.addEventListener("change", (event) => sync(event.target), { capture: true });
lower.form?.addEventListener("reset", () => queueMicrotask(sync));
sync();
root.setAttribute("data-enhanced", "");
}
const init = () => document.querySelectorAll<HTMLElement>("[data-haze-range-slider]").forEach(enhance);
init();
if (!document.documentElement.hasAttribute("data-haze-range-slider-listener")) {
document.documentElement.setAttribute("data-haze-range-slider-listener", "");
document.addEventListener("astro:page-load", init);
}
</script>When to use
- A filter or threshold setting needs a lower and an upper numeric bound
- Each end needs its own keyboard target and form value
When not to use
- Only a single numeric value matters. Use slider.
- Exact values are better typed. Use two number inputs.
Installation
Add the Haze registry to components.json once, then install Range slider with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/range-slidernpx shadcn@latest add @haze/range-slideryarn dlx shadcn@latest add @haze/range-sliderbunx --bun shadcn@latest add @haze/range-sliderFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| id* | string | – | Unique prefix for the component and its labelled controls. |
| label | string | "Budget range" | Legend for the interval. |
| lowerLabel | string | "Minimum" | Label for the lower endpoint. |
| upperLabel | string | "Maximum" | Label for the upper endpoint. |
| lowerName | string | id + -lower | Name of the submitted lower endpoint. |
| upperName | string | id + -upper | Name of the submitted upper endpoint. |
| lower | number | 25% of the available span | Initial lower endpoint. |
| upper | number | 75% of the available span | Initial upper endpoint. |
| min | number | 0 | Smallest value on the scale. |
| max | number | 100 | Largest value on the scale. |
| step | number | 1 | Positive increment for both endpoints. |
| unit | string | "" | Display and spoken unit. |
| disabled | boolean | – | Disables both native sliders. |
| class | string | – | Additional classes on the fieldset. |
Keyboard interactions
- Tab
- Move between endpoints.
- Arrows, Home or End
- Set the focused endpoint on the available scale.
Accessibility
- The endpoints remain independent native ranges in the keyboard tab order.
- With scripting, the currently edited endpoint stops at the other endpoint. Validate the interval on submission.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
Install and use
Install the Haze UI component "Range slider" with `npx shadcn@latest add @haze/range-slider`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "http://localhost:4329/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import RangeSlider from "@/components/haze/ui/range-slider.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: http://localhost:4329/ui/range-slider.md
Customize with real content
Adapt the Haze range-slider ui for our product. Replace the demo values of these props with our real content: id, label, lowerLabel, upperLabel, lowerName, upperName, lower, upper, min, max, step, unit, disabled, class. Keep the layout, the heading level rules and these accessibility details: The endpoints remain independent native ranges in the keyboard tab order. With scripting, the currently edited endpoint stops at the other endpoint. Validate the interval on submission. Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using range-slider, check it fits: use it when a filter or threshold setting needs a lower and an upper numeric bound; each end needs its own keyboard target and form value. Avoid it when only a single numeric value matters. use slider.; exact values are better typed. use two number inputs.. Alternatives: @haze/slider, @haze/quantity-stepper.
Dependencies
- @haze/slider
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.