Components
Stat
A key figure with a label and an accessible change indicator.
- Revenue
- TZS 1,240,000
- Increase of 12%vs last month
- Failed payments
- 14
- Decrease of 3%vs last month
- Active users
- 2,310
- No change: 0%
Use Stat to show one important number, such as revenue or failed payments, with how it changed.
Installation
pnpm dlx shadcn@latest add https://components.abdulqadirmsingi.com/r/stat.json
Usage
Pass the value already formatted. For money, keep amounts as integer minor units and format them with Intl.NumberFormat using an explicit currency; never do floating-point maths on amounts.
import { Stat } from "@/components/ui/stat";
<Stat
label="Revenue"
value={new Intl.NumberFormat("en-TZ", { style: "currency", currency: "TZS" }).format(1240000)}
change="12%"
trend="up"
changeLabel="vs last month"
/>Props
Stat extends native <dl> props (except children) and spreads them on, including ref (React 19).
| Prop | Type | Default | Description |
|---|---|---|---|
| label | ReactNode | – | What is measured. Required. |
| value | ReactNode | – | The figure, already formatted. Required. |
| change | ReactNode | – | The change, already formatted, e.g. 12%. |
| trend | "up" | "down" | "neutral" | "neutral" | Direction of the change. Whether up is good is up to you: this only sets the arrow and wording. |
| changeLabel | ReactNode | – | Context, e.g. vs last month. |
Accessibility
- It is a description list: the label is the term and the value is its description, read in order.
- The change has an arrow and a hidden word ("Increase of 12%"), so it is never colour alone.
- Colour does not say good or bad. A falling failure count is "down" but good news, so say that in your own text if it matters.
Tokens used
--muted-foreground, --success, --danger.
Known limitations
- Success and danger colours follow the trend direction only, not whether it is good.
- No sparkline or loading state yet.
Source
import { cva, type VariantProps } from "class-variance-authority";import type { ComponentProps, ReactNode } from "react";import { cn } from "@/lib/utils";const changeVariants = cva("inline-flex items-center gap-1 text-sm font-medium", { variants: { trend: { up: "text-success", down: "text-danger", neutral: "text-muted-foreground" }, }, defaultVariants: { trend: "neutral" },});const arrows = { up: "↑", down: "↓", neutral: "→" } as const;const words = { up: "Increase of", down: "Decrease of", neutral: "No change:" } as const;export interface StatProps extends Omit<ComponentProps<"dl">, "children">, VariantProps<typeof changeVariants> { /** What is being measured, e.g. "Revenue". */ label: ReactNode; /** The figure, already formatted. For money use Intl.NumberFormat with an explicit currency. */ value: ReactNode; /** The change, already formatted, e.g. "12%". */ change?: ReactNode; /** Context for the change, e.g. "vs last month". */ changeLabel?: ReactNode;}/** * A single key figure. It is a description list (label, value, change), which screen readers * read in order. The change always has an arrow and a hidden word, never colour alone. */export function Stat({ label, value, change, changeLabel, trend = "neutral", className, ...props}: StatProps) { const t = trend ?? "neutral"; return ( <dl className={cn("flex flex-col gap-1", className)} {...props}> <dt className="text-sm text-muted-foreground">{label}</dt> <dd className="text-3xl font-semibold tracking-tight tabular-nums">{value}</dd> {change ? ( <dd className="flex flex-wrap items-center gap-x-2"> <span className={changeVariants({ trend: t })}> <span aria-hidden="true">{arrows[t]}</span> <span className="sr-only">{words[t]} </span> {change} </span> {changeLabel ? ( <span className="text-sm text-muted-foreground">{changeLabel}</span> ) : null} </dd> ) : null} </dl> );}export { changeVariants };