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. Nostra convallis sit, tortor ipsum dolor suspendisse. Metus cubilia efficitur primis fermentum interdum consectetur commodo molestie. Ultrices aliquet habitant maecenas curae in suspendisse enim cubilia. Lacinia pretium facilisi interdum congue eleifend hac faucibus. Leo tincidunt adipiscing cubilia accumsan lacus ex faucibus.
Lorem ipsum odor amet, consectetuer adipiscing elit. Mi ornare libero nascetur pulvinar ante. Torquent senectus molestie orci class eget tristique id id sollicitudin. Hendrerit cras potenti tempus etiam facilisis id. Mattis odio vivamus diam habitasse semper et nullam gravida. Neque est vulputate erat augue sodales turpis. Nec neque molestie eros est laoreet leo orci aenean.
Lorem ipsum odor amet, consectetuer adipiscing elit. Torquent enim malesuada montes scelerisque est donec nibh aliquam nullam. Platea habitasse per, tempor pulvinar mus sapien litora. Convallis ipsum vestibulum ipsum enim magna, nam ligula. Amet lacinia sociosqu eu integer hac augue fusce. Justo tempor in per egestas potenti, feugiat aptent cursus rutrum.
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. Malesuada natoque feugiat etiam sociosqu, natoque vestibulum leo lorem dui. Eleifend placerat ad aenean tincidunt netus ligula. Scelerisque enim placerat etiam platea suscipit gravida dolor cras. Est habitasse convallis posuere elit dapibus. Eu molestie venenatis dolor etiam hendrerit aliquet. Leo tempor nibh fusce elementum dictumst pretium ligula. Quam lobortis hendrerit est blandit mattis.
Lorem ipsum odor amet, consectetuer adipiscing elit. Dapibus quam nisl rutrum pellentesque pretium venenatis tellus montes. Ex porta natoque sagittis at, cras quam. Porta viverra purus sollicitudin eu; turpis eros. Tincidunt cubilia odio varius potenti vehicula ac platea. Sagittis enim fringilla erat justo laoreet primis. Finibus orci pretium orci massa posuere id venenatis magna. Curae elementum senectus ipsum lectus sapien himenaeos curabitur.
Lorem ipsum odor amet, consectetuer adipiscing elit. Sagittis netus scelerisque placerat feugiat erat odio. Augue natoque magna dis ante est. Proin senectus nec tincidunt vestibulum sodales donec vitae suspendisse. Platea potenti dictum habitasse nunc massa molestie euismod magnis. Adipiscing euismod lobortis quam hendrerit sagittis sem non. Nam arcu magna sit laoreet congue nulla suscipit porttitor.
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. Faucibus in sollicitudin litora integer amet inceptos pellentesque justo. Orci torquent maecenas himenaeos malesuada neque curae. Urna at curae ut porttitor posuere suscipit primis vel ipsum. Commodo rutrum efficitur cubilia cras vulputate litora nostra pretium. Eleifend at adipiscing ornare maecenas eget nec. Nullam molestie pharetra ipsum dictumst ex tincidunt rhoncus. Facilisi finibus in bibendum pulvinar suspendisse cras dictumst. Tincidunt eget curabitur, quisque auctor bibendum placerat. Pretium ipsum suspendisse; ultrices nulla consectetur facilisis consequat.
Lorem ipsum odor amet, consectetuer adipiscing elit. Vestibulum elit semper tellus habitant malesuada ullamcorper litora semper. Tellus viverra ante augue ridiculus pellentesque montes semper. Duis eu quam aptent fermentum cras convallis aptent. Nisl tortor consectetur facilisis vitae et aliquam erat? Lobortis scelerisque vulputate faucibus tristique consequat primis feugiat vivamus? Porttitor fermentum neque porta conubia in, a cras a. Taciti ut rhoncus sociosqu diam turpis ligula netus viverra.
Lorem ipsum odor amet, consectetuer adipiscing elit. Vulputate penatibus aptent sapien sagittis pretium ultrices nullam. Ligula risus vulputate dui suscipit; natoque tincidunt ullamcorper. Senectus imperdiet tincidunt eros ullamcorper eleifend odio. Aurna velit dignissim nullam ante justo ridiculus. Convallis vivamus pellentesque lacus congue vitae augue; lacus dis vitae. Et viverra bibendum duis hendrerit congue mauris augue? Nunc vestibulum consectetur massa porta elementum parturient risus dapibus.
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. |