# Histogram range slider

> Filter a numeric dataset with two native range inputs and a histogram that highlights the selected interval.

- Kind: ui · Category: Forms · Interactivity: script
- Install: `npx shadcn@latest add @haze/range-histogram-slider`
- File: `src/components/haze/ui/range-histogram-slider.astro`
- npm dependencies: none
- Registry dependencies: @haze/range-slider, @haze/utils
- Docs page: https://hazeui.com/ui/range-histogram-slider

## Use 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 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
- [range-slider](https://hazeui.com/ui/range-slider.md)
- [window-range](https://hazeui.com/ui/window-range.md)
- [slider](https://hazeui.com/ui/slider.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| id | `string` |  | yes | Unique caller supplied prefix for the control and its associated content. |
| label | `string` | Nightly budget | no | Legend describing this numeric filter. |
| data | `number[]` | Demo nightly budgets | no | Finite numeric observations to bin; values outside the domain are excluded. |
| bins | `number` | 16 | no | Histogram resolution, limited to 4–40 bins. |
| min | `number` | 0 | no | Smallest filterable observation. |
| max | `number` | 200 | no | Largest filterable observation. |
| step | `number` | 5 | no | Increment of both native range controls. |
| lower | `number` |  | no | Initial lower edge; clamped into the domain. |
| upper | `number` |  | no | Initial upper edge; never starts below the lower edge. |
| lowerName | `string` | id-lower | no | Form name for the minimum input. |
| upperName | `string` | id-upper | no | Form name for the maximum input. |
| unit | `string` | credits | no | Unit spoken by each bound and shown in its value. |
| disabled | `boolean` | false | no | Prevents user changes through the native disabled state. |
| class | `string` |  | no | Additional classes on this component's outer wrapper. |

## 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.
- Key Tab: Move between the minimum and maximum inputs.
- Key Arrow keys / Home / End: Adjust the focused range bound using native behavior.

## Agent prompts

### 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": "https://hazeui.com/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: https://hazeui.com/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.

## Example requests that should lead here
- Add a nightly budget filter with a histogram of listing prices
- Highlight matching durations above a two bound native range

## Source

```astro
---
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>
```
