Avatar
A graphical representation of the user, often used in profile sections.
Anatomy
To set up the avatar correctly, you'll need to understand its anatomy and how we name its parts.
Each part includes a
data-partattribute to help identify them in the DOM.
Examples
Learn how to use the Avatar component in your project. Let's take a look at the most basic example:
import { Avatar } from '@ark-ui/react/avatar'
export const Basic = () => (
<Avatar.Root>
<Avatar.Fallback>PA</Avatar.Fallback>
<Avatar.Image src="https://i.pravatar.cc/300" alt="avatar" />
</Avatar.Root>
)
import { Avatar } from '@ark-ui/solid/avatar'
export const Basic = () => (
<Avatar.Root>
<Avatar.Fallback>PA</Avatar.Fallback>
<Avatar.Image src="https://i.pravatar.cc/300" alt="avatar" />
</Avatar.Root>
)
<script setup lang="ts">
import { Avatar } from '@ark-ui/vue/avatar'
</script>
<template>
<Avatar.Root>
<Avatar.Fallback>PA</Avatar.Fallback>
<Avatar.Image src="https://i.pravatar.cc/3000" alt="avatar" />
</Avatar.Root>
</template>
Handling Events
Avatar allows you to listen for loading state changes.
import { Avatar } from '@ark-ui/react/avatar'
export const Events = () => {
const handleStatusChange = (details: Avatar.StatusChangeDetails) => {
console.log(details.status)
}
return (
<Avatar.Root onStatusChange={handleStatusChange}>
<Avatar.Fallback>PA</Avatar.Fallback>
<Avatar.Image src="https://i.pravatar.cc/3000" alt="avatar" />
</Avatar.Root>
)
}
import { Avatar } from '@ark-ui/solid/avatar'
export const Events = () => {
const handleStatusChange = (details: Avatar.StatusChangeDetails) => {
console.log(details.status)
}
return (
<Avatar.Root onStatusChange={handleStatusChange}>
<Avatar.Fallback>PA</Avatar.Fallback>
<Avatar.Image src="https://i.pravatar.cc/3000" alt="avatar" />
</Avatar.Root>
)
}
<script setup lang="ts">
import { Avatar } from '@ark-ui/vue/avatar'
</script>
<template>
<Avatar.Root @status-change="(e) => console.log(e.status)">
<Avatar.Fallback>PA</Avatar.Fallback>
<Avatar.Image src="https://i.pravatar.cc/3000" alt="avatar" />
</Avatar.Root>
</template>
Using the Root Provider
The RootProvider component provides a context for the avatar. It accepts the value of the useAvatar hook. You can
leverage it to access the component state and methods from outside the avatar.
import { Avatar, useAvatar } from '@ark-ui/react/avatar'
export const RootProvider = () => {
const avatar = useAvatar()
return (
<>
<button onClick={() => avatar.setSrc('https://avatars.githubusercontent.com/u/6916170?v=4')}>
Change Source
</button>
<Avatar.RootProvider value={avatar}>
<Avatar.Fallback>PA</Avatar.Fallback>
<Avatar.Image src="https://avatars.githubusercontent.com/u/1846056?v=4" alt="avatar" />
</Avatar.RootProvider>
</>
)
}
import { Avatar, useAvatar } from '@ark-ui/solid/avatar'
export const RootProvider = () => {
const avatar = useAvatar()
return (
<>
<button onClick={() => avatar().setSrc('https://new-source.com')}>Change Source</button>
<Avatar.RootProvider value={avatar}>
<Avatar.Fallback>PA</Avatar.Fallback>
<Avatar.Image src="https://i.pravatar.cc/300" alt="avatar" />
</Avatar.RootProvider>
</>
)
}
<script setup lang="ts">
import { Avatar, useAvatar } from '@ark-ui/vue/avatar'
const avatar = useAvatar()
</script>
<template>
<button @click="avatar.setSrc('https://new-source.com')">Change Source</button>
<Avatar.RootProvider :value="avatar">
<Avatar.Fallback>PA</Avatar.Fallback>
<Avatar.Image src="https://i.pravatar.cc/3000" alt="avatar" />
</Avatar.RootProvider>
</template>
If you're using the
RootProvidercomponent, you don't need to use theRootcomponent.
Next.js Image
Here's an example of how to use the Image component from next/image.
import { Avatar, useAvatarContext } from '@ark-ui/react/avatar'
import Image, { ImageProps } from 'next/image'
const AvatarNextImage = (props: ImageProps) => {
const avatar = useAvatarContext()
const { hidden, ...imageProps } = avatar.getImageProps() as ImageProps
return (
<Image
{...imageProps}
{...props}
style={{
...props.style,
visibility: hidden ? 'hidden' : 'visible',
objectFit: 'cover',
}}
/>
)
}
const Demo = () => {
return (
<Avatar.Root>
<Avatar.Fallback>JD</Avatar.Fallback>
<AvatarNextImage src="..." width={80} height={80} />
</Avatar.Root>
)
}
Refer to this Github Discussion for more information.