Cómo funciona
The hierarchy is fixed: a quiet label with an optional icon, the value set large in tabular figures with its delta badge on the same line, a smooth sparkline, then the period. The value's size follows the card's own width (a container query), so a row of narrow cards never overflows. One tone, derived from the delta and goodDirection, colours the badge and the line together. On first view the number counts up with an ease-out over 900ms, the line is revealed from the left and the end point pops in.
Úsalo para
- Dashboards and overviews that show a few headline metrics side by side.
- Reports where the direction of change matters as much as the number.
Busca otra opción cuando
- Detailed trends people need to read precisely: use a full chart with axes.
- More than six or so metrics in a row: use a table.
Uso
import { KpiCard } from './KpiCard.jsx'
const eur = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'EUR', maximumFractionDigits: 0 })
export function RevenueCard({ revenue, change, daily }) {
return (
<KpiCard
label="Revenue"
value={revenue}
format={(n) => eur.format(n)}
delta={change}
data={daily}
period="vs previous 30 days"
/>
)
}
// Lower is better: a fall shows green
// <KpiCard label="Refund rate" value={2.1} format={(n) => n.toFixed(1) + '%'} delta={-0.6} goodDirection="down" />Propiedades
| Propiedad | Tipo | Por defecto | Qué hace |
|---|---|---|---|
label | ReactNode | — | Metric name. |
value | number | ReactNode | — | A number counts up through format; anything else is shown as is. |
format | (n: number) => string | n => n.toLocaleString() | Formats the value and the sparkline's spoken range. |
delta | number | — | Change; its sign sets the arrow. Omit to hide the badge. |
deltaFormat | (d: number) => string | d => Math.abs(d).toFixed(1) + '%' | Text of the badge (receives the signed delta). |
goodDirection | 'up' | 'down' | 'up' | Which direction is good; colours the badge and line green or red. |
data | number[] | [] | Sparkline points, oldest first. Fewer than two hides it. |
period | ReactNode | — | Comparison period, shown under the line. |
icon | ReactNode | — | Small icon before the label. |
loading | boolean | false | Shows a skeleton with role=status. |
animate | boolean | true | Counts the number up on first view. |
labels | object | — | Replaces spoken text: up, down, flat, loading and trend(first, last). Shallow-merged over English. |
Accesibilidad
- The delta badge carries hidden text (“Up”, “Down”, “No change”) before the number, so the direction is not conveyed by colour or arrow alone.
- The sparkline is role="img" with a label giving its first and last values; replace the wording through labels.trend.
- The loading skeleton has role="status" and the card sets aria-busy while loading.
- Text is at least 12px; badge text mixes the tone with the body colour to keep 4.5:1.
- Reduced motion shows the final number at once and the line without the reveal.
Movimiento
Count-up 900ms with an ease-out quart; the line reveals over 1100ms ease-out after 120ms; the end point pops in with a spring at 1000ms.
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.
KpiCard.jsxkpi-card.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.





