import {
AccordionItem,
AccordionItemContent,
AccordionItemTrigger,
AccordionRoot,
} from "@/components/ui/accordion"
const Demo = () => {
return (
<AccordionRoot collapsible defaultValue={["b"]}>
{items.map((item, index) => (
<AccordionItem key={index} value={item.value}>
<AccordionItemTrigger>{item.title}</AccordionItemTrigger>
<AccordionItemContent>{item.text}</AccordionItemContent>
</AccordionItem>
))}
</AccordionRoot>
)
}
const items = [
{ value: "a", title: "First Item", text: "Some value 1..." },
{ value: "b", title: "Second Item", text: "Some value 2..." },
{ value: "c", title: "Third Item", text: "Some value 3..." },
]
Setup
If you don't already have the snippet, run the following command to add the
accordion
snippet
chakra snippet add accordion
The snippet includes a closed component composition for the Accordion
component.
Usage
import {
AccordionItem,
AccordionItemContent,
AccordionItemTrigger,
AccordionRoot,
} from "@/components/ui/accordion"
<AccordionRoot>
<AccordionItem>
<AccordionItemTrigger />
<AccordionItemContent />
</AccordionItem>
</AccordionRoot>
Examples
Controlled
Set the accordion that shows up by default.
Expanded: second-item
"use client"
import { Stack, Text } from "@chakra-ui/react"
import {
AccordionItem,
AccordionItemContent,
AccordionItemTrigger,
AccordionRoot,
} from "@/components/ui/accordion"
import { useState } from "react"
const Demo = () => {
const [value, setValue] = useState(["second-item"])
return (
<Stack gap="4">
<Text fontWeight="medium">Expanded: {value.join(", ")}</Text>
<AccordionRoot value={value} onValueChange={(e) => setValue(e.value)}>
{items.map((item, index) => (
<AccordionItem key={index} value={item.value}>
<AccordionItemTrigger>{item.title}</AccordionItemTrigger>
<AccordionItemContent>{item.text}</AccordionItemContent>
</AccordionItem>
))}
</AccordionRoot>
</Stack>
)
}
const items = [
{ value: "first-item", title: "First Item", text: "Some value 1..." },
{ value: "second-item", title: "Second Item", text: "Some value 2..." },
{ value: "third-item", title: "Third Item", text: "Some value 3..." },
]
With Icon
Use the AccordionItemIcon
to add an icon to each accordion item.
Product details
import { Heading, Icon, Stack } from "@chakra-ui/react"
import {
AccordionItem,
AccordionItemContent,
AccordionItemTrigger,
AccordionRoot,
} from "@/components/ui/accordion"
import { LuBarChart, LuTags } from "react-icons/lu"
const Demo = () => {
return (
<Stack width="full" maxW="400px">
<Heading size="md">Product details</Heading>
<AccordionRoot collapsible defaultValue={["info"]}>
{items.map((item) => (
<AccordionItem key={item.value} value={item.value}>
<AccordionItemTrigger>
<Icon fontSize="lg" color="fg.subtle">
{item.icon}
</Icon>
{item.title}
</AccordionItemTrigger>
<AccordionItemContent>{item.content}</AccordionItemContent>
</AccordionItem>
))}
</AccordionRoot>
</Stack>
)
}
const items = [
{
value: "info",
icon: <LuTags />,
title: "Product Info",
content:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur nec odio vel dui euismod fermentum.",
},
{
value: "stats",
icon: <LuBarChart />,
title: "Stats",
content:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur nec odio vel dui euismod fermentum.",
},
]
Expand Multiple Items
Use the multiple
prop to allow multiple items to be expanded at once.
import {
AccordionItem,
AccordionItemContent,
AccordionItemTrigger,
AccordionRoot,
} from "@/components/ui/accordion"
const Demo = () => {
return (
<AccordionRoot multiple defaultValue={["b"]}>
{items.map((item, index) => (
<AccordionItem key={index} value={item.value}>
<AccordionItemTrigger>{item.title}</AccordionItemTrigger>
<AccordionItemContent>{item.text}</AccordionItemContent>
</AccordionItem>
))}
</AccordionRoot>
)
}
const items = [
{ value: "a", title: "First Item", text: "Some value 1..." },
{ value: "b", title: "Second Item", text: "Some value 2..." },
{ value: "c", title: "Third Item", text: "Some value 3..." },
]
Sizes
Use the size
prop to change the size of the accordion.
sm
md
lg
import { For, Stack, Text } from "@chakra-ui/react"
import {
AccordionItem,
AccordionItemContent,
AccordionItemTrigger,
AccordionRoot,
} from "@/components/ui/accordion"
const Demo = () => {
return (
<Stack gap="8">
<For each={["sm", "md", "lg"]}>
{(size) => (
<Stack gap="2">
<Text fontWeight="semibold">{size}</Text>
<AccordionRoot
size={size}
key={size}
collapsible
defaultValue={["b"]}
>
{items.map((item, index) => (
<AccordionItem key={index} value={item.value}>
<AccordionItemTrigger>{item.title}</AccordionItemTrigger>
<AccordionItemContent>{item.text}</AccordionItemContent>
</AccordionItem>
))}
</AccordionRoot>
</Stack>
)}
</For>
</Stack>
)
}
const items = [
{ value: "a", title: "First Item", text: "Some value 1..." },
{ value: "b", title: "Second Item", text: "Some value 2..." },
{ value: "c", title: "Third Item", text: "Some value 3..." },
]
Variants
Use the variant
prop to change the visual style of the accordion. Values can
be either outline
, elevated
, contained
or plain
.
outline
subtle
enclosed
plain
import { For, Stack, Text } from "@chakra-ui/react"
import {
AccordionItem,
AccordionItemContent,
AccordionItemTrigger,
AccordionRoot,
} from "@/components/ui/accordion"
const Demo = () => {
return (
<Stack gap="8">
<For each={["outline", "subtle", "enclosed", "plain"]}>
{(variant) => (
<Stack gap="2">
<Text fontWeight="semibold">{variant}</Text>
<AccordionRoot
variant={variant}
key={variant}
collapsible
defaultValue={["b"]}
>
{items.map((item, index) => (
<AccordionItem key={index} value={item.value}>
<AccordionItemTrigger>{item.title}</AccordionItemTrigger>
<AccordionItemContent>{item.text}</AccordionItemContent>
</AccordionItem>
))}
</AccordionRoot>
</Stack>
)}
</For>
</Stack>
)
}
const items = [
{ value: "a", title: "First Item", text: "Some value 1..." },
{ value: "b", title: "Second Item", text: "Some value 2..." },
{ value: "c", title: "Third Item", text: "Some value 3..." },
]
Disabled Item
Pass the disabled
prop to disable an accordion item to prevent it from being
expanded or collapsed.
import {
AccordionItem,
AccordionItemContent,
AccordionItemTrigger,
AccordionRoot,
} from "@/components/ui/accordion"
const Demo = () => {
return (
<AccordionRoot collapsible defaultValue={["b"]}>
{items.map((item, index) => (
<AccordionItem key={index} value={item.value} disabled={item.disabled}>
<AccordionItemTrigger>{item.title}</AccordionItemTrigger>
<AccordionItemContent>{item.text}</AccordionItemContent>
</AccordionItem>
))}
</AccordionRoot>
)
}
const items = [
{ value: "a", title: "First Item", text: "Some value 1..." },
{ value: "b", title: "Second Item", text: "Some value 2..." },
{ value: "c", title: "Third Item", text: "Some value 3...", disabled: true },
]
With Avatar
Here's an example of composing an accordion with an avatar.
Lorem ipsum odor amet, consectetuer adipiscing elit. At class netus elementum mus cursus ex maecenas diam hendrerit? Dignissim neque mi in taciti imperdiet rutrum porta curae. Dapibus consectetur semper consectetur quam ultrices ex. Sodales adipiscing velit congue metus montes. Viverra fusce mollis; ipsum metus consequat vitae. Class cubilia commodo ante imperdiet rutrum cras sapien ad semper. Velit quam rutrum metus est nullam.
Lorem ipsum odor amet, consectetuer adipiscing elit. Gravida malesuada magna, pulvinar mus finibus sem. Iaculis fames et dignissim lacus tristique eros nullam. Dapibus velit odio posuere rhoncus quam felis penatibus sollicitudin integer. Accumsan egestas gravida amet porta ultrices. Congue scelerisque dui id platea dui dapibus auctor duis. Varius lobortis aliquet metus duis elementum quis sollicitudin inceptos.
Lorem ipsum odor amet, consectetuer adipiscing elit. Congue penatibus malesuada pellentesque aliquam nibh porta dis platea. Ipsum suscipit turpis non porttitor nisl molestie suspendisse vivamus tempor. Ex finibus fringilla faucibus duis imperdiet suspendisse. Sed etiam rhoncus suspendisse, ipsum dapibus donec. Pulvinar quis id facilisis cubilia libero venenatis gravida. Cubilia felis cursus conubia maximus faucibus.
import { Badge, HStack } from "@chakra-ui/react"
import {
AccordionItem,
AccordionItemContent,
AccordionItemTrigger,
AccordionRoot,
} from "@/components/ui/accordion"
import { Avatar } from "@/components/ui/avatar"
import { LuTrophy } from "react-icons/lu"
import { LoremIpsum } from "react-lorem-ipsum"
const Demo = () => {
return (
<AccordionRoot collapsible defaultValue={["b"]}>
{items.map((item, index) => (
<AccordionItem key={index} value={item.name}>
<AccordionItemTrigger>
<Avatar shape="rounded" src={item.image} name={item.name} />
<HStack>
{item.name}{" "}
{item.topRated && (
<Badge colorPalette="green">
<LuTrophy />
Top Rated
</Badge>
)}
</HStack>
</AccordionItemTrigger>
<AccordionItemContent>{item.bio}</AccordionItemContent>
</AccordionItem>
))}
</AccordionRoot>
)
}
const items = [
{
name: "Alex",
bio: <LoremIpsum />,
image: "https://i.pravatar.cc/150?u=a",
topRated: false,
},
{
name: "Benji",
bio: <LoremIpsum />,
image: "https://i.pravatar.cc/150?u=b",
topRated: true,
},
{
name: "Charlie",
bio: <LoremIpsum />,
image: "https://i.pravatar.cc/150?u=c",
topRated: false,
},
]
With Subtext
Here's an example of adding a subtext to an accordion item.
Lorem ipsum odor amet, consectetuer adipiscing elit. Ultricies donec suspendisse aenean vehicula hac ligula, torquent netus. Donec mauris congue; nunc vehicula adipiscing vulputate integer cras sed. Non conubia hendrerit aliquam leo egestas vel. Penatibus elementum quis condimentum etiam imperdiet. Sit condimentum mattis cras augue curabitur tempus. Vulputate dis nec habitant senectus nostra eros dictum integer. In turpis suspendisse penatibus lorem dis gravida bibendum. Hendrerit sollicitudin tempor efficitur vel suspendisse at quis?
Lorem ipsum odor amet, consectetuer adipiscing elit. Lacus mi ac feugiat odio per augue curae. Nisi tincidunt interdum lacus iaculis curae fringilla porta integer? Condimentum dui mus sagittis turpis ligula consectetur eros. Inceptos amet donec vestibulum bibendum diam. Per facilisi malesuada rhoncus elit pellentesque morbi pretium. Penatibus ante nam laoreet magna, laoreet lacinia felis a. Tortor integer natoque taciti odio maximus; luctus ac proin.
Lorem ipsum odor amet, consectetuer adipiscing elit. Sit pulvinar torquent natoque pellentesque lorem integer. Torquent adipiscing cras quisque semper odio. Id ante diam turpis iaculis urna nulla hac. Augue dui venenatis felis consectetur nec interdum sapien odio. Mattis fusce mauris odio; aliquet cubilia condimentum. Mauris et risus in non et tristique. Ipsum ultricies vehicula luctus dui cubilia mattis et quisque? Molestie integer placerat sit cras diam lacinia nisl. Ullamcorper tristique tellus tincidunt nam proin magnis.
import { Stack, Text } from "@chakra-ui/react"
import {
AccordionItem,
AccordionItemContent,
AccordionItemTrigger,
AccordionRoot,
} from "@/components/ui/accordion"
import { LoremIpsum } from "react-lorem-ipsum"
const items = [
{ value: "a", title: "First Item", text: <LoremIpsum p={1} /> },
{ value: "b", title: "Second Item", text: <LoremIpsum p={1} /> },
{ value: "c", title: "Third Item", text: <LoremIpsum p={1} /> },
]
const Demo = () => {
return (
<AccordionRoot collapsible>
{items.map((item, index) => (
<AccordionItem key={index} value={item.value}>
<AccordionItemTrigger>
<Stack gap="1">
<Text>{item.title}</Text>
<Text fontSize="sm" color="fg.muted">
Click to expand
</Text>
</Stack>
</AccordionItemTrigger>
<AccordionItemContent>{item.text}</AccordionItemContent>
</AccordionItem>
))}
</AccordionRoot>
)
}
With Actions
Here's an example of adding actions to an accordion item trigger.
Lorem ipsum odor amet, consectetuer adipiscing elit. Maecenas conubia elit morbi magna a elit tincidunt. Mus aliquam lacus nisl id imperdiet convallis dapibus vivamus. Taciti iaculis vivamus at sagittis, eu nisl est amet efficitur. Diam nostra auctor dolor turpis bibendum mollis platea pellentesque. Venenatis nibh tempus pretium at erat sit turpis bibendum. Iaculis ante pulvinar rutrum ad posuere sociosqu cras taciti. Nunc magna praesent himenaeos a scelerisque et velit.
Lorem ipsum odor amet, consectetuer adipiscing elit. Per purus dis tempor risus cras mattis amet feugiat. Etiam risus vestibulum himenaeos, aenean montes quam. Ut laoreet porttitor nunc pellentesque justo quisque. Eros arcu fames fringilla urna ad feugiat risus. Velit lectus efficitur hac cubilia; leo venenatis.
Lorem ipsum odor amet, consectetuer adipiscing elit. Habitant conubia erat aliquet curae ac dignissim volutpat. Diam metus curabitur est odio pharetra magna justo. Sociosqu vivamus phasellus auctor torquent ultrices elementum. Suspendisse ipsum platea semper efficitur varius rutrum ut. Maximus litora interdum euismod et torquent mattis quisque pretium. Sem dui magnis dictumst libero vehicula nisl consequat fermentum penatibus? Eu efficitur litora hendrerit porttitor luctus tristique ad. Curae aenean massa cursus elit fusce montes. Nisl magnis sociosqu luctus libero venenatis.
import { AbsoluteCenter, Box, Button } from "@chakra-ui/react"
import {
AccordionItem,
AccordionItemContent,
AccordionItemTrigger,
AccordionRoot,
} from "@/components/ui/accordion"
import LoremIpsum from "react-lorem-ipsum"
const Demo = () => {
return (
<AccordionRoot spaceY="4" variant="plain" collapsible defaultValue={["b"]}>
{items.map((item, index) => (
<AccordionItem key={index} value={item.value}>
<Box position="relative">
<AccordionItemTrigger indicatorPlacement="start">
{item.title}
</AccordionItemTrigger>
<AbsoluteCenter axis="vertical" insetEnd="0">
<Button variant="subtle" colorPalette="blue">
Action
</Button>
</AbsoluteCenter>
</Box>
<AccordionItemContent>{item.text}</AccordionItemContent>
</AccordionItem>
))}
</AccordionRoot>
)
}
const items = [
{ value: "a", title: "First Item", text: <LoremIpsum /> },
{ value: "b", title: "Second Item", text: <LoremIpsum /> },
{ value: "c", title: "Third Item", text: <LoremIpsum /> },
]
Manual
Here's how to setup the accordion without using the snippet.
import { Accordion } from "@chakra-ui/react"
const Demo = () => {
return (
<Accordion.Root collapsible defaultValue={["b"]}>
{items.map((item, index) => (
<Accordion.Item key={index} value={item.value}>
<Accordion.ItemTrigger>
{item.title}
<Accordion.ItemIndicator />
</Accordion.ItemTrigger>
<Accordion.ItemContent>
<Accordion.ItemBody>{item.text}</Accordion.ItemBody>
</Accordion.ItemContent>
</Accordion.Item>
))}
</Accordion.Root>
)
}
const items = [
{ value: "a", title: "First Item", text: "Some value 1..." },
{ value: "b", title: "Second Item", text: "Some value 2..." },
{ value: "c", title: "Third Item", text: "Some value 3..." },
]
Props
Root
Prop | Default | Type |
---|---|---|
collapsible | false | boolean Whether an accordion item can be closed after it has been expanded. |
lazyMount | false | boolean Whether to enable lazy mounting |
multiple | false | boolean Whether multple accordion items can be expanded at the same time. |
orientation | '\'vertical\'' | 'horizontal' | 'vertical' The orientation of the accordion items. |
unmountOnExit | false | boolean Whether to unmount on exit. |
colorPalette | 'gray' | 'gray' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'cyan' | 'purple' | 'pink' | 'accent' The color palette of the component |
variant | 'outline' | 'outline' | 'subtle' | 'enclosed' | 'plain' The variant of the component |
size | 'md' | 'sm' | 'md' | 'lg' The size of the component |
asChild | boolean Use the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
defaultValue | string[] The initial value of the accordion when it is first rendered. Use when you do not need to control the state of the accordion. | |
disabled | boolean Whether the accordion items are disabled | |
id | string The unique identifier of the machine. | |
ids | Partial<{
root: string
item(value: string): string
itemContent(value: string): string
itemTrigger(value: string): string
}> The ids of the elements in the accordion. Useful for composition. | |
onFocusChange | (details: FocusChangeDetails) => void The callback fired when the focused accordion item changes. | |
onValueChange | (details: ValueChangeDetails) => void The callback fired when the state of expanded/collapsed accordion items changes. | |
value | string[] The `value` of the accordion items that are currently being expanded. |
Item
Prop | Default | Type |
---|---|---|
value * | string The value of the accordion item. | |
asChild | boolean Use the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
disabled | boolean Whether the accordion item is disabled. |