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
Use RootProvider with the useAvatar hook to access the state and methods from outside the component.
AvatarStack
Stack the first 3 avatars. The +N overflow is a Menu.Trigger that lists everyone else.
Guides
Next.js Image
Render the Image component from next/image inside the avatar.
import { Avatar, useAvatarContext } from '@ark-ui/react/avatar'
import { getImageProps, type ImageProps } from 'next/image'
const AvatarNextImage = (props: ImageProps) => {
const avatar = useAvatarContext()
const { hidden, ...arkImageProps } = avatar.getImageProps()
const nextImage = getImageProps(props)
return (
<img
{...arkImageProps}
{...nextImage.props}
style={{
...props.style,
// use visibility instead
visibility: hidden ? 'hidden' : 'visible',
}}
/>
)
}
const Demo = () => {
return (
<Avatar.Root>
<Avatar.Fallback>JD</Avatar.Fallback>
<AvatarNextImage src="..." alt="" width={80} height={80} />
</Avatar.Root>
)
}
See this GitHub Discussion for details.
API Reference
Props
Root
Defines the <span> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
ids | ElementIdsThe ids of the elements in the avatar. Useful for composition. | |
onStatusChange | (details: StatusChangeDetails) => voidFunctional called when the image loading status changes. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-avatar-root] | <uid> |
[data-status] |
Fallback
Defines the <span> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-avatar-fallback] | <uid> |
[data-state] | "hidden" | "visible" |
Image
Defines the <img> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-avatar-image] | <uid> |
[data-state] | "visible" | "hidden" |
RootProvider
Defines the <span> element.
| Attribute | Default | Type |
|---|---|---|
value | UseAvatarReturn | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
Context
API
| Property | Type |
|---|---|
status | LoadStatusThe current image loading status. |
loaded | booleanWhether the image is loaded. |
setLoaded | VoidFunctionFunction to set loaded state. |
setError | VoidFunctionFunction to set error state. |
getRootState | () => RootStateReturns the state of the root. |