Histogram range slider
Filter a numeric dataset with two native range inputs and a histogram that highlights the selected interval.
pnpm dlx shadcn@latest add @haze/range-histogram-slider---
import { cn } from "../../../lib/haze/utils";
import RangeSlider from "./range-slider.astro";
interface Props {
id: string;
label?: string;
data?: number[];
bins?: number;
min?: number;
max?: number;
lower?: number;
upper?: number;
lowerName?: string;
upperName?: string;
unit?: string;
step?: number;
disabled?: boolean;
class?: string;
}
const { id, label = "Nightly budget", data = [35, 42, 48, 52, 60, 65, 68, 70, 72, 75, 78, 80, 85, 90, 96, 105, 110, 118, 125, 145, 170, 190], bins = 16, min = 0, max = 200, lower = 50, upper = 140, lowerName, upperName, unit = "credits", step = 5, disabled, class: className } = Astro.props;
const floor = Number.isFinite(min) ? min : 0;
const ceiling = Number.isFinite(max) && max > floor ? max : floor + 100;
const count = Math.max(4, Math.min(40, Math.round(Number.isFinite(bins) ? bins : 16)));
const values = data.filter((value) => Number.isFinite(value) && value >= floor && value <= ceiling);
const buckets = Array.from({ length: count }, (_, index) => ({ start: floor + index / count * (ceiling - floor), end: floor + (index + 1) / count * (ceiling - floor), count: 0 }));
values.forEach((value) => buckets[Math.min(count - 1, Math.floor((value - floor) / (ceiling - floor) * count))].count++);
const peak = Math.max(1, ...buckets.map((bucket) => bucket.count));
---
<div data-haze-range-histogram-slider data-values={JSON.stringify(values)} class={cn("grid w-full min-w-0 gap-3", className)}>
<svg viewBox={`0 0 ${count * 12} 64`} preserveAspectRatio="none" aria-hidden="true" class="h-20 w-full overflow-visible">
{buckets.map((bucket, index) => <rect data-bin data-start={bucket.start} data-end={bucket.end} x={index * 12 + 1} y={64 - Math.max(2, bucket.count / peak * 60)} width="10" height={Math.max(2, bucket.count / peak * 60)} rx="2" fill="currentColor" class={cn("transition-colors duration-150 ease-snappy", bucket.end >= lower && bucket.start <= upper ? "text-brand" : "text-muted-foreground/25")} />)}
</svg>
<RangeSlider id={id} label={label} lower={lower} upper={upper} lowerName={lowerName} upperName={upperName} min={floor} max={ceiling} step={step} unit={unit} disabled={disabled} />
<p data-matches role="status" class="text-[0.8125rem]/5 text-muted-foreground">{values.filter((value) => value >= lower && value <= upper).length} of {values.length} options in this range.</p>
</div>
<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 result = root.querySelector<HTMLElement>("[data-matches]");
if (!lower || !upper || !result) return;
const values = JSON.parse(root.dataset.values || "[]") as number[];
const render = (announce: boolean) => {
const start = lower.valueAsNumber;
const end = upper.valueAsNumber;
root.querySelectorAll<SVGRectElement>("[data-bin]").forEach((bar) => {
const selected = Number(bar.dataset.end) >= start && Number(bar.dataset.start) <= end;
bar.classList.toggle("text-brand", selected);
bar.classList.toggle("text-muted-foreground/25", !selected);
});
const text = `${values.filter((value) => value >= start && value <= end).length} of ${values.length} options in this range.`;
if (announce) result.textContent = text;
else result.setAttribute("aria-label", text);
};
root.addEventListener("input", () => render(false));
root.addEventListener("change", () => { result.removeAttribute("aria-label"); render(true); });
lower.form?.addEventListener("reset", () => queueMicrotask(() => render(true)));
root.setAttribute("data-enhanced", "");
render(true);
}
const init = () => document.querySelectorAll<HTMLElement>("[data-haze-range-histogram-slider]").forEach(enhance);
init();
if (!document.documentElement.hasAttribute("data-haze-range-histogram-slider-listener")) {
document.documentElement.setAttribute("data-haze-range-histogram-slider-listener", "");
document.addEventListener("astro:page-load", init);
}
</script>When to use
- People need to see how results are distributed before choosing a price or size band.
- A filter should report how many supplied values fall inside the current interval.
When not to use
- Use range-slider when there is no underlying distribution to show.
- Use window-range when the selection moves across an ordered time series.
Installation
Add the Haze registry to components.json once, then install Histogram range slider with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/range-histogram-slidernpx shadcn@latest add @haze/range-histogram-slideryarn dlx shadcn@latest add @haze/range-histogram-sliderbunx --bun shadcn@latest add @haze/range-histogram-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 caller supplied prefix for the control and its associated content. |
| label | string | Nightly budget | Legend describing this numeric filter. |
| data | number[] | Demo nightly budgets | Finite numeric observations to bin; values outside the domain are excluded. |
| bins | number | 16 | Histogram resolution, limited to 4–40 bins. |
| min | number | 0 | Smallest filterable observation. |
| max | number | 200 | Largest filterable observation. |
| step | number | 5 | Increment of both native range controls. |
| lower | number | – | Initial lower edge; clamped into the domain. |
| upper | number | – | Initial upper edge; never starts below the lower edge. |
| lowerName | string | id-lower | Form name for the minimum input. |
| upperName | string | id-upper | Form name for the maximum input. |
| unit | string | credits | Unit spoken by each bound and shown in its value. |
| disabled | boolean | false | Prevents user changes through the native disabled state. |
| class | string | – | Additional classes on this component's outer wrapper. |
Keyboard interactions
- Tab
- Move between the minimum and maximum inputs.
- Arrow keysHomeEnd
- Adjust the focused range bound using native behavior.
Accessibility
- The decorative histogram stays outside the accessibility tree; separate Minimum and Maximum inputs expose the selected values.
- The result count is announced when a change commits, rather than flooding the live region on every drag frame.
- RangeSlider prevents the enhanced bounds from crossing; without script both named native inputs remain usable.
- Bars use the supplied observations and selected interval, so empty datasets render an empty distribution with zero matches.
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 "Histogram range slider" with `npx shadcn@latest add @haze/range-histogram-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 RangeHistogramSlider from "@/components/haze/ui/range-histogram-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-histogram-slider.md
Customize with real content
Adapt the Haze range-histogram-slider ui for our product. Replace the demo values of these props with our real content: id, label, data, bins, min, max, step, lower, upper, lowerName, upperName, unit, disabled, class. Keep the layout, the heading level rules and these accessibility details: The decorative histogram stays outside the accessibility tree; separate Minimum and Maximum inputs expose the selected values. The result count is announced when a change commits, rather than flooding the live region on every drag frame. RangeSlider prevents the enhanced bounds from crossing; without script both named native inputs remain usable. Bars use the supplied observations and selected interval, so empty datasets render an empty distribution with zero matches. Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using range-histogram-slider, check it fits: use it when people need to see how results are distributed before choosing a price or size band.; a filter should report how many supplied values fall inside the current interval.. Avoid it when use range-slider when there is no underlying distribution to show.; use window-range when the selection moves across an ordered time series.. Alternatives: @haze/range-slider, @haze/window-range, @haze/slider.
Dependencies
- @haze/range-slider
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.