Avatar
A graphical representation of the user, often used in profile sections.
Anatomy
<Avatar.Root>
<Avatar.Fallback />
<Avatar.Image />
</Avatar.Root>
Examples
Basic
Display a user's profile image with a fallback.
Events
Use onStatusChange to listen for loading state changes.
Root Provider
An alternative way to control the avatar is to use the RootProvider component and the useAvatar hook. This way you
can access the state and methods from outside the component.
API Reference
Props
Root
Renders a <div> element.
| Prop | Default | Type |
|---|---|---|
asChild | Component<{ propsFn: (userProps?: Object) => Object }>Use the provided named component as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
ids | Partial<{ root: string; image: string; fallback: string }>The ids of the elements in the avatar. Useful for composition. | |
onStatusChange | (details: StatusChangeDetails) => voidFunctional called when the image loading status changes. |
Fallback
Renders a <span> element.
| Prop | Default | Type |
|---|---|---|
asChild | Component<{ propsFn: (userProps?: Object) => Object }>Use the provided named component as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
| Attribute | Description |
|---|---|
[data-scope] | avatar |
[data-part] | fallback |
[data-state] | "hidden" | "visible" |
Image
Renders a <img> element.
| Prop | Default | Type |
|---|---|---|
asChild | Component<{ propsFn: (userProps?: Object) => Object }>Use the provided named component as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
| Attribute | Description |
|---|---|
[data-scope] | avatar |
[data-part] | image |
[data-state] | "visible" | "hidden" |
RootProvider
Renders a <div> element.
| Prop | Default | Type |
|---|---|---|
value | UseAvatarReturn | |
asChild | Component<{ propsFn: (userProps?: Object) => Object }>Use the provided named component as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Context
API
| Property | Type |
|---|---|
loaded | booleanWhether the image is loaded. |
setSrc | (src: string) => voidFunction to set new src. |
setLoaded | VoidFunctionFunction to set loaded state. |
setError | VoidFunctionFunction to set error state. |