Skip to Content

Avatar

A visual representation of a user or entity in the product.

JD

Usage

import { Avatar, AvatarGroup } from "@opengovsg/oui"
<Avatar name="John Doe">
  <Avatar.Image src="https://example.com/avatar.jpg" />
  <Avatar.Fallback />
</Avatar>

OUI exports 2 avatar-related components:

  • Avatar: The main component to display an avatar. It has compound components Avatar.Image and Avatar.Fallback.
  • AvatarGroup: A wrapper component to display a group of avatars.

Examples

Sizes

Use the size prop to change the size of the avatar. Available sizes are 2xs, xs, sm, and md.

2
X
S
M

Radius

Use the radius prop to change the border radius of the avatar.

N
S
M
L
F

Colors

Use the prominence and color props to customize the avatar's appearance.

  • prominence: strong (filled background) or subtle (light background)
  • color: primary or white
SP
SP
SW

Avatar Fallbacks

If the image fails to load, the avatar will display a fallback. There are 2 fallback options:

  1. If there's a name prop, the initials are generated from the name.
  2. If there's no name prop, a default user icon is displayed.
John Doe
JD

Custom Fallback

You can provide a custom fallback component to be displayed when the image fails to load.

Custom Initials Logic

You can customize the logic used to generate initials by passing a function to the getInitials prop. By default, we use the first character of each word in the name prop.

<Avatar
  name="John Doe"
  getInitials={(name) =>
    name
      .split(" ")
      .map((n) => n[0])
      .join("")
      .toUpperCase()
  }
>
  <Avatar.Image src="https://example.com/avatar.jpg" />
  <Avatar.Fallback />
</Avatar>

Avatar Group

Use the AvatarGroup component to display a group of avatars together.

John Doe
JD
Jane Smith
JS
Bruce Wayne
BW
Bob Wilson
BW

Max Count

You can limit the number of avatars displayed by passing the max prop to the AvatarGroup component.

JD
JS
BW
+1

Total Count

You can display the total number of avatars by passing the total prop to the AvatarGroup component. This is useful when you have more avatars than what you're displaying.

John Doe
JD
Jane Smith
JS
Bruce Wayne
BW
+10

Custom Count

AvatarGroup provides a renderCount prop to customize the count displayed.

John Doe
JD
Jane Smith
JS
Bruce Wayne
BW

+10 others

Slots

Avatar Slots

  • base: Avatar wrapper, includes styles for position and general appearance.
  • image: Image element within the avatar, includes styles for opacity transition and size.
  • fallback: Fallback content when the image fails to load, includes styles for centering.
  • icon: Icon element within the avatar fallback.

AvatarGroup Slots

  • base: The wrapper for the avatar group.
  • count: The count avatar element.

Data Attributes

Avatar has the following attributes on the base element:

  • data-loaded: When the avatar image has loaded successfully.

API Reference

Avatar Props

PropTypeDefaultDescription
childrenReactNode-The avatar content (Image and Fallback)
namestring-The name used for initials and alt text
size"2xs" | "xs" | "sm" | "md""md"The size of the avatar
radius"none" | "sm" | "md" | "lg" | "full""full"The border radius of the avatar
prominence"strong" | "subtle""strong"The visual prominence of the avatar
color"primary" | "white""primary"The color scheme of the avatar
getInitials(name: string) => string-Custom function to generate initials from name
classNamesRecord<Slot, string>-Classes to apply to each slot

Avatar.Image Props

PropTypeDescription
srcstringThe image source URL
...-All native img attributes

Avatar.Fallback Props

PropTypeDescription
childrenReactNodeCustom fallback content (defaults to icon/initials)

AvatarGroup Props

PropTypeDefaultDescription
childrenReactNode-The avatars to display
maxnumber5Maximum number of visible avatars
totalnumber-Total count (for calculating overflow)
size"2xs" | "xs" | "sm" | "md"-Size applied to all avatars
color"primary" | "white"-Color applied to all avatars
prominence"strong" | "subtle"-Prominence applied to all avatars
radius"none" | "sm" | "md" | "lg" | "full"-Radius applied to all avatars
renderCount(count: number) => ReactNode-Custom render function for overflow count
classNamesRecord<"base" | "count", string>-Classes to apply to each slot

Templates

Usage with Menu

You can use the Avatar component as a trigger for a Menu to create a user profile menu.