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
npx @chakra-ui/cli 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. Libero lectus ridiculus neque pretium augue quis feugiat etiam tristique. Felis ultrices vestibulum sit blandit ad euismod. Montes tellus vulputate mus aliquam non tristique volutpat magnis. Nostra commodo sodales taciti curabitur aliquam. Curabitur gravida ipsum libero; semper cubilia vel. Metus maximus lacus urna nisi lacus, magnis quisque nascetur. Nam nisl nostra fringilla duis eros libero nulla. Efficitur bibendum nullam euismod; dignissim aenean gravida.
Lorem ipsum odor amet, consectetuer adipiscing elit. Conubia fringilla ut tristique facilisis tempus convallis ullamcorper lacinia himenaeos. Cursus amet commodo donec in maximus elementum quis conubia. Dapibus laoreet scelerisque arcu porttitor arcu bibendum mauris convallis ridiculus. At augue tristique magnis lobortis tempus. Nam fringilla ac vehicula convallis orci magnis scelerisque varius. Pharetra pulvinar phasellus ante laoreet hendrerit lorem. Habitant urna convallis sagittis ridiculus facilisi commodo et. Commodo morbi sodales viverra eleifend vehicula. Sagittis vivamus ipsum maximus, quam taciti aliquet.
Lorem ipsum odor amet, consectetuer adipiscing elit. Dictum cras faucibus conubia himenaeos ipsum justo porta inceptos. Phasellus aptent congue aptent ad ante habitasse fames. Dictum non sagittis non vulputate eget facilisi erat risus. Malesuada viverra arcu primis sociosqu fames metus varius. Laoreet quam rutrum maecenas sed pretium rutrum ultrices quam. Adipiscing nam proin inceptos mattis urna nulla nibh. Dolor varius per euismod ligula; quis euismod sapien. Elementum netus massa ornare in eu mus.
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. Metus felis phasellus mollis mi ad dolor imperdiet pharetra erat. Amet primis natoque euismod elementum venenatis metus fames dignissim. Sodales quisque ipsum libero tellus purus interdum. Molestie congue convallis sagittis quis nisl aptent. Iaculis at himenaeos posuere posuere egestas vestibulum. Lobortis senectus nisl tellus morbi orci. Commodo nisl commodo egestas conubia auctor ornare augue viverra dapibus. Diam lorem habitant phasellus penatibus condimentum adipiscing augue vulputate.
Lorem ipsum odor amet, consectetuer adipiscing elit. Nulla malesuada in massa interdum habitant quis orci phasellus. Netus orci varius libero curabitur felis. Eros morbi arcu himenaeos; praesent rhoncus dignissim tempus. Ligula class nullam tristique laoreet vivamus proin curabitur ultricies interdum. Aliquam congue dictum elementum nam sem posuere ut et. Lacinia molestie magna rutrum finibus ridiculus bibendum taciti convallis fames. Consequat molestie lacus a enim justo ante vel.
Lorem ipsum odor amet, consectetuer adipiscing elit. Convallis proin magna posuere nec faucibus eros. Per hendrerit ex ac vulputate dui nostra rutrum urna. Fusce velit viverra hendrerit; litora lectus tellus. Maximus habitant suscipit ridiculus bibendum platea. Erat luctus pellentesque ad, quam curae tortor finibus. Sem semper per phasellus amet auctor nascetur hac. Aenean est per ullamcorper ligula aenean nisi bibendum. Tristique cursus semper duis velit est consectetur commodo quis. Laoreet feugiat imperdiet; rhoncus ut gravida sit elementum.
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 rutrum maximus ad curabitur litora duis. Aliquam consequat ac risus suscipit maecenas sagittis magna. Rhoncus vehicula habitant mollis quam rutrum. Nullam cursus lorem vitae pharetra vivamus malesuada purus. Felis fringilla imperdiet maecenas leo, ridiculus luctus fermentum aenean. Ullamcorper ac sollicitudin tempor sociosqu pharetra, dictum quis.
Lorem ipsum odor amet, consectetuer adipiscing elit. Erat ultricies bibendum sociosqu sem congue maximus elementum. Facilisi amet blandit massa integer, amet sed suscipit. Sagittis donec molestie ad sem mollis sem. Duis nostra venenatis placerat; molestie mauris cras ut. Potenti nullam phasellus lobortis risus ridiculus. Vulputate himenaeos ac pulvinar primis suscipit. Morbi turpis dapibus convallis semper risus fermentum. Efficitur gravida montes malesuada sollicitudin sollicitudin neque; pulvinar mi.
Lorem ipsum odor amet, consectetuer adipiscing elit. Ullamcorper eget enim diam dictum efficitur pretium. Litora phasellus fusce magna potenti accumsan ante et. Morbi ligula vel euismod auctor ligula semper. Urna id curabitur quisque nunc risus ac. Nec ipsum eu curabitur potenti; aenean ornare.
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. |