Skip to contents

Avatar

An easily stylable avatar component.

LT

Anatomy

Import the component and assemble its parts:

Anatomy

Optimized and lazy-loaded images

By default, <Avatar.Image> preloads src and renders the image only once it has loaded. This doesn’t compose with image optimizers such as next/image, which serve a different URL than the raw src, or with loading="lazy".

Add the keepMounted prop to render the image element right away and let it load in place. Only the image that is actually displayed is requested:

Using next/image

Stacking

With keepMounted, the image and the fallback are both present until the image loads. The image is hidden from assistive technology until then, so the fallback provides the accessible name on its own.

Stack the two in the same box, and place <Avatar.Image> after <Avatar.Fallback>. Both are positioned, so whichever comes later in the DOM paints on top. The fallback then shows through until the image covers it.

A loading image paints nothing, so the fallback shows through on its own. An image that failed to load paints a broken-image icon on top of it. Hide the image in either state with the data-loading and data-error attributes:

Stacked image and fallback

Avoid display: none here: an element without a box never intersects the viewport, so loading="lazy" would never fetch the image. visibility and opacity both keep lazy loading working.

Server rendering

With keepMounted, the image is part of the server-rendered HTML and starts loading before hydration. So is the fallback, which stays visible until hydration resolves the loading status. A cached image is displayed immediately, without an enter animation.

API reference

Root

Displays a user’s profile picture, initials, or fallback icon. Renders a <span> element.

classNamestring | function
Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
styleReact.CSSProperties | function
Name
Description

Style applied to the element, or a function that returns a style object based on the component’s state.

Type
renderReactElement | function
Name
Description

Allows you to replace the component’s HTML element with a different tag, or compose it with another component.

Accepts a ReactElement or a function that returns the element to render.

Type

Avatar.Root.PropsHide

Re-Export of Root props as AvatarRootProps

Avatar.Root.StateHide

Image

The image to be displayed in the avatar. Renders an <img> element.

onLoadingStatusChangefunction
Description

Callback fired when the loading status changes.

Type
classNamestring | function
Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
styleReact.CSSProperties | function
Name
Description

Style applied to the element, or a function that returns a style object based on the component’s state.

Type
keepMountedbooleanfalse
Description

Whether the image element stays mounted and loads in place instead of being preloaded. Supports loading="lazy" and optimized image components such as next/image.

Type
Default
false
renderReactElement | function
Name
Description

Allows you to replace the component’s HTML element with a different tag, or compose it with another component.

Accepts a ReactElement or a function that returns the element to render.

Type
data-error

Present when the image failed to load.

data-loading

Present while the image is loading.

data-starting-style

Present when the image begins animating in.

data-ending-style

Present when the image is animating out.

Attribute
Description
data-error

Present when the image failed to load.

data-loading

Present while the image is loading.

data-starting-style

Present when the image begins animating in.

data-ending-style

Present when the image is animating out.

Avatar.Image.PropsHide

Re-Export of Image props as AvatarImageProps

Avatar.Image.StateHide

Fallback

Rendered when the image fails to load or when no image is provided. Renders a <span> element.

delaynumber0
Name
Description

How long to wait before showing the fallback. Specified in milliseconds.

Type
Default
0
classNamestring | function
Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
styleReact.CSSProperties | function
Name
Description

Style applied to the element, or a function that returns a style object based on the component’s state.

Type
renderReactElement | function
Name
Description

Allows you to replace the component’s HTML element with a different tag, or compose it with another component.

Accepts a ReactElement or a function that returns the element to render.

Type

Avatar.Fallback.PropsHide

Re-Export of Fallback props as AvatarFallbackProps

Avatar.Fallback.StateHide

Additional types

ImageLoadingStatusHide