# Lens range

> Choose a numeric band on two native sliders with a magnified tick scale and named edge presets.

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

## Use when
- A selected band needs context from a fine scale and a few known reference points.
- Named presets should move whichever range edge the person last focused.

## Avoid when
- Use range-slider when the extra tick scale adds no useful context.
- Use range-histogram-slider when the scale must show the data distribution.

## Alternatives
- [range-slider](https://hazeui.com/ui/range-slider.md)
- [range-histogram-slider](https://hazeui.com/ui/range-histogram-slider.md)
- [ruler-slider](https://hazeui.com/ui/ruler-slider.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| id | `string` |  | yes | Unique caller supplied prefix for the control and its associated content. |
| label | `string` | Experience band | no | Legend naming the selected band. |
| min | `number` | 0 | no | Lower limit represented on the tick scale. |
| max | `number` | 20 | no | Upper limit represented on the tick scale. |
| step | `number` | 1 | no | Native increment for both edges. |
| 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` | years | no | Unit added to both accessible values. |
| presets | `{ value: number; label: string }[]` | Associate / Senior / Principal | no | Named values applied to the most recently focused edge. |
| 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 minimum and maximum are labelled native inputs; the tick magnification is a decorative reading aid.
- Each preset is a normal button with a label and numeric value, and its result is clamped to the other edge.
- Focus expands the nearby ticks just as editing does; reduced motion keeps all tick scales at their resting size.
- Before an edge receives focus, presets apply to the minimum; the helper text makes this rule visible.
- Key Tab: Focus either bound or one of the named preset buttons.
- Key Enter / Space: Apply the focused preset to the last focused edge.
- Key Arrow keys: Adjust a range edge one step at a time.

## Agent prompts

### Install and use

Install the Haze UI component "Lens range" with `npx shadcn@latest add @haze/lens-range`. 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 LensRange from "@/components/haze/ui/lens-range.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/lens-range.md

### Customize with real content

Adapt the Haze lens-range ui for our product. Replace the demo values of these props with our real content: id, label, min, max, step, lower, upper, lowerName, upperName, unit, presets, disabled, class. Keep the layout, the heading level rules and these accessibility details: The minimum and maximum are labelled native inputs; the tick magnification is a decorative reading aid. Each preset is a normal button with a label and numeric value, and its result is clamped to the other edge. Focus expands the nearby ticks just as editing does; reduced motion keeps all tick scales at their resting size. Before an edge receives focus, presets apply to the minimum; the helper text makes this rule visible. Do not add raw color classes; use the Haze tokens.

### Choose the right block

Before using lens-range, check it fits: use it when a selected band needs context from a fine scale and a few known reference points.; named presets should move whichever range edge the person last focused.. Avoid it when use range-slider when the extra tick scale adds no useful context.; use range-histogram-slider when the scale must show the data distribution.. Alternatives: @haze/range-slider, @haze/range-histogram-slider, @haze/ruler-slider.

## Example requests that should lead here
- Add an experience band with Associate, Senior and Principal presets
- Use a magnified tick scale to give a selected numeric range more context

## Source

```astro
---
import { cn } from "../../../lib/haze/utils";
import RangeSlider from "./range-slider.astro";

interface Props {
  id: string;
  label?: string;
  min?: number;
  max?: number;
  step?: number;
  lower?: number;
  upper?: number;
  lowerName?: string;
  upperName?: string;
  unit?: string;
  presets?: { value: number; label: string }[];
  disabled?: boolean;
  class?: string;
}
const { id, label = "Experience band", min = 0, max = 20, step = 1, lower = 4, upper = 12, lowerName, upperName, unit = "years", presets = [{ value: 2, label: "Associate" }, { value: 8, label: "Senior" }, { value: 15, label: "Principal" }], disabled, class: className } = Astro.props;
const floor = Number.isFinite(min) ? min : 0;
const ceiling = Number.isFinite(max) && max > floor ? max : floor + 100;
const stops = presets.filter((preset) => Number.isFinite(preset.value) && preset.value >= floor && preset.value <= ceiling);
---
<div data-haze-lens-range class={cn("grid w-full min-w-0 gap-3", className)}>
  <div aria-hidden="true" class="flex h-14 items-center justify-between gap-0.5 overflow-hidden rounded-lg border border-border bg-card px-2">
    {Array.from({ length: 41 }, (_, index) => <span data-lens-tick data-value={floor + index / 40 * (ceiling - floor)} class={cn("w-px origin-center bg-muted-foreground/30 transition-[scale,color] duration-150 ease-snappy", index % 5 === 0 ? "h-5" : "h-3")} />)}
  </div>
  <RangeSlider id={id} label={label} min={floor} max={ceiling} step={step} lower={lower} upper={upper} lowerName={lowerName} upperName={upperName} unit={unit} disabled={disabled} />
  <div class="flex flex-wrap gap-1.5" aria-label="Set the focused range edge to a preset">
    {stops.map((preset) => <button type="button" data-preset={preset.value} disabled class="min-h-8 cursor-pointer rounded-lg border border-frame bg-field px-3 text-[0.8125rem]/5 font-medium text-foreground transition-[background-color,scale] duration-150 ease-snappy hover:bg-accent hover:shadow-key focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring active:scale-[0.97] active:duration-75 disabled:cursor-default disabled:opacity-50" aria-label={`${preset.label}, ${preset.value} ${unit}`}>{preset.label}</button>)}
  </div>
  <p class="text-[0.8125rem]/5 text-muted-foreground">Presets move the most recently focused edge. Start with the minimum.</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]");
    if (!lower || !upper) return;
    const reduced = matchMedia("(prefers-reduced-motion: reduce)");
    const minimum = Number(lower.min), span = Number(lower.max) - minimum;
    let active = lower;
    const render = () => {
      root.querySelectorAll<HTMLElement>("[data-lens-tick]").forEach((tick) => {
        const value = Number(tick.dataset.value);
        const selected = value >= lower.valueAsNumber && value <= upper.valueAsNumber;
        const distance = Math.min(Math.abs(value - lower.valueAsNumber), Math.abs(value - upper.valueAsNumber)) / span;
        const strength = root.contains(document.activeElement) ? 1.1 : 0.5;
        tick.style.scale = `1 ${reduced.matches ? 1 : 1 + Math.max(0, 1 - distance / 0.12) * strength}`;
        tick.classList.toggle("bg-primary", selected);
        tick.classList.toggle("bg-muted-foreground/30", !selected);
      });
      root.querySelectorAll<HTMLButtonElement>("[data-preset]").forEach((button) => { button.disabled = lower.disabled || upper.disabled || !!lower.closest("fieldset:disabled"); });
    };
    [lower, upper].forEach((input) => input.addEventListener("focus", () => { active = input; render(); }));
    root.addEventListener("focusin", render);
    root.addEventListener("focusout", () => queueMicrotask(render));
    root.addEventListener("input", render);
    root.querySelectorAll<HTMLButtonElement>("[data-preset]").forEach((button) => button.addEventListener("click", () => {
      const value = Number(button.dataset.preset);
      active.value = String(active === lower ? Math.min(value, upper.valueAsNumber) : Math.max(value, lower.valueAsNumber));
      active.dispatchEvent(new Event("input", { bubbles: true }));
      active.dispatchEvent(new Event("change", { bubbles: true }));
      active.focus();
    }));
    lower.form?.addEventListener("reset", () => queueMicrotask(render));
    root.setAttribute("data-enhanced", "");
    render();
  }
  const init = () => document.querySelectorAll<HTMLElement>("[data-haze-lens-range]").forEach(enhance);
  init();
  if (!document.documentElement.hasAttribute("data-haze-lens-range-listener")) {
    document.documentElement.setAttribute("data-haze-lens-range-listener", "");
    document.addEventListener("astro:page-load", init);
  }
</script>
```
