Profile card
A person card with a cover gradient drawn from one of the theme's chart colours, an initials avatar, name with optional verified mark, handle, role, location and bio. It shows three stats, a follow button that toggles optimistically (and rolls back if your handler rejects) while keeping the follower count in step, an optional message button, and icon links with accessible names.
Source
"use client";
import * as React from "react";
import { BadgeCheck, Check, MapPin, MessageCircle, UserPlus, type LucideIcon } from "lucide-react";
import { cn } from "@/lib/utils";
export interface ProfileLink {
label: string;
href: string;
icon: LucideIcon;
}
export interface ProfileStat {
label: string;
value: number;
}
export interface ProfileCardProps {
name: string;
handle?: string;
role?: string;
location?: string;
bio?: string;
initials?: string;
verified?: boolean;
/** Which theme chart colour (1–5) tints the cover. */
accent?: 1 | 2 | 3 | 4 | 5;
followers: number;
/** Extra stats shown after followers, e.g. following and projects. */
stats?: ProfileStat[];
defaultFollowing?: boolean;
/** Called after the optimistic toggle; reject to roll it back. */
onFollowChange?: (following: boolean) => void | Promise<void>;
onMessage?: () => void;
links?: ProfileLink[];
className?: string;
}
function formatCount(value: number) {
return value >= 10_000 ? new Intl.NumberFormat("en", { notation: "compact", maximumFractionDigits: 1 }).format(value) : value.toLocaleString("en");
}
function initialsOf(name: string) {
const parts = name.trim().split(/\s+/);
return ((parts[0]?.[0] || "") + (parts.length > 1 ? parts[parts.length - 1][0] : "")).toUpperCase();
}
const focusRing = "outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-card";
export function ProfileCard({
name,
handle,
role,
location,
bio,
initials,
verified = false,
accent = 1,
followers,
stats = [],
defaultFollowing = false,
onFollowChange,
onMessage,
links = [],
className,
}: ProfileCardProps) {
const [following, setFollowing] = React.useState(defaultFollowing);
const [pending, setPending] = React.useState(false);
const id = React.useId();
const color = `var(--chart-${accent})`;
const next = `var(--chart-${(accent % 5) + 1})`;
const count = followers + (following && !defaultFollowing ? 1 : 0) - (!following && defaultFollowing ? 1 : 0);
async function toggle() {
const value = !following;
setFollowing(value);
setPending(true);
try {
await onFollowChange?.(value);
} catch {
setFollowing(!value);
} finally {
setPending(false);
}
}
return (
<article aria-labelledby={`${id}-name`} className={cn("overflow-hidden rounded-xl border bg-card text-card-foreground shadow-xs", className)}>
<div
aria-hidden
className="relative h-24 overflow-hidden"
style={{ background: `linear-gradient(125deg, color-mix(in oklab, ${color} 70%, white) 0%, ${color} 45%, color-mix(in oklab, ${next} 75%, black) 100%)` }}
>
<svg viewBox="0 0 400 96" preserveAspectRatio="none" className="absolute inset-0 size-full text-white opacity-25 mix-blend-soft-light">
{[0, 1, 2, 3, 4, 5].map((i) => (
<path key={i} d={`M-10 ${70 - i * 14} C 80 ${40 - i * 12}, 160 ${110 - i * 14}, 250 ${60 - i * 12} S 380 ${30 - i * 10}, 420 ${50 - i * 12}`} fill="none" stroke="currentColor" strokeWidth="1.5" />
))}
</svg>
</div>
<div className="px-5 pb-5">
<div className="relative -mt-9 flex items-end justify-between gap-3">
<span
aria-hidden
className="grid size-[72px] shrink-0 place-items-center rounded-full text-xl font-semibold text-foreground ring-4 ring-card"
style={{ background: `color-mix(in oklab, ${color} 22%, var(--card))` }}
>
{initials || initialsOf(name)}
</span>
<div className="flex items-center gap-2 pb-0.5">
{onMessage && (
<button
type="button"
onClick={onMessage}
aria-label={`Message ${name}`}
className={cn("grid size-9 place-items-center rounded-md border bg-background text-muted-foreground hover:bg-accent hover:text-accent-foreground", focusRing)}
>
<MessageCircle aria-hidden className="size-4" />
</button>
)}
<button
type="button"
aria-pressed={following}
onClick={toggle}
disabled={pending}
className={cn(
"inline-flex h-9 min-w-28 items-center justify-center gap-1.5 rounded-md px-3.5 text-sm font-medium transition-colors disabled:cursor-wait",
focusRing,
following ? "border bg-background hover:bg-accent hover:text-accent-foreground" : "bg-primary text-primary-foreground hover:bg-primary/90",
)}
>
{following ? <Check aria-hidden className="size-4" /> : <UserPlus aria-hidden className="size-4" />}
{following ? "Following" : "Follow"}
</button>
</div>
</div>
<div className="mt-3">
<h3 id={`${id}-name`} className="flex items-center gap-1.5 text-lg font-semibold tracking-tight">
<span className="truncate">{name}</span>
{verified && (
<span title="Verified" className="text-sky-600 dark:text-sky-400">
<BadgeCheck aria-hidden className="size-[18px]" />
<span className="sr-only">Verified</span>
</span>
)}
</h3>
{handle && <p className="text-sm text-muted-foreground">@{handle}</p>}
</div>
{(role || location) && (
<p className="mt-3 flex flex-wrap items-center gap-x-3 gap-y-1 text-sm">
{role && <span>{role}</span>}
{location && (
<span className="inline-flex items-center gap-1 text-muted-foreground">
<MapPin aria-hidden className="size-3.5" />
{location}
</span>
)}
</p>
)}
{bio && <p className="mt-2 text-sm leading-relaxed text-muted-foreground">{bio}</p>}
<dl className="mt-4 grid grid-cols-3 divide-x rounded-lg border bg-muted/30">
{[{ label: "Followers", value: count }, ...stats].slice(0, 3).map((stat) => (
<div key={stat.label} className="flex flex-col-reverse items-center px-2 py-2.5 text-center">
<dt className="text-xs text-muted-foreground">{stat.label}</dt>
<dd className="text-sm font-semibold tabular-nums">{formatCount(stat.value)}</dd>
</div>
))}
</dl>
{links.length > 0 && (
<ul className="mt-4 flex items-center gap-1" aria-label={`${name} elsewhere`}>
{links.map((link) => (
<li key={link.label}>
<a
href={link.href}
aria-label={link.label}
title={link.label}
className={cn("grid size-8 place-items-center rounded-md text-muted-foreground transition-colors hover:bg-accent hover:text-accent-foreground", focusRing)}
>
<link.icon aria-hidden className="size-4" />
</a>
</li>
))}
</ul>
)}
</div>
</article>
);
}
