Data display
Avatar
People are circular initials on a soft tint (24 in tables, 32 in lists, 40 in headers). Companies are square marks.
Preview
+2
import { Avatar, AvatarGroup, CompanyMark } from "@/components/ui/avatar";
export default function Example() {
return (
<div className="flex flex-col gap-5">
<div className="flex items-center gap-3">
<Avatar name="Jordan Blake" size="sm" />
<Avatar name="Jordan Blake" size="md" />
<Avatar name="Jordan Blake" size="lg" />
</div>
<div className="flex items-center gap-2">
{(["blue", "green", "amber", "rose", "violet", "slate", "teal"] as const).map((c) => (
<Avatar key={c} name="Sarah Chen" color={c} />
))}
</div>
<AvatarGroup names={["Sarah Chen", "Daniel Brooks", "Aisha Bello", "Oliver Grant", "Ryan Walsh", "Leah Moss"]} />
<div className="flex items-center gap-3">
<CompanyMark name="Ledgerly" size="sm" />
<CompanyMark name="Ledgerly" size="md" />
<CompanyMark name="Ledgerly" size="lg" />
<CompanyMark name="Kinto" size="lg" color="#0f766e" />
</div>
</div>
);
}Status and verified
Presence dot bottom-right (online · away · busy · offline), verified check top-right. Six sizes from 20 to 56px.
import { Avatar } from "@/components/ui/avatar";
export default function Example() {
return (
<div className="flex flex-col items-center gap-6">
<div className="flex items-end gap-4">
<Avatar name="Sarah Chen" size="xs" status="online" />
<Avatar name="Sarah Chen" size="sm" status="online" />
<Avatar name="Sarah Chen" size="md" status="away" />
<Avatar name="Sarah Chen" size="lg" status="busy" />
<Avatar name="Sarah Chen" size="xl" status="offline" />
<Avatar name="Sarah Chen" size="2xl" status="online" />
</div>
<div className="flex items-end gap-4">
<Avatar name="Jordan Blake" size="md" verified />
<Avatar name="Jordan Blake" size="lg" verified />
<Avatar name="Jordan Blake" size="xl" verified status="online" />
</div>
</div>
);
}Installation
Components are copied into your project — no runtime package.
Create a
avatar.tsxfile and paste the following code into it.src/components/ui/avatar.tsximport { cx } from "@/utils/cx"; export type AvatarColor = "blue" | "green" | "amber" | "rose" | "violet" | "slate" | "teal"; export type AvatarSize = "xs" | "sm" | "md" | "lg" | "xl" | "2xl"; export type AvatarStatus = "online" | "away" | "busy" | "offline"; const colors: Record<AvatarColor, string> = { blue: "bg-avatar-blue-bg text-avatar-blue-fg", green: "bg-avatar-green-bg text-avatar-green-fg", amber: "bg-avatar-amber-bg text-avatar-amber-fg", rose: "bg-avatar-rose-bg text-avatar-rose-fg", violet: "bg-avatar-violet-bg text-avatar-violet-fg", slate: "bg-avatar-slate-bg text-avatar-slate-fg", teal: "bg-avatar-teal-bg text-avatar-teal-fg", }; // size/avatar-sm|md|lg — 24 in tables, 32 in lists, 40 in headers. const sizes: Record<AvatarSize, string> = { xs: "size-5 text-[8px]", sm: "size-6 text-[9px]", md: "size-8 text-xs", lg: "size-10 text-base", xl: "size-12 text-lg", "2xl": "size-14 text-xl", }; // Indicator diameter scales with the avatar. const indicator: Record<AvatarSize, string> = { xs: "size-1.5 ring-1", sm: "size-2 ring-[1.5px]", md: "size-2.5 ring-2", lg: "size-3 ring-2", xl: "size-3.5 ring-2", "2xl": "size-4 ring-[2.5px]", }; const statusColor: Record<AvatarStatus, string> = { online: "bg-success", away: "bg-warning", busy: "bg-error", offline: "bg-fg-disabled", }; const palette = Object.keys(colors) as AvatarColor[]; function initialsOf(name: string) { return name .split(/\s+/) .filter(Boolean) .slice(0, 2) .map((p) => p[0]!.toUpperCase()) .join(""); } /** Stable color per name, so the same person always gets the same tint. */ function colorFor(name: string): AvatarColor { let h = 0; for (const ch of name) h = (h * 31 + ch.charCodeAt(0)) >>> 0; return palette[h % palette.length]!; } export interface AvatarProps { name: string; src?: string; size?: AvatarSize; color?: AvatarColor; /** Presence dot, bottom-right. */ status?: AvatarStatus; /** Verified check, top-right. */ verified?: boolean; className?: string; } /** People are circular initials on a soft tint, or a photo. Optional presence and verified marks. */ export function Avatar({ name, src, size = "md", color, status, verified, className }: AvatarProps) { const face = ( <span title={name} className={cx( "inline-flex shrink-0 items-center justify-center overflow-hidden rounded-full font-semibold", sizes[size], !src && colors[color ?? colorFor(name)], className, )} > {src ? ( // eslint-disable-next-line @next/next/no-img-element <img src={src} alt={name} className="size-full object-cover" /> ) : ( <span aria-hidden>{initialsOf(name)}</span> )} </span> ); if (!status && !verified) return face; return ( <span className="relative inline-flex shrink-0"> {face} {status && ( <span role="img" aria-label={status} className={cx("absolute bottom-0 right-0 rounded-full ring-surface", indicator[size], statusColor[status])} /> )} {verified && ( <span role="img" aria-label="Verified" className={cx("absolute -right-0.5 -top-0.5 flex items-center justify-center rounded-full bg-accent text-white ring-surface", indicator[size])}> <svg viewBox="0 0 12 12" className="size-[70%]" aria-hidden> <path d="M3 6.2 5 8l4-4.5" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" /> </svg> </span> )} </span> ); } export function AvatarGroup({ names, size = "sm", max = 4 }: { names: string[]; size?: AvatarSize; max?: number }) { const shown = names.slice(0, max); const rest = names.length - shown.length; return ( <div className="flex -space-x-1.5"> {shown.map((n) => ( <Avatar key={n} name={n} size={size} className="ring-2 ring-surface" /> ))} {rest > 0 && ( <span className={cx("inline-flex items-center justify-center rounded-full bg-muted font-medium text-fg-secondary ring-2 ring-surface", sizes[size])}> +{rest} </span> )} </div> ); } export interface CompanyMarkProps { name: string; size?: AvatarSize; /** Brand color for the square mark. Defaults to inverse. */ color?: string; className?: string; } /** Companies are square marks with a brand color. */ export function CompanyMark({ name, size = "md", color, className }: CompanyMarkProps) { return ( <span title={name} style={color ? { backgroundColor: color } : undefined} className={cx( "inline-flex shrink-0 items-center justify-center bg-inverse font-semibold text-fg-inverse", size === "xs" || size === "sm" ? "size-6 rounded-[6px] text-[10px]" : size === "md" ? "size-8 rounded-sm text-sm" : "size-10 rounded-[10px] text-base", className, )} > <span aria-hidden>{name[0]?.toUpperCase()}</span> </span> ); }Add the theme tokens and the
cxhelper from Installation if you haven’t yet, then update the import paths to match your project setup.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| name | string | — | Used for initials, the title and a stable color. |
| src | string | — | Photo URL. Falls back to initials. |
| size | "xs" | "sm" | "md" | "lg" | "xl" | "2xl" | "md" | 20, 24, 32, 40, 48 or 56px. |
| status | "online" | "away" | "busy" | "offline" | — | Presence indicator. |
| verified | boolean | — | Verified check. |
| color | "blue" | "green" | "amber" | "rose" | "violet" | "slate" | "teal" | — | Overrides the color picked from the name. |