Skip to content
Thrum

Avatar

A picture of a person, a team or a product. While the image loads, when it fails and when there is none, it shows the initials of the name. A mark in the corner can show presence. For several people in a row use the avatar group.

Install

npx shadcn@latest add https://thrumui.dev/r/avatar.json

Adds components/ui/avatar.tsx and installs @base-ui/react@1.9.0. Nothing else in your project changes.

import { initials } from "@/components/ui/avatar";

Automatically installs icon user from the icon set.

Props

PropTypeDefaultDescription
Avatar: namestring–Whose avatar it is. It becomes the image's alt text, and its initials are the fallback.
Avatar: size"xs" | "sm" | "md" | "lg" | "xl"Default "md"20, 24, 32, 40 or 56 pixels. Inside an avatar group the group's size applies unless this is set.
Avatar: squarebooleanDefault falseSquare with the corners of a control, for a team or a product. Without it the avatar is round, or square in a project whose radius is 0.
Avatar: status"online" | "away" | "busy" | "offline"–A presence mark at the bottom end corner: a disc, a crescent, a disc with a bar or a ring.
Avatar: statusLabelstringDefault "Online", "Away", "Busy", "Offline"The words a screen reader hears for the status. Set it to translate them.
AvatarImage: srcstring–The picture. It is drawn once it has loaded.
AvatarImage: altstringDefault the avatar's nameThe text alternative. Set an empty string when the name is printed next to the avatar.
AvatarImage: onLoadingStatusChange(status: "idle" | "loading" | "loaded" | "error") => void–Called when the image starts loading, loads or fails.
AvatarFallback: childrenReactNode–What shows without a picture, in place of the initials: an icon, other letters.
AvatarFallback: delaynumberDefault 0Milliseconds to wait before the fallback shows, so a fast image never flashes initials first.
classNamestring–On every part: extra classes, merged last.
initials()(name: string, max?: number) => string–A second export: the upper case first letters of the first and last word of a name. initials("Ana de Souza") is “AS”.

Motion and sound

Motion

  • The avatar does not move. The image replaces the fallback in one step when it has loaded.

Sound

Silent. This component makes no sound.

Accessibility

  • The image carries the name as its alt text. The fallback shows the initials to the eye and gives the full name to screen readers.
  • When the name is also printed beside the avatar, hide one of the two from screen readers, so it is read once.
  • The four states of the presence mark differ in shape as well as colour, and each has a hidden word: “Online”, “Away”, “Busy”, “Offline”.
  • Initials are the theme's text colour on its muted colour. The mark has a ring of the page colour, so it stays clear of the picture.
  • It takes no focus. To make it a control, put it inside a button or a link that has a name.

Source

components/ui/avatar.tsx
"use client";

import { Avatar as AvatarPrimitive } from "@base-ui/react/avatar";
import { createContext, useContext } from "react";

import { cn } from "@/lib/utils";
import { UserIcon } from "@/components/icons/user";
import { STATUS } from "@/lib/thrum-chart";

export type AvatarSize = "xs" | "sm" | "md" | "lg" | "xl";
export type AvatarStatus = "online" | "away" | "busy" | "offline";

export type AvatarProps = AvatarPrimitive.Root.Props & {
  /** Whose avatar it is. It is the image's text alternative, and its initials are the fallback. */
  name?: string;
  /** `xs` is 20 pixels, `sm` 24, `md` 32, `lg` 40, `xl` 56. Inside an avatar group the group's size is used unless this is set. */
  size?: AvatarSize;
  /** Square with the corners of a control, for a team or a product. Without it the avatar is round. */
  square?: boolean;
  /** A presence mark at the bottom corner. Each state has its own shape as well as its own colour. */
  status?: AvatarStatus;
  /** The words a screen reader hears for the status. They default to "Online", "Away", "Busy" and "Offline". */
  statusLabel?: string;
};

/** The size an avatar group hands to the avatars inside it. */
export const AvatarSizeContext = createContext<AvatarSize | undefined>(undefined);
const NameContext = createContext<string | undefined>(undefined);

/** The box and the fallback text of each size. An avatar group draws its "+3" item with the same classes. */
export const AVATAR_SIZE: Record<AvatarSize, string> = {
  xs: "size-5 text-[0.5625rem]",
  sm: "size-6 text-[0.625rem]",
  md: "size-8 text-xs",
  lg: "size-10 text-sm",
  xl: "size-14 text-lg",
};

const MARK_SIZE: Record<AvatarSize, string> = { xs: "size-1.5", sm: "size-2", md: "size-2.5", lg: "size-3", xl: "size-4" };
const STATUS_WORD: Record<AvatarStatus, string> = { online: "Online", away: "Away", busy: "Busy", offline: "Offline" };
const STATUS_COLOR: Record<AvatarStatus, string> = { online: STATUS.ok, away: STATUS.warn, busy: STATUS.alarm, offline: "var(--muted-foreground)" };

/**
 * Up to two initials of a name: the first letter of the first word and of the last one, in upper case.
 * Punctuation is skipped, one word gives one letter, and a name with no letter or digit gives an empty string.
 */
export function initials(name: string, max = 2): string {
  const letters = name
    .trim()
    .split(/\s+/)
    .map((word) => /[\p{L}\p{N}]/u.exec(word)?.[0] ?? "")
    .filter(Boolean);
  // More words than room: keep the last one, the family name in most Western names.
  const picked = max > 1 && letters.length > max ? [...letters.slice(0, max - 1), letters[letters.length - 1]] : letters.slice(0, Math.max(0, max));
  return picked.join("").toLocaleUpperCase();
}

/**
 * A picture of a person, a team or a product. Put an `AvatarImage` and an `AvatarFallback` inside:
 * the fallback shows until the image has loaded, and stays when it fails or there is none.
 */
export function Avatar({ name, size, square = false, status, statusLabel, className, children, ...props }: AvatarProps) {
  const inherited = useContext(AvatarSizeContext);
  const resolved = size ?? inherited ?? "md";
  return (
    <AvatarPrimitive.Root
      data-slot="avatar"
      data-size={resolved}
      className={cn(
        "relative inline-flex shrink-0 align-middle font-medium select-none",
        square ? "rounded-md" : "rounded-[calc(var(--radius)*9999)]",
        AVATAR_SIZE[resolved],
        className,
      )}
      {...props}
    >
      <NameContext value={name}>{children}</NameContext>
      {status && (
        <span data-slot="avatar-status" data-status={status} className={cn("absolute end-0 bottom-0 rounded-full ring-2 ring-background", MARK_SIZE[resolved])}>
          {/* One shape per state: a disc, a crescent, a disc with a bar, a ring. */}
          <svg viewBox="0 0 10 10" className="block size-full rounded-full bg-background" fill={STATUS_COLOR[status]} aria-hidden="true">
            {status === "offline" ? <circle cx={5} cy={5} r={3.75} fill="none" stroke={STATUS_COLOR[status]} strokeWidth={2.5} /> : <circle cx={5} cy={5} r={5} />}
            {status === "away" && <circle cx={3} cy={3} r={3} fill="var(--background)" />}
            {status === "busy" && <rect x={2} y={4} width={6} height={2} fill="var(--background)" />}
          </svg>
          <span className="sr-only">{statusLabel ?? STATUS_WORD[status]}</span>
        </span>
      )}
    </AvatarPrimitive.Root>
  );
}

/** The picture. It is drawn once it has loaded; until then, and if it fails, the fallback shows. Its `alt` is the avatar's `name` unless you set one. */
export function AvatarImage({ className, alt, ...props }: AvatarPrimitive.Image.Props) {
  const name = useContext(NameContext);
  return (
    <AvatarPrimitive.Image
      data-slot="avatar-image"
      alt={alt ?? name ?? ""}
      className={cn("size-full rounded-[inherit] object-cover transition-opacity duration-150 ease-out motion-reduce:transition-none", className)}
      {...props}
    />
  );
}

/**
 * What shows without a picture: the initials of the avatar's `name`, or the children you give it.
 * With neither it draws the outline of a person.
 */
export function AvatarFallback({ className, children, ...props }: AvatarPrimitive.Fallback.Props) {
  const name = useContext(NameContext);
  const letters = name ? initials(name) : "";
  return (
    <AvatarPrimitive.Fallback
      data-slot="avatar-fallback"
      className={cn("grid size-full place-items-center overflow-hidden rounded-[inherit] bg-foreground/12 leading-none text-foreground ring-1 ring-inset ring-foreground/22", className)}
      {...props}
    >
      {children ??
        (letters ? (
          <>
            <span aria-hidden="true">{letters}</span>
            <span className="sr-only">{name}</span>
          </>
        ) : (
          <>
            <UserIcon className="size-[60%]" />
            {name && <span className="sr-only">{name}</span>}
          </>
        ))}
    </AvatarPrimitive.Fallback>
  );
}

Files and dependencies

Adds
components/ui/avatar.tsx
Packages
@base-ui/react@1.9.0
Brings along
Chart foundations
Icons
user

Changelog

  1. 1.0.0 · 2026-12-03First release.

Loading index…

Keyboard shortcuts

/Ctrl KSearch components, docs and actions
?Show this list
AAppearance studio
SSound on or off
MFull or reduced motion
TTheme: auto, light, dark
GSurface: solid or frosted
BBackdrop photograph (under glass)
CCorner radius: square to round
RReplay the demo, on a component page
DDraw new data, on a component page
←→HomeEndMove the cursor in a focused chart
EscClose a dialog, or leave a chart cursor