Accordion
A stack of headings that each show or hide a section of content. Use it to keep a long page short.
Usage
import { Accordion } from '@leverui/components';
<Accordion.Root>
<Accordion.Item value="item-1">
<Accordion.ItemTrigger>
<Accordion.ItemIndicator />
</Accordion.ItemTrigger>
<Accordion.ItemContent>
<Accordion.ItemBody />
</Accordion.ItemContent>
</Accordion.Item>
</Accordion.Root>
Examples
Controlled
Set the accordion that shows up by default.
Expanded: second-item
With Icon
Here's an example of rendering a custom icon in each accordion item.
Product details
Expand Multiple Items
Use the multiple prop to allow multiple items to be expanded at once.
Sizes
Use the size prop to change the size of the accordion.
sm
md
lg
Variants
Use the variant prop to change the visual style of the accordion. Values can be either outline, subtle, enclosed or plain.
outline
subtle
enclosed
plain
Disabled Item
Pass the disabled prop to any Accordion.Item to prevent it from being expanded or collapsed.
With Avatar
Here's an example of composing an accordion with an avatar.
Part of the customer support team. Answers questions about orders, billing and returns, and replies to most messages within one working day. Send a message if you need help with something the help pages do not cover.
Part of the customer support team. Answers questions about orders, billing and returns, and replies to most messages within one working day. Send a message if you need help with something the help pages do not cover.
Part of the customer support team. Answers questions about orders, billing and returns, and replies to most messages within one working day. Send a message if you need help with something the help pages do not cover.
With Subtext
Here's an example of adding a subtext to an accordion item.
The small line under the title gives a hint about what is inside. Keep the hint to a few words so the title stays easy to scan. When the item opens, the panel shows the full details, and the hint stays where it is so people can still see which item they are reading.
The small line under the title gives a hint about what is inside. Keep the hint to a few words so the title stays easy to scan. When the item opens, the panel shows the full details, and the hint stays where it is so people can still see which item they are reading.
The small line under the title gives a hint about what is inside. Keep the hint to a few words so the title stays easy to scan. When the item opens, the panel shows the full details, and the hint stays where it is so people can still see which item they are reading.
With Actions
Here's an example of adding actions to an accordion item trigger.
The button beside the title works on its own. Clicking it does not open or close this item. Use a button like this for a quick task, such as editing or sharing the item, so people can do it without opening the panel first. The rest of the row still opens and closes the panel as usual.
The button beside the title works on its own. Clicking it does not open or close this item. Use a button like this for a quick task, such as editing or sharing the item, so people can do it without opening the panel first. The rest of the row still opens and closes the panel as usual.
The button beside the title works on its own. Clicking it does not open or close this item. Use a button like this for a quick task, such as editing or sharing the item, so people can do it without opening the panel first. The rest of the row still opens and closes the panel as usual.
Styling Expanded Style
Use the data-state='open' selector on an Accordion.ItemTrigger or Accordion.Item to style it when expanded.
Guides
Accessing Root State
Use useAccordionContext inside Accordion.Root. It returns a function. Call
it on every read so the text updates.
import { Accordion, Box, useAccordionContext } from '@leverui/components';
function AccordionValueText() {
const accordion = useAccordionContext();
return <Box>Opened: {accordion().value.join(', ')}</Box>;
}
export function Demo() {
return (
<Accordion.Root collapsible defaultValue={['item-1']}>
<AccordionValueText />
<Accordion.Item value="item-1">
<Accordion.ItemTrigger>
Details <Accordion.ItemIndicator />
</Accordion.ItemTrigger>
<Accordion.ItemContent>
<Accordion.ItemBody>Content</Accordion.ItemBody>
</Accordion.ItemContent>
</Accordion.Item>
</Accordion.Root>
);
}
Accessing Item State
useAccordionItemContext returns a function. Call it inside the item so the
label updates.
import { Accordion, Box, useAccordionItemContext } from '@leverui/components';
function AccordionItemStatus() {
const item = useAccordionItemContext();
return (
<Box
style={{
color: item().expanded
? 'var(--leverui-colors-green-fg)'
: 'var(--leverui-colors-fg-muted)',
}}
>
{item().expanded ? 'Expanded' : 'Collapsed'}
</Box>
);
}
export function Demo() {
return (
<Accordion.Root collapsible>
<Accordion.Item value="item-1">
<Accordion.ItemTrigger>
<AccordionItemStatus />
<Accordion.ItemIndicator />
</Accordion.ItemTrigger>
<Accordion.ItemContent>
<Accordion.ItemBody>Content</Accordion.ItemBody>
</Accordion.ItemContent>
</Accordion.Item>
</Accordion.Root>
);
}
Props
Root
| Prop | Default | Type | Description |
|---|---|---|---|
orientation | vertical | 'horizontal' | 'vertical' | Layout direction of the items. |
colorPalette | gray | string | Color palette, built-in or your own. |
variant | outline | 'outline' | 'subtle' | 'enclosed' | 'plain' or responsive map | Accordion style. |
size | md | 'sm' | 'md' | 'lg' or responsive map | Item size. |
as | div | Native HTML tag | HTML tag to render. |
asChild | — | Solid render function | Render your own element with the accordion's props. |
unstyled | false | boolean | Turn off the Lever UI styles. |
collapsible | false | boolean | Let people close the open item. |
defaultValue | — | string[] | Values of the items open at first. |
disabled | false | boolean | Disable every item. |
id | — | string | Unique id for this accordion. |
ids | — | Element ID map | Your own ids for the root, item, trigger, or content. |
lazyMount | false | boolean | Render an item's content when it first opens. |
multiple | false | boolean | Allow more than one open item. |
onFocusChange | — | (details) => void | Called when focus moves to another item. |
onValueChange | — | (details) => void | Called when the open items change. |
unmountOnExit | false | boolean | Remove an item's content after it closes. |
value | — | string[] | Values of the open items, when you control them. |
Item
| Prop | Default | Type | Description |
|---|---|---|---|
value | required | string | Unique value for the item. Should not change. |
as | div | Native HTML tag | HTML tag to render. |
asChild | — | Solid render function | Render your own element with the item's props. |
disabled | false | boolean | Stop this item from opening or closing. |
Explorer
Click a part name to highlight it in the preview.
Product details
Component Anatomy
Hover to highlight, click to select parts