Stat cards
The row of figures at the top of a dashboard: the number, how it moved since last month and what it means. With trend badges, small charts, progress toward a goal or a period switch.
4 blocks below, each one file and one command. They wear whichever DressCode theme you install, in light and dark.
With trend badges
Four figures for the month in one card split four ways, each with its change on last month as an up or down badge and a line of context.
npx shadcn@latest add https://dresscodeui.com/r/dresscode/stat-cards-trend.json// Stat cards with trend badges: four figures for the month so far, in one card split by hairlines. Each
// badge shows the change against the same days of last month; its color says whether the move is good
// for that figure (outstanding going up is bad news), and its arrow says which way it went. Screen
// readers get the change as a sentence.
import { ArrowDownRight, ArrowRight, ArrowUpRight, CircleDollarSign, Clock3, FileText, Users } from "lucide-react"
import { Button } from "@/components/ui/button"
import { cn } from "@/lib/utils"
const STATS = [
{
label: "Revenue",
value: "$48,210",
change: "12.4%",
up: true,
good: true,
icon: CircleDollarSign,
context: "Up from $42,880 in October",
},
{
label: "Outstanding",
value: "$9,860",
change: "17.1%",
up: true,
good: false,
icon: FileText,
context: "5 open invoices, 2 of them late",
},
{
label: "Clients",
value: "64",
change: "6",
up: true,
good: true,
icon: Users,
context: "6 new this month, none lost",
},
{
label: "Days to get paid",
value: "11",
change: "3 days",
up: false,
good: true,
icon: Clock3,
context: "Your terms are 14 days",
},
]
// One card split into four: stacked on phones, two by two on tablets, a row on desktops.
const DIVIDERS = ["", "border-t sm:border-t-0 sm:border-l", "border-t lg:border-t-0 lg:border-l", "border-t sm:border-l lg:border-t-0"]
export default function StatCardsTrend() {
return (
<section aria-labelledby="stat-cards-trend-title" className="mx-auto w-full max-w-[1200px] px-5 py-10 sm:px-8">
<div className="flex flex-col gap-3 sm:flex-row sm:items-end sm:justify-between">
<div>
<h2 id="stat-cards-trend-title" className="dc-h text-[24px] leading-tight sm:text-[28px]">
November so far
</h2>
<p className="mt-1.5 text-[15px] text-muted-foreground">Nov 1 to 24, against the same days of October.</p>
</div>
<Button variant="outline" className="self-start sm:self-auto" nativeButton={false} render={<a href="#reports" />}>
Full report <ArrowRight aria-hidden="true" />
</Button>
</div>
<dl className="dc-card mt-6 grid overflow-hidden sm:grid-cols-2 lg:grid-cols-4">
{STATS.map((s, i) => {
const Arrow = s.up ? ArrowUpRight : ArrowDownRight
return (
<div key={s.label} className={cn("flex flex-col border-border p-5 sm:p-6", DIVIDERS[i])}>
<dt className="flex items-center justify-between gap-3 text-[14px] font-medium text-muted-foreground">
{s.label}
<span className="dc-icon size-8" aria-hidden="true">
<s.icon className="size-4" />
</span>
</dt>
<dd className="mt-3 mb-5 flex flex-wrap items-center gap-x-2.5 gap-y-2">
<span className="dc-num text-[32px] leading-none font-semibold tracking-tight">{s.value}</span>
<span className={cn("dc-pill", s.good ? "dc-pill-hl" : "dc-pill-danger")} aria-hidden="true">
<Arrow className="size-3.5" />
<span className="dc-num">
{s.up ? "+" : "−"}
{s.change}
</span>
</span>
<span className="sr-only">
, {s.up ? "up" : "down"} {s.change} on October, {s.good ? "a good change" : "a change for the worse"}.
</span>
</dd>
<dd className="mt-auto text-[13px] leading-snug text-muted-foreground">{s.context}</dd>
</div>
)
})}
</dl>
</section>
)
}With small charts
Four wide cards, each pairing a figure with its own small chart: a running total, weekly bars, a line against your terms and a stepped balance.
npx shadcn@latest add https://dresscodeui.com/r/dresscode/stat-cards-sparkline.json"use client"
// Stat cards with small charts: four wide cards, the figure on the left and its own chart on the right,
// each chart the shape that suits the number: a running total against last month, weekly bars, a line
// against your payment terms and a stepped balance. The charts are pictures for sighted readers (with a
// tooltip on hover); each card says what its chart shows in a sentence for screen readers. The charts don't
// animate in: a draw-in that starts before the card has its real width can stop part way.
import { ArrowDownRight, ArrowUpRight } from "lucide-react"
import type { ReactNode } from "react"
import { Area, AreaChart, Bar, BarChart, Cell, Line, LineChart, ReferenceLine, XAxis, YAxis } from "recharts"
import { ChartContainer, ChartTooltip, type ChartConfig } from "@/components/ui/chart"
import { cn } from "@/lib/utils"
const usd = (n: number) => `$${Math.round(n).toLocaleString("en-US")}`
// Money in each day of November so far, and on the same days of October.
const NOV_DAILY = [0, 1200, 3400, 0, 860, 2300, 0, 0, 4200, 1860, 640, 7450, 0, 2100, 0, 980, 3900, 2300, 0, 1500, 6200, 1250, 3570, 4500]
const OCT_DAILY = [800, 0, 2600, 1900, 0, 0, 3200, 1500, 0, 4100, 2250, 0, 0, 1800, 3600, 900, 0, 5200, 0, 0, 2750, 3900, 1480, 6900]
const running = (xs: number[]) => xs.map((_, i) => xs.slice(0, i + 1).reduce((a, b) => a + b, 0))
const NOV_RUN = running(NOV_DAILY)
const OCT_RUN = running(OCT_DAILY)
const REVENUE = NOV_RUN.map((nov, i) => ({ day: i + 1, nov, oct: OCT_RUN[i] }))
// Invoices sent each week (weeks start on Sunday); the last four are November's.
const WEEKS = ["Sep 6", "Sep 13", "Sep 20", "Sep 27", "Oct 4", "Oct 11", "Oct 18", "Oct 25", "Nov 1", "Nov 8", "Nov 15", "Nov 22"]
const SENT = [5, 4, 6, 5, 4, 5, 6, 4, 6, 7, 6, 4].map((count, i) => ({ week: WEEKS[i], count, now: i >= 8 }))
// Average days from sending an invoice to the money arriving, by month.
const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov"]
const DAYS = [19, 18, 18, 17, 16, 16, 15, 15, 14, 14, 11].map((days, i) => ({ month: MONTHS[i], days }))
// What clients owe you at the end of each day: invoices sent push it up, payments bring it down.
const OWED = [8420, 8420, 11820, 11820, 10960, 10960, 10960, 10960, 6760, 6760, 6120, 6120, 9520, 9520, 9520, 8540, 12440, 10140, 10140, 8640, 11950, 10700, 10700, 9860].map(
(owed, i) => ({ day: i + 1, owed }),
)
const revenueConfig = { nov: { label: "November", color: "var(--chart-1)" }, oct: { label: "October", color: "var(--muted-foreground)" } } satisfies ChartConfig
const sentConfig = { count: { label: "Invoices", color: "var(--chart-1)" } } satisfies ChartConfig
const daysConfig = { days: { label: "Days", color: "var(--chart-1)" } } satisfies ChartConfig
const owedConfig = { owed: { label: "Outstanding", color: "var(--chart-1)" } } satisfies ChartConfig
type TipProps = {
active?: boolean
label?: string | number
payload?: ReadonlyArray<{ name?: string | number; value?: unknown; color?: string; payload?: { fill?: string } }>
title: (label: string | number | undefined) => ReactNode
format: (v: number) => string
}
/** A small themed tooltip: the day, week or month, then one line per series. */
function Tip({ active, label, payload, title, format }: Partial<TipProps> & Pick<TipProps, "title" | "format">) {
if (!active || !payload?.length) return null
return (
<div className="min-w-32 rounded-[var(--radius)] border border-border bg-popover px-2.5 py-1.5 text-[12px] text-popover-foreground shadow-md">
<p className="mb-0.5 font-semibold">{title(label)}</p>
{payload.map((p, i) => (
<p key={i} className="flex items-center gap-2">
<span aria-hidden="true" className="size-2 rounded-[2px]" style={{ background: p.color ?? p.payload?.fill }} />
<span className="text-muted-foreground">{p.name}</span>
<span className="dc-num ml-auto pl-3 font-semibold">{format(Number(p.value))}</span>
</p>
))}
</div>
)
}
function Legend({ items }: { items: { label: string; swatch: "solid" | "dashed" | "faint" }[] }) {
return (
<ul className="flex flex-wrap gap-x-3.5 gap-y-1 text-[12px] text-muted-foreground" aria-hidden="true">
{items.map((it) => (
<li key={it.label} className="flex items-center gap-1.5">
{it.swatch === "dashed" ? (
<span className="w-3.5 border-t-2 border-dashed border-[color:var(--muted-foreground)]" />
) : (
<span className={cn("size-2.5 rounded-[3px] bg-[color:var(--chart-1)]", it.swatch === "faint" && "opacity-35")} />
)}
{it.label}
</li>
))}
</ul>
)
}
function StatChartCard({
title,
value,
change,
up,
good,
against,
legend,
summary,
axis,
children,
}: {
title: string
value: string
change: string
up: boolean
good: boolean
against: string
legend: ReactNode
summary: string
axis: [string, string]
children: ReactNode
}) {
const Arrow = up ? ArrowUpRight : ArrowDownRight
return (
<article className="dc-card grid gap-5 p-5 sm:grid-cols-[180px_minmax(0,1fr)] sm:gap-6">
<div className="flex flex-col">
<h3 className="text-[14px] font-medium text-muted-foreground">{title}</h3>
<p className="dc-num mt-2 text-[30px] leading-none font-semibold tracking-tight">{value}</p>
<p className="mt-3 flex flex-wrap items-center gap-x-2 gap-y-1 text-[13px] text-muted-foreground">
<span className={cn("dc-pill", good ? "dc-pill-hl" : "dc-pill-danger")}>
<Arrow className="size-3.5" aria-hidden="true" />
<span className="dc-num">{change}</span>
</span>
{against}
<span className="sr-only">{good ? ", a good change." : ", a change for the worse."}</span>
</p>
<div className="mt-auto pt-5">{legend}</div>
</div>
<div className="flex min-w-0 flex-col justify-end">
<p className="sr-only">{summary}</p>
<div aria-hidden="true">
{children}
<div className="mt-1.5 flex justify-between text-[11px] text-muted-foreground">
<span>{axis[0]}</span>
<span>{axis[1]}</span>
</div>
</div>
</div>
</article>
)
}
const chartBox = "aspect-auto h-[128px] w-full [&_g[tabindex]]:outline-hidden"
export default function StatCardsSparkline() {
return (
<section aria-labelledby="stat-cards-sparkline-title" className="mx-auto w-full max-w-[1200px] px-5 py-10 sm:px-8">
<div>
<h2 id="stat-cards-sparkline-title" className="dc-h text-[24px] leading-tight sm:text-[28px]">
Cash flow
</h2>
<p className="mt-1.5 text-[15px] text-muted-foreground">How money moved in November, with the weeks and months behind it.</p>
</div>
<div className="mt-6 grid gap-4 lg:grid-cols-2">
<StatChartCard
title="Revenue, month to date"
value={usd(NOV_RUN[NOV_RUN.length - 1])}
change="+12.4%"
up
good
against="on October"
legend={<Legend items={[{ label: "November", swatch: "solid" }, { label: "October", swatch: "dashed" }]} />}
summary={`A running total from Nov 1 to Nov 24, reaching ${usd(NOV_RUN[23])}; October had reached ${usd(OCT_RUN[23])} by the same day.`}
axis={["Nov 1", "Nov 24"]}
>
<ChartContainer config={revenueConfig} className={chartBox}>
<AreaChart data={REVENUE} margin={{ top: 6, right: 2, bottom: 0, left: 2 }} accessibilityLayer={false}>
<defs>
<linearGradient id="stat-cards-sparkline-rev" x1="0" y1="0" x2="0" y2="1">
<stop offset="0%" stopColor="var(--color-nov)" stopOpacity={0.3} />
<stop offset="100%" stopColor="var(--color-nov)" stopOpacity={0.02} />
</linearGradient>
</defs>
<XAxis dataKey="day" hide />
<YAxis hide domain={[0, "dataMax"]} />
<ChartTooltip cursor={{ stroke: "var(--border)" }} content={<Tip title={(d) => `Nov ${d}`} format={usd} />} />
<Area dataKey="oct" name="October" type="monotone" stroke="var(--color-oct)" strokeWidth={1.5} strokeDasharray="4 4" fill="transparent" dot={false} isAnimationActive={false} />
<Area dataKey="nov" name="November" type="monotone" stroke="var(--color-nov)" strokeWidth={2.25} fill="url(#stat-cards-sparkline-rev)" dot={false} isAnimationActive={false} />
</AreaChart>
</ChartContainer>
</StatChartCard>
<StatChartCard
title="Invoices sent"
value="23"
change="+4"
up
good
against="on October so far"
legend={<Legend items={[{ label: "November weeks", swatch: "solid" }, { label: "Earlier", swatch: "faint" }]} />}
summary="Invoices sent each week for the last 12 weeks, between 4 and 7 a week; 23 in November's four weeks."
axis={["Week of Sep 6", "Nov 22"]}
>
<ChartContainer config={sentConfig} className={chartBox}>
<BarChart data={SENT} margin={{ top: 6, right: 0, bottom: 0, left: 0 }} barCategoryGap="22%" accessibilityLayer={false}>
<XAxis dataKey="week" hide />
<YAxis hide domain={[0, "dataMax"]} />
<ChartTooltip cursor={{ fill: "var(--muted)" }} content={<Tip title={(w) => `Week of ${w}`} format={(v) => `${v} sent`} />} />
<Bar dataKey="count" name="Invoices" radius={[3, 3, 0, 0]} isAnimationActive={false}>
{SENT.map((w) => (
<Cell key={w.week} fill="var(--color-count)" fillOpacity={w.now ? 1 : 0.35} />
))}
</Bar>
</BarChart>
</ChartContainer>
</StatChartCard>
<StatChartCard
title="Days to get paid"
value="11 days"
change="−3 days"
up={false}
good
against="on October"
legend={<Legend items={[{ label: "Monthly average", swatch: "solid" }, { label: "Your terms, 14 days", swatch: "dashed" }]} />}
summary="The average days to get paid each month this year, falling from 19 in January to 11 in November, now under your 14-day terms."
axis={["Jan", "Nov"]}
>
<ChartContainer config={daysConfig} className={chartBox}>
<LineChart data={DAYS} margin={{ top: 8, right: 6, bottom: 4, left: 6 }} accessibilityLayer={false}>
<XAxis dataKey="month" hide />
<YAxis hide domain={[8, 21]} />
<ReferenceLine y={14} stroke="var(--muted-foreground)" strokeDasharray="4 4" strokeOpacity={0.8} />
<ChartTooltip cursor={{ stroke: "var(--border)" }} content={<Tip title={(m) => String(m)} format={(v) => `${v} days`} />} />
<Line
dataKey="days"
name="Average"
type="monotone"
stroke="var(--color-days)"
strokeWidth={2.25}
dot={{ r: 2.5, fill: "var(--card)", strokeWidth: 2 }}
activeDot={{ r: 4, fill: "var(--color-days)", stroke: "var(--card)", strokeWidth: 2 }}
isAnimationActive={false}
/>
</LineChart>
</ChartContainer>
</StatChartCard>
<StatChartCard
title="Outstanding"
value={usd(OWED[OWED.length - 1].owed)}
change="+17.1%"
up
good={false}
against="on Nov 1"
legend={<p className="text-[12px] text-muted-foreground">2 invoices are late: $3,110</p>}
summary="What clients owed at the end of each day of November: it rose as invoices went out and fell as they were paid, from $8,420 on Nov 1 to $9,860 today."
axis={["Nov 1", "Nov 24"]}
>
<ChartContainer config={owedConfig} className={chartBox}>
<AreaChart data={OWED} margin={{ top: 6, right: 2, bottom: 0, left: 2 }} accessibilityLayer={false}>
<defs>
<linearGradient id="stat-cards-sparkline-owed" x1="0" y1="0" x2="0" y2="1">
<stop offset="0%" stopColor="var(--color-owed)" stopOpacity={0.22} />
<stop offset="100%" stopColor="var(--color-owed)" stopOpacity={0.04} />
</linearGradient>
</defs>
<XAxis dataKey="day" hide />
<YAxis hide domain={[0, "dataMax"]} />
<ChartTooltip cursor={{ stroke: "var(--border)" }} content={<Tip title={(d) => `End of Nov ${d}`} format={usd} />} />
<Area dataKey="owed" name="Owed" type="stepAfter" stroke="var(--color-owed)" strokeWidth={2} fill="url(#stat-cards-sparkline-owed)" dot={false} isAnimationActive={false} />
</AreaChart>
</ChartContainer>
</StatChartCard>
</div>
</section>
)
}Goals with progress
This month's targets: a ring for the revenue goal with what is left and the pace, and bars for the other goals.
npx shadcn@latest add https://dresscodeui.com/r/dresscode/stat-cards-progress.json// Stat cards as goals: the month's revenue target as a ring with what is left, the pace and what is still
// coming in, then the other goals as bars with the figure and what is left. The ring and bars are real
// progress bars for screen readers (role, value and a value text); the drawing itself is hidden.
// The fill grows in once on load (CSS only, and not for people who turn motion off).
import { Check, Pencil } from "lucide-react"
import { Button } from "@/components/ui/button"
import { Progress, ProgressLabel } from "@/components/ui/progress"
import { cn } from "@/lib/utils"
const KEYFRAMES = `
@keyframes dc-stat-ring { from { stroke-dashoffset: 100 } }
@keyframes dc-stat-grow { from { transform: scaleX(0) } }
`
const usd = (n: number) => `$${n.toLocaleString("en-US")}`
const PAID = 48210
const TARGET = 55000
const DUE = 9860
const DAY = 24
const DAYS_IN_MONTH = 30
const pct = Math.round((PAID / TARGET) * 100)
const left = TARGET - PAID
const daysLeft = DAYS_IN_MONTH - DAY
const pace = Math.round(((PAID / DAY) * DAYS_IN_MONTH) / 10) * 10
const SCALE = 60000
const GOALS = [
{ label: "New clients", value: 6, goal: 8, figure: "6 of 8", note: "Fernwood and Halcyon came by referral", left: "2 to go" },
{ label: "Hours billed", value: 112, goal: 140, figure: "112 of 140 h", note: "About 5 h a day to the end of the month", left: "28 h to go" },
{ label: "Paid on time", value: 34, goal: 36, figure: "34 of 36", note: "Goal: 90% of this month's 40 invoices", left: "2 to go" },
// Lower is better here, so the bar is full once the figure is under the goal.
{ label: "Days to get paid", value: 1, goal: 1, figure: "11 days", note: "Goal: 12 days or fewer", left: "Reached" },
]
function Ring({ value }: { value: number }) {
return (
<svg viewBox="0 0 160 160" className="size-full -rotate-90" aria-hidden="true">
<circle cx="80" cy="80" r="68" fill="none" strokeWidth="14" className="stroke-[color:color-mix(in_oklab,var(--foreground)_10%,transparent)]" />
<circle
cx="80"
cy="80"
r="68"
fill="none"
strokeWidth="14"
strokeLinecap="round"
pathLength={100}
strokeDasharray="100"
strokeDashoffset={100 - value}
className="animate-[dc-stat-ring_900ms_ease-out] stroke-[color:var(--chart-1)] motion-reduce:animate-none"
/>
</svg>
)
}
export default function StatCardsProgress() {
return (
<section aria-labelledby="stat-cards-progress-title" className="mx-auto w-full max-w-[1200px] px-5 py-10 sm:px-8">
<style href="dc-stat-progress" precedence="default">
{KEYFRAMES}
</style>
<div className="flex flex-col gap-3 sm:flex-row sm:items-end sm:justify-between">
<div>
<h2 id="stat-cards-progress-title" className="dc-h text-[24px] leading-tight sm:text-[28px]">
November goals
</h2>
<p className="mt-1.5 text-[15px] text-pretty text-muted-foreground">
{daysLeft} days left in the month. Goals start again on Dec 1.
</p>
</div>
<Button variant="outline" className="self-start sm:self-auto" nativeButton={false} render={<a href="#goals" />}>
<Pencil aria-hidden="true" /> Edit goals
</Button>
</div>
<div className="mt-6 grid items-stretch gap-4 lg:grid-cols-[minmax(0,1.15fr)_minmax(0,1fr)]">
<article className="dc-card flex flex-col p-5 sm:p-6" aria-labelledby="stat-cards-progress-revenue">
<div className="flex items-center justify-between gap-3">
<h3 id="stat-cards-progress-revenue" className="text-[15px] font-semibold">
Monthly revenue
</h3>
<span className="dc-pill dc-pill-hl">On pace</span>
</div>
<div className="mt-5 flex flex-col items-center gap-6 sm:flex-row sm:gap-8">
<div
role="progressbar"
aria-labelledby="stat-cards-progress-revenue"
aria-valuemin={0}
aria-valuemax={100}
aria-valuenow={pct}
aria-valuetext={`${usd(PAID)} of ${usd(TARGET)}, ${pct}%`}
className="relative size-[176px] shrink-0"
>
<Ring value={pct} />
<div className="absolute inset-0 grid place-content-center text-center" aria-hidden="true">
<p className="dc-num text-[36px] leading-none font-semibold tracking-tight">{pct}%</p>
<p className="mt-1 text-[12px] text-muted-foreground">of target</p>
</div>
</div>
<div className="w-full min-w-0">
<p className="dc-num text-[34px] leading-none font-semibold tracking-tight">{usd(PAID)}</p>
<p className="mt-2 text-[14px] text-muted-foreground">
paid of a <span className="dc-num font-medium text-foreground">{usd(TARGET)}</span> target
</p>
<p className="mt-4 text-[14px] leading-snug text-pretty">
<span className="dc-num font-semibold">{usd(left)}</span> to go in {daysLeft} days. At this month's pace you finish at{" "}
<span className="dc-num font-semibold">{usd(pace)}</span>.
</p>
</div>
</div>
<dl className="mt-6 grid grid-cols-3 gap-3 border-t border-border pt-4">
<div>
<dt className="text-[12px] text-muted-foreground">Needed a day</dt>
<dd className="dc-num mt-0.5 text-[16px] font-semibold">{usd(Math.ceil(left / daysLeft))}</dd>
</div>
<div>
<dt className="text-[12px] text-muted-foreground">Paid a day so far</dt>
<dd className="dc-num mt-0.5 text-[16px] font-semibold">{usd(Math.round(PAID / DAY))}</dd>
</div>
<div>
<dt className="text-[12px] text-muted-foreground">October, all month</dt>
<dd className="dc-num mt-0.5 text-[16px] font-semibold">$42,880</dd>
</div>
</dl>
<div className="mt-auto pt-5">
<div className="rounded-[calc(var(--radius)+2px)] bg-[color:color-mix(in_oklab,var(--foreground)_4%,transparent)] p-4">
<p className="text-[13px] leading-snug">
<span className="font-semibold">{usd(DUE)} is due before Nov 30.</span>{" "}
<span className="text-muted-foreground">If those five invoices are paid, you pass the target by {usd(PAID + DUE - TARGET)}.</span>
</p>
<div className="relative mt-3" aria-hidden="true">
<div className="flex h-2.5 overflow-hidden rounded-full bg-[color:color-mix(in_oklab,var(--foreground)_10%,transparent)]">
<span className="h-full bg-[color:var(--chart-1)]" style={{ width: `${(PAID / SCALE) * 100}%` }} />
<span className="h-full border-l-2 border-[color:var(--card)] bg-[color:color-mix(in_oklab,var(--chart-1)_40%,transparent)]" style={{ width: `${(DUE / SCALE) * 100}%` }} />
</div>
<span className="absolute -top-1 -bottom-1 w-0.5 rounded-full bg-foreground" style={{ left: `${(TARGET / SCALE) * 100}%` }} />
</div>
<ul className="mt-2.5 flex flex-wrap gap-x-4 gap-y-1 text-[12px] text-muted-foreground" aria-hidden="true">
<li className="flex items-center gap-1.5">
<span className="size-2.5 rounded-[3px] bg-[color:var(--chart-1)]" /> Paid
</li>
<li className="flex items-center gap-1.5">
<span className="size-2.5 rounded-[3px] bg-[color:color-mix(in_oklab,var(--chart-1)_40%,transparent)]" /> Due by Nov 30
</li>
<li className="flex items-center gap-1.5">
<span className="h-3 w-0.5 rounded-full bg-foreground" /> Target
</li>
</ul>
</div>
</div>
</article>
<article className="dc-card flex flex-col p-5 sm:p-6" aria-labelledby="stat-cards-progress-other">
<div className="flex items-center justify-between gap-3">
<h3 id="stat-cards-progress-other" className="text-[15px] font-semibold">
Other goals
</h3>
<span className="text-[13px] text-muted-foreground">1 of 4 reached</span>
</div>
<ul className="mt-2 flex flex-1 flex-col divide-y divide-border">
{GOALS.map((g) => {
const done = g.value >= g.goal
const value = Math.round((g.value / g.goal) * 100)
return (
<li key={g.label} className="flex flex-1 flex-col justify-center py-4 last:pb-0">
<Progress
value={value}
aria-valuetext={`${g.figure}, ${done ? "goal reached" : g.left}`}
className={cn(
"gap-x-3 gap-y-2.5 [&_[data-slot=progress-track]]:h-2 [&_[data-slot=progress-track]]:bg-[color:color-mix(in_oklab,var(--foreground)_10%,transparent)] [&_[data-slot=progress-indicator]]:origin-left [&_[data-slot=progress-indicator]]:animate-[dc-stat-grow_800ms_ease-out] [&_[data-slot=progress-indicator]]:rounded-full motion-reduce:[&_[data-slot=progress-indicator]]:animate-none",
"[&_[data-slot=progress-indicator]]:bg-[color:var(--chart-1)]",
)}
>
<ProgressLabel className="text-[14px] font-medium">{g.label}</ProgressLabel>
<span className="dc-num ml-auto text-[14px] font-semibold">{g.figure}</span>
</Progress>
<p className="mt-2 flex items-center justify-between gap-3 text-[12px] text-muted-foreground">
<span className="min-w-0">{g.note}</span>
{done ? (
<span className="dc-pill dc-pill-hl shrink-0">
<Check className="size-3" aria-hidden="true" /> Reached
</span>
) : (
<span className="shrink-0 font-medium text-foreground">{g.left}</span>
)}
</p>
</li>
)
})}
</ul>
</article>
</div>
</section>
)
}With a period switch
Switch between this month, last month and this quarter; every figure, change and comparison bar follows.
npx shadcn@latest add https://dresscodeui.com/r/dresscode/stat-cards-comparison.json"use client"
// Stat cards with a period switch: this month, last month or this quarter, each against the period
// before it. Switching changes every figure, every change badge and the pair of bars under each figure
// (this period against the last, on one scale). The line under the heading says what is being compared
// and is announced when it changes.
import { ArrowDownRight, ArrowUpRight, Minus } from "lucide-react"
import { useState } from "react"
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"
import { cn } from "@/lib/utils"
type Metric = "revenue" | "paid" | "average" | "days"
const METRICS: { id: Metric; label: string; better: "up" | "down"; change: "percent" | "count" | "days"; format: (n: number) => string }[] = [
{ id: "revenue", label: "Revenue", better: "up", change: "percent", format: (n) => `$${n.toLocaleString("en-US")}` },
{ id: "paid", label: "Invoices paid", better: "up", change: "count", format: String },
{ id: "average", label: "Average invoice", better: "up", change: "percent", format: (n) => `$${n.toLocaleString("en-US")}` },
{ id: "days", label: "Days to get paid", better: "down", change: "days", format: (n) => `${n} ${n === 1 ? "day" : "days"}` },
]
const PERIODS = {
month: {
label: "This month",
now: "Nov",
before: "Oct",
summary: "Nov 1 to 24, against October",
against: "on October",
values: { revenue: [48210, 42880], paid: [17, 15], average: [2836, 2859], days: [11, 14] },
},
last: {
label: "Last month",
now: "Oct",
before: "Sep",
summary: "October, against September",
against: "on September",
values: { revenue: [42880, 41590], paid: [15, 16], average: [2859, 2599], days: [14, 15] },
},
quarter: {
label: "This quarter",
now: "Q4",
before: "Q3",
summary: "Oct 1 to Nov 24, against July to September",
against: "on Q3",
values: { revenue: [91090, 76810], paid: [32, 29], average: [2847, 2649], days: [12, 16] },
},
} satisfies Record<string, { label: string; now: string; before: string; summary: string; against: string; values: Record<Metric, [number, number]> }>
type Period = keyof typeof PERIODS
function change(now: number, before: number, kind: "percent" | "count" | "days") {
const d = now - before
const sign = d > 0 ? "+" : d < 0 ? "−" : ""
const abs = Math.abs(d)
if (kind === "percent") return `${sign}${((abs / before) * 100).toFixed(1)}%`
if (kind === "days") return `${sign}${abs} ${abs === 1 ? "day" : "days"}`
return `${sign}${abs}`
}
export default function StatCardsComparison() {
const [period, setPeriod] = useState<Period>("month")
const p = PERIODS[period]
return (
<section aria-labelledby="stat-cards-comparison-title" className="mx-auto w-full max-w-[1200px] px-5 py-10 sm:px-8">
<div className="flex flex-col gap-4 md:flex-row md:items-end md:justify-between">
<div>
<h2 id="stat-cards-comparison-title" className="dc-h text-[24px] leading-tight sm:text-[28px]">
How the studio is doing
</h2>
<p className="mt-1.5 text-[15px] text-muted-foreground" aria-live="polite">
{p.summary}
</p>
</div>
<ToggleGroup
aria-label="Period"
value={[period]}
onValueChange={(v) => v[0] && setPeriod(v[0] as Period)}
spacing={1}
className="max-w-full self-start rounded-[calc(var(--radius)+2px)] border border-border bg-[color:color-mix(in_oklab,var(--foreground)_5%,transparent)] p-[3px] md:self-auto"
>
{(Object.keys(PERIODS) as Period[]).map((k) => (
<ToggleGroupItem
key={k}
value={k}
className="h-8 rounded-[var(--radius)] px-3 text-[13px] text-muted-foreground hover:bg-transparent hover:text-foreground aria-pressed:bg-[color:var(--card)] aria-pressed:font-semibold aria-pressed:text-foreground aria-pressed:shadow-sm aria-pressed:ring-1 aria-pressed:ring-[color:var(--input)]"
>
{PERIODS[k].label}
</ToggleGroupItem>
))}
</ToggleGroup>
</div>
<dl className="mt-6 grid gap-4 sm:grid-cols-2 lg:grid-cols-4">
{METRICS.map((m) => {
const [now, before] = p.values[m.id]
const d = now - before
const good = d === 0 || (m.better === "up" ? d > 0 : d < 0)
const text = change(now, before, m.change)
const Arrow = d === 0 ? Minus : d > 0 ? ArrowUpRight : ArrowDownRight
const max = Math.max(now, before)
return (
<div key={m.id} className="dc-card flex flex-col p-5">
<dt className="text-[14px] font-medium text-muted-foreground">{m.label}</dt>
<dd className="dc-num mt-2 text-[30px] leading-none font-semibold tracking-tight">{m.format(now)}</dd>
<dd className="mt-3 flex flex-wrap items-center gap-x-2 gap-y-1 text-[13px] text-muted-foreground">
<span className={cn("dc-pill", good ? "dc-pill-hl" : "dc-pill-danger")}>
<Arrow className="size-3.5" aria-hidden="true" />
<span className="dc-num">{text}</span>
</span>
{p.against}
<span className="sr-only">{good ? ", a good change" : ", a change for the worse"}</span>
</dd>
<dd className="mt-auto pt-5">
<span className="sr-only">
{p.now}: {m.format(now)}. {p.before}: {m.format(before)}.
</span>
<span className="grid grid-cols-[28px_minmax(0,1fr)_auto] items-center gap-x-2.5 gap-y-2 text-[12px]" aria-hidden="true">
<span className="font-semibold">{p.now}</span>
<span className="h-2 overflow-hidden rounded-full bg-[color:color-mix(in_oklab,var(--foreground)_9%,transparent)]">
<span
className="block h-full rounded-full bg-[color:var(--chart-1)] transition-[width] duration-500 ease-out motion-reduce:transition-none"
style={{ width: `${(now / max) * 100}%` }}
/>
</span>
<span className="dc-num text-right font-medium">{m.format(now)}</span>
<span className="text-muted-foreground">{p.before}</span>
<span className="h-2 overflow-hidden rounded-full bg-[color:color-mix(in_oklab,var(--foreground)_9%,transparent)]">
<span
className="block h-full rounded-full bg-[color:var(--muted-foreground)] opacity-45 transition-[width] duration-500 ease-out motion-reduce:transition-none"
style={{ width: `${(before / max) * 100}%` }}
/>
</span>
<span className="dc-num text-right text-muted-foreground">{m.format(before)}</span>
</span>
</dd>
</div>
)
})}
</dl>
</section>
)
}