Avatar
Pictures for people and workspaces. Shows initials when there is no image or the image fails. Built with Base UI.
Installation
pnpm dlx shadcn@latest add https://sprout.tulipfarm.ai/r/avatar.jsonCopies Avatar into your app, installs the packages, and adds the styles. Then import it from @/components/ui/avatar. Needs Tailwind CSS 4 and a components.json from shadcn init.
Install the following dependencies:
pnpm add clsx tailwind-merge @base-ui/reactAdd Sprout base once per app: the
cn()helper and the design tokens.lib/sprout/classes.ts- Design tokens, for your global CSS
Copy and paste the following code into your project.
'use client'; import type { ComponentProps, CSSProperties } from 'react'; import { Avatar as AvatarPrimitive } from '@base-ui/react/avatar'; import { cn, stateClassName } from '@/lib/sprout/classes'; import { hash } from '@/lib/sprout/hash'; export type AvatarSize = 'sm' | 'md' | 'lg'; export type EntityAvatarSize = AvatarSize; export type EntityAvatarShape = 'circle' | 'square'; export interface AvatarProps extends AvatarPrimitive.Root.Props { size?: AvatarSize; } export function Avatar({ className, size = 'md', ...props }: AvatarProps) { return ( <AvatarPrimitive.Root data-slot="avatar" data-size={size} {...props} className={stateClassName<AvatarPrimitive.Root.State>('sprout-avatar', className)} /> ); } export type AvatarImageProps = AvatarPrimitive.Image.Props; export function AvatarImage({ className, ...props }: AvatarImageProps) { return ( <AvatarPrimitive.Image data-slot="avatar-image" {...props} className={stateClassName<AvatarPrimitive.Image.State>('sprout-avatar-image', className)} /> ); } export type AvatarFallbackProps = AvatarPrimitive.Fallback.Props; export function AvatarFallback({ className, ...props }: AvatarFallbackProps) { return ( <AvatarPrimitive.Fallback data-slot="avatar-fallback" {...props} className={stateClassName<AvatarPrimitive.Fallback.State>('sprout-avatar-fallback', className)} /> ); } export function AvatarBadge({ className, ...props }: ComponentProps<'span'>) { return <span data-slot="avatar-badge" {...props} className={cn('sprout-avatar-badge', className)} />; } export function AvatarGroup({ className, ...props }: ComponentProps<'div'>) { return <div data-slot="avatar-group" {...props} className={cn('sprout-avatar-group', className)} />; } export function AvatarGroupCount({ className, ...props }: ComponentProps<'div'>) { return <div data-slot="avatar-group-count" {...props} className={cn('sprout-avatar-group-count', className)} />; } function initial(name: string) { return name.trim().charAt(0).toUpperCase() || '?'; } function gradientFor(seed: string) { const h = hash(seed); const from = h % 360; const to = (from + 40 + ((h >>> 9) % 60)) % 360; return `linear-gradient(135deg, oklch(0.62 0.15 ${from}), oklch(0.5 0.15 ${to}))`; } export interface EntityAvatarProps extends Omit<AvatarPrimitive.Root.Props, 'children'> { seed: string; name: string; image?: string | null | undefined; size?: EntityAvatarSize; shape?: EntityAvatarShape; } export function EntityAvatar({ seed, name, image, size = 'md', shape = 'circle', className, ...props }: EntityAvatarProps) { return ( <AvatarPrimitive.Root data-slot="entity-avatar" data-size={size} data-shape={shape} {...props} className={stateClassName<AvatarPrimitive.Root.State>('sprout-entity-avatar', className)} > {image && ( <AvatarPrimitive.Image src={image} alt="" referrerPolicy="no-referrer" className="sprout-avatar-image" /> )} <AvatarPrimitive.Fallback className="sprout-entity-avatar-fallback" style={{ backgroundImage: gradientFor(seed) } as CSSProperties} > {initial(name)} </AvatarPrimitive.Fallback> </AvatarPrimitive.Root> ); }And the files it imports:
lib/sprout/hash.ts
Add the styles to your global CSS, after
@import "tailwindcss".@layer components { .sprout-avatar, .sprout-entity-avatar { --sprout-avatar-size: 2rem; width: var(--sprout-avatar-size); height: var(--sprout-avatar-size); font-family: var(--sprout-font); font-size: var(--sprout-text-sm); user-select: none; border-radius: 9999px; flex-shrink: 0; line-height: 1; display: flex; position: relative; } .sprout-avatar:after { content: ""; border: 1px solid var(--sprout-line); border-radius: inherit; mix-blend-mode: darken; pointer-events: none; position: absolute; inset: 0; } .dark .sprout-avatar:after { mix-blend-mode: lighten; } @media (prefers-color-scheme: dark) { :root:not(.light):not(.dark) .sprout-avatar:after { mix-blend-mode: lighten; } } :is(.sprout-avatar, .sprout-entity-avatar)[data-size="sm"] { --sprout-avatar-size: 1.5rem; font-size: var(--sprout-text-xs); } .sprout-avatar[data-size="lg"] { --sprout-avatar-size: 2.5rem; } .sprout-entity-avatar[data-size="lg"] { --sprout-avatar-size: 2.5rem; font-size: var(--sprout-text-base); } .sprout-entity-avatar { overflow: hidden; } .sprout-entity-avatar[data-shape="square"] { border-radius: .5rem; } .sprout-entity-avatar[data-shape="square"][data-size="sm"] { border-radius: .25rem; } .sprout-avatar-image { aspect-ratio: 1; border-radius: inherit; object-fit: cover; width: 100%; height: 100%; transition: opacity var(--sprout-motion-normal, .18s) var(--sprout-motion-ease, ease); } .sprout-avatar-image[data-starting-style], .sprout-avatar-image[data-ending-style] { opacity: 0; } .sprout-avatar-fallback, .sprout-entity-avatar-fallback { border-radius: inherit; justify-content: center; align-items: center; width: 100%; height: 100%; display: flex; } .sprout-avatar-fallback { background: linear-gradient(var(--sprout-hover), var(--sprout-hover)) var(--sprout-avatar-ring, var(--sprout-canvas)); color: var(--sprout-muted); } .sprout-entity-avatar-fallback { color: oklch(100% 0 0); font-weight: 600; } .sprout-avatar-badge { z-index: 1; background: var(--sprout-primary); width: .625rem; height: .625rem; color: var(--sprout-on-primary); box-shadow: 0 0 0 2px var(--sprout-avatar-ring, var(--sprout-canvas)); user-select: none; border-radius: 9999px; justify-content: center; align-items: center; display: inline-flex; position: absolute; bottom: 0; right: 0; } .sprout-avatar-badge > svg, .sprout-avatar[data-size="sm"] > .sprout-avatar-badge { width: .5rem; height: .5rem; } .sprout-avatar[data-size="sm"] > .sprout-avatar-badge > svg { display: none; } .sprout-avatar[data-size="lg"] > .sprout-avatar-badge { width: .75rem; height: .75rem; } .sprout-avatar-group { display: flex; } .sprout-avatar-group > * + * { margin-inline-start: -.5rem; } .sprout-avatar-group > :is(.sprout-avatar, .sprout-entity-avatar, .sprout-avatar-group-count) { box-shadow: 0 0 0 2px var(--sprout-avatar-ring, var(--sprout-canvas)); } .sprout-avatar-group-count { background: linear-gradient(var(--sprout-hover), var(--sprout-hover)) var(--sprout-avatar-ring, var(--sprout-canvas)); width: 2rem; height: 2rem; color: var(--sprout-muted); font-family: var(--sprout-font); font-size: var(--sprout-text-sm); border-radius: 9999px; flex-shrink: 0; justify-content: center; align-items: center; line-height: 1; display: flex; position: relative; } .sprout-avatar-group-count > svg { width: 1rem; height: 1rem; } .sprout-avatar-group:has( > [data-size="sm"]) > .sprout-avatar-group-count { width: 1.5rem; height: 1.5rem; font-size: var(--sprout-text-xs); } .sprout-avatar-group:has( > [data-size="sm"]) > .sprout-avatar-group-count > svg { width: .75rem; height: .75rem; } .sprout-avatar-group:has( > [data-size="lg"]) > .sprout-avatar-group-count { width: 2.5rem; height: 2.5rem; } .sprout-avatar-group:has( > [data-size="lg"]) > .sprout-avatar-group-count > svg { width: 1.25rem; height: 1.25rem; } }Import it from
@/components/ui/avatar. Update the paths if your@/alias points elsewhere.
pnpm add @tulipfarm/sprout@import "tailwindcss";
@import "@tulipfarm/sprout/styles.css";The package keeps the source and ships fixes with each release. Import from @tulipfarm/sprout/avatar.
Image status: idle
Usage
import {
Avatar, AvatarImage, AvatarFallback, AvatarBadge,
} from "@tulipfarm/sprout/avatar";
<Avatar size="lg">
<AvatarImage src={user.image} alt={user.name} />
<AvatarFallback>AM</AvatarFallback>
<AvatarBadge role="img" aria-label="Verified">
<CheckIcon aria-hidden="true" />
</AvatarBadge>
</Avatar>Avatar takes size: "sm" (24px), "md" (32px, the default), or "lg" (40px). The fallback shows while the image loads, when it fails, and when there is no image. Usedelay on AvatarFallback to avoid a flash on fast networks. The image fades in over 180ms; reduced motion turns this off.
AvatarBadge sits on the bottom edge. Its icon hides at small sizes.AvatarGroup overlaps avatars, including EntityAvatar, and AvatarGroupCount matches their size. Set --sprout-avatar-ring to the surface color behind a group or badge.
Agent avatars
For agents, use AgentAvatar. It picks built-in artwork from the agent's id.
Entity avatars
<EntityAvatar
seed={workspace.id}
name={workspace.name}
image={workspace.icon}
size="sm"
shape="square"
/>EntityAvatar takes a seed, name, optional image, size ("sm", "md", or "lg"), and shape ("circle" or "square"). Without an image it shows the first letter on a gradient. The same seed always gives the same gradient. An empty name shows ?.
Member photo
Workspace icon
- AAlex Meadow
- RRobin Field
- SSam Orchard
- MMember (not loaded)
- AAlex Meadow
- RRobin Field
- [email protected]
In conversations
Image upload, file checks, menus, tables, and messages above are sample code. The package only draws the avatar. Your application owns data, uploads, and navigation.
Accessibility
Give AvatarImage real alt text when the avatar is the only name on screen. Use alt="" when a name is next to it. Avatars are not focusable; put them inside a link or button for actions. Give badges and icon counts an accessible label.