Accordion for Astro
Build Astro accordions for FAQs and expandable content. Preview examples, install with shadcn, and read the keyboard and composition guide.
Installation
bunx --bun shadcn@latest add @gnostic/accordionnpx shadcn@latest add @gnostic/accordionpnpm dlx shadcn@latest add @gnostic/accordionInstall component packages
bun add @data-slot/accordionnpm install @data-slot/accordionpnpm add @data-slot/accordionCopy the files
src/components/ui/accordion/accordion-content.astrosrc/components/ui/accordion/accordion-item.astrosrc/components/ui/accordion/accordion-trigger.astrosrc/components/ui/accordion/accordion.astrosrc/components/ui/accordion/index.tssrc/components/ui/accordion/types.ts
Usage
import {
Accordion,
AccordionContent,
AccordionItem,
AccordionTrigger,
} from "@/components/ui/accordion"
<Accordion>
<AccordionItem value="item-1">
<AccordionTrigger>How do I open a question with the keyboard?</AccordionTrigger>
<AccordionContent>
Use Tab to reach the question, then Enter or Space to open its answer.
</AccordionContent>
</AccordionItem>
</Accordion>
Composition
Arrange the parts as shown for an Accordion:
Accordion
├── AccordionItem
│ ├── AccordionTrigger
│ └── AccordionContent
└── AccordionItem
├── AccordionTrigger
└── AccordionContent
Examples
Multiple
Add multiple when readers need to compare answers without closing the previous item.
With Disabled
Set disabled on an AccordionItem when its trigger must be unavailable.
With Borders
For this divided layout, put border on the root and border-b last:border-b-0 on each item. The last item uses the root border as its lower edge.
In Card
Wrap the Accordion in a Card component.
API Reference
AccordionProps
multiple?boolean
Allow multiple items open at once
booleandefaultValue?string | string[]
Initially expanded item(s)
string | string[]disabled?boolean
Disable the entire accordion
booleanorientation?AccordionOrientation
Accordion orientation for roving focus
AccordionOrientationloopFocus?boolean
Whether arrow-key focus wraps at the ends
booleanhiddenUntilFound?boolean
Use hidden="until-found" on closed panels
booleanSee the data-slot docs for more information on interactivity.
AccordionTrigger slots
indicator?Astro content
Replaces the icon beside the trigger label.
Astro contentChevron downEvents
Dispatch these events on the accordion root. The root emits the outbound events.
Outbound events
| Event | Detail |
|---|---|
accordion:change |
{ value: string[] } |
Inbound events
| Event | Detail |
|---|---|
accordion:set |
{ value: string | string[] } |
Read the installed source for the complete prop types.
When to use Accordion
Group related questions with Accordion items. Give each trigger a clear question or topic and place its answer in the matching content part. Keep the item structure from the examples so the controller can connect each trigger to its panel.
Keyboard and behavior
The data-slot controller handles trigger activation and expanded state. Use real trigger buttons so Enter and Space work. Keep essential instructions outside closed panels when the user needs them to complete a form. Test keyboard movement when items are disabled or added dynamically.
Theme and layout
Borders separate items and the indicator rotates with data-panel-open. Panel height uses the controller variable, so long content can animate without a fixed height. Theme changes affect text, focus, and borders. Do not apply a second height animation to the content wrapper.