# Range slider

> Two native sliders for the ends of an interval, with an overview track and labelled numeric readouts.

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

## Use 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 when
- Only a single numeric value matters. Use slider.
- Exact values are better typed. Use two number inputs.

## Alternatives
- [slider](https://hazeui.com/ui/slider.md)
- [quantity-stepper](https://hazeui.com/ui/quantity-stepper.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| id | `string` |  | yes | Unique prefix for the component and its labelled controls. |
| label | `string` | "Budget range" | no | Legend for the interval. |
| lowerLabel | `string` | "Minimum" | no | Label for the lower endpoint. |
| upperLabel | `string` | "Maximum" | no | Label for the upper endpoint. |
| lowerName | `string` | id + -lower | no | Name of the submitted lower endpoint. |
| upperName | `string` | id + -upper | no | Name of the submitted upper endpoint. |
| lower | `number` | 25% of the available span | no | Initial lower endpoint. |
| upper | `number` | 75% of the available span | no | Initial upper endpoint. |
| min | `number` | 0 | no | Smallest value on the scale. |
| max | `number` | 100 | no | Largest value on the scale. |
| step | `number` | 1 | no | Positive increment for both endpoints. |
| unit | `string` | "" | no | Display and spoken unit. |
| disabled | `boolean` |  | no | Disables both native sliders. |
| class | `string` |  | no | Additional classes on the fieldset. |

## 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.
- Key Tab: Move between endpoints.
- Key Arrows, Home or End: Set the focused endpoint on the available scale.

## Agent prompts

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

## Example requests that should lead here
- Let users set a minimum and maximum capacity in gigabytes using two labelled range controls.

## Source

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