Avatar
The Avatar component is used to represent user, and displays the profile picture, initials or fallback icon.
Import#
Nature UI exports 3 avatar-related components:
Avatar
: The image that represents the user.AvatarBadge
: A wrapper that displays its content on the right corner of the avatar.AvatarGroup
: A wrapper to stack multiple Avatars together.
import { Avatar, AvatarBadge } from "@nature-ui/core";
Usage#
<Stack row><Avatar name="Dan Abrahmov" src="https://bit.ly/dan-abramov" /><Avatar name="Divine Nature" src="http://bit.ly/divine_n" /><Avatar name="Adam Wathan" src="https://bit.ly/adam-wathan" /><Avatar name="Segun Adebayo" src="https://bit.ly/sage-adebayo" /><Avatar /><Avatar name="Kent Dodds" src="https://bit.ly/kent-c-dodds" /><Avatar name="Ryan Florence" src="https://bit.ly/ryan-florence" /></Stack>
Avatar Sizes#
The Avatar
component comes in 7 sizes.
<Stack row><Avatar size="xs" name="Dan Abrahmov" src="https://bit.ly/dan-abramov" /><Avatar size="sm" name="Ryan Florence" src="https://bit.ly/ryan-florence" /><Avatar size="md" name="Segun Adebayo" src="https://bit.ly/sage-adebayo" /><Avatar size="lg" name="Kent Dodds" src="https://bit.ly/kent-c-dodds" /><Avatar size="xl" name="Adam Wathan" src="https://bit.ly/adam-wathan" /><Avatar size="2xl" name="Divine Nature" src="http://bit.ly/divine_n" /></Stack>
Avatar Fallbacks#
If there is an error loading the src
of the avatar, there are 2 fallbacks:
- If there's a
name
prop, we use it to generate the initials and a random, accessible background color. - If there's no
name
prop, we use a default avatar.
<Stack row><Avatar name="Kent Dodds" src="http://bit.ly/broken-link" /><Avatar name="Oshigaki Kisame" src="https://bit.ly/broken-link" /><Avatar src="https://bit.ly/broken-link" /></Stack>
Customize the fallback avatar#
You can customize the background color and icon of the fallback avatar icon to match your design requirements.
- To update the background, pass the usual
color
prop. - To update the icon svg, pass the
icon
prop.
<Stack row spacing={14}><Avatarcolor="bg-primary-500"icon={<AiOutlineUser color="white" size="2rem" />}/><Avatar color="bg-teal-500" /></Stack>
Avatar with badge#
In some products, you might need to show a badge on the right corner of the avatar. We call this a badge. Here's an example that shows if the user is online:
<Stack row spacing={8}><Avatar><AvatarBadge size="1.6em" className="bg-green-500" /></Avatar>{/* You can also change the borderColor and bg of the badge */}<Avatar><AvatarBadge className="bg-orange-500 border-blue-500" size="1.25em" /></Avatar></Stack>
AvatarGroup#
In some cases, you might need to stack avatars as a group. Use the AvatarGroup
component.
- To limit the amount of avatars to show, use the
max
prop. It'll truncate the avatars and show a "+X" label (where X is the remaining avatars). - To size all the avatars equally, pass the
size
prop. - To adjust the spacing between the avatars, pass the
spacing
prop.
<AvatarGroup size="md" max={2}><Avatar name="Divine Nature" src="http://bit.ly/divine_n" /><Avatar name="Dan Abrahmov" src="https://bit.ly/dan-abramov" /><Avatar name="Ryan Florence" src="https://bit.ly/ryan-florence" /><Avatar name="Christian Nwamba" src="https://bit.ly/code-beast" /><Avatar name="Segun Adebayo" src="https://bit.ly/sage-adebayo" /><Avatar name="Kent Dodds" src="https://bit.ly/kent-c-dodds" /></AvatarGroup>
Changing the initials logic#
Added getInitials
prop to allow users to manage how initials are generated
from name. By default we merge the first characters of each word in the name
prop.
Props#
Name | Type | Default | Description |
---|---|---|---|
size | xs , sm , md , lg , xl , 2xl | md | The size of the avatar |
showBorder | boolean | false | If true , the Avatar will show a border around it. Best for a group of avatars |
name | string | The name of the user in the avatar | |
src | string | The image url of the Avatar | |
color | string | tailwindcss color property Avatar | |
icon | React.ReactElement | The icon to use for the fallback avatar | |
children | React.ReactNode | The badge at the bottom right corner of the Avatar. |