Charts and graphs · @jengacore/stat-tile
StatTile
KPI tile: value and unit, an up, down or flat change badge that says its direction in words, and an optional sparkline.
Transfers today
48,210Up 4.1% versus last Monday
Failed transfers
0.42%Down 0.08 pt versus last week
Median latency
158MSUp 12 ms versus last week
Active agents
1,284No change versus yesterday
Install
terminal
npx shadcn@latest add @jengacore/stat-tileWithout the namespace configured: npx shadcn@latest add https://ui.jengacore.com/r/<new-york-v4|base-nova>/stat-tile.json
Registry dependencies
npm packages
lucide-react
Source
components/jc/stat-tile.tsx
import * as React from "react"
import { ArrowDownRight, ArrowRight, ArrowUpRight } from "lucide-react"
import { cn } from "@/lib/utils"
import { Sparkline } from "@/components/jc/sparkline"
export type StatDirection = "up" | "down" | "flat"
const WORD: Record<StatDirection, string> = { up: "Up", down: "Down", flat: "No change" }
const ICON = { up: ArrowUpRight, down: ArrowDownRight, flat: ArrowRight } as const
/** The direction of a change, from its sign. Zero (or under `epsilon`) is flat. */
export function directionOf(change: number, epsilon = 0): StatDirection {
if (Math.abs(change) <= epsilon) return "flat"
return change > 0 ? "up" : "down"
}
/**
* A KPI tile: label, value with its unit, a change badge and an optional sparkline. The badge says the direction in
* words to screen readers ("Up 4.1% versus last week") and is coloured by whether the change is good for the reader:
* set `goodWhen="down"` for failures, latency or cost. Use KpiStrip for a ledger of 2–4 numbers in one band; use
* StatTile when each number stands in its own card on a dashboard.
*/
export function StatTile({
label,
value,
unit,
change,
direction,
goodWhen = "up",
comparison = "versus last period",
spark,
sparkColor,
className,
}: {
label: string
value: React.ReactNode
/** Unit or currency, set in mono after the value. */
unit?: string
/** The change as shown, already formatted: "+4.1%", "−12", "0%". */
change?: string
/** Direction of the change; required with `change`, since a formatted string cannot be trusted to carry it. */
direction?: StatDirection
goodWhen?: "up" | "down"
/** What the change is measured against, read after it: "versus last week". */
comparison?: string
/** Recent values, oldest first. */
spark?: readonly number[]
sparkColor?: string
className?: string
}) {
const dir = direction ?? "flat"
const Icon = ICON[dir]
const tone = dir === "flat" ? "neutral" : dir === goodWhen ? "good" : "bad"
const magnitude = change?.replace(/^[+\-−]/, "")
return (
<div
data-slot="stat-tile"
className={cn("flex min-w-0 flex-col gap-1.5 rounded-lg border border-border bg-card px-5 pt-4 pb-3 shadow-card contrast:border-input", className)}
>
<span className="eyebrow">{label}</span>
<div className="flex flex-wrap items-baseline gap-x-2 gap-y-1">
<span className="font-display text-[30px] leading-9 font-semibold tracking-[-0.01em] tabular-nums">
{value}
{unit ? <small className="ml-1.5 font-mono text-xs font-medium tracking-normal text-muted-foreground">{unit}</small> : null}
</span>
{change !== undefined ? (
<span
data-direction={dir}
data-tone={tone}
className={cn(
"inline-flex items-center gap-0.5 rounded-sm px-1.5 py-px font-mono text-[11.5px] leading-4 font-medium tabular-nums",
tone === "good" && "bg-success-subtle text-success",
tone === "bad" && "bg-destructive-subtle text-destructive",
tone === "neutral" && "bg-muted text-muted-foreground"
)}
>
<Icon aria-hidden className="size-3.5" />
<span aria-hidden>{change}</span>
<span className="sr-only">{dir === "flat" ? `${WORD.flat} ${comparison}` : `${WORD[dir]} ${magnitude} ${comparison}`}</span>
</span>
) : null}
</div>
{change !== undefined ? (
<span aria-hidden className="text-[12.5px] text-muted-foreground">
{comparison}
</span>
) : null}
{spark ? <Sparkline values={spark} color={sparkColor} baseline className="mt-1" /> : null}
</div>
)
}