# Compare bars

> This period, and with one press the one before: every bar splits in two, then what was gained turns green and what fell short shows in red.

Comparing with last period usually means a second series drawn over the first, or a page reload with grouped bars that start over from nothing. Here comparing is a press. Each bar is a column of 2px hairlines, faint where it is empty and lit as far as its value. Each bar narrows and steps right while last period's bar slides out from behind it, so you see where each one came from. The split runs out from where you were: the bar you last pointed at, or the end of the chart by the button. Once a pair has split, what changed fills in. The lines of this period over last period turn green, and a shortfall lights the lines up to where last period reached in red. The change chip unfolds beside the total and rolls to its figure. Press again and it plays backwards, and since it is one even-paced move, pressing mid-way turns it round from where it is. A readout follows the pointer with this period, and when comparing, the one before and the change.

## Install

```bash
npx shadcn@latest add https://hairlineui.com/r/compare-bars.json
```

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`, `text-morph`
- Docs and live preview: https://hairlineui.com/ui/compare-bars
- Registry item (source): https://hairlineui.com/r/compare-bars.json

## Usage

```tsx
import { CompareBars } from "@/components/compare-bars";

<CompareBars
  title="Revenue"
  currentLabel="This year"
  previousLabel="Last year"
  data={months} // [{ key: "2026-07", label: "Jul", current: 8120, previous: 11420 }, …]
  format={{ style: "currency", currency: "USD", maximumFractionDigits: 0 }}
/>
```

## From your database

Fetch both periods in one query and line them up by position: the first month of this year against the first of last year.

```tsx
import { CompareBars, type CompareBarsDatum } from "@/components/compare-bars";
import { db } from "@/lib/db";

export async function RevenueCompare() {
  const [now, before] = await Promise.all([db.revenueByMonth({ year: 2026 }), db.revenueByMonth({ year: 2025 })]);
  const data: CompareBarsDatum[] = now.map((m, i) => ({
    key: m.month,
    label: new Date(m.month).toLocaleDateString("en-GB", { month: "short" }),
    title: new Date(m.month).toLocaleDateString("en-GB", { month: "long" }),
    current: m.total,
    previous: before[i]?.total ?? 0,
  }));
  return <CompareBars title="Revenue" currentLabel="This year" previousLabel="Last year" data={data} />;
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `data` | `CompareBarsDatum[]` | – | One pair per bar: key, label, optional title, current and previous. |
| `compared / defaultCompared` | `boolean` | `false` | Whether last period is shown; controlled or not. |
| `onComparedChange` | `(compared: boolean) => void` | – | Called when the chip is pressed. |
| `title` | `string` | `"Revenue"` | Above the total. |
| `currentLabel` | `string` | `"This week"` | Names this period under the total. |
| `previousLabel` | `string` | `"Last week"` | Names last period on the chip and in the readout. |
| `format / locales` | `Intl.NumberFormatOptions / string` | – | How the figures read. |

## States

- **arrive**: In view for the first time, the bars rise out of the baseline one after another (640ms, 30ms apart) and the total rolls up.
- **compare**: One even-paced move (0.95s) runs the split, staggered out from the last bar you pointed at or the chart's end: each bar narrows and steps right while last period's bar slides out from behind to the left. Once a pair has split, the gain over last period fades in green, or the shortfall rises in as a pale red block. The change chip unfolds (460ms) and rolls; the line under the total morphs.
- **point**: A readout glides over the bar you point at (380ms, slight overshoot) and the other pairs dim; when comparing it also shows last period and the change.
- **back**: Pressing again runs the same move backwards: the change drains, then last period slides back behind. Mid-way it turns round from where it is.
- **reduced motion**: No growth, stagger or slide: the split and the colours change in place.

## What it handles

A comparison series usually gets drawn on top or the chart redraws as grouped bars, and the eye loses which bar was which.

- **One press to compare**: Each bar splits into this period and the last, out from where you were, and back again.
- **Gains and shortfalls at a glance**: Green on the part of a bar over last period; a pale red block where it fell short.
- **The total and its change**: The change chip unfolds beside the total, emerald or red with its arrow, and rolls.
- **A readout for each pair**: Point at a bar for this period, last period and the change; on touch, tap or drag.
- **Turn round any time**: The split is one even-paced move, so a second press mid-way reverses it from where it is.
- **Accessible by default**: The chip is a toggle button; arrow keys read each pair aloud; both periods are in a hidden table.

## Replaces

a grouped bar chart, a previous period series, a compare toggle, a change chip, a chart library

## Where it fits

- **Revenue dashboards**: This month or year against the one before, on the same bars.
- **Store and seller analytics**: Sales by day this week against last week.
- **Marketing reports**: Traffic or signups by month against the same months last year.
- **Team and ops metrics**: Tickets closed or deploys per week, compared with the previous period.

## Notes

- The chip is a toggle button (aria-pressed). The plot is one tab stop: arrow keys, Home and End move the readout between pairs and it is announced politely.
- Both periods are in a visually hidden table, with the totals and the change in its caption.
- Green and red only mark a gain or a shortfall, and the chip always carries an arrow that points the same way.
- The split is one even-paced progress and each pair eases its own part of it, so the move never front-loads; bars move by transform and opacity only.
- Installs Number roll and Text morph. With reduced motion everything changes in place.

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