Skip to content
Esc
↑↓navigate↵open⌘Jpreview
On this page

Accordion for Astro

Build Astro accordions for FAQs and expandable content. Preview examples, install with shadcn, and read the keyboard and composition guide.

API Reference

Installation

bunx --bun shadcn@latest add @gnostic/accordion
npx shadcn@latest add @gnostic/accordion
pnpm dlx shadcn@latest add @gnostic/accordion

Component classes: Default Gnostic.

Complete the Installation guide first. Then copy the component files below.

Install component packages

bun add @data-slot/accordion

Copy the files

src/components/ui/accordion/accordion-content.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
---
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
---
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
---
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
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
import type { AccordionOptions } from '@data-slot/accordion'

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

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.

FAQ

API Reference

AccordionProps

PropType
multiple?boolean

Allow multiple items open at once

Typeboolean
defaultValue?string | string[]

Initially expanded item(s)

Typestring | string[]
disabled?boolean

Disable the entire accordion

Typeboolean
orientation?AccordionOrientation

Accordion orientation for roving focus

TypeAccordionOrientation
loopFocus?boolean

Whether arrow-key focus wraps at the ends

Typeboolean
hiddenUntilFound?boolean

Use hidden="until-found" on closed panels

Typeboolean

See the data-slot docs 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 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.