Traffic Sources
Stacked share bar and source list where hovering or focusing a row highlights its segment.
"use client"
import { useState } from "react"
import { cn } from "cn"
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card"
const COPY = {
title: "Traffic sources",
description: "Where visitors came from this month",
total: "Total visits",
}
const SOURCES = [
{ id: "search", label: "Organic search", visits: 18420, color: "bg-primary" },
{ id: "direct", label: "Direct", visits: 11260, color: "bg-primary/70" },
{ id: "social", label: "Social", visits: 6840, color: "bg-primary/45" },
{ id: "email", label: "Email", visits: 4210, color: "bg-primary/30" },
{ id: "referral", label: "Referral", visits: 2970, color: "bg-primary/15" },
] as const
const TOTAL = SOURCES.reduce((sum, s) => sum + s.visits, 0)
/** Stacked share bar with a source list; hover or focus a row to highlight it. */
export default function MetricsTrafficSources01() {
const [active, setActive] = useState<string | null>(null)
return (
<Card className="mx-auto w-full max-w-xl">
<CardHeader>
<CardTitle>
<h2>{COPY.title}</h2>
</CardTitle>
<CardDescription>{COPY.description}</CardDescription>
</CardHeader>
<CardContent className="flex flex-col gap-5">
<div>
<p className="text-xs text-muted-foreground">{COPY.total}</p>
<p className="text-2xl font-semibold tabular-nums">
{TOTAL.toLocaleString("en-US")}
</p>
</div>
<div
className="flex h-3 gap-0.5 overflow-hidden rounded-full"
aria-hidden
>
{SOURCES.map((s) => (
<div
key={s.id}
className={cn(
"h-full transition-opacity",
s.color,
active && active !== s.id && "opacity-30"
)}
style={{ width: `${(s.visits / TOTAL) * 100}%` }}
/>
))}
</div>
<ul className="flex flex-col">
{SOURCES.map((s) => (
<li key={s.id}>
<button
type="button"
onMouseEnter={() => setActive(s.id)}
onMouseLeave={() => setActive(null)}
onFocus={() => setActive(s.id)}
onBlur={() => setActive(null)}
className="flex w-full items-center gap-3 rounded-lg px-2 py-2 text-left text-sm transition-colors hover:bg-muted focus-visible:ring-3 focus-visible:ring-ring/50 focus-visible:outline-none"
>
<span
className={cn("size-2.5 shrink-0 rounded-full", s.color)}
/>
<span className="flex-1">{s.label}</span>
<span className="text-muted-foreground tabular-nums">
{s.visits.toLocaleString("en-US")}
</span>
<span className="w-12 text-right tabular-nums">
{((s.visits / TOTAL) * 100).toFixed(1)}%
</span>
</button>
</li>
))}
</ul>
</CardContent>
</Card>
)
}
Installation
pnpm dlx shadcn@latest add @01orbital/traffic-sources-01npx shadcn@latest add @01orbital/traffic-sources-01yarn dlx shadcn@latest add @01orbital/traffic-sources-01bunx --bun shadcn@latest add @01orbital/traffic-sources-01Make it yours
- Its words are in
COPYat the top ofcomponents/blocks/stats/traffic-sources-01/index.tsx: change them there, not in the markup. SOURCESandTOTAL, also at the top of the file, hold its lists and settings.
Built from
npm: cn. The CLI installs it 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