仕組み
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.
向いている場面
- Dashboards and overviews that show a few headline metrics side by side.
- Reports where the direction of change matters as much as the number.
向かない場面
- 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.
使い方
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" />プロパティ
| プロパティ | 型 | 既定値 | 説明 |
|---|---|---|---|
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. |
アクセシビリティ
- 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.
モーション
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.
プロジェクトに追加
プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。
KpiCard.jsxkpi-card.csstokens.cssREADME.md
2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。





