Cómo funciona
A complete widget card: title row, hero number, bars and labels. Bars sit in a CSS grid and every height is a percentage, so the server render is already correct. Each bar is a clipped column whose fill slides up with transform, so round ends stay round and a data change moves a bar from its old height to the new one. Resting bars use the soft colour; the selected or hovered one turns solid with a light gradient, its label becomes an ink pill and the hero counts to its value.
Úsalo para
- A weekly or monthly total where the latest period is the story and earlier ones give context.
- Daily values inside weeks, drawn as a bucket of thin bars per week.
- Days measured against a budget or goal, with a threshold line that makes the days over it stand out.
- A compact revenue or progress card with a few columns beside the number.
Busca otra opción cuando
- Long series (over about 30 points) or trends: use a line or area chart.
- Several series compared side by side: one card has one colour; use one card per series or a stacked bar chart.
- Rankings with long names: use ranked horizontal bars.
Uso
import { BarChart } from './BarChart.jsx'
export function CyclingCard() {
return (
<BarChart
title="Cycling"
qualifier="Last 8 weeks"
tone={4}
caption={(label, i) => (i === 7 ? 'This week' : `Week ${label}`)}
unit="km"
categories={['1', '2', '3', '4', '5', '6', '7', '8']}
data={[42, 18, 64, 82, 36, 33, 96, 86]}
average
onAction={() => {}}
/>
)
}Propiedades
| Propiedad | Tipo | Por defecto | Qué hace |
|---|---|---|---|
title | string | — | Card title, 15px semibold. |
qualifier | string | — | Grey words after the title, such as “Last 8 weeks”. |
icon | ReactNode | — | An 18px icon shown in a 32px square tinted with the card colour. |
onAction | () => void | — | Shows a round button at the right of the title row and calls this on press. |
actionIcon | 'open' | 'more' | 'open' | The button glyph: a chevron (open details) or three dots (more options). |
actionLabel | string | — | Accessible name of the button; defaults to text.open or text.more. |
action | ReactNode | — | Your own element in the title row instead of the built-in button. |
caption | string | (label, index) => string | — | Line above the hero number. A function receives the current label and index, so it can follow the selection (“Week 5”). |
value | number | — | Fixed hero number. Without it the hero shows the current bar (the mean of a bucket of thin bars) and counts to the hovered one. |
unit | string | — | Unit beside the hero number, 15px grey. |
chip | { label: string, value: string } | — | A small chip at the right of the hero such as “OVER · 7 days”; only the value takes the card colour. |
note | ReactNode | — | A sentence under the hero; <b> inside it takes the card colour. |
categories | string[] | — | Axis categories, one per bar or bucket. Without them there is no axis row. `labels` is the older name and still works (deprecated: in blocks `labels` means text overrides). |
data | number[] | number[][] | — | Numbers draw one column per label; arrays draw a bucket of thin bars per label (the days of a week). Non-numbers count as 0. |
selected | number | — | Index of the selected bar, solid with an ink pill label. Defaults to the last one; when set, the chart is controlled. |
onSelect | (index) => void | — | Called when a bar is clicked, tapped or chosen with the arrow keys. |
average | boolean | number | false | true draws a line at the mean of the bars, a number draws it there. Its label counts up with the bars. |
threshold | number | — | Draws a line in the card colour; bars at or above it are solid, the rest neutral. Bars default to 4px wide. |
min / max | number | 0 / automatic | Value at the baseline and at the top of the plot. Max defaults to 5% above the tallest bar or line. |
layout | 'stacked' | 'side' | 'stacked' | stacked puts the hero above the bars; side puts it on the left with a few columns on the right. |
tone | 1-8 | 1 | Which --ds-chart-N colour the card uses; resting bars take its -soft partner. |
height | number | 120 (side: 150) | Plot height in px, without the axis labels. |
barWidth | number | — | Column width in px (at most 58% of its slot). Default 40, or 4 with a threshold. |
format | 'number' | 'percent' | 'currency' | 'duration' | (n) => string | 'number' | How values read: a number with the data's decimals (one-decimal data keeps one decimal, also in the average), a percent (42 reads 42%), money in `currency`, a duration in seconds (4m 30s), or your function. |
locale / currency | string / string | 'en-US' / 'USD' | Language of the number format and the ISO code used by format='currency'. |
formatValue | (n) => string | — | Your own formatter, ahead of `format`. While counting it receives in-between values rounded to the target's decimals. |
text | object | — | Replaces built-in words (average, limit, open, more, chart, describe, dataTitle, period, value); shallow-merged over the English defaults. |
label | string | — | Accessible name of the plot and caption of the hidden table; defaults to the title. |
replayKey | any | — | Change it to play the entrance again from zero. |
...rest | HTMLAttributes<HTMLDivElement> | — | Other attributes, such as className and style, go to the card. |
Accesibilidad
- The plot is a focusable group; Left and Right move the selection, Home and End jump to the ends, and each move is announced through a live region.
- A visually hidden table lists every label and value, so the tooltip is never the only way to read the data.
- The selected bar is marked by its ink pill label and the hero number, not by colour alone.
- Bars have no hover-only content that a keyboard cannot reach: keyboard focus shows the same tooltip.
- With reduced motion the card appears in its final state with no rise and no counting.
Movimiento
When 30% of the card is on screen, bars rise from the baseline over 900ms on cubic-bezier(0.22, 1, 0.36, 1), 12-30ms apart; the average line draws left to right and its label and the hero count up on the same curve. A data change moves bars from old to new heights. Hover swaps the solid bar in 160ms, the tooltip follows in 200ms and the axis pill springs in over 240ms.
Añádelo a tu proyecto
Copia la instrucción en Codex o Claude Code. El agente lee el código publicado, te muestra lo que va a escribir y añade el componente junto a los tuyos.
BarChart.jsxbar-chart.csstokens.cssREADME.md
Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.

