A unique id for the accordion item.
Accordion
Accordions display a list of high-level options that can expand/collapse to reveal more information.
Import#
Chakra UI exports 5 accordion-related components.
Accordion.Root
: The wrapper that uses cloneElement to pass props toAccordion.Item
children.Accordion.Item
: A single accordion item.Accordion.Trigger
: The button that toggles the expand/collapse state of the accordion item. This button must be wrapped in an element with roleheading
.Accordion.Content
: The container for the details to be revealed.Accordion.Icon
: Achevron-down
icon that rotates based on the expanded/collapsed state
import { Accordion } from '@chakra-ui/react'
Usage#
By default, only one item may be expanded and it can only be collapsed again by expanding another.
Accordion
now supports keyboard navigation between accordion buttons. Pressing
the up
and down
arrow keys will move focus between accordion buttons.
<Accordion.Root><Accordion.Item><h2><Accordion.Trigger><Box as='span' flex='1' textAlign='left'>Section 1 title</Box><Accordion.Icon /></Accordion.Trigger></h2><Accordion.Content pb={4}>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmodtempor incididunt ut labore et dolore magna aliqua. Ut enim ad minimveniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex eacommodo consequat.</Accordion.Content></Accordion.Item><Accordion.Item><h2><Accordion.Trigger><Box as='span' flex='1' textAlign='left'>Section 2 title</Box><Accordion.Icon /></Accordion.Trigger></h2><Accordion.Content pb={4}>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmodtempor incididunt ut labore et dolore magna aliqua. Ut enim ad minimveniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex eacommodo consequat.</Accordion.Content></Accordion.Item></Accordion.Root>
Expand multiple items at once#
If you set multiple
to true
then the accordion will permit multiple items to
be expanded at once.
If you pass this prop, ensure that the
index
ordefaultIndex
prop is an array.
<Accordion.Root defaultIndex={[0]} multiple><Accordion.Item><h2><Accordion.Trigger><Box as='span' flex='1' textAlign='left'>Section 1 title</Box><Accordion.Icon /></Accordion.Trigger></h2><Accordion.Content pb={4}>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmodtempor incididunt ut labore et dolore magna aliqua. Ut enim ad minimveniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex eacommodo consequat.</Accordion.Content></Accordion.Item><Accordion.Item><h2><Accordion.Trigger><Box as='span' flex='1' textAlign='left'>Section 2 title</Box><Accordion.Icon /></Accordion.Trigger></h2><Accordion.Content pb={4}>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmodtempor incididunt ut labore et dolore magna aliqua. Ut enim ad minimveniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex eacommodo consequat.</Accordion.Content></Accordion.Item></Accordion.Root>
Toggle each accordion item#
If you set collapsible
to true
, any expanded item may be collapsed again.
<Accordion.Root collapsible><Accordion.Item><h2><Accordion.Trigger><Box as='span' flex='1' textAlign='left'>Section 1 title</Box><Accordion.Icon /></Accordion.Trigger></h2><Accordion.Content pb={4}>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmodtempor incididunt ut labore et dolore magna aliqua. Ut enim ad minimveniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex eacommodo consequat.</Accordion.Content></Accordion.Item><Accordion.Item><h2><Accordion.Trigger><Box as='span' flex='1' textAlign='left'>Section 2 title</Box><Accordion.Icon /></Accordion.Trigger></h2><Accordion.Content pb={4}>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmodtempor incididunt ut labore et dolore magna aliqua. Ut enim ad minimveniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex eacommodo consequat.</Accordion.Content></Accordion.Item></Accordion.Root>
Styling the expanded state#
The Accordion.Trigger
component has aria-expanded
set to true
or false
depending on the state of the Accordion.Item
. Use the style prop _expanded
to style this state.
<Accordion.Root><Accordion.Item><h2><Accordion.Trigger _expanded={{ bg: 'tomato', color: 'white' }}><Box as='span' flex='1' textAlign='left'>Click me to see a different style</Box><Accordion.Icon /></Accordion.Trigger></h2><Accordion.Content>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmodtempor incididunt ut labore et dolore magna aliqua. Ut enim ad minimveniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex eacommodo consequat.</Accordion.Content></Accordion.Item></Accordion.Root>
Accessing the internal state#
If you need access to the internal state of each accordion item, you can use a
render prop. It provides 2 internal state props: isExpanded
and isDisabled
.
<Accordion.Root multiple><Accordion.Item><h2><Accordion.Trigger><Box as='span' flex='1' textAlign='left'>Section 1 title</Box><Accordion.Icon /></Accordion.Trigger></h2><Accordion.Content pb={4}>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmodtempor incididunt ut labore et dolore magna aliqua. Ut enim ad minimveniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex eacommodo consequat.</Accordion.Content></Accordion.Item><Accordion.Item>{({ isExpanded }) => (<><h2><Accordion.Trigger><Box as='span' flex='1' textAlign='left'>Section 2 title</Box>{isExpanded ? (<MinusIcon fontSize='12px' />) : (<AddIcon fontSize='12px' />)}</Accordion.Trigger></h2><Accordion.Content pb={4}>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed doeiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim adminim veniam, quis nostrud exercitation ullamco laboris nisi utaliquip ex ea commodo consequat.</Accordion.Content></>)}</Accordion.Item></Accordion.Root>
Accessibility#
Pressing space
or enter
when focus is on the accordion panel header will
toggle (expand or collapse) the accordion.
Props#
Accordion.Root Props#
Accordion.Item Props#
id
id
string
isDisabled
isDisabled
If true
, the accordion item will be disabled.
boolean
false
isFocusable
isFocusable
If true
, the accordion item will be focusable.
boolean
false
Accordion.Trigger Props#
Accordion.Trigger renders a button
and composes Box
, this means you can
style it by passing the pseudo style props _expanded
, _disabled
, _hover
,
etc.
Accordion.Content Props#
Accordion.Content renders a div
and composes Collapse
to provide the height
animation.
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:
root
- C:
trigger
- D:
content
- E:
icon
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 { 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 stylecontainer: {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.Root size="xl"> ... </Accordion.Root>
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.Root variant="custom"> ... </Accordion.Root>
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.Root size="xl" variant="custom"> ... </Accordion.Root>
Showcase#
import { Box, useColorMode, Accordion, AccordionButton, AccordionIcon, AccordionItem, AccordionPanel, IconButton, } from "@chakra-ui/react"; import { FaMoon, FaSun } from "react-icons/fa"; export default function App() { const { toggleColorMode, colorMode } = useColorMode(); return ( <Box pt={12} position="relative" h="100vh"> <Accordion> <AccordionItem m={4}> <h2> <AccordionButton> <Box flex="1" textAlign="left"> Section 1 title </Box> <AccordionIcon /> </AccordionButton> </h2> <AccordionPanel pb={4}> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </AccordionPanel> </AccordionItem> <AccordionItem m={4}> <h2> <AccordionButton> <Box flex="1" textAlign="left"> Section 2 title </Box> <AccordionIcon /> </AccordionButton> </h2> <AccordionPanel pb={4}> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </AccordionPanel> </AccordionItem> </Accordion> <IconButton aria-label="toggle theme" rounded="full" size="xs" position="absolute" bottom={4} left={4} onClick={toggleColorMode} icon={colorMode === "dark" ? <FaSun /> : <FaMoon />} /> </Box> ); }