How it works
A widget card for "where did it go" questions. Each bar is a rounded fill translated inside a rounded clip, so its ends stay round while it grows (a scaled bar would squash its corners). The tooltip is placed with container query units, so it rides the bar end with the same easing and needs no measuring; on entry it rides the longest bar while that bar grows. The axis picks at most four tidy segments and dashed gridlines sit behind the bars.
Use it for
- The top few categories of one total: spending by category, visits by page, sales by product.
- A card where one row deserves a callout, such as the biggest expense.
- Switching the same ranking between periods without leaving the card.
Look elsewhere when
- More than five or six rows: use a table or a full bar chart.
- Change over time: use a line or column chart.
- Parts that must add up to 100%: use a stacked distribution bar.
Usage
import { RankBars } from './RankBars.jsx'
const usd = (n) => '$' + n.toLocaleString('en-US', { minimumFractionDigits: 2 })
export function ExpensesCard() {
return (
<RankBars
title="Major expenses"
onOpen={() => {}}
periods={[
{ value: 'week', label: 'Weekly' },
{ value: 'month', label: 'Monthly' },
]}
value={1861.7}
delta={{ value: 4.2, format: (n) => n + '%', positive: 'down' }}
data={[
{ name: 'Rent', value: 1099.55 },
{ name: 'Groceries', value: 412.3 },
{ name: 'Transport', value: 156.2 },
{ name: 'Dining', value: 128.75 },
]}
formatValue={usd}
color={7}
/>
)
}Props
| Prop | Type | Default | What it does |
|---|---|---|---|
data | { name: string, value: number, color?: number | string }[] | — | Rows. Negative or missing values count as 0. color overrides the card colour for one row (1–8 or a CSS colour). |
limit | number | 5 | How many rows to show after sorting. Three to five keeps the card readable. |
sorted | boolean | true | Sorts from largest to smallest. false keeps the given order, for a fixed sequence such as weekdays. |
max | number | — | Value at the right edge. By default the axis rounds the largest value up to a tidy tick. |
title / subtitle / icon | ReactNode | — | Card header: a tinted icon tile, a 15px title and a quieter subtitle after it. |
onOpen / action | () => void / ReactNode | — | onOpen adds a chevron button at the end of the header. action replaces it with any node. |
periods / period / defaultPeriod / onPeriodChange | { value: string, label: string }[] / string / string / (value) => void | — | A small native select in the header, such as Weekly or Monthly. Pass period to control it; swap data in onPeriodChange and the bars slide to their new lengths. |
value / unit / caption | number | string / string / string | — | Optional hero above the bars, such as the total. Numbers count up. Without value there is no hero. |
delta | { value: number, label?: string, positive?: 'up' | 'down', format?: (n) => string } | — | Change badge after the hero. positive: 'down' makes a fall green, for spending. |
highlight | number | — | Row index that carries the tooltip at rest. Defaults to the longest bar. |
color | number | string | 1 | Bar colour: 1–8 for --ds-chart-N (soft at rest, solid when active), or a CSS colour. |
formatValue | (n: number) => string | (n) => n.toLocaleString('en-US') | Format for the tooltip, the hero, the delta and the screen-reader table. |
formatTick | (n: number) => string | 500 / 1.5K / 2M | Axis tick format. The axis has at most four segments. |
label | string | — | Accessible name of the bars and caption of the hidden table. Defaults to the title. |
labels | object | — | Replaces built-in text: chart, period, open, group(name, count), row(rank, name, value, share), colName, colValue, colShare. |
replayKey | string | number | — | Change it to play the entrance again. Changing data alone slides the bars instead. |
className / style / ...rest | HTMLAttributes | — | Passed to the card root, which fills its container width. |
Accessibility
- The bars form one focusable group named after the chart. Up and Down (or Left and Right) move the tooltip between rows, Home and End jump to the ends, Escape returns it to the highlighted row.
- Each move is announced through a polite live region with rank, value and share of the total.
- A visually hidden table lists every row with its value and share; the drawing is hidden from screen readers.
- The period selector is a native select with an accessible name.
- Reduced motion shows the bars at full length at once and the numbers do not count.
Motion
Entrance waits until 30% of the card is visible. Bars grow from the left over 900ms on cubic-bezier(0.22, 1, 0.36, 1), 70ms apart; the ink tooltip rides the end of the longest bar while its number counts; names and the axis fade in. Hover moves the tooltip in 900ms on the same curve with a faster count; switching period slides every bar to its new length.
Add it to your project
Pro members copy a prompt and their coding agent installs the source, the same way as the free components.
RankBars.jsxrank-bars.csstokens.cssREADME.md
Both files install together. The stylesheet reads the design tokens in tokens.css and carries fallbacks, so it also works alone.



