Lens range
Choose a numeric band on two native sliders with a magnified tick scale and named edge presets.
pnpm dlx shadcn@latest add @haze/lens-range---
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>When to use
- 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.
When not to use
- Use range-slider when the extra tick scale adds no useful context.
- Use range-histogram-slider when the scale must show the data distribution.
Installation
Add the Haze registry to components.json once, then install Lens range with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/lens-rangenpx shadcn@latest add @haze/lens-rangeyarn dlx shadcn@latest add @haze/lens-rangebunx --bun shadcn@latest add @haze/lens-rangeFirst 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 | Experience band | Legend naming the selected band. |
| min | number | 0 | Lower limit represented on the tick scale. |
| max | number | 20 | Upper limit represented on the tick scale. |
| step | number | 1 | Native increment for both edges. |
| 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 | years | Unit added to both accessible values. |
| presets | { value: number; label: string }[] | Associate / Senior / Principal | Named values applied to the most recently focused edge. |
| disabled | boolean | false | Prevents user changes through the native disabled state. |
| class | string | – | Additional classes on this component's outer wrapper. |
Keyboard interactions
- Tab
- Focus either bound or one of the named preset buttons.
- EnterSpace
- Apply the focused preset to the last focused edge.
- Arrow keys
- Adjust a range edge one step at a time.
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.
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 "Lens range" with `npx shadcn@latest add @haze/lens-range`. 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 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: http://localhost:4329/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.
Dependencies
- @haze/range-slider
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.