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. Molestie libero dolor faucibus libero platea. Erat nulla tortor erat sollicitudin dui convallis. Elementum dui hac magnis ipsum leo vehicula viverra bibendum. Sit per felis nam montes morbi. Eros senectus pulvinar per sed finibus habitant in. Mauris eros proin dis vivamus vitae curae volutpat. Dui aliquam amet ac semper vel.
Lorem ipsum odor amet, consectetuer adipiscing elit. Mauris felis enim mattis aenean eros luctus cras dignissim. Curabitur metus maecenas sapien quisque nisl praesent phasellus. Varius quam luctus habitant, ornare maximus pharetra. Varius et vel sem quisque lacinia vitae aenean. Imperdiet leo dictum leo justo nibh erat. Faucibus sagittis posuere orci nibh enim sem suscipit phasellus.
Lorem ipsum odor amet, consectetuer adipiscing elit. Pharetra iaculis nisi fringilla pharetra risus ut vitae vestibulum. Sit consectetur ornare donec interdum odio amet quisque condimentum. Vestibulum nam rhoncus vel maecenas himenaeos suspendisse. Nulla sapien fusce morbi aptent primis mi pretium finibus senectus. Hendrerit magna at nec auctor montes netus tellus netus. Ultricies bibendum nibh vestibulum; sed urna per volutpat. Dis rutrum non arcu risus egestas netus? Est eu per mus commodo habitasse finibus.
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. Amet consequat litora integer integer ligula finibus ut ridiculus. Pretium viverra quam sollicitudin nisl maximus vitae tristique suscipit orci. Mauris ridiculus rutrum gravida pulvinar; habitant placerat dictum. Conubia turpis fringilla euismod pellentesque ad ipsum ridiculus at. Dui molestie mattis dictumst sociosqu penatibus semper. Non quis metus class vel massa justo. Tincidunt felis tristique imperdiet parturient eu eros netus.
Lorem ipsum odor amet, consectetuer adipiscing elit. Enim diam per posuere bibendum elementum sagittis purus. Convallis accumsan volutpat massa diam volutpat ut suscipit potenti. Fringilla justo lectus fusce aenean pharetra ultrices. Nisl condimentum lorem cras sollicitudin natoque quis. Pellentesque ipsum adipiscing tortor molestie; posuere in molestie. Eget tincidunt mus rutrum condimentum elementum.
Lorem ipsum odor amet, consectetuer adipiscing elit. Donec eleifend nec torquent aptent elementum quis. Dignissim facilisi potenti ex lectus magnis aenean finibus. Congue mauris fermentum tempus laoreet sit inceptos. Congue nascetur posuere auctor integer odio fusce nec congue. Velit volutpat porta id mauris imperdiet interdum porttitor pellentesque. Litora malesuada vel feugiat sodales rhoncus. Habitant eu aliquet vivamus elit sit. Pellentesque natoque non posuere diam gravida interdum donec?
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. Finibus curabitur dis, odio cursus purus enim lacinia eros? Consectetur etiam molestie imperdiet quisque turpis feugiat arcu placerat. Convallis diam nam turpis condimentum aliquet facilisi sollicitudin. Dolor aptent per faucibus tincidunt ullamcorper. Accumsan sodales potenti molestie pharetra et curabitur vivamus mattis. Maecenas lacus commodo gravida semper nec in ad montes. Maecenas iaculis convallis ridiculus, etiam lacus ac metus. Magna elementum sollicitudin arcu sollicitudin habitasse ante urna non.
Lorem ipsum odor amet, consectetuer adipiscing elit. Duis luctus urna platea hac parturient leo lacinia. Sollicitudin nibh torquent, natoque maecenas tempus non nisi? Ut quam molestie ligula, cubilia turpis maximus vel commodo. Ut nibh condimentum pulvinar dictumst placerat dui faucibus nisi blandit. Magna hendrerit platea ultricies laoreet; est fusce vulputate at bibendum. Habitant etiam sodales consectetur ex vitae vestibulum. Consequat congue donec sodales, taciti inceptos cras nisi bibendum. Imperdiet primis ultricies feugiat; donec ex tellus.
Lorem ipsum odor amet, consectetuer adipiscing elit. Non ornare leo nulla lectus feugiat neque! Egestas leo proin iaculis tincidunt dis risus morbi blandit. Neque sollicitudin ut fusce magnis a habitant netus at. Erat aptent facilisis senectus semper euismod arcu hac. Interdum vivamus ultricies sollicitudin enim natoque mauris sed nisi. Inceptos purus neque venenatis odio primis duis per fusce. Accumsan ullamcorper vitae dignissim dolor phasellus fames natoque vitae mauris. Senectus rutrum imperdiet class aptent aliquet.
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. |