Skip to content

Avatar

Avatar

A circular picture of a person: their image, an icon, or the initials of their name.

- **Image**: image (URL string, element or { src, alt } object) or src. If the image fails to load, the avatar falls back to icon, the initials or a person glyph, with their background; a new image is tried again. A server-rendered image that failed before hydration is detected after mount (settled without a natural size, confirmed by decode()).
- **Accessible name**: an image uses name (whitespace collapsed) as its alt, or '' when the name is blank. Without an image (none given, or it failed to load) the avatar is role="img" with aria-label={name} (without a name: the image slot's own alt) and the initials/icon are hidden; without either (and without aria-label/aria-labelledby) it is decorative (aria-hidden). A consumer aria-label, aria-labelledby or role (other than presentation/none) puts the name on the avatar visual, and an image inside it gets alt="". decorative hides it in every mode. aria-describedby, aria-description and aria-details go to the element that carries the name: the <img> when a non-blank alt names it, otherwise the avatar visual (never an alt="" image).
- **Badge**: badge renders at the bottom end corner (logical, RTL-safe) as a sibling of the avatar visual, so a PresenceBadge keeps its own name. With a badge, ref, className and the rest props go to the outer wrapper.

Examples

Default

export const Default: Story = {};

With Image

Jane Doe
export const WithImage: Story = {
  args: {
    src: 'data:image/svg+xml,%3Csvg%20xmlns=%27http://www.w3.org/2000/svg%27%20viewBox=%270%200%2064%2064%27%3E%3Crect%20width=%2764%27%20height=%2764%27%20fill=%27%23c8c6c4%27/%3E%3Ccircle%20cx=%2732%27%20cy=%2725%27%20r=%2711%27%20fill=%27%23605e5c%27/%3E%3Cpath%20d=%27M12%2064a20%2020%200%200%201%2040%200z%27%20fill=%27%23605e5c%27/%3E%3C/svg%3E',
  },
};

Image Fallback

Jane Doe
export const ImageFallback: Story = {
  args: {
    src: 'data:image/png;base64,broken',
  },
};

With Name

export const WithName: Story = {
  args: {
    name: 'John Smith',
    size: 'large',
  },
};

With Icon

export const WithIcon: Story = {
  args: {
    name: undefined,
    icon: <PersonGlyph />,
    'aria-label': 'Guest',
  },
};

With Badge

export const WithBadge: Story = {
  args: {
    size: 'large',
    badge: <PresenceBadge status="available" />,
  },
};

Sizes

export const Sizes: Story = {
  render: (args) => (
    <div className="flex items-center gap-2">
      {SIZES.map((size) => (
        <Avatar key={size} {...args} size={size} />
      ))}
    </div>
  ),
};

Numeric Sizes

export const NumericSizes: Story = {
  render: (args) => (
    <div className="flex items-end gap-2">
      {NUMERIC_SIZES.map((size) => (
        <Avatar key={size} {...args} size={size} aria-label={`${args.name}, ${size} pixels`} />
      ))}
    </div>
  ),
};

Shapes

export const Shapes: Story = {
  args: { size: 56, badge: <PresenceBadge status="busy" /> },
  render: (args) => (
    <div className="flex items-center gap-3">
      <Avatar {...args} shape="circular" aria-label="Jane Doe, circular" />
      <Avatar {...args} shape="square" aria-label="Jane Doe, square" />
    </div>
  ),
};

Colors

export const Colors: Story = {
  render: (args) => (
    <div className="flex flex-wrap items-center gap-2">
      <Avatar {...args} color="neutral" aria-label="Neutral" />
      <Avatar {...args} color="brand" aria-label="Brand" />
      {NAMED_COLORS.map((color) => (
        <Avatar key={color} {...args} color={color} aria-label={color} />
      ))}
    </div>
  ),
};

Colorful From Name Or Id

export const ColorfulFromNameOrId: Story = {
  render: (args) => (
    <div className="flex items-center gap-2">
      {PEOPLE.map((name) => (
        <Avatar key={name} {...args} name={name} color="colorful" />
      ))}
      <Avatar {...args} name="Renamed Person" color="colorful" idForColor="user-1042" />
    </div>
  ),
};

Active And Inactive

export const ActiveAndInactive: Story = {
  args: { size: 56 },
  render: (args) => (
    <div className="flex items-center gap-4">
      {(['ring', 'ring-shadow', 'shadow', 'glow'] as const).map((appearance) => (
        <Avatar
          key={appearance}
          {...args}
          active="active"
          activeAppearance={appearance}
          aria-label={`Active, ${appearance}`}
        />
      ))}
      <Avatar {...args} active="inactive" aria-label="Inactive" />
    </div>
  ),
};

Initials

export const Initials: Story = {
  render: (args) => (
    <div className="flex items-center gap-2">
      {['Ada Lovelace', 'Plato', 'Mary-Jane O’Neil', 'Ola Nordmann (UiO)'].map((name) => (
        <Avatar key={name} {...args} name={name} color="colorful" />
      ))}
      <Avatar {...args} name="Custom" initials="XY" />
    </div>
  ),
};

Props

NameTypeRequiredDefaultDescription
srcstringNo

URL of the avatar image. When it fails to load (also before hydration, for server-rendered markup), the avatar falls back to its icon or initials; a new src is tried again.

namestringNo

Full name of the person. It names the avatar for assistive technology (the image alt; in initials and icon mode, or with a consumer role, the avatar's aria-label, with role="img" unless role is given) and generates the initials shown when there is no image.

size'extra-small' | 'small' | 'medium' | 'large' | 'extra-large' | 16 | 20 | 24 | 28 | 32 | 36 | 40 | 48 | 56 | 64 | 72 | 96 | 120 | 128No'medium'

Size of the avatar: a named size (extra-small 24px to extra-large 56px) or a pixel size from 16 to 128. Without it, an Avatar in the media of a tag (Tag, InteractionTag) takes the tag's media box instead: 24, 20 or 16px by the tag's size.

shape'circular' | 'square'No'circular'

Shape of the avatar. Without it, an Avatar in the media of a rounded tag is square.

color'neutral' | 'brand' | 'colorful' | 'red' | 'pumpkin' | 'gold' | 'forest' | 'teal' | 'blue' | 'navy' | 'lavender' | 'purple' | 'magenta' | 'brown' | 'steel'No'brand'

Color of the avatar when it shows initials: brand (the primary color), neutral (muted), colorful (one of the named colors, chosen from idForColor, else from name; brand without either) or a named color of the persona palette. An image, or an icon, keeps its own look.

active'active' | 'inactive' | 'unset'No'unset'

Whether the person is active: active draws the activeAppearance around the avatar, inactive dims it, unset shows neither.

activeAppearance'ring' | 'ring-shadow' | 'shadow' | 'glow'No'ring'

How an active avatar is marked: a ring, a shadow, both (ring-shadow) or a glow in the primary color.

idForColorstringNo

A stable id (a user id, say) that picks the color of color="colorful", so the same person has the same color wherever they are shown, whatever their name looks like.

initialsSlot<'span'>No

The initials shown when there is no image and no icon, instead of the ones computed from name (see getInitials): a string, an element or an object of <span> attributes. A value that renders nothing ('', []) keeps the computed initials. Rendered in an aria-hidden <span>.

iconSlot<'span'>No

Icon shown instead of the initials when there is no image. Rendered in an aria-hidden <span> (a slot object can override its attributes). A falsy value (null, false, '', 0, NaN, e.g. icon={count && <Icon />}) counts as no icon, as in 0.4, and so does an array, Set or generator whose items render nothing. Without icon and without a usable name, a generic person glyph is shown.

imageSlot<'img'>No

Custom image. Accepts a URL string (treated like src), an element (rendered as the image itself, e.g. <img> or a framework image component) or an object of <img> attributes ({ src, alt, … }). Takes precedence over src; its alt defaults to name (an alt that is undefined counts as not given). When its alt names the avatar, the avatar's aria-describedby ids are joined with the slot's own, while the slot's own aria-description and aria-details win over the avatar's. When the image fails to load, an avatar without a name keeps the name of the slot's own alt. After a load failure, a new image is tried again: another src or srcSet of any type (an equal inline object is the same image), or for an element slot another component type or key.

badgeSlot<'span'>No

Badge shown at the bottom end corner, e.g. <PresenceBadge status="busy" />. With a badge the avatar is wrapped in an outer <span> that receives ref, className and the other props; role, aria-label, aria-labelledby, aria-describedby, aria-description and aria-details stay on the avatar visual, so the badge keeps its own accessible name. A falsy value (null, false, '', 0, NaN, e.g. badge={count && <CounterBadge count={count} />}) counts as no badge, as in 0.4, and so does an array, Set or generator whose items render nothing: the avatar then renders without the wrapper.

decorativebooleanNofalse

Hides the avatar visual from assistive technology (aria-hidden, empty image alt, no role="img"). Use it when the name is already shown next to the avatar (Persona). A badge stays exposed.

refReact.Ref<HTMLSpanElement>No

Ref to the root <span>: the avatar itself, or the badge wrapper when badge is set.

slotProps{ visual?: SlotPart<'span'> }No

Props for the element the avatar draws, by part, merged into the part's own: your classes after the part's (yours win a conflict), your styles over its styles, your attributes and handlers added (yours run first), your ref beside its ref, and your ids added to its aria-labelledby and aria-describedby. Every attribute the avatar sets on the part is locked and keeps the avatar's value (set it through the avatar's own props, which win over these); you add the attributes it does not set. A part takes no children (the avatar draws its image, icon or initials); the image, the icon, the initials and the badge are slots of their own.

Any other prop is passed to the element it renders (HTMLSpanElement).

Parts

  • visual

Stable class names

PartClass
rootwave-avatar
visualwave-avatar__visual
imagewave-avatar__image
iconwave-avatar__icon
initialswave-avatar__initials
badgewave-avatar__badge