About
A traffic stat card whose stacked bars morph when you switch Day and Week, digits blur in one by one, and a lens glides across the bars showing a tooltip. More like this, plus a designer your coding agent can use, at studios.lou15.com/?ref=21st
Built with React and Tailwind. Copy the prompt and paste it into Claude Code, Cursor or Lovable: your agent adds Traffic Bars to your project and fits it to your theme.
Code
How Traffic Bars is used: the demo's code. The component's own source comes with a free account.
Usage.tsx
"use client";
import * as React from "react";
import { TrafficBars, type TrafficBarsPoint, type TrafficBarsRange } from "@/components/ui/traffic-bars";
/** True while the page (or any ancestor) carries the `dark` class. */
function useDarkClass(ref: React.RefObject<HTMLElement | null>) {
const [dark, setDark] = React.useState(false);
React.useEffect(() => {
const read = () => setDark(!!ref.current?.closest(".dark"));
read();
const mo = new MutationObserver(read);
mo.observe(document.documentElement, { attributes: true, attributeFilter: ["class"] });
mo.observe(document.body, { attributes: true, attributeFilter: ["class"] });
return () => mo.disconnect();
}, [ref]);
return dark;
}
/* Sample data for a small recipe site: four sources and the visits that bounced, made up in code. */
const SERIES = ["Search", "Direct", "Social", "Newsletter"];
function seeded(seed: number) {
let s = seed;
return () => ((s = (s * 16807) % 2147483647) - 1) / 2147483646;
}
function build(n: number, base: number[], wave: (i: number) => number, seed: number, label: (i: number) => string): TrafficBarsPoint[] {
const r = seeded(seed);
return Array.from({ length: n }, (_, i) => {
const k = wave(i);
const values = base.map((b) => Math.round(b * k * (0.78 + r() * 0.44)));
const sum = values.reduce((a, b) => a + b, 0);
return { label: label(i), values, bounced: Math.round(sum * (0.22 + r() * 0.16)) };
});
}
const DAYS = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
const MONTHS: [string, number][] = [["Nov", 30], ["Dec", 31], ["Jan", 31], ["Feb", 28], ["Mar", 31]];
/** "Nov 4 – 10" style label for the week that starts `d` days after Nov 1. */
function weekLabel(d: number) {
const at = (n: number): [string, number] => {
let m = 0;
while (n >= MONTHS[m][1]) n -= MONTHS[m++][1];
return [MONTHS[m][0], n + 1];
};
const [m0, d0] = at(d);
const [m1, d1] = at(d + 6);
return m0 === m1 ? `${m0} ${d0} – ${d1}` : `${m0} ${d0} – ${m1} ${d1}`;
}
const RANGES: TrafficBarsRange[] = [
{
id: "day",
label: "Day",
subtitle: "Last 20 days",
start: "Mar 3",
end: "Mar 22",
// weekends bake more: Saturday and Sunday run high
points: build(20, [420, 260, 180, 120], (i) => [0.82, 0.78, 0.8, 0.86, 0.96, 1.32, 1.24][i % 7], 7, (i) => `${DAYS[i % 7]}, Mar ${3 + i}`),
},
{
id: "week",
label: "Week",
subtitle: "Last 20 weeks",
start: "Nov 4",
end: "Mar 17",
// a slow climb into the holidays, a dip in January, then spring
points: build(20, [2900, 1800, 1250, 860], (i) => 0.78 + 0.32 * Math.sin((i / 19) * Math.PI * 1.15) + i * 0.012, 11, (i) => weekLabel(3 + i * 7)),
},
];
const LIGHT = {
page: "#f1f1f3",
surface: "#ffffff",
edge: "rgba(0, 0, 0, 0.06)",
ink: "#0a0a0a",
muted: "#66666e",
colors: ["#8f63ef", "#5580e8", "#93b0d3", "#eabe66"],
bounce: "#d42e46",
bars: { background: "transparent", surface: "#ffffff", color: "#0a0a0a", muted: "#606066", faint: "#98989e", track: "#efeff1", line: "#e5e6ea", lens: "#727272", accent: "#8f63ef" },
};
const DARK = {
page: "#101012",
surface: "#19191c",
edge: "rgba(255, 255, 255, 0.07)",
ink: "#f4f4f5",
muted: "#a1a1aa",
colors: ["#9c76f5", "#6690f0", "#86a2c6", "#eec46f"],
bounce: "#e5475e",
bars: { background: "transparent", surface: "#19191c", color: "#f4f4f5", muted: "#a1a1aa", faint: "#71717a", track: "#26262a", line: "#2c2c31", lens: "#a1a1aa", accent: "#9c76f5" },
};
const fmt = (n: number) => Math.round(n).toLocaleString("en-US");
/**
* A traffic dashboard tile. The chart switches Day and Week and plays a short tour; hover or drag
* across the bars (or use the arrow keys) and the Sources panel follows through onHover. Colours,
* series names and data all come in through props, with a light and a dark palette.
*/
export default function TrafficBarsDemo() {
const ref = React.useRef<HTMLElement>(null);
const dark = useDarkClass(ref);
const P = dark ? DARK : LIGHT;
const [range, setRange] = React.useState("week");
const [point, setPoint] = React.useState<TrafficBarsPoint | null>(null);
const active = RANGES.find((r) => r.id === range)!;
const values = point ? point.values : SERIES.map((_, j) => active.points.reduce((a, p) => a + p.values[j], 0));
const bounced = point ? point.bounced : active.points.reduce((a, p) => a + p.bounced, 0);
const total = values.reduce((a, b) => a + b, 0);
return (
<main ref={ref} className="relative flex h-screen min-h-[340px] w-full items-center justify-center overflow-hidden bg-background p-3">
<section
className="flex items-stretch rounded-[28px]"
style={{ background: P.page, boxShadow: `inset 0 0 0 1px ${P.edge}` }}
>
<div style={{ width: 354, height: 324 }}>
<TrafficBars
title="Visitors"
unit="visits"
ranges={RANGES}
value={range}
onValueChange={setRange}
onHover={(_, p) => setPoint(p)}
seriesNames={SERIES}
colors={P.colors}
bounceName="Bounced"
bounceColor={P.bounce}
autoplay
{...P.bars}
style={{ minHeight: 0 }}
/>
</div>
{/* the chart box is 8px wider than the card on every side, so the panel gets the same inset */}
<aside
className="my-2 mr-2 hidden w-[232px] flex-col rounded-[21px] px-[18px] py-[17px] sm:flex"
style={{ background: P.surface, color: P.ink }}
aria-live="polite"
>
<p className="text-[16px] font-medium leading-none">Sources</p>
<p className="mt-[7px] truncate text-[13px] leading-none" style={{ color: P.muted }}>
{point ? point.label : active.subtitle}
</p>
<ul className="mt-[22px] flex flex-col gap-[16px]">
{SERIES.map((name, j) => {
const share = total ? values[j] / total : 0;
return (
<li key={name}>
<div className="flex items-center text-[13px] leading-none">
<span className="mr-2 h-[9px] w-[9px] rounded-[2.5px]" style={{ background: P.colors[j] }} />
<span style={{ color: P.muted }}>{name}</span>
<span className="ml-auto font-medium tabular-nums">{fmt(values[j])}</span>
</div>
<div className="mt-[7px] h-[4px] rounded-full" style={{ background: dark ? "#26262a" : "#efeff1" }}>
<div
className="h-full rounded-full transition-[width] duration-300 ease-out"
style={{ width: `${Math.max(2, share * 100)}%`, background: P.colors[j] }}
/>
</div>
</li>
);
})}
</ul>
<div className="mt-auto flex items-center border-t pt-[12px] text-[13px] leading-none" style={{ borderColor: dark ? "#2c2c31" : "#ececef" }}>
<span className="mr-2 h-[9px] w-[9px] rounded-[2.5px]" style={{ background: P.bounce }} />
<span style={{ color: P.muted }}>Bounced</span>
<span className="ml-auto font-medium tabular-nums">{total ? Math.round((bounced / total) * 100) : 0}%</span>
</div>
</aside>
</section>
</main>
);
}








