Avatar
Avatars are used to represent a user or an entity in the UI. They can be used in various contexts, such as user profiles, comments, or lists.
import {Avatar} from "@qualcomm-ui/react/avatar"Usage
Avatars can display a user or entity image, initials, or custom content when an image is unavailable. They can also indicate the status of the user or entity.
Overview
The anatomy of the avatar component is as follows:
Avatar.Root: The main component.Avatar.Image: (optional) Displays an image as the avatar.Avatar.Content: (optional) Displays alternate content, such as initials or an icon, and acts as a fallback if the image fails to load..Avatar.Status: (optional) Shows a status indicator dot.
Examples
Showcase
Here's a typical usage example:
<Avatar.Root status="active">
<Avatar.Image alt="John Doe" src="/images/avatar-man.png" />
<Avatar.Content>JD</Avatar.Content>
<Avatar.Status />
</Avatar.Root>Content
The Avatar.Content component displays alternative content within the avatar. Use it by itself to show initials or an icon, or together with Avatar.Image to provide fallback content if the image cannot be loaded.
Initials
<Avatar.Root>
<Avatar.Content>OK</Avatar.Content>
</Avatar.Root>
Icon
<Avatar.Root>
<Avatar.Content icon={User} />
</Avatar.Root>
Fallback
<Avatar.Root>
<Avatar.Image alt="John Doe" src="https://example.invalid" />
<Avatar.Content>JD</Avatar.Content>
</Avatar.Root>Sizes
Use the size prop to set the size of the avatar. The available sizes are xs, sm, md (default), lg, and xl.
{sizes.map((size) => (
<div key={size} className="flex flex-col items-center gap-2">
<Avatar.Root size={size}>
<Avatar.Image alt="Jane Doe" src="/images/avatar-woman.png" />
<Avatar.Content>JD</Avatar.Content>
</Avatar.Root>
{size}
</div>
))}Variants
When displaying initials or an icon, use the variant prop on Avatar.Content to set the avatar's style. Available variants are neutral (default), contrast, and brand.
Neutral
<Avatar.Root variant="neutral">
<Avatar.Content>OK</Avatar.Content>
</Avatar.Root>
High Contrast
<Avatar.Root variant="contrast">
<Avatar.Content>OK</Avatar.Content>
</Avatar.Root>
Brand
<Avatar.Root variant="brand">
<Avatar.Content>OK</Avatar.Content>
</Avatar.Root>Status
To indicate the status of the user or entity, use the Avatar.Status component and set its status prop to one of: active, offline (default), away, or busy.
Active
<Avatar.Root status="active">
<Avatar.Image alt="Jane Doe" src="/images/avatar-woman.png" />
<Avatar.Content>JD</Avatar.Content>
<Avatar.Status />
</Avatar.Root>
Offline
<Avatar.Root status="offline">
<Avatar.Image alt="Jane Doe" src="/images/avatar-woman.png" />
<Avatar.Content>JD</Avatar.Content>
<Avatar.Status />
</Avatar.Root>
Away
<Avatar.Root status="away">
<Avatar.Image alt="Jane Doe" src="/images/avatar-woman.png" />
<Avatar.Content>JD</Avatar.Content>
<Avatar.Status />
</Avatar.Root>
Busy
<Avatar.Root status="busy">
<Avatar.Image alt="Jane Doe" src="/images/avatar-woman.png" />
<Avatar.Content>JD</Avatar.Content>
<Avatar.Status />
</Avatar.Root>State Callback
You can listen for changes to the avatar's state by using the onStateChange prop.
import {type ReactElement, useState} from "react"
import {Avatar} from "@qualcomm-ui/react/avatar"
export default function AvatarStateCallbackDemo(): ReactElement {
const [currentStateValid, setCurrentStateValid] = useState<string | null>(
null,
)
const [currentStateInvalid, setCurrentStateInvalid] = useState<string | null>(
null,
)
return (
<div className="flex items-center gap-4">
<Avatar.Root onStateChange={(event) => setCurrentStateValid(event.state)}>
<Avatar.Image alt="John Doe" src="/images/avatar-man.png" />
<Avatar.Content>JD</Avatar.Content>
</Avatar.Root>
<output>current state: {currentStateValid}</output>
<Avatar.Root
onStateChange={(event) => setCurrentStateInvalid(event.state)}
>
<Avatar.Image alt="John Doe" src="https://example.invalid" />
<Avatar.Content>JD</Avatar.Content>
</Avatar.Root>
<output>current state: {currentStateInvalid}</output>
</div>
)
}
API
<Avatar.Root>
| Prop | Type | Default |
|---|---|---|
The document's text/writing direction. | 'ltr' | 'rtl' | 'ltr' |
id attribute. If
omitted, a unique identifier will be automatically generated for accessibility. | string | |
(event: { | ||
Allows you to replace the component's HTML element with a different tag or component. Learn more | | ReactElement | |
Governs the width and height of the avatar as well as the font size of its
content. | | 'xs' | |
Optional status for the avatar, renders a dot indicator next to the avatar. | | 'offline' | |
The style variant of the avatar. | | 'neutral' |
'ltr' | 'rtl'
string(event: {
state: string
}) => void
| ReactElement
| ((
props: object,
) => ReactElement)
| 'xs'
| 'sm'
| 'md'
| 'lg'
| 'xl'
| 'offline'
| 'active'
| 'away'
| 'busy'
| 'neutral'
| 'contrast'
| 'brand'
| Attribute / Property | Value |
|---|---|
className | 'qui-avatar__root' |
data-part | 'root' |
data-scope | 'avatar' |
data-size | | 'xs' |
className'qui-avatar__root'data-part'root'data-scope'avatar'data-size| 'xs'
| 'sm'
| 'md'
| 'lg'
| 'xl'
<Avatar.Image>
<img> element by default.| Prop | Type |
|---|---|
id attribute. If
omitted, a unique identifier will be automatically generated for accessibility. | string |
Allows you to replace the component's HTML element with a different tag or component. Learn more | | ReactElement |
string| ReactElement
| ((
props: object,
) => ReactElement)
| Attribute / Property | Value |
|---|---|
data-part | 'image' |
data-scope | 'avatar' |
data-state | | 'hidden' |
hidden | boolean |
data-part'image'data-scope'avatar'data-state| 'hidden'
| 'visible'
hiddenboolean<Avatar.Content>
<span> element by default.| Prop | Type |
|---|---|
Icon content. | | ReactNode |
id attribute. If
omitted, a unique identifier will be automatically generated for accessibility. | string |
Allows you to replace the component's HTML element with a different tag or component. Learn more | | ReactElement |
| ReactNode
| LucideIcon
string| ReactElement
| ((
props: object,
) => ReactElement)
| Attribute / Property | Value |
|---|---|
className | 'qui-avatar__content' |
data-part | 'content' |
data-scope | 'avatar' |
data-state | | 'hidden' |
data-variant | | 'neutral' |
hidden | boolean |
className'qui-avatar__content'data-part'content'data-scope'avatar'data-state| 'hidden'
| 'visible'
data-variant| 'neutral'
| 'contrast'
| 'brand'
hiddenboolean<Avatar.Status>
<div> element by
default.| Prop | Type |
|---|---|
id attribute. If
omitted, a unique identifier will be automatically generated for accessibility. | string |
Allows you to replace the component's HTML element with a different tag or component. Learn more | | ReactElement |
string| ReactElement
| ((
props: object,
) => ReactElement)
| Attribute / Property | Value |
|---|---|
className | 'qui-avatar__status' |
data-part | 'status' |
data-scope | 'avatar' |
data-status | | 'offline' |
className'qui-avatar__status'data-part'status'data-scope'avatar'data-status| 'offline'
| 'active'
| 'away'
| 'busy'
<span>element by default.