Ark Logo
Undefined
Components
Avatar

Avatar

A graphical representation of the user, often used in profile sections.

JD

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-part attribute 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:

Handling Events

Avatar allows you to listen for loading state changes.

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.

If you're using the RootProvider component, you don't need to use the Root component.

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.

API Reference