---
title: Accordion for Astro
description: "Build Astro accordions for FAQs and expandable content. Preview examples, install with shadcn, and read the keyboard and composition guide."
seo:
    title: Accordion for Astro | Shadcn & Tailwind CSS
related:
    - /components/collapsible
    - /components/tabs
sidebar:
    label: "Accordion"
---

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

<Accordion class="max-w-lg">
  <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>
  <AccordionItem value="item-2">
    <AccordionTrigger>Can the section use my site colors?</AccordionTrigger>
    <AccordionContent>
      The section reads shared text, border, and focus colors from the active
      theme.
    </AccordionContent>
  </AccordionItem>
  <AccordionItem value="item-3">
    <AccordionTrigger>How does the panel open?</AccordionTrigger>
    <AccordionContent>
      The panel uses its measured height for the transition. Reduced-motion
      settings shorten the animation.
    </AccordionContent>
  </AccordionItem>
</Accordion>
```

## Installation

### Command

```sh
bunx --bun shadcn@latest add @gnostic/accordion
```

### Manual

Complete the [Installation guide](/docs/installation) first. Then copy the component files below.

Install the component packages:

```sh
bun add @data-slot/accordion
```

#### src/components/ui/accordion/accordion-content.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, hidden = true, ...props } = Astro.props
---

<div
  data-slot="accordion-content"
  hidden={hidden}
  class="h-(--accordion-panel-height) overflow-hidden text-muted-foreground text-sm transition-[height] duration-200 ease-in-out data-ending-style:h-0 data-starting-style:h-0"
  {...props}
>
  <div
    data-slot="accordion-content-inner"
    class={cn("pt-0 pb-4 [&_a]:underline [&_a]:underline-offset-3 [&_a]:hover:text-foreground [&_p:not(:last-child)]:mb-4", className)}
  >
    <slot />
  </div>
</div>
```

#### src/components/ui/accordion/accordion-item.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div"> & {
  value: string
  disabled?: boolean
}

const { class: className, value, disabled, ...props } = Astro.props
---

<div
  data-slot="accordion-item"
  data-value={value}
  data-disabled={disabled ? "" : undefined}
  class={cn("border-b last:border-b-0", className)}
  {...props}
>
  <slot />
</div>
```

#### src/components/ui/accordion/accordion-trigger.astro

```astro
---
import type { HTMLAttributes } from "astro/types"
import ChevronDownIcon from "@lucide/astro/icons/chevron-down"

import { cn } from "cn"

type Props = HTMLAttributes<"button">

const { class: className, type = "button", ...props } = Astro.props
---

<button
  data-slot="accordion-trigger"
  type={type}
  class={cn("flex flex-1 cursor-pointer items-start justify-between gap-4 rounded-md py-4 text-left font-medium text-sm outline-none transition-all focus-visible:ring-[3px] focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-64 data-panel-open:*:data-[slot=accordion-indicator]:rotate-180 w-full aria-disabled:pointer-events-none aria-disabled:opacity-64 group/accordion-trigger", className)}
  {...props}
>
  <slot />
  <ChevronDownIcon
    data-slot="accordion-indicator"
    aria-hidden="true"
    class="pointer-events-none size-4 shrink-0 translate-y-0.5 opacity-80 transition-transform duration-200 ease-in-out"
  />
</button>
```

#### src/components/ui/accordion/accordion.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"
import type { AccordionProps } from "./types.ts"

type Props = HTMLAttributes<"div"> & AccordionProps

const {
  class: className,
  multiple,
  defaultValue,
  disabled,
  orientation = "vertical",
  loopFocus,
  hiddenUntilFound,
  ...props
} = Astro.props

const serializedDefaultValue = Array.isArray(defaultValue)
  ? JSON.stringify(defaultValue)
  : defaultValue
---

<div
  data-slot="accordion"
  data-multiple={multiple}
  data-default-value={serializedDefaultValue}
  data-disabled={disabled}
  data-orientation={orientation}
  data-loop-focus={loopFocus}
  data-hidden-until-found={hiddenUntilFound}

  class={cn("flex w-full flex-col", className)}
  {...props}
>
  <slot />
</div>

<script>
  import { create } from "@data-slot/accordion"
  import type { AccordionController } from "@data-slot/accordion"

  // Destroy bindings before a view transition so the next page-load binds once.
  const controllers: AccordionController[] = []
  const initialize = () => controllers.push(...create())
  initialize()
  document.addEventListener("astro:page-load", initialize)
  document.addEventListener("astro:before-swap", () => {
    for (const controller of controllers) controller.destroy()
    controllers.length = 0
  })
</script>
```

#### src/components/ui/accordion/index.ts

```ts
export { default as Accordion } from './accordion.astro'
export { default as AccordionContent } from './accordion-content.astro'
export { default as AccordionItem } from './accordion-item.astro'
export { default as AccordionTrigger } from './accordion-trigger.astro'
export type { AccordionProps } from './types.ts'
```

#### src/components/ui/accordion/types.ts

```ts
import type { AccordionOptions } from '@data-slot/accordion'

/**
 * Accordion configuration rendered as data attributes.
 */
export type AccordionProps = Pick<
    AccordionOptions,
    | 'multiple'
    | 'defaultValue'
    | 'disabled'
    | 'orientation'
    | 'loopFocus'
    | 'hiddenUntilFound'
>
```

Component classes use the preset configured during project setup. Select the same preset in the docs before copying Manual source. To change installed component classes, edit or reinstall their source. See [Theming](/docs/theming) for details.


## Usage

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

```astro
<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`:

```text
Accordion
├── AccordionItem
│   ├── AccordionTrigger
│   └── AccordionContent
└── AccordionItem
    ├── AccordionTrigger
    └── AccordionContent
```

## Examples

### Multiple

Add `multiple` when readers need to compare answers without closing the previous item.

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

<Accordion class="w-full max-w-sm" multiple>
  <AccordionItem value="item-1">
    <AccordionTrigger>Account</AccordionTrigger>
    <AccordionContent>
      Manage your profile, billing details, and email preferences.
    </AccordionContent>
  </AccordionItem>
  <AccordionItem value="item-2">
    <AccordionTrigger>Security</AccordionTrigger>
    <AccordionContent>
      Update your password, passkeys, and sign-in methods.
    </AccordionContent>
  </AccordionItem>
  <AccordionItem value="item-3">
    <AccordionTrigger>Notifications</AccordionTrigger>
    <AccordionContent>
      Choose which product updates and alerts you want to receive.
    </AccordionContent>
  </AccordionItem>
</Accordion>
```

### With Disabled

Set `disabled` on an `AccordionItem` when its trigger must be unavailable.

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

<Accordion class="w-full max-w-sm">
  <AccordionItem value="item-1">
    <AccordionTrigger>Available item</AccordionTrigger>
    <AccordionContent>
      This item can be opened and closed normally.
    </AccordionContent>
  </AccordionItem>
  <AccordionItem value="item-2" disabled>
    <AccordionTrigger>Disabled item</AccordionTrigger>
    <AccordionContent>
      This panel stays unavailable to keyboard and pointer interaction.
    </AccordionContent>
  </AccordionItem>
  <AccordionItem value="item-3">
    <AccordionTrigger>Another item</AccordionTrigger>
    <AccordionContent>
      You can still interact with the other items in the list.
    </AccordionContent>
  </AccordionItem>
</Accordion>
```

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

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

<Accordion class="w-full max-w-sm rounded-lg border px-4">
  <AccordionItem class="border-b last:border-b-0" value="item-1">
    <AccordionTrigger>Design system</AccordionTrigger>
    <AccordionContent>
      Keep styles, spacing, and behavior consistent across your UI.
    </AccordionContent>
  </AccordionItem>
  <AccordionItem class="border-b last:border-b-0" value="item-2">
    <AccordionTrigger>Accessibility</AccordionTrigger>
    <AccordionContent>
      Ensure controls are readable and keyboard friendly.
    </AccordionContent>
  </AccordionItem>
  <AccordionItem class="border-b last:border-b-0" value="item-3">
    <AccordionTrigger>Performance</AccordionTrigger>
    <AccordionContent>
      Only reveal the content people need when they need it.
    </AccordionContent>
  </AccordionItem>
</Accordion>
```

### In Card

Wrap the `Accordion` in a `Card` component.

```astro
---
import {
  Accordion,
  AccordionContent,
  AccordionItem,
  AccordionTrigger,
} from "@/components/ui/accordion"
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
---

<Card class="w-full max-w-sm">
  <CardHeader>
    <CardTitle>FAQ</CardTitle>
  </CardHeader>
  <CardContent>
    <Accordion>
      <AccordionItem value="item-1">
        <AccordionTrigger>Do you offer support?</AccordionTrigger>
        <AccordionContent>
          Yes. Support is included for all active subscriptions.
        </AccordionContent>
      </AccordionItem>
      <AccordionItem value="item-2">
        <AccordionTrigger>Can I cancel anytime?</AccordionTrigger>
        <AccordionContent>
          Yes. You can cancel your subscription whenever you want.
        </AccordionContent>
      </AccordionItem>
      <AccordionItem value="item-3">
        <AccordionTrigger>Is there a free trial?</AccordionTrigger>
        <AccordionContent>
          A trial is available for eligible plans and new accounts.
        </AccordionContent>
      </AccordionItem>
    </Accordion>
  </CardContent>
</Card>
```

## API Reference

### AccordionProps

<AutoTypeTable
    path="../../packages/ui/src/components/ui/accordion/types.ts"
    name="AccordionProps"
/>

See the [data-slot docs](https://github.com/bejamas/data-slot/blob/main/packages/accordion/README.md) for more information on interactivity.

### Events

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](/r/accordion.json) 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.
