Delta
A signed change next to a figure. Direction and sentiment are separate, so a falling cost can point down and still read as good news.
Preview
RevenueUp12.4%vs last month
SignupsDown3.1%vs last month
Cost per leadDown8.0%vs last month
Open ticketsNo change0.0%vs last month
Install
npx shadcn@latest add @dxbh-ui/delta
Add the @dxbh-ui registry to components.json once:
{
"registries": {
"@dxbh-ui": "https://ui.dxbh.dev/r/{name}.json"
}
}Usage
"use client";
import { Delta } from "@/components/ui/delta/delta";
export default function DeltaDemo() {
return (
<div className="grid w-full max-w-sm gap-3 text-sm">
<div className="flex items-center justify-between gap-4">
<span className="text-muted-foreground">Revenue</span>
<Delta dir="up" value="12.4%" caption="vs last month" />
</div>
<div className="flex items-center justify-between gap-4">
<span className="text-muted-foreground">Signups</span>
<Delta dir="down" value="3.1%" caption="vs last month" />
</div>
<div className="flex items-center justify-between gap-4">
<span className="text-muted-foreground">Cost per lead</span>
{/* Falling cost is good news: direction and sentiment are separate. */}
<Delta dir="down" value="8.0%" tone="positive" caption="vs last month" />
</div>
<div className="flex items-center justify-between gap-4">
<span className="text-muted-foreground">Open tickets</span>
<Delta dir="flat" value="0.0%" caption="vs last month" />
</div>
</div>
);
}Props
| Prop | Type | Default |
|---|---|---|
dir*Which way the figure moved. | "up" | "down" | "flat" | — |
valueThe formatted magnitude, e.g. "8.4%". | string | — |
toneSentiment. Defaults to following dir. | "positive" | "negative" | "neutral" | — |
captionQuiet trailing context. | string | — |
labelsSpoken direction words, for localisation. | Partial<Record<DeltaDir, string>> | — |
Polish
- Colour comes from --positive / --negative; the arrow glyph carries the meaning when colour can't be seen.
- Direction is spoken ("Up", "Down", "No change") so screen readers don't hear a bare percentage.
- Adds a flat direction with a neutral tone.
- Hand-drawn SVG replaced with lucide-react arrows.
- Tabular digits so a column of deltas lines up.
Source
import { ArrowRight, ArrowUpRight } from "lucide-react";
import { cn } from "@/lib/utils";
export type DeltaDir = "up" | "down" | "flat";
export type DeltaTone = "positive" | "negative" | "neutral";
export type DeltaProps = {
dir: DeltaDir;
/** The magnitude, already formatted ("8.4%"). The arrow carries the sign. */
value?: string;
/** Sentiment. Defaults to following `dir`: up is positive, down negative, flat neutral. */
tone?: DeltaTone;
/** Quiet trailing context, e.g. "vs last week". */
caption?: string;
/** Spoken direction words, for localisation. */
labels?: Partial<Record<DeltaDir, string>>;
className?: string;
};
const TONE: Record<DeltaTone, string> = {
positive: "text-positive",
negative: "text-negative",
neutral: "text-muted-foreground",
};
const SPOKEN: Record<DeltaDir, string> = { up: "Up", down: "Down", flat: "No change" };
/**
* A signed change next to a figure.
*
* Direction and sentiment are separate: a falling cost points down but is
* good news, so pass `tone="positive"`. The arrow is the glyph that keeps the
* meaning when colour can't be seen, and the direction is spoken to screen
* readers, because "8.4%" sounds the same whether it rose or fell.
*/
export function Delta({ dir, value, tone, caption, labels, className }: DeltaProps) {
const resolved: DeltaTone = tone ?? (dir === "up" ? "positive" : dir === "down" ? "negative" : "neutral");
const Icon = dir === "flat" ? ArrowRight : ArrowUpRight;
return (
<span
data-slot="delta"
data-dir={dir}
className={cn("inline-flex items-center gap-1 text-xs font-medium tabular-nums", TONE[resolved], className)}
>
<Icon aria-hidden="true" strokeWidth={2} className={cn("size-3.5 shrink-0", dir === "down" && "rotate-90")} />
<span className="sr-only">{labels?.[dir] ?? SPOKEN[dir]}</span>
{value}
{caption ? <span className="ml-0.5 font-normal text-muted-foreground">{caption}</span> : null}
</span>
);
}Extracted from Vesrelle/components/data/delta.tsx at 4619651.