# Share bar

> Parts of a whole as one bar that comes apart into a ranked list: each segment drops out and slides into its own row, keeping its width.

A breakdown card usually picks one picture: a stacked bar that is compact but hard to read, or a list of bars that is clear but tall. Share bar is both, and the same picture. Closed, it is one bar with a legend, each part a run of upright 2px hairlines in its colour. Press it and the stack comes apart. Each segment drops out of the bar and slides left into a row of its own, largest first, on a curve, the next one a beat behind. Each keeps exactly its width, so a row's bar is the same piece you saw in the stack, laid on a faint track of the same hairlines that stands for 100%. Each row's name and figure arrive once its bar has landed, and the card grows to fit as they go. Close it and the last row goes back first, each segment sliding up and right into its place. Point at any part and the rest dim, while the total rolls to that part's figure and the line under it morphs to its name and share. One even-paced move drives it, so pressing again mid-way turns it round from where it is.

## Install

```bash
npx shadcn@latest add https://hairlineui.com/r/share-bar.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/share-bar
- Registry item (source): https://hairlineui.com/r/share-bar.json

## Usage

```tsx
import { ShareBar } from "@/components/share-bar";

<ShareBar
  title="Visitors"
  caption="By source, last 30 days"
  items={[
    { key: "search", label: "Organic search", value: 18420 },
    { key: "direct", label: "Direct", value: 12960 },
    { key: "social", label: "Social", value: 7310 },
  ]}
/>
```

## From your analytics

Group sessions by source on the server; the bar sorts and colours them for you.

```tsx
import { ShareBar } from "@/components/share-bar";
import { analytics } from "@/lib/analytics";

export async function SourcesCard() {
  const sources = await analytics.sessionsBySource({ days: 30 }); // [{ source: "google", name: "Organic search", sessions: 18420 }, …]
  return (
    <ShareBar
      title="Visitors"
      caption="By source, last 30 days"
      items={sources.map((s) => ({ key: s.source, label: s.name, value: s.sessions }))}
    />
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `items` | `ShareBarItem[]` | – | Each with key, label, value and an optional colour. Sorted largest first for you. |
| `expanded / defaultExpanded` | `boolean` | `false` | Whether the list is open; controlled or not. |
| `onExpandedChange` | `(expanded: boolean) => void` | – | Called when it opens or closes. |
| `title` | `string` | `"Traffic"` | Above the total. |
| `caption` | `string` | `"Total"` | Under the total when nothing is pointed at. |
| `format / locales` | `Intl.NumberFormatOptions / string` | – | How the figures read; shares are percentages. |

## States

- **arrive**: In view for the first time, the bar is revealed from the left (900ms) and the total rolls up.
- **point**: The other parts dim to a third (220ms); the total rolls to the part's figure (600ms) and the caption morphs to its name and share.
- **open**: One even-paced move (1.1s) takes the stack apart, largest first, each segment a beat after the last: it drops, then slides left into its row on a curve, keeping its width. Its track, name and figure arrive as it lands; the card grows as the rows come, and the legend lifts away.
- **close**: The same move backwards: the last row goes first, each segment sliding up and right into its place in the stack; the legend comes back.
- **reduced motion**: No reveal or travel: the bar and the list swap in place.

## What it handles

A stacked bar is compact but hard to read, and a list of bars is clear but tall. Cards usually pick one, or switch between two pictures that share nothing.

- **One picture, two sizes**: The bar opens into a ranked list and each segment keeps its width, so nothing has to be read twice.
- **Ranked for you**: Parts are sorted largest first, in the bar and in the list.
- **Point to read**: The other parts dim; the total rolls to the part's figure and its name and share morph in.
- **Colours from your theme**: Your chart colours in turn, or any colour per part.
- **Turn round any time**: One even-paced move drives it all, so a second press mid-way reverses it from where it is.
- **Accessible by default**: Breakdown is a disclosure button for a real list; every part is in a hidden table with its share.

## Replaces

a stacked bar chart, a bar list, a donut chart, a legend, an expand toggle

## Where it fits

- **Traffic sources**: Where visitors came from, as one bar on an overview and a list when you want detail.
- **Spend by category**: Where the money went this month in a finance or expenses app.
- **Storage and usage**: What is using the disk, the plan's seats or the API quota.
- **Survey and poll results**: How answers split, compact in a feed and ranked when opened.

## Notes

- Breakdown is a disclosure button (aria-expanded) that controls the list; pressing the bar does the same.
- Every part is in a visually hidden table with its value and share, and the total in its caption.
- Parts take --chart-1 to --chart-5 in turn; the legend and rows always carry the name too, so colour is never the only label.
- One even-paced progress drives the move and each segment eases its own part of it, so it never front-loads; segments move by transform only, and the card's height follows the same progress.
- Installs Number roll and Text morph. With reduced motion the bar and the list swap in place.

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