Stat card
A dashboard tile: a metric, its change against the previous period in words and colour, and a sparkline.
Installation
npm i @uisatin/ui @uisatin/proimport '@uisatin/ui/styles.css'
import '@uisatin/pro/styles.css'
import { StatCard } from '@uisatin/pro/stat-card'Part of Satin Pro, the licensed edition: @uisatin/pro on top of @uisatin/ui at the same version, its stylesheet after Free's. Satin Pro isn't on npm yet.
About 5.2 kB of JavaScript, minified and gzipped, on top of the @uisatin/uicomponents it uses (React aside).
First time with Satin? Set it up once, styles and fonts included, with the installation guide.
API Reference
The StatCard component (Satin Pro, @uisatin/pro/stat-card) is the first tile on a dashboard, small enough for a four-up row at 240px: the label with the change's period at the row's end, the value with its change as a small Badge, and a sparkline drawn edge to edge, its line from the card's left edge to its right and its gradient down to the bottom edge. It is not a Line chart: no period Select, no axes. The shell matches the chart cards (bg/card, border/default, radius-2xl, 16px in). change is { value, period, direction, good, format }: value is a percent shown without its sign, since the arrow carries it (↑, ↓ or a dash for "flat"); the Badge is green when the change went the good way ("up" by default), red when not, grey when flat. CAC, refund rate and time to hire pass good: "down". change.format writes another unit ("0.3 pp"). Leave change out (or null) when there is no previous period: no Badge, no period. value is a number written by format (the sparkline's points too), a string shown as given, or null for no data ("—"). A number counts up from 0 once, the first time it shows on the client (250 ms, also when loading ends); a number already in the server's HTML stays as it is through hydration, and countUp={false} and reduced motion show it at once. A long value pushes the Badge onto a line of its own; the value is never cut. The sparkline needs two finite points or more (with fewer its row stays, empty, so tiles keep their height); sparklineLabels titles its tooltip ("Week 3"). Without href the card is a group and the sparkline a tab stop: hover or the arrow keys pick a point, which shows a dot and the value; after the pointer the dot and the tooltip glide from point to point, with the keys they move at once. With href the whole card is one link and the sparkline only a picture. loading shows Skeletons at the loaded card's height (pass sparkline, even [], to keep its row); error (true or the reason) keeps the label and puts the reason, with Retry when onRetry is set, where the value was; disabled fades the content and takes the link and the sparkline out of the tab order. The same tile fits Sales (pipeline value, win rate), HR (time to hire, 28 days, where down is good) and Commerce (today's revenue; refund rate, where up is bad): pick them from Dashboard above. Parts: StatCardChange (the Badge with its words, period read, not shown) and StatCardSparkline (data, labels, format, highlight, interactive, title). Its words (statName, statUp, statDown, statFlat, statGood, statBad, statVerdict, statNoValue, statLoading, statError, statRetry, statTrend, statPointValue…) are translated with SatinLabels; numbers and the change's percent are written in dateLocale.
| Prop | Type | Default |
|---|---|---|
label | ReactNode | — |
value | numberstringnull | — |
format | (value: number) => string | the number in dateLocale |
change | StatChangenull | — |
change.good | "up""down" | "up" |
sparkline | number[] | — |
sparklineLabels | string[] | — |
href | string | — |
loading | boolean | false |
error | ReactNodeboolean | — |
onRetry | () => void | — |
disabled | boolean | false |
countUp | boolean | true |
className | string | — |
ref | Ref<HTMLDivElement> | — |
...props | ComponentProps<'div'>Other attributes go to <div> | — |
Accessibility
Without href the card is a group named in words: "Signups, 1,284, up 12% vs last week" (statName, with statUp, statDown, statFlat), so the change is heard with the value and colour is never the only signal; on screen the arrow carries the direction. Only a metric where down is good (good: "down") adds ", good" or ", bad": for the rest up is good and the words already say it. That is deliberate, so the common case stays short. A null value is read "No data", a loading card "Revenue, Loading" with aria-busy, a failed one by its label and the reason ("Revenue Couldn't load"). The reason sits in a polite status region, so a failure is read out when loading ends; Retry is a button in the tab order, described by the card's label, so a row of Retry buttons can be told apart. The visible label, value and Badge are hidden, since the name already says them; a label that isn't a string, and the reason, are read from where they show, so nothing is said twice. The sparkline's drawing is hidden; a sentence sums it up ("Upward trend over 12 points, from 980 to 1,284", statTrend). Without href the sparkline is one tab stop, a group named "Signups, 12 points" (chartPoints) and described by that sentence; focusing it picks the last point, and a polite live region reads the picked point as "Week 12: 1,284" (statPointValue). With href the card is one link with the same name, described by the sentence; the sparkline has no tab stop or tooltip, and the focus ring is drawn around the card. A disabled card's link has no href and aria-disabled. The count-up is hidden from screen readers, which get the final value at once.
| Key | Action |
|---|---|
| Tab | Without href: focuses the sparkline and picks its last point, then Retry on a failed card. With href: focuses the card's link. |
| ←or→ | On the sparkline: picks the previous or next point; stops at the ends. The drawing runs left to right in right-to-left pages too, so → is always the next point. |
| Enter | On the card's link: opens it. On Retry: runs onRetry. |
Something wrong or missing on this page?
Report a problemSuggest an improvement