Avatar
A person's image with initials fallback, sizes and groups.
Give AvatarFallback the person's name and it takes one of six tints, the same one wherever that name appears. The fallback shows until the image has loaded, and stays when there is no image or it fails.
Initials
IQPLAFTRWANF?
import { Avatar, AvatarFallback } from "@/components/ui/avatar"
const people = [
{ name: "Ines Quill", initials: "IQ" },
{ name: "Pax Linden", initials: "PL" },
{ name: "Arlo Finch", initials: "AF" },
{ name: "Tam Rook", initials: "TR" },
{ name: "Wren Alder", initials: "WA" },
{ name: "Nia Fern", initials: "NF" },
]
export default function AvatarInitials() {
return (
<div className="flex flex-wrap items-center gap-3">
{people.map((person) => (
<Avatar key={person.name} role="img" aria-label={person.name}>
<AvatarFallback name={person.name}>{person.initials}</AvatarFallback>
</Avatar>
))}
<Avatar role="img" aria-label="Unassigned">
<AvatarFallback>?</AvatarFallback>
</Avatar>
</div>
)
}
Sizes
xssmdefaultrowlgimport { Avatar, AvatarFallback } from "@/components/ui/avatar"
const sizes = ["xs", "sm", "default", "row", "lg"] as const
export default function AvatarSizes() {
return (
<div className="flex flex-wrap items-end gap-6">
{sizes.map((size) => (
<div key={size} className="flex flex-col items-center gap-2">
<Avatar size={size} aria-hidden>
<AvatarFallback name="Arlo Finch">AF</AvatarFallback>
</Avatar>
<code className="text-xs text-muted-foreground">{size}</code>
</div>
))}
</div>
)
}
With an image
- AFArlo Finch
- TRTam Rook
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"
// A picture drawn here, in the colours of the sky tint, so the example needs no file.
const drawing = `data:image/svg+xml,${encodeURIComponent(
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 40 40"><rect width="40" height="40" fill="#dff1f1"/><circle cx="20" cy="15" r="7" fill="#1a5556"/><path d="M5 40a15 15 0 0 1 30 0z" fill="#1a5556"/></svg>',
)}`
const people = [
{ name: "Arlo Finch", initials: "AF", picture: drawing },
{ name: "Tam Rook", initials: "TR", picture: undefined },
]
export default function AvatarWithAnImage() {
return (
<ul className="flex flex-col gap-3">
{people.map((person) => (
<li key={person.name} className="flex items-center gap-3">
<Avatar size="lg">
<AvatarImage src={person.picture} alt="" />
<AvatarFallback name={person.name}>{person.initials}</AvatarFallback>
</Avatar>
{person.name}
</li>
))}
</ul>
)
}
With a badge
WAIQPL
import { CheckIcon } from "@keyline-icons/react"
import { Avatar, AvatarBadge, AvatarFallback } from "@/components/ui/avatar"
export default function AvatarWithABadge() {
return (
<div className="flex flex-wrap items-center gap-4">
<Avatar size="sm" role="img" aria-label="Wren Alder, online">
<AvatarFallback name="Wren Alder">WA</AvatarFallback>
<AvatarBadge />
</Avatar>
<Avatar role="img" aria-label="Ines Quill, online">
<AvatarFallback name="Ines Quill">IQ</AvatarFallback>
<AvatarBadge />
</Avatar>
<Avatar size="lg" role="img" aria-label="Pax Linden, verified">
<AvatarFallback name="Pax Linden">PL</AvatarFallback>
<AvatarBadge>
<CheckIcon />
</AvatarBadge>
</Avatar>
</div>
)
}
Group
IQTRWA
+4
IQTRWA
+4
import { Avatar, AvatarFallback, AvatarGroup, AvatarGroupCount } from "@/components/ui/avatar"
const reviewers = [
{ name: "Ines Quill", initials: "IQ" },
{ name: "Tam Rook", initials: "TR" },
{ name: "Wren Alder", initials: "WA" },
]
export default function AvatarGroupExample() {
return (
<div className="flex flex-col items-center gap-4">
{(["sm", "default"] as const).map((size) => (
<AvatarGroup key={size} role="img" aria-label="Reviewers: Ines Quill, Tam Rook, Wren Alder and 4 more">
{reviewers.map((reviewer) => (
<Avatar key={reviewer.name} size={size}>
<AvatarFallback name={reviewer.name}>{reviewer.initials}</AvatarFallback>
</Avatar>
))}
<AvatarGroupCount>+4</AvatarGroupCount>
</AvatarGroup>
))}
</div>
)
}
Installation
npx shadcn@latest add @khaeli/avatarbunx --bun shadcn@latest add @khaeli/avatarInstall @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.
Installs with
- Registry items
- npm packages
- @base-ui/react ^1.7.0
Accessible name
- Initials alone are read as letters. An avatar that stands for a person with no name written beside it takes
role="img"and anaria-labelwith the name. - Beside the written name the avatar is decoration: hide it with
aria-hidden, and giveAvatarImagean emptyalt. AvatarImagealways needsalt: the name when the avatar stands alone, empty when the name is beside it.AvatarBadgehas no text. Say what it means in the avatar's label: Wren Alder, online.- Name an
AvatarGroupas one image that lists who is in it, or name each avatar. The text ofAvatarGroupCount, such as +4, is read as written.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/avatar.json