Join our Discord Community

Accordion

A vertically stacked set of interactive headings that each reveal a section of content.

Loading component...

Installation

npx shadcn@latest add accordion

Usage

import {
  Accordion,
  AccordionContent,
  AccordionItem,
  AccordionTrigger,
} from "@/components/ui/accordion"

export function AccordionDemo() {
  return (
    <Accordion type="single" collapsible className="w-full">
      <AccordionItem value="item-1">
        <AccordionTrigger>Is it accessible?</AccordionTrigger>
        <AccordionContent>
          Yes. It adheres to the WAI-ARIA design pattern.
        </AccordionContent>
      </AccordionItem>
      <AccordionItem value="item-2">
        <AccordionTrigger>Is it styled?</AccordionTrigger>
        <AccordionContent>
          Yes. It comes with default styles that matches the other components' aesthetic.
        </AccordionContent>
      </AccordionItem>
      <AccordionItem value="item-3">
        <AccordionTrigger>Is it animated?</AccordionTrigger>
        <AccordionContent>
          Yes. It's animated by default, but you can disable it if you prefer.
        </AccordionContent>
      </AccordionItem>
    </Accordion>
  )
}

API Reference

Accordion

The root component that wraps all accordion items.

PropTypeDefaultDescription
type"single" | "multiple"-Determines whether one or multiple items can be opened at the same time.
valuestring | string[]-The controlled value of the item(s) to expand.
defaultValuestring | string[]-The value of the item(s) to expand when initially rendered (uncontrolled).
collapsiblebooleanfalseWhen type is "single", allows closing content when clicking the trigger of an open item.
disabledbooleanfalseWhen true, prevents the user from interacting with the accordion.
dir"ltr" | "rtl""ltr"The reading direction of the accordion.
orientation"horizontal" | "vertical""vertical"The orientation of the accordion.
asChildbooleanfalseMerge props and behavior onto a child element.
onValueChange(value: string | string[]) => void-Event handler called when the expanded state changes.

AccordionItem

A single collapsible item within the accordion.

PropTypeDefaultDescription
valuestring-A unique value for the item.
disabledbooleanfalseWhen true, prevents the user from interacting with the item.
asChildbooleanfalseMerge props and behavior onto a child element.

AccordionTrigger

The button that toggles the collapsed state of an item.

PropTypeDefaultDescription
asChildbooleanfalseMerge props and behavior onto a child element.

AccordionContent

The collapsible content area.

PropTypeDefaultDescription
asChildbooleanfalseMerge props and behavior onto a child element.
forceMountboolean-Used to force mount when more control is needed.

Accessibility

The accordion component follows the WAI-ARIA Accordion Pattern, including:

Keyboard Interactions

KeyDescription
SpaceWhen focus is on an accordion trigger, activates the trigger.
EnterWhen focus is on an accordion trigger, activates the trigger.
TabMoves focus to the next focusable element.
Shift + TabMoves focus to the previous focusable element.
Arrow DownWhen focus is on a trigger, moves focus to the next trigger.
Arrow UpWhen focus is on a trigger, moves focus to the previous trigger.
HomeWhen focus is on a trigger, moves focus to the first trigger.
EndWhen focus is on a trigger, moves focus to the last trigger.

ARIA Attributes

The component automatically handles the following ARIA attributes:

  • role="region" on the content area
  • aria-controls linking triggers to their content
  • aria-expanded indicating the expanded state
  • aria-disabled when items are disabled

Customization

Styling

The accordion component uses CSS variables for theming and can be customized using Tailwind classes:

<Accordion className="w-full">
  <AccordionItem className="border-b">
    <AccordionTrigger className="text-left">
      Custom styled trigger
    </AccordionTrigger>
    <AccordionContent className="text-muted-foreground">
      Custom styled content
    </AccordionContent>
  </AccordionItem>
</Accordion>

Animation

The expand/collapse animation can be customized through CSS:

[data-state="open"] {
  animation: accordion-down 0.2s ease-out;
}

[data-state="closed"] {
  animation: accordion-up 0.2s ease-out;
}

Examples

Single Mode (Non-collapsible)

Loading component...

Multiple Mode

Loading component...

Controlled State

Loading component...

Custom Styling

Loading component...

SEO Considerations

For better SEO, consider:

  1. Semantic HTML: The accordion uses proper heading hierarchy and semantic markup.
  2. Content Visibility: All accordion content is in the DOM (even when collapsed), making it accessible to search engines.
  3. Structured Data: Consider adding FAQ schema markup when using accordions for FAQs:
<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "FAQPage",
  "mainEntity": [{
    "@type": "Question",
    "name": "Question text",
    "acceptedAnswer": {
      "@type": "Answer",
      "text": "Answer text"
    }
  }]
}
</script>
  1. Progressive Enhancement: The accordion content remains accessible even without JavaScript.