"use client";
import { ArrowDownRight, ArrowUpRight } from "lucide-react";
import { motion, useReducedMotion } from "motion/react";
import { useState, type KeyboardEvent } from "react";
import { cn } from "@/lib/utils";
const DIGITS = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
/** Odometer text: digits roll to their new value, other characters stay put. */
export function RollingNumber({ value, className }: { value: string; className?: string }) {
const reduce = useReducedMotion();
const chars = value.split("");
return (
<span className={cn("inline-flex tabular-nums", className)}>
<span className="sr-only">{value}</span>
{chars.map((ch, i) => {
// Key from the right so units stay units when the length changes.
const key = `p${chars.length - i}`;
if (!/\d/.test(ch)) return <span key={key} aria-hidden>{ch}</span>;
return (
<span key={key} aria-hidden className="relative inline-block h-[1.15em] w-[1ch] overflow-hidden">
<motion.span
className="absolute inset-x-0 top-0 flex flex-col"
initial={false}
animate={{ y: `${-Number(ch) * 10}%` }}
transition={reduce ? { duration: 0 } : { type: "spring", stiffness: 140, damping: 20, delay: (chars.length - i) * 0.035 }}
>
{DIGITS.map((d) => (
<span key={d} className="flex h-[1.15em] items-center justify-center">
{d}
</span>
))}
</motion.span>
</span>
);
})}
</span>
);
}
export type RevenuePeriod = {
key: string;
label: string;
total: number;
delta: number;
bars: number[];
split: { label: string; value: number }[];
};
type RevenueCardProps = { periods: RevenuePeriod[]; defaultPeriod?: string; className?: string };
const usd = (v: number) => `$${v.toLocaleString("en")}`;
export function RevenueCard({ periods, defaultPeriod, className }: RevenueCardProps) {
const reduce = useReducedMotion();
const [key, setKey] = useState(defaultPeriod ?? periods[0].key);
const idx = Math.max(0, periods.findIndex((p) => p.key === key));
const p = periods[idx];
const max = Math.max(...p.bars);
const up = p.delta >= 0;
function onKeyDown(e: KeyboardEvent<HTMLDivElement>) {
if (e.key !== "ArrowLeft" && e.key !== "ArrowRight") return;
e.preventDefault();
const next = (idx + (e.key === "ArrowRight" ? 1 : -1) + periods.length) % periods.length;
setKey(periods[next].key);
(e.currentTarget.children[next] as HTMLElement | undefined)?.focus();
}
return (
<div className={cn("w-full rounded-2xl border border-border bg-card p-5 shadow-sm", className)}>
<div className="flex items-center justify-between gap-4">
<p className="text-sm text-muted-foreground">Revenue</p>
<div role="radiogroup" aria-label="Period" onKeyDown={onKeyDown} className="flex rounded-full bg-muted p-0.5 text-xs">
{periods.map((o) => (
<button
key={o.key}
type="button"
role="radio"
aria-checked={o.key === key}
tabIndex={o.key === key ? 0 : -1}
onClick={() => setKey(o.key)}
className={cn(
"relative rounded-full px-3 py-1 font-medium outline-none transition-colors focus-visible:ring-2 focus-visible:ring-ring",
o.key === key ? "text-foreground" : "text-muted-foreground hover:text-foreground",
)}
>
{o.key === key && (
<motion.span
layoutId="dash-revenue-card-pill"
className="absolute inset-0 rounded-full bg-background shadow-sm"
transition={{ type: "spring", stiffness: 420, damping: 34 }}
/>
)}
<span className="relative">{o.key}</span>
</button>
))}
</div>
</div>
<div className="mt-2 flex items-end gap-3">
<RollingNumber value={usd(p.total)} className="text-4xl font-semibold tracking-tight text-card-foreground" />
<span
className={cn(
"mb-1.5 inline-flex items-center gap-0.5 rounded-full px-2 py-0.5 text-xs font-medium",
up ? "bg-emerald-500/10 text-emerald-600 dark:text-emerald-400" : "bg-destructive/10 text-destructive",
)}
>
{up ? <ArrowUpRight className="size-3.5" /> : <ArrowDownRight className="size-3.5" />}
<RollingNumber value={`${Math.abs(p.delta).toFixed(1)}%`} />
</span>
</div>
<p className="mt-0.5 text-xs text-muted-foreground">{p.label}</p>
<div className="mt-5 flex h-24 items-end gap-1.5" role="img" aria-label={`Revenue trend for ${p.label}`}>
{p.bars.map((v, i) => (
<motion.span
key={i}
className={cn("flex-1 rounded-t-md", i === p.bars.length - 1 ? "bg-primary" : "bg-primary/25")}
initial={reduce ? false : { height: "4%" }}
animate={{ height: `${Math.max(4, (v / max) * 100)}%` }}
transition={reduce ? { duration: 0 } : { type: "spring", stiffness: 170, damping: 20, delay: i * 0.025 }}
/>
))}
</div>
<dl className="mt-5 grid grid-cols-3 gap-3 border-t border-border pt-4">
{p.split.map((s) => (
<div key={s.label}>
<dt className="text-xs text-muted-foreground">{s.label}</dt>
<dd className="mt-0.5 text-sm font-medium text-card-foreground">
<RollingNumber value={`${s.value < 0 ? "−" : ""}${usd(Math.abs(s.value))}`} />
</dd>
</div>
))}
</dl>
</div>
);
}
const split = (a: number, b: number, c: number) => [
{ label: "New", value: a },
{ label: "Expansion", value: b },
{ label: "Churned", value: c },
];
const periods: RevenuePeriod[] = [
{ key: "7D", label: "Last 7 days · vs previous 7", total: 18420, delta: 8.2, bars: [42, 55, 48, 61, 58, 40, 36, 52, 66, 71, 63, 78], split: split(11260, 8940, -1780) },
{ key: "30D", label: "Last 30 days · vs previous 30", total: 76935, delta: 12.6, bars: [38, 41, 47, 44, 52, 58, 55, 63, 61, 70, 74, 82], split: split(44180, 39870, -7115) },
{ key: "90D", label: "Last 90 days · vs previous 90", total: 214608, delta: -3.4, bars: [80, 76, 84, 71, 69, 73, 64, 67, 60, 63, 58, 61], split: split(118450, 121300, -25142) },
{ key: "12M", label: "Last 12 months · vs previous 12", total: 847250, delta: 31.9, bars: [22, 26, 31, 30, 38, 44, 49, 57, 62, 71, 84, 96], split: split(503900, 441720, -98370) },
];
export default function DashRevenueCardDemo() {
return <RevenueCard periods={periods} defaultPeriod="30D" className="max-w-md" />;
}