The visual color appearance of the component
Avatar
The Avatar component is used to represent user, and displays the profile picture, initials or fallback icon.
Import#
Chakra UI exports 3 avatar-related components:
Avatar.Root
: The image that represents the user.Avatar.Badge
: A wrapper that displays its content on the right corner of the avatar.Avatar.Group
: A wrapper to stack multiple Avatars together.
import { Avatar } from '@chakra-ui/react'
Usage#
<Wrap><WrapItem><Avatar.Root name='Dan Abrahmov' src='https://bit.ly/dan-abramov'><Avatar.Image /><Avatar.Fallback /></Avatar.Root></WrapItem><WrapItem><Avatar.Root name='Kola Tioluwani' src='https://bit.ly/tioluwani-kolawole'><Avatar.Image /><Avatar.Fallback /></Avatar.Root></WrapItem><WrapItem><Avatar.Root name='Kent Dodds' src='https://bit.ly/kent-c-dodds'><Avatar.Image /><Avatar.Fallback /></Avatar.Root></WrapItem><WrapItem><Avatar.Root name='Ryan Florence' src='https://bit.ly/ryan-florence'><Avatar.Image /><Avatar.Fallback /></Avatar.Root></WrapItem><WrapItem><Avatar.Root name='Prosper Otemuyiwa' src='https://bit.ly/prosper-baba'><Avatar.Image /><Avatar.Fallback /></Avatar.Root></WrapItem><WrapItem><Avatar.Root name='Christian Nwamba' src='https://bit.ly/code-beast'><Avatar.Image /><Avatar.Fallback /></Avatar.Root></WrapItem><WrapItem><Avatar.Root name='Segun Adebayo' src='https://bit.ly/sage-adebayo'><Avatar.Image /><Avatar.Fallback /></Avatar.Root></WrapItem></Wrap>
Avatar Sizes#
The Avatar
component comes in 7 sizes.
<Wrap><WrapItem><Avatar.Rootsize='2xs'name='Dan Abrahmov'src='https://bit.ly/dan-abramov'><Avatar.Image /><Avatar.Fallback /></Avatar.Root></WrapItem><WrapItem><Avatar.Rootsize='xs'name='Kola Tioluwani'src='https://bit.ly/tioluwani-kolawole'><Avatar.Image /><Avatar.Fallback /></Avatar.Root>{' '}</WrapItem><WrapItem><Avatar.Root size='sm' name='Kent Dodds' src='https://bit.ly/kent-c-dodds'><Avatar.Image /><Avatar.Fallback /></Avatar.Root>{' '}</WrapItem><WrapItem><Avatar.Rootsize='md'name='Ryan Florence'src='https://bit.ly/ryan-florence'><Avatar.Image /><Avatar.Fallback /></Avatar.Root>{' '}</WrapItem><WrapItem><Avatar.Rootsize='lg'name='Prosper Otemuyiwa'src='https://bit.ly/prosper-baba'><Avatar.Image /><Avatar.Fallback /></Avatar.Root>{' '}</WrapItem><WrapItem><Avatar.Rootsize='xl'name='Christian Nwamba'src='https://bit.ly/code-beast'><Avatar.Image /><Avatar.Fallback /></Avatar.Root>{' '}</WrapItem><WrapItem><Avatar.Rootsize='2xl'name='Segun Adebayo'src='https://bit.ly/sage-adebayo'><Avatar.Image /><Avatar.Fallback /></Avatar.Root>{' '}</WrapItem></Wrap>
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 direction='row'><Avatar.Root name='Oshigaki Kisame' src='https://bit.ly/broken-link'><Avatar.Image /><Avatar.Fallback /></Avatar.Root><Avatar.Root name='Sasuke Uchiha' src='https://bit.ly/broken-link'><Avatar.Image /><Avatar.Fallback /></Avatar.Root><Avatar.Root src='https://bit.ly/broken-link'><Avatar.Image /><Avatar.Fallback /></Avatar.Root></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
bg
prop. - To update the icon svg, pass the
icon
prop.
<Avatar.Group spacing='1rem'><Avatar.Root bg='red.500' icon={<AiOutlineUser fontSize='1.5rem' />}><Avatar.Image /><Avatar.Fallback /></Avatar.Root><Avatar.Root bg='teal.500'><Avatar.Image /><Avatar.Fallback /></Avatar.Root></Avatar.Group>
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 direction='row' spacing={4}><Avatar.Root><Avatar.Image /><Avatar.Fallback /><Avatar.Badge boxSize='1.25em' bg='green.500' /></Avatar.Root>{/* You can also change the borderColor and bg of the badge */}<Avatar.Root><Avatar.Image /><Avatar.Fallback /><Avatar.Badge borderColor='papayawhip' bg='tomato' boxSize='1.25em' /></Avatar.Root></Stack>
Note the use of
em
for the size of theAvatar.Badge
. This is useful to size the badge relative to the avatar font size.
Avatar.Group#
In some cases, you might need to stack avatars as a group. Use the
Avatar.Group
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.
<Avatar.Group size='md' max={2}><Avatar.Root name='Ryan Florence' src='https://bit.ly/ryan-florence'><Avatar.Image /><Avatar.Fallback /></Avatar.Root><Avatar.Root name='Segun Adebayo' src='https://bit.ly/sage-adebayo'><Avatar.Image /><Avatar.Fallback /></Avatar.Root><Avatar.Root name='Kent Dodds' src='https://bit.ly/kent-c-dodds'><Avatar.Image /><Avatar.Fallback /></Avatar.Root><Avatar.Root name='Prosper Otemuyiwa' src='https://bit.ly/prosper-baba'><Avatar.Image /><Avatar.Fallback /></Avatar.Root><Avatar.Root name='Christian Nwamba' src='https://bit.ly/code-beast'><Avatar.Image /><Avatar.Fallback /></Avatar.Root></Avatar.Group>
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#
Avatar.Root Props#
Avatar.Root
composes the Box
component so you can pass all its props. These
are props specific to the Avatar.Root
component:
colorScheme
colorScheme
"whiteAlpha" | "blackAlpha" | "gray" | "red" | "orange" | "yellow" | "green" | "teal" | "blue" | "cyan" | "purple" | "pink"
crossOrigin
crossOrigin
"" | "anonymous" | "use-credentials"
getInitials
getInitials
Function to get the initials to display
(name: string) => string
icon
icon
The default avatar used as fallback when name
, and src
is not specified.
ReactElement<any, string | JSXElementConstructor<any>>
iconLabel
iconLabel
string
ignoreFallback
ignoreFallback
If true
, opt out of the avatar's fallback
logic and
renders the img
at all times.
boolean
false
loading
loading
Defines loading strategy
"eager" | "lazy"
name
name
The name of the person in the avatar.
- if src
has loaded, the name will be used as the alt
attribute of the img
- If src
is not loaded, the name will be used to create the initials
string
onError
onError
Function called when image failed to load
() => void
referrerPolicy
referrerPolicy
Defining which referrer is sent when fetching the resource.
HTMLAttributeReferrerPolicy
showBorder
showBorder
If true
, the Avatar
will show a border around it.
Best for a group of avatars
boolean
false
size
size
The size of the Avatar
"2xs" | "xs" | "sm" | "md" | "lg" | "xl" | "2xl" | "full"
md
src
src
The image url of the Avatar
string
srcSet
srcSet
List of sources to use for different screen resolutions
string
variant
variant
The variant of the Avatar
string
Avatar.Group Props#
Avatar.Group
composes the Box
component so you can pass all its props. These
are props specific to the Avatar.Group
component:
max
max
The maximum number of visible avatars
number
spacing
spacing
The space between the avatars in the group.
ResponsiveValue<string | number | (string & {})>
-0.75rem
The Avatar
component is a multipart component. The styling needs to be applied
to each part specifically.
To learn more about styling multipart components, visit the Component Style page.
Anatomy#
- A:
badge
- B:
root
- C:
excessLabel
- D:
group
Theming properties#
The properties that affect the theming of the Avatar
component are:
size
: The size of the button. Defaults tomd
.
Theming utilities#
createMultiStyleConfigHelpers
: a function that returns a set of utilities for creating style configs for a multipart component (definePartsStyle
anddefineMultiStyleConfig
).definePartsStyle
: a function used to create multipart style objects.defineMultiStyleConfig
: a function used to define the style configuration for a multipart component.
import { avatarAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(avatarAnatomy.keys)
Customizing the default theme#
import { avatarAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(avatarAnatomy.keys)const baseStyle = definePartsStyle({// define the part you're going to stylebadge: {bg: 'gray.500',border: '2px solid',},container: {borderRadius: 'xl',},excessLabel: {bg: 'gray.800',color: 'white',borderRadius: 'xl',},})export const avatarTheme = defineMultiStyleConfig({ baseStyle })
After customizing the avatar theme, we can import it in our theme file and add
it in the components
property:
import { extendTheme } from '@chakra-ui/react'import { avatarTheme } from './components/avatar'export const theme = extendTheme({components: { Avatar: avatarTheme },})
This is a crucial step to make sure that any changes that we make to the avatar theme are applied.
Adding a custom size#
Let's assume we want to include an super large avatar size. Here's how we can do that:
import { avatarAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(avatarAnatomy.keys)const superLg = defineStyle({width: 40,height: 40,fontSize: "6xl"})const sizes = {superLg: definePartsStyle({ container: superLg }),}export const avatarTheme = defineMultiStyleConfig({ sizes })// Now we can use the new `superLg` size<Avatar.Root size="superLg" ... />// or<Avatar.Group size="superLg">...</Avatar.Group>
Every time you're adding anything new to the theme, you'd need to run the CLI command to get proper autocomplete in your IDE. You can learn more about the CLI tool here.
Adding a custom variant#
Let's assume we want to include a custom rounded square variant. Here's how we can do that:
import { avatarAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(avatarAnatomy.keys)const roundedSquare = definePartsStyle({badge: {bg: "gray.500",border: "2px solid"},container: {borderRadius: "xl"},excessLabel: {bg: "gray.800",color: "white",borderRadius: "xl",border: "2px solid",// let's also provide dark mode alternatives_dark: {bg: "gray.400",color: "gray.900"}}})export const avatarTheme = defineMultiStyleConfig({variants: { roundedSquare },})// Now we can use the new `roundedSquare` variant<Avatar.Root variant="roundedSquare" ... />// or<Avatar.Group variant="roundedSquare">...</Avatar.Group>
Changing the default properties#
Let's assume we want to change the default size and variant of every avatar in our app. Here's how we can do that:
import { avatarAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'const { defineMultiStyleConfig } = createMultiStyleConfigHelpers(avatarAnatomy.keys,)export const avatarTheme = defineMultiStyleConfig({defaultProps: {size: 'superLg',variant: 'roundedSquare',},})// This saves you time, instead of manually setting the size and variant every time you use an avatar:<Avatar.Root variant="roundedSquare" size="superLg" ... />// or<Avatar.Group variant="roundedSquare" size="superLg">...</Avatar.Group>
Showcase#
import { Box, HStack, IconButton, Avatar, AvatarBadge, AvatarGroup, useColorMode } from "@chakra-ui/react"; import { FaMoon, FaSun } from "react-icons/fa"; export default function App() { const { toggleColorMode, colorMode } = useColorMode(); return ( <Box position="relative" h="100vh"> <HStack spacing={8} p={12}> <Avatar name="Segun Adebayo" /> <Avatar name="Lazar Nikolov" variant="roundedSquare"> <AvatarBadge boxSize="1rem" /> </Avatar> <AvatarGroup max={2} size="superLg" variant="roundedSquare"> <Avatar name="Lazar Nikolov" variant="roundedSquare"> <AvatarBadge /> </Avatar> <Avatar name="Segun Adebayo" variant="roundedSquare"> <AvatarBadge bg="orange.500" /> </Avatar> <Avatar name="Segun Adebayo" variant="roundedSquare"> <AvatarBadge bg="orange.500" /> </Avatar> </AvatarGroup> </HStack> <IconButton aria-label="toggle theme" rounded="full" size="xs" position="absolute" bottom={4} left={4} onClick={toggleColorMode} icon={colorMode === "dark" ? <FaSun /> : <FaMoon />} /> </Box> ); }