Device Split
Donut chart of sessions by desktop, mobile and tablet with a selectable legend.
"use client"
import { useState } from "react"
import {
DeviceMobileIcon,
DeviceTabletIcon,
LaptopIcon,
} from "@phosphor-icons/react"
import { cn } from "cn"
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card"
const COPY = {
title: "Devices",
description: "Sessions by device type, last 30 days",
}
const DEVICES = [
{
id: "desktop",
label: "Desktop",
share: 54,
sessions: 68240,
icon: LaptopIcon,
stroke: "var(--primary)",
},
{
id: "mobile",
label: "Mobile",
share: 38,
sessions: 48010,
icon: DeviceMobileIcon,
stroke: "color-mix(in oklab, var(--primary) 55%, transparent)",
},
{
id: "tablet",
label: "Tablet",
share: 8,
sessions: 10105,
icon: DeviceTabletIcon,
stroke: "color-mix(in oklab, var(--primary) 25%, transparent)",
},
] as const
const R = 52
const C = 2 * Math.PI * R
/** Donut chart of sessions by device with a selectable legend. */
export default function MetricsDeviceSplit01() {
const [selected, setSelected] = useState<string>("desktop")
const current = DEVICES.find((d) => d.id === selected) ?? DEVICES[0]
let offset = 0
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 items-center gap-6 sm:flex-row">
<div className="relative size-40 shrink-0">
<svg
viewBox="0 0 140 140"
className="size-full -rotate-90"
role="img"
aria-label="Device share donut"
>
<circle
cx="70"
cy="70"
r={R}
fill="none"
stroke="var(--muted)"
strokeWidth="8"
/>
{DEVICES.map((d) => {
const len = (d.share / 100) * C
const el = (
<circle
key={d.id}
cx="70"
cy="70"
r={R}
fill="none"
stroke={d.stroke}
strokeWidth={selected === d.id ? 10 : 8}
strokeDasharray={`${len - 2} ${C - len + 2}`}
strokeDashoffset={-offset}
className="transition-all"
/>
)
offset += len
return el
})}
</svg>
<div className="absolute inset-0 flex flex-col items-center justify-center">
<p className="text-2xl font-semibold tabular-nums">
{current.share}%
</p>
<p className="text-xs text-muted-foreground">{current.label}</p>
</div>
</div>
<ul className="flex w-full flex-col gap-1">
{DEVICES.map((d) => (
<li key={d.id}>
<button
type="button"
aria-pressed={selected === d.id}
onClick={() => setSelected(d.id)}
className={cn(
"flex w-full items-center gap-3 rounded-lg px-3 py-2 text-left text-sm transition-colors hover:bg-muted focus-visible:ring-3 focus-visible:ring-ring/50 focus-visible:outline-none",
selected === d.id && "bg-muted"
)}
>
<d.icon className="size-4 text-muted-foreground" />
<span className="flex-1">{d.label}</span>
<span className="text-muted-foreground tabular-nums">
{d.sessions.toLocaleString("en-US")}
</span>
</button>
</li>
))}
</ul>
</CardContent>
</Card>
)
}
Installation
pnpm dlx shadcn@latest add @01orbital/device-split-01npx shadcn@latest add @01orbital/device-split-01yarn dlx shadcn@latest add @01orbital/device-split-01bunx --bun shadcn@latest add @01orbital/device-split-01Make it yours
- Its words are in
COPYat the top ofcomponents/blocks/charts/device-split-01/index.tsx: change them there, not in the markup. DEVICES,RandC, also at the top of the file, hold its lists and settings.
Built from
npm: @phosphor-icons/react, cn. The CLI installs them 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