Stat card

Slant / Data

A headline number with a label, a delta and an optional sparkline.

Demo

Stat cards

Usage

@tuttn/slant
import { List, StatCard } from '@tuttn/slant'

<StatCard label="Checkpoints saved" value="1,284" delta={12.4} series={[140, 162, 155, 190, 176, 214, 247]} />

// Lower is better: a negative delta reads as good.
<StatCard label="Avg. handoff" value="42s" delta={-18} lowerIsBetter series={[58, 55, 51, 49, 47, 44, 42]} />

// format writes the badge. The default is a signed percent: +12.4%, −18%.
<StatCard label="Success rate" value="97.8%" delta={1.2} format={delta => `${delta < 0 ? '−' : '+'}${Math.abs(delta)} pts`} />

// Progress toward a target: a bar under the figure, captioned "66% of 40 h".
<StatCard label="Focus hours" value="26.5 h" progress={{ value: 26.5, target: 40, format: n => `${n} h` }} />

// At or over the target the bar is full, in the success colour; a caption of your own.
<StatCard label="Reviews done" value="52" progress={{ value: 52, target: 50, caption: 'Weekly goal met' }} />

// Just the number: leave out delta, series and progress.
<StatCard label="Open tasks" value="17" />

// Inside a List, render each card as an li.
<List>{stats.map(stat => <StatCard key={stat.label} as="li" {...stat} />)}</List>

Props

PropTypeDefaultDescription
labelReactNode—What the number measures.
valueReactNode—The headline figure, set in monospace.
deltanumber—Change since the previous period, in percent, shown as a success or danger badge. Positive is good unless `lowerIsBetter`.
lowerIsBetterbooleanfalseTreats a negative delta as good, e.g. for a response time.
seriesnumber[]—Values for the decorative sparkline beside the figure.
format(delta: number) => stringformatDeltaFormats the delta badge. The default writes a signed percent: +12.4%, −18%.
progress{ value: number; target: number; caption?: ReactNode; format?: (n: number) => string }—Progress toward a target, drawn as a slim bar under the figure: a meter named by the card's label, full (in the success colour) once `value` reaches `target`. The caption under it defaults to "66% of 40" (the percent rounded, the target written by `format`, which defaults to `toLocaleString()`); pass `caption` to replace it, or `false` for none.
as'li' | 'div''div'The root element; use `li` inside a List.
…restHTMLAttributes<HTMLElement>—className, style, other attributes and the ref go on the root, a div or (with as="li") an li.

Accessibility

  • The label and the figure are paragraphs and the delta is text, so a screen reader reads them in that order: "Avg. handoff, 42s, −18%".
  • The badge's tone (success or danger) says whether the change is good, but colour alone doesn't reach everyone. The sign is in the text; if good or bad matters, say it in the label or beside the card.
  • The card's sparkline is decorative (aria-hidden): the figure and the delta carry the data. For a trend people should hear, use a standalone Sparkline with a label.
  • The progress bar is a role="meter" named by the card's label and described by its caption: "Focus hours", then "66% of 40 h". Its aria-valuemin is 0, aria-valuemax the target and aria-valuenow the value, stopped at the target, so 52 of 50 reports 50 while the default caption says 104%.
  • The bar turns to the success colour when the target is met, and the default caption says it in text (104%); a caption of your own should too, as "Weekly goal met" does. With caption: false the meter has no description, so put the target in the label.
  • Inside a List, pass as="li" so each card is a real list item.

StatCard is read-only: it shows what you pass and keeps no state. To switch periods, swap the props; a new series replays the sparkline's draw-in, as the Stat cards piece's range toggle does.

Targets

progress takes { value, target, format?, caption? } and draws a slim bar under the figure, full once the value reaches the target. The caption defaults to the root's messages.ofTarget, "66% of 40 h": the percent rounded and not capped, so 52 of 50 reads "104% of 50", and the target written by progress.format (toLocaleString() by default). Pass progress.caption to write your own, or false for none. A new value eases the bar across; under reduced motion it moves at once.

For a figure on its own, without a card's rule, delta or sparkline, use a Stat; for a reading on a scale, a RangeMeter.

Full source