Skip to main content

Theming

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: container
  • C: excessLabel
  • D: group

Theming properties

The properties that affect the theming of the Avatar component are:

  • size: The size of the button. Defaults to md.

Theming utilities

  • createMultiStyleConfigHelpers: a function that returns a set of utilities for creating style configs for a multipart component (definePartsStyle and defineMultiStyleConfig).
  • 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 style
badge: {
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 size="superLg" ... />
// or
<AvatarGroup size="superLg">...</AvatarGroup>

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 variant="roundedSquare" ... />
// or
<AvatarGroup variant="roundedSquare">...</AvatarGroup>

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 variant="roundedSquare" size="superLg" ... />
// or
<AvatarGroup variant="roundedSquare" size="superLg">...</AvatarGroup>

Showcase

import { ChakraProvider, extendTheme } from "@chakra-ui/react";
import Avatar from "./avatar";
import { avatarTheme } from "./avatar-theme";

const theme = extendTheme({
  components: {
    Avatar: avatarTheme,
  }
});

const App = () => {
  return (
    <ChakraProvider theme={theme}>
      <Avatar />
    </ChakraProvider>
  );
}

export default App;