---
title: Expandable for Astro
description: Open a panel in place and animate its contents with one of five effects.
seo:
    title: Expandable for Astro | Shadcn & Tailwind CSS
related:
    - /components/collapsible
    - /components/reveal
sidebar:
    label: Expandable
---

```astro
---
import ChevronDown from "@lucide/astro/icons/chevron-down"
import {
  Expandable,
  ExpandableContent,
  ExpandableTrigger,
} from "@/components/ui/expandable"

const agenda = [
  { time: "10:00", topic: "Review the first sketches" },
  { time: "10:15", topic: "Compare the two layouts" },
  { time: "10:30", topic: "Choose the next steps" },
]
---

<Expandable class="w-72 max-w-full rounded-xl border bg-card text-card-foreground open:w-80 motion-safe:transition-[width] motion-safe:duration-400">
  <ExpandableTrigger class="flex items-center justify-between gap-4 rounded-xl px-5 py-4 font-medium focus-visible:outline-ring">
    Workshop agenda
    <ChevronDown
      aria-hidden="true"
      class="size-4 text-muted-foreground group-open/expandable:rotate-180 motion-safe:transition-transform"
    />
  </ExpandableTrigger>
  <ExpandableContent effect="slide-up" stagger staggerDelay={0.07}>
    {agenda.map(({ time, topic }) => (
      <p class="flex gap-4 border-t px-5 py-3 text-sm">
        <time class="text-muted-foreground tabular-nums">{time}</time>
        {topic}
      </p>
    ))}
  </ExpandableContent>
</Expandable>
```

## Installation

### Command

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

### Manual

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

No extra packages are required.

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

```astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
import type { ExpandableContentProps } from "./types.ts"

type Props = HTMLAttributes<"div"> & ExpandableContentProps

const {
  class: className,
  effect = "fade",
  stagger = false,
  staggerDelay = 0.04,
  ...props
} = Astro.props
const delay = Number.isFinite(staggerDelay) ? Math.max(0, staggerDelay) : 0.04
---

<div
  data-slot="expandable-content"
  data-effect={effect}
  class={cn("min-h-0 overflow-hidden", className)}
  {...props}
>
  <div
    data-expandable-body
    data-stagger={stagger || undefined}
    style={stagger ? { "--expandable-delay": delay + "s" } : undefined}
  >
    <slot />
  </div>
</div>

<style is:global>
  @media (prefers-reduced-motion: no-preference) {
    [data-slot="expandable-content"] {
      --expandable-transform: none;
      --expandable-filter: none;

      &[data-effect="blur"] {
        --expandable-filter: blur(6px);
      }
      &[data-effect="slide-up"] {
        --expandable-transform: translateY(12px);
      }
      &[data-effect="slide-down"] {
        --expandable-transform: translateY(-12px);
      }
      &[data-effect="scale"] {
        --expandable-transform: scale(0.96);
      }

      > [data-expandable-body]:not([data-stagger]),
      > [data-expandable-body][data-stagger] > * {
        transition:
          opacity 250ms,
          transform 250ms,
          filter 250ms;
      }

      [data-slot="expandable"]:not([open]) > & {
        > [data-expandable-body]:not([data-stagger]),
        > [data-expandable-body][data-stagger] > * {
          opacity: 0;
          transform: var(--expandable-transform);
          filter: var(--expandable-filter);
        }
      }

      /* The browser can skip style calculation inside a closed details element. */
      @starting-style {
        [data-slot="expandable"][open]:not([data-initial-open]) > & {
          > [data-expandable-body]:not([data-stagger]),
          > [data-expandable-body][data-stagger] > * {
            opacity: 0;
            transform: var(--expandable-transform);
            filter: var(--expandable-filter);
          }
        }
      }

      [data-slot="expandable"][open]
        > &
        > [data-expandable-body][data-stagger]
        > * {
        transition-delay: calc((sibling-index() - 1) * var(--expandable-delay));
      }
    }
  }
</style>
```

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

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

type Props = HTMLAttributes<"summary">

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

<summary
  data-slot="expandable-trigger"
  class={cn("cursor-pointer", className)}
  {...props}
>
  <slot />
</summary>
```

#### src/components/ui/expandable/expandable.astro

```astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
import type { ExpandableProps } from "./types.ts"

type Props = HTMLAttributes<"details"> & ExpandableProps

const {
  class: className,
  defaultOpen = false,
  open = defaultOpen,
  ...props
} = Astro.props
---

<details
  data-slot="expandable"
  class={cn("group/expandable", className)}
  data-initial-open={open || undefined}
  {open}
  {...props}
>
  <slot />
</details>

<style>
  @media (prefers-reduced-motion: no-preference) {
    details::details-content {
      display: grid;
      grid-template-rows: minmax(0, 0fr);
      overflow: clip;
      transition:
        grid-template-rows 400ms cubic-bezier(0.22, 1, 0.36, 1),
        content-visibility 400ms allow-discrete;
    }

    details[open]::details-content {
      grid-template-rows: minmax(0, 1fr);
    }
  }
</style>
```

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

```ts
export { default as Expandable } from './expandable.astro'
export { default as ExpandableContent } from './expandable-content.astro'
export { default as ExpandableTrigger } from './expandable-trigger.astro'
export type {
    ExpandableContentProps,
    ExpandableEffect,
    ExpandableProps,
} from './types.ts'
```

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

```ts
/** Initial state of the disclosure. */
export interface ExpandableProps {
    /**
     * Render the panel open on first load.
     * @default false
     */
    defaultOpen?: boolean
}

/** Motion effect for the panel contents. */
export type ExpandableEffect =
    | 'fade'
    | 'blur'
    | 'slide-up'
    | 'slide-down'
    | 'scale'

/** Options for the content that enters or leaves the panel. */
export interface ExpandableContentProps {
    /**
     * Effect to apply when the panel opens or closes.
     * @default 'fade'
     */
    effect?: ExpandableEffect
    /**
     * Animate each direct child element in sequence when the panel opens.
     * @default false
     */
    stagger?: boolean
    /**
     * Time in seconds between the start of each child effect.
     * @default 0.04
     */
    staggerDelay?: number
}
```

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

```astro
---
import { Expandable, ExpandableContent, ExpandableTrigger } from "@/components/ui/expandable"
---

<Expandable defaultOpen>
  <ExpandableTrigger>Meeting location</ExpandableTrigger>
  <ExpandableContent effect="fade">
    <p>Meet in the workshop on the first floor.</p>
  </ExpandableContent>
</Expandable>
```

Place the trigger first and the content second, both as direct children of
`Expandable`. The parts render as `details`, `summary`, and `div`. The browser
handles clicks, Enter, and Space. To control the panel from a script, set the
native `open` property and listen for the `toggle` event on the root.

## Motion

The panel animates to the height of its content. Set `effect` to `fade`, `blur`,
`slide-up`, `slide-down`, or `scale`. Set `stagger` to show direct child
elements in sequence. The delay is in seconds. Children exit together.

Use the Tailwind `open:` variant to change the width of the root. Use
`group-open/expandable:` to style an element inside the trigger.
Reduced motion skips the panel and content transitions. The disclosure works
with JavaScript disabled. Browsers without CSS `sibling-index()` show the
children together.

## API Reference

<AutoTypeTable path="../../packages/ui/src/components/ui/expandable/types.ts" name="ExpandableProps" />

<AutoTypeTable path="../../packages/ui/src/components/ui/expandable/types.ts" name="ExpandableContentProps" />

[View the registry source](/ui/r/expandable.json).
