Accordion

A stack of headings that each show or hide a section of content. Use it to keep a long page short.

This panel is open when the page loads.

Usage

Code
import { Accordion } from '@leverui/components';
Code
<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

This panel is open when the page loads.

With Icon

Here's an example of rendering a custom icon in each accordion item.

Product details

A lightweight running shoe that weighs 240 g. More than 12,000 pairs were sold this year.

Expand Multiple Items

Use the multiple prop to allow multiple items to be expanded at once.

This panel is open when the page loads.

Sizes

Use the size prop to change the size of the accordion.

sm

This panel is open when the page loads.

md

This panel is open when the page loads.

lg

This panel is open when the page loads.

Variants

Use the variant prop to change the visual style of the accordion. Values can be either outline, subtle, enclosed or plain.

outline

This panel is open when the page loads.

subtle

This panel is open when the page loads.

enclosed

This panel is open when the page loads.

plain

This panel is open when the page loads.

Disabled Item

Pass the disabled prop to any Accordion.Item to prevent it from being expanded or collapsed.

This panel is open when the page loads.

With Avatar

Here's an example of composing an accordion with an avatar.

With Subtext

Here's an example of adding a subtext to an accordion item.

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.

Styling Expanded Style

Use the data-state='open' selector on an Accordion.ItemTrigger or Accordion.Item to style it when expanded.

The title gets a muted gray background while its item is open.

Guides

Accessing Root State

Use useAccordionContext inside Accordion.Root. It returns a function. Call it on every read so the text updates.

Code
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.

Code
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

PropDefaultTypeDescription
orientationvertical'horizontal' | 'vertical'Layout direction of the items.
colorPalettegraystringColor palette, built-in or your own.
variantoutline'outline' | 'subtle' | 'enclosed' | 'plain' or responsive mapAccordion style.
sizemd'sm' | 'md' | 'lg' or responsive mapItem size.
asdivNative HTML tagHTML tag to render.
asChild—Solid render functionRender your own element with the accordion's props.
unstyledfalsebooleanTurn off the Lever UI styles.
collapsiblefalsebooleanLet people close the open item.
defaultValue—string[]Values of the items open at first.
disabledfalsebooleanDisable every item.
id—stringUnique id for this accordion.
ids—Element ID mapYour own ids for the root, item, trigger, or content.
lazyMountfalsebooleanRender an item's content when it first opens.
multiplefalsebooleanAllow more than one open item.
onFocusChange—(details) => voidCalled when focus moves to another item.
onValueChange—(details) => voidCalled when the open items change.
unmountOnExitfalsebooleanRemove an item's content after it closes.
value—string[]Values of the open items, when you control them.

Item

PropDefaultTypeDescription
valuerequiredstringUnique value for the item. Should not change.
asdivNative HTML tagHTML tag to render.
asChild—Solid render functionRender your own element with the item's props.
disabledfalsebooleanStop this item from opening or closing.

Explorer

Click a part name to highlight it in the preview.

Product details

A lightweight running shoe that weighs 240 g. More than 12,000 pairs were sold this year.

Component Anatomy

Hover to highlight, click to select parts