Metric comparison
One metric for this period against the last: the value, the change as a signed percentage, and both values as thin bars to one scale. Use it where the earlier value matters as much as the change. For a value with a trend line, use the stat card; for the change alone, use the trend badge.
Install
npx shadcn@latest add @thrum-pro/metric-comparisonThis is a Pro component. The command works once your project has a licence key: set up Pro or see pricing.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
label | string | Required | What is measured. Shown as the heading and names the group for screen readers. |
current | number | null | Required | The value for this period. null prints a dash and “No data”. |
previous | number | null | Required | The value it is compared with. null says “No earlier data”; 0 says “Up from zero” or “Down from zero” instead of a percentage. |
currentLabel | string | Default "This period" | Name of this period, shown beside its bar. Long names are cut with an ellipsis and kept in a title. |
previousLabel | string | Default "Last period" | Name of the earlier period. |
invert | boolean | Default false | A fall is the good direction (churn, latency). It changes the colour of the arrow, never its shape. |
format | Intl.NumberFormatOptions | Default compact | How both values are written. The change is always a percentage with one decimal. |
locale | string | Default "en-US" | Locale for number formatting. |
color | string | Default var(--chart-1) | Colour of this period's bar. The earlier period is always drawn in the muted text colour. |
sound | boolean | Default true | Play a soft tone when the value lands after a change, if the visitor has sound on. |
className | string | – | Merged onto the root. It fills the width it is given. |
Motion and sound
Motion
- First draw: both bars grow from zero in 250 ms, the earlier one 40 ms later. The number appears complete.
- A new value rolls from the number on screen and the bars move to their new lengths in 400 ms with no overshoot.
- Reduced motion: the bars and the number appear at their values at once.
Sound
- A soft tone when the value lands after a change. Nothing plays on first draw.
Sound is off until the visitor turns it on. Pass sound={false} to keep this component silent. How sound works
Accessibility
- The root is a group named by the label. Each period is a line of text with its name and value, so a screen reader reads “December $48,210”.
- The change is a trend badge: “Up +12.4%”. Its direction is an arrow and a sign, and colour only adds whether that is good news.
- The bars are hidden from screen readers: both values are in the text. Bars show the size of a value; the sign is in the number.
- Focusing the comparison bars announces the absolute difference against the earlier period and reveals the floating readout.
| Keys | Action |
|---|---|
| Tab | Focus the comparison bars to reveal the exact difference against the earlier period. |
| Esc | Hide the difference readout. |
Source
The source comes with Pro
TypeScript source · Copy-paste ready · MIT-compatible for client projects · Includes all dependencies
Files and dependencies
- Adds
components/ui/metric-comparison.tsx- Packages
motion- Brings along
- Chart foundationsMotion tokensAnimated numberTrend badge
Changelog
- 1.0.0 · 2026-12-03First release.