Skip to main content

Theming

The Accordion 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: root
  • B: container
  • C: button
  • D: panel
  • E: icon

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 { accordionAnatomy } from "@chakra-ui/anatomy";
import { createMultiStyleConfigHelpers } from "@chakra-ui/react";

const { definePartsStyle, defineMultiStyleConfig } =
createMultiStyleConfigHelpers(accordionAnatomy.keys);

Customizing the default theme

import { accordionAnatomy } from "@chakra-ui/anatomy";
import { createMultiStyleConfigHelpers } from "@chakra-ui/react";

const { definePartsStyle, defineMultiStyleConfig } =
createMultiStyleConfigHelpers(accordionAnatomy.keys);

const baseStyle = definePartsStyle({
// define the part you're going to style
container: {
bg: "red.200", // change the backgroundColor of the container
},
});

export const accordionTheme = defineMultiStyleConfig({ baseStyle });

After customizing the accordion theme, we can import it in our theme file and add it in the components property:

import { extendTheme } from "@chakra-ui/react";
import { accordionTheme } from "./components/accordion";

export const theme = extendTheme({
components: { Accordion: accordionTheme },
});

This is a crucial step to make sure that any changes that we make to the accordion theme are applied.

Adding a custom size

Let's assume we want to include an extra large accordion icon size. Here's how we can do that:

import { accordionAnatomy } from '@chakra-ui/anatomy'
import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'

const { definePartsStyle, defineMultiStyleConfig } =
createMultiStyleConfigHelpers(accordionAnatomy.keys)

const xl = defineStyle({
fontSize: 'lg',
px: '4',
h: '12',
})

const sizes = {
xl: definePartsStyle({ icon: xl }),
}

export const accordionTheme = defineMultiStyleConfig({ sizes })

// Now we can use the new `xl` size
<Accordion size="xl"> ... </Accordion>

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 pill variant. Here's how we can do that:

import { accordionAnatomy } from '@chakra-ui/anatomy'
import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'

const { definePartsStyle, defineMultiStyleConfig } =
createMultiStyleConfigHelpers(accordionAnatomy.keys)

const custom = definePartsStyle({
panel: {
border: '1px solid',
borderColor: 'gray.200',
background: 'gray.50',
borderRadius: 'full',

// Let's also provide dark mode alternatives
_dark: {
borderColor: 'gray.600',
background: 'gray.800',
},
},
icon: {
border: '1px solid',
borderColor: 'gray.200',
background: 'gray.200',
borderRadius: 'full',
color: 'gray.500',

_dark: {
borderColor: 'gray.600',
background: 'gray.600',
color: 'gray.400',
},
},
})

export const accordionTheme = defineMultiStyleConfig({
variants: { custom },
})

// Now we can use the new `custom` variant
<Accordion variant="custom"> ... </Accordion>

Changing the default properties

Let's assume we want to change the default size and variant of every accordion in our app. Here's how we can do that:

import { accordionAnatomy } from '@chakra-ui/anatomy'
import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'

const { defineMultiStyleConfig } = createMultiStyleConfigHelpers(
accordionAnatomy.keys,
)

export const accordionTheme = defineMultiStyleConfig({
defaultProps: {
size: 'xl',
variant: 'custom',
},
})

// This saves you time, instead of manually setting the size and
// variant every time you use an accordion:
<Accordion size="xl" variant="custom"> ... </Accordion>

Showcase

import { ChakraProvider, extendTheme } from "@chakra-ui/react";
import Accordion from "./accordion";
import { accordionTheme } from "./accordion-theme";

const theme = extendTheme({
  components: {
    Accordion: accordionTheme,
  }
});

export default function App() {
  return (
    <ChakraProvider theme={theme}>
      <Accordion />
    </ChakraProvider>
  );
}

export default App;