Avatar
Avatar is a component that displays a profile picture or an icon with initials representing a user.
Import
import { Avatar, AvatarBadge, AvatarGroup } from "@yamada-ui/react"
Avatar: A component that displays an image representing a user.AvatarBadge: A component that displays content in the corner of an avatar.AvatarGroup: A wrapper component for stacking multiple avatars.
Usage
Editable example
<Avatar name="Hirotomo Yamada" src="https://avatars.githubusercontent.com/u/84060430?v=4" />
Change Size
Editable example
<Wrap gap="md"> <For each={["2xs", "xs", "sm", "md", "lg", "xl", "2xl"]}> {(size, index) => <Avatar size={size} name="Hirotomo Yamada" key={index} />} </For> </Wrap>
Display Initials
When you set a string to name, it generates a random background color based on the initials and the provided string.
If you want to format the initials, use format.
If you want to customize the background color, specify the color with bg, background, or backgroundColor.
Editable example
<Wrap gap="md"> <Avatar name="Hirotomo Yamada" /> <Avatar name="Hirotomo Yamada" format={(name) => { const names = name.split(" ") const firstName = names[0] || "" const lastName = names.length > 1 ? names[names.length - 1] : "" return firstName && lastName ? `${lastName.charAt(0)}${firstName.charAt(0)}` : firstName.charAt(0) }} /> </Wrap>
Display an Image
If you want to display an image in the avatar, set the path to src.
Editable example
<Avatar src="https://avatars.githubusercontent.com/u/84060430?v=4" />
Add a Badge
In some applications, you may want to show whether a user is online or not. In that case, use the AvatarBadge component.
You can change the position of the badge by setting placement. The default is "bottom-end".
Editable example
<Wrap gap="md"> <For each={[ { color: "primary" }, { color: "secondary", placement: "top-start" }, { color: "green", placement: "top-end" }, { color: "pink", placement: "bottom-start" }, { color: "red", placement: "bottom-end" }, ]} > {({ color, placement }, key) => ( <Avatar name="Hirotomo Yamada" src="https://avatars.githubusercontent.com/u/84060430?v=4" > <AvatarBadge bg={color} placement={placement} /> </Avatar> )} </For> </Wrap>
Highlight the Badge
Editable example
<Avatar name="Hirotomo Yamada" src="https://avatars.githubusercontent.com/u/84060430?v=4" > <AvatarBadge bg="primary" ping pingColor="primary.300" pingScale={2} /> </Avatar>
Style the Badge
Editable example
<Avatar name="Hirotomo Yamada" src="https://avatars.githubusercontent.com/u/84060430?v=4" > <AvatarBadge bg="red" borderColor="white" /> </Avatar>
Fallback
There are two fallbacks if the loading of src fails:
- If
nameis provided: It uses a random background color based on the initials and the provided string. - If
nameis not provided: It uses the default avatar icon.
Editable example
<Wrap gap="md"> <Avatar name="Hirotomo Yamada" src="https://avatars.githubusercontent.com/u/84060430?v=4" /> <Avatar name="Hirotomo Yamada" src="https://not-found.com" /> <Avatar src="https://not-found.com" /> </Wrap>
Customize the Fallback
Here is an example of customizing the fallback icon and background color.
Editable example
<Wrap gap="md"> <Avatar bg="secondary" src="https://not-found.com" /> <Avatar icon={<GhostIcon />} src="https://not-found.com" /> </Wrap>
Avatar Group
If you want to control the number of avatars displayed, specify a number with max. If there are more avatars than the specified number, it truncates and displays the remaining avatars as +X.
Editable example
<AvatarGroup max={3}> <Avatar name="Hirotomo Yamada" src="https://avatars.githubusercontent.com/u/84060430?v=4" /> <Avatar name="Hirotomo Yamada" src="https://avatars.githubusercontent.com/u/84060430?v=4" /> <Avatar name="Hirotomo Yamada" src="https://avatars.githubusercontent.com/u/84060430?v=4" /> <Avatar name="Hirotomo Yamada" src="https://avatars.githubusercontent.com/u/84060430?v=4" /> <Avatar name="Hirotomo Yamada" src="https://avatars.githubusercontent.com/u/84060430?v=4" /> </AvatarGroup>
Edit this page on GitHub

