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. Viverra tempor viverra; tristique suscipit eleifend fusce neque. Lobortis magnis odio lacinia cursus vehicula. Potenti dui diam habitant nunc natoque? Imperdiet vivamus tincidunt himenaeos tristique vivamus netus. Dignissim condimentum vulputate dolor lacus nulla cubilia hendrerit sed.
Lorem ipsum odor amet, consectetuer adipiscing elit. Conubia class consectetur platea mus convallis. Mus posuere donec habitasse aenean venenatis iaculis. Curae ornare aliquet dapibus torquent habitasse velit vitae senectus. Fames pellentesque iaculis per vel maecenas litora neque. Eget odio suspendisse eleifend facilisis ultricies himenaeos fusce justo nulla. Eu lobortis potenti suscipit ligula mus facilisis. Etiam integer nostra nisl tempor arcu adipiscing faucibus suscipit curae.
Lorem ipsum odor amet, consectetuer adipiscing elit. Torquent consectetur ullamcorper bibendum egestas taciti. Neque venenatis tortor massa ornare aptent vestibulum. Orci vel ante elit pellentesque felis quam. Mattis inceptos per torquent platea volutpat natoque metus nam cursus. Consectetur a augue efficitur integer blandit curae ullamcorper diam litora. Molestie accumsan turpis felis, montes sem malesuada. Ridiculus felis ipsum curabitur mus; malesuada tempor lacus vitae mus. Class inceptos euismod nisl taciti habitant risus tristique. Augue eros elementum sem adipiscing semper fusce dui ante eleifend.
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. Mauris erat diam adipiscing netus vitae litora cubilia vehicula? Et velit tempor fusce dui consequat posuere tempor cubilia. Tortor nec netus velit taciti magna dictumst lacinia. Nulla scelerisque commodo aenean aenean lacinia. Auctor odio semper facilisis; dictumst massa fames dignissim scelerisque. Vel tincidunt sodales dignissim himenaeos egestas mattis pulvinar sed cubilia?
Lorem ipsum odor amet, consectetuer adipiscing elit. Adipiscing consequat nam varius molestie lectus sociosqu. Sollicitudin ut sapien aptent; donec lacus cursus efficitur? Ultricies facilisi finibus posuere cursus turpis porttitor hac eleifend tristique. Blandit aptent at feugiat condimentum inceptos. Et phasellus condimentum orci conubia integer. Ultrices enim pulvinar fames feugiat imperdiet morbi. Luctus tortor potenti inceptos viverra ante mus ipsum ultricies lacinia. Tristique bibendum augue venenatis felis volutpat morbi nulla. Nullam vel per curae mauris ad parturient.
Lorem ipsum odor amet, consectetuer adipiscing elit. Hac est taciti quisque duis etiam. Ullamcorper nullam dictum senectus cubilia arcu natoque accumsan. Scelerisque interdum elit augue nibh fusce enim a scelerisque felis. Senectus porta vitae nibh natoque habitant viverra tortor. Penatibus curae mollis enim integer vestibulum.
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. Ultricies finibus duis iaculis varius enim tortor tempus velit. Et tempus sem massa nulla iaculis finibus, tempus arcu. Nascetur integer eleifend ultricies sem enim amet ornare. Justo nostra suscipit nulla elit tincidunt, vestibulum malesuada. Vehicula vivamus non ultricies curabitur taciti rutrum aenean volutpat imperdiet.
Lorem ipsum odor amet, consectetuer adipiscing elit. Orci massa finibus interdum suspendisse mus libero primis. Nascetur placerat integer condimentum fermentum sodales finibus leo. Quam leo platea facilisi orci elementum. Cursus quis efficitur tortor hendrerit sollicitudin porttitor per lacus mi. Imperdiet maximus venenatis sollicitudin nullam nec natoque vestibulum. Pretium volutpat lacus scelerisque fermentum fringilla dapibus montes. Egestas at ante laoreet sed convallis convallis phasellus. Iaculis interdum dui maximus; mollis urna hendrerit finibus.
Lorem ipsum odor amet, consectetuer adipiscing elit. Conubia luctus nam enim phasellus inceptos a per. Ridiculus quis nulla amet torquent pellentesque aptent. Porttitor dolor nisl torquent elit dui mollis nunc cursus efficitur. Viverra ante sit amet magnis curae malesuada nec placerat. Gravida platea torquent netus natoque netus. Lobortis aliquam pretium condimentum nam cras fusce ante. Elit aenean quisque laoreet luctus pretium tincidunt ad!
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 /> },
]
Without Snippet
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. |