Weekly Support KPI Card
Weekly metrics card: compact support stats with six figures in two columns, each with a change arrow and comparison text, kept level by a subgrid. Worsening changes show in red. A dashboard KPI card.
import { ArrowDownIcon, ArrowUpIcon } from "@phosphor-icons/react/ssr"
import { cn } from "cn"
import {
Stat,
StatGroup,
StatLabel,
StatValue,
} from "@/components/stats/stats"
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card"
// The copy: change text here, not in the markup.
const COPY = {
title: "Support this week",
description: "Oct 19–25, against Oct 12–18",
against: "than last week",
} as const
type Metric = {
label: string
value: string
note?: string
change: { direction: "up" | "down"; text: string; worse?: boolean }
}
// The week opened with 57 tickets: 57 + 132 new − 141 solved leaves 48.
// Satisfaction is 49 good ratings out of 52.
const METRICS: Metric[] = [
{
label: "Open",
value: "48",
change: { direction: "down", text: "9 fewer" },
},
{ label: "New", value: "132", change: { direction: "up", text: "14 more" } },
{
label: "Solved",
value: "141",
change: { direction: "up", text: "22 more" },
},
{
label: "First reply",
value: "1h 12m",
note: "median",
change: { direction: "down", text: "8m faster" },
},
{
label: "Resolution",
value: "9h 40m",
note: "median",
change: { direction: "up", text: "1h 5m slower", worse: true },
},
{
label: "Satisfaction",
value: "94%",
note: "of 52 ratings",
change: { direction: "up", text: "2 pts higher" },
},
]
export default function SupportKpiCard() {
return (
<Card className="mx-auto w-full max-w-sm">
<CardHeader>
<CardTitle>
<h2>{COPY.title}</h2>
</CardTitle>
<CardDescription>{COPY.description}</CardDescription>
</CardHeader>
<CardContent>
{/* Volume runs down the first column, speed and satisfaction down the
second. Each cell spans three of the nine rows (label, value,
change), so the lines stay level across a row even when one wraps;
add a row of cells and the grid needs three more rows. */}
<StatGroup
cols={1}
gap="none"
className="grid-flow-col grid-cols-2 grid-rows-[repeat(9,auto)] gap-x-4 gap-y-6"
>
{METRICS.map((metric) => {
const Arrow =
metric.change.direction === "up" ? ArrowUpIcon : ArrowDownIcon
return (
<Stat
key={metric.label}
className="row-span-3 min-w-0 grid-rows-subgrid gap-y-1"
>
<StatLabel className="text-xs/4 text-muted-foreground">
{metric.label}
</StatLabel>
<StatValue className="flex flex-wrap items-baseline gap-x-1 text-xl/8 font-medium tabular-nums">
{metric.value}
{metric.note ? (
<span className="text-xs font-normal text-muted-foreground">
{metric.note}
</span>
) : null}
</StatValue>
<p
className={cn(
"flex items-center gap-1 text-xs",
metric.change.worse
? "text-destructive"
: "text-muted-foreground"
)}
>
<Arrow aria-hidden="true" className="size-3 shrink-0" />
<span>
{metric.change.text}
<span className="sr-only"> {COPY.against}</span>
</span>
</p>
</Stat>
)
})}
</StatGroup>
</CardContent>
</Card>
)
}
import type { ComponentProps } from "react"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"
import { Grid, type GridProps } from "@/components/grid/grid"
/**
* A list of `Stat` items laid out by `Grid`, rendered as a `ul`. It defaults
* to 3 columns and the large gap. Like `Grid`, its columns follow the width
* it is given, so size its parent to narrow it. `className` lands on the
* list.
*/
function StatGroup({
cols = 3,
gap = "lg",
...props
}: ComponentProps<"ul"> & {
/**
* The most columns, 3 by default. They step up as the list's space
* widens, the way `Grid`'s do.
*/
cols?: GridProps["cols"]
/**
* Space between stats: `xs` 8px, `sm` 16px, `md` 24px, `lg` 40px, `xl` 64px, `px` 1px, `none` 0. `lg`
* is the default.
*/
gap?: GridProps["gap"]
}) {
return (
<Grid
data-slot="stat-group"
render={<ul role="list" />}
cols={cols}
gap={gap}
// Grid's props are typed for its default div; this group renders a list.
{...(props as Omit<GridProps, "cols" | "gap">)}
/>
)
}
// card sets the stat on a filled panel; rule sets a line down its left side.
const statVariants = cva("grid content-start gap-2", {
variants: {
/**
* The stat style. `default` (the default) adds nothing. `card` sets it
* on a `rounded-xl` panel with 32px padding, filled with `muted`, or
* with `card` and `card-foreground` text in dark mode. `rule` draws a
* 1px line in `border` down its left side, 24px from the text.
*/
variant: {
default: "",
card: "rounded-xl bg-muted p-8 dark:bg-card dark:text-card-foreground",
rule: "border-l border-border pl-6",
},
},
defaultVariants: { variant: "default" },
})
/**
* One figure and its label, as an `li` inside `StatGroup`. Holds a
* `StatValue` and a `StatLabel`, stacked with 8px gaps.
*
* `card` sets it on a filled, rounded panel with 32px padding. `rule` draws a
* line down its left side.
*/
function Stat({
className,
variant = "default",
...props
}: ComponentProps<"li"> & VariantProps<typeof statVariants>) {
return (
<li
data-slot="stat"
data-variant={variant}
className={cn(statVariants({ variant }), className)}
{...props}
/>
)
}
/** The figure itself, such as a number or a percentage, at 32px in the body font. */
function StatValue({ className, ...props }: ComponentProps<"p">) {
return (
<p
data-slot="stat-value"
className={cn("text-[2rem]/10 tracking-tight", className)}
{...props}
/>
)
}
/**
* The caption under the figure: what it measures, in a few words. Small
* muted text, 14px on a 20px line, so the number carries the weight. Longer
* explanation belongs in the section's lead or a note, not here.
*/
function StatLabel({ className, ...props }: ComponentProps<"p">) {
return (
<p
data-slot="stat-label"
className={cn("text-sm/5 text-pretty text-muted-foreground", className)}
{...props}
/>
)
}
export { StatGroup, Stat, StatValue, StatLabel }
import { mergeProps } from "@base-ui/react/merge-props"
import { useRender } from "@base-ui/react/use-render"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"
const GRID_DEFAULTS = { cols: 3, gap: "lg" } as const
const containerGridVariants = cva("grid w-full grid-cols-1", {
variants: {
/**
* The most columns. Below 40rem of screen width the grid is one
* column (two for `6`); it steps up at 40rem and 48rem: `2` is two
* columns from 40rem, `3` two then three, `4` two then four, `5` three
* then five, and `6` three, then four, then six from 64rem. The default
* is 3.
*/
cols: {
1: "",
2: "sm:grid-cols-2",
3: "sm:grid-cols-2 md:grid-cols-3",
4: "sm:grid-cols-2 md:grid-cols-4",
5: "sm:grid-cols-3 md:grid-cols-5",
6: "grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-6",
},
/**
* Space between items: `xs` 8px, `sm` 16px, `md` 24px, `lg` 40px, `xl` 64px, `px` 1px, `none` 0. `lg`
* is the default. The sizes are the `--gap-*` values in globals.css.
*/
gap: {
xs: "gap-(--gap-xs)",
sm: "gap-(--gap-sm)",
md: "gap-(--gap-md)",
lg: "gap-(--gap-lg)",
xl: "gap-(--gap-xl)",
px: "gap-px",
none: "gap-0",
},
},
defaultVariants: GRID_DEFAULTS,
})
type GridState = { slot: "grid" } & VariantProps<typeof containerGridVariants>
type GridProps = useRender.ComponentProps<"div", GridState> &
VariantProps<typeof containerGridVariants>
/**
* Columns follow the screen, stepping at 40rem and 48rem (and 64rem for six).
* Gap sizes read the shared `--gap-*` values in globals.css. The grid fills
* its parent's width: without that, a grid given `mx-auto` or set inside a
* centering parent shrinks to fit its content, and a part built on it that
* is a container collapses to nothing.
*
* className, render and every other prop go to the grid. To narrow a Grid, pass
* a `max-w-*` class or size its parent.
*/
function Grid({
className,
cols = GRID_DEFAULTS.cols,
gap = GRID_DEFAULTS.gap,
render,
...props
}: GridProps) {
const grid = useRender({
defaultTagName: "div",
render,
state: { slot: "grid", cols, gap },
props: mergeProps<"div">(
{ className: cn(containerGridVariants({ cols, gap }), className) },
props
),
})
return grid
}
export { Grid, type GridProps }
Installation
npx shadcn@latest add @01orbital/support-kpi-cardMake it yours
- Its words are in
COPYat the top ofcomponents/blocks/metrics/support-kpi-card/index.tsx: change them there, not in the markup. METRICS, also at the top of the file, holds its lists and settings.
Built from
npm: @phosphor-icons/react, cn. The CLI installs them for you.
Built from these parts. The CLI adds each one that is missing and won't replace a file you already have unless you choose to:
The shadcn parts here are their Base UI versions, and this piece uses Base UI's API. A Radix version you already have won't work with it.
Last updated on