# Range ruler

> Two hairline needles across a graduated scale. Thumbs glide without layout thrash and repel each other on contact.

A dual-needle control for intervals and thresholds. Hairline ticks mark intervals along the axis, needles glide with zero pointer latency, and metric pills roll to settled values. When the lower thumb reaches the upper one, it pushes it forward with physical repulsion rather than clipping. Within 48px, badges merge into a single span readout.

## Install

```bash
npx shadcn@latest add @hairline/range-ruler
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: none
- Hairline UI components it needs (the CLI installs them too): `number-roll`
- Docs and live preview: https://hairlineui.com/ui/range-ruler
- Registry item (source): https://hairlineui.com/r/range-ruler.json

## Usage

```tsx
import { RangeRuler } from "@/components/range-ruler";

<RangeRuler
  label="Price interval"
  min={0}
  max={500}
  step={5}
  unit="$"
  defaultValue={[50, 250]}
  onValueCommit={setRange}
/>
```

## Filter intervals in an interactive catalog

Keep expensive database or API queries deferred until the gesture completes. The ruler updates its visual needles and text immediately during drag, calling onValueCommit when the user lets go.

```tsx
"use client";
import { useState } from "react";
import { RangeRuler } from "@/components/range-ruler";

export function CatalogFilter() {
  const [range, setRange] = useState<[number, number]>([25, 150]);

  return (
    <div className="space-y-4 rounded-xl border border-border p-4">
      <RangeRuler
        label="Target budget"
        value={range}
        min={0}
        max={300}
        step={5}
        unit="USD"
        onValueCommit={(next) => {
          setRange(next);
          filterProducts(next);
        }}
      />
    </div>
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `label` | `string` | – | Accessible label displayed above the ruler. |
| `value / defaultValue` | `[number, number]` | `[20, 80]` | Controlled or initial interval bounds. |
| `min / max / step` | `number` | `0 / 100 / 1` | Lower and upper boundaries and step increment. |
| `minDistance` | `number` | `0` | Minimum allowed distance between the two needles. |
| `unit` | `string` | – | Optional unit suffix for badges and readouts (e.g. $, ms, MB). |
| `onValueChange` | `(value: [number, number]) => void` | – | Fires during dragging for lightweight live previews. |
| `onValueCommit` | `(value: [number, number]) => void` | – | Fires on pointer release or keyboard adjustment. |
| `disabled` | `boolean` | `false` | Disables all interactions and cancels any active gesture. |

## States

- **glide**: Needles follow pointer movement directly with zero React state updates or layout recalculations.
- **repulsion**: Dragging a needle into its sibling pushes the sibling forward or backward, keeping the range valid.
- **merge**: When needles come within 48px, two floating metric pills glide together into a single span pill.
- **commit**: Pointer release commits values, and NumberRoll rolls each digit to its final position over 380ms.
- **cancel**: Escape or pointer interruption restores values from before the gesture without triggering onValueCommit.

## What it handles

A range slider needs two thumbs that behave: they should not cross, should not lag, and should show what the range means. This is a ruler with two needles that push each other.

- **Two needles on a scale**: Hairline ticks mark the axis, and the needles follow the pointer without React updates.
- **They push, they don’t clip**: Drag one into the other and it pushes its sibling along. Set minDistance to keep a gap.
- **Readouts that merge**: When the needles come within 48px, their two value pills glide together into one span.
- **Query on release**: onValueChange fires while dragging for live previews, and onValueCommit fires on release or a keyboard change.
- **Escape cancels**: Escape or an interrupted gesture restores the values from before, without committing.
- **Keyboard and screen readers**: Arrow keys step by 1, Shift with arrows or Page Up and Down step by 10, and Home and End jump to the limits.

## Replaces

a dual range slider, thumb collision logic, value badges, drag cancellation, keyboard step handling

## Where it fits

- **Price filters**: A min and max range in a product catalogue or marketplace.
- **Performance thresholds**: Latency or error-rate bounds in monitoring tools.
- **Budgets and quotas**: A lower and upper limit for spend or storage.
- **Search filters**: Size, year or rating ranges in a search sidebar.

## Notes

- Both needles expose native button slider semantics with aria-valuemin, aria-valuemax, and aria-valuenow.
- Arrow keys step by 1 unit; Shift + Arrow or Page Up/Down step by 10 units. Home and End jump to boundary limits.
- Needle transforms and highlight widths are written directly to DOM elements during dragging to prevent React layout thrash.
- Under prefers-reduced-motion, transitions are removed and values settle immediately.

License: MIT (https://hairlineui.com/license#components)
