Show and hide a panel, preserve its contents, and animate its height.

Use a collapsible to show and hide one panel of content. For a set of panels, use an accordion.

import { Component } from "@angular/core"
import { LucideChevronDown } from "@lucide/angular"
import {
  Collapsible,
  CollapsiblePanel,
  CollapsibleTrigger,
} from "@ui-sdk/angular/collapsible"

@Component({
  imports: [
    Collapsible,
    CollapsiblePanel,
    CollapsibleTrigger,
    LucideChevronDown,
  ],
  template: `
    <div uiCollapsible data-variant="card">
      <button uiCollapsibleTrigger>
        <span data-slot="stack">
          <span>Order #4189</span>
          <span data-slot="hint">3 items, $84.00</span>
        </span>
        <svg lucideChevronDown></svg>
      </button>
      <div uiCollapsiblePanel>
        @for (item of items; track item.name) {
          <div data-slot="row">
            <span>{{ item.name }}</span>
            <span>{{ item.price }}</span>
          </div>
        }
      </div>
    </div>
  `,
})
class Example {
  readonly items = [
    { name: "Linen shirt", price: "$48.00" },
    { name: "Canvas tote", price: "$18.00" },
    { name: "Wool socks", price: "$18.00" },
  ]
}

export { Example }
Press Order #4189, or focus it and press Enter or Space, to show the items.

Installation

npm install @ui-sdk/angular @ui-sdk/core

Composition

Use the following composition to build Collapsible:

uiCollapsible
├── uiCollapsibleTrigger
└── uiCollapsiblePanel

The root and panel render divs, and the trigger renders a native button. Put other content around them, like a heading or a summary that always shows.

Usage

Import the parts and put them together:

import { Component } from "@angular/core"
import {
  Collapsible,
  CollapsiblePanel,
  CollapsibleTrigger,
} from "@ui-sdk/angular/collapsible"

@Component({
  selector: "app-example",
  imports: [Collapsible, CollapsiblePanel, CollapsibleTrigger],
  template: `
    <div uiCollapsible>
      <button uiCollapsibleTrigger>Order details</button>
      <div uiCollapsiblePanel>2 × Linen shirt</div>
    </div>
  `,
})
class Example {}

export { Example }

Build Your Own

@ui-sdk/angular/collapsible comes with a ready-made Collapsible. If you'd like to build your own, with your own names, start from the collapsible primitive in @ui-sdk/core:

spoiler.ts
import { Directive, inject } from "@angular/core"
import { collapsible } from "@ui-sdk/core/collapsible"
import { usePart, usePrimitive } from "@ui-sdk/angular/primitive"

@Directive({ selector: "[appSpoiler]" })
class Spoiler {
  readonly handle = usePrimitive(collapsible, () => ({}))
  readonly state = usePart(this.handle, "root").state
}

@Directive({ selector: "[appSpoilerToggle]" })
class SpoilerToggle {
  readonly state = usePart(inject(Spoiler).handle, "trigger").state
}

@Directive({ selector: "[appSpoilerContent]" })
class SpoilerContent {
  readonly state = usePart(inject(Spoiler).handle, "panel").state
}

export { Spoiler, SpoilerContent, SpoilerToggle }
import { Component } from "@angular/core"
import { Spoiler, SpoilerContent, SpoilerToggle } from "./spoiler"

@Component({
  selector: "app-spoiler-example",
  imports: [Spoiler, SpoilerContent, SpoilerToggle],
  template: `
    <div appSpoiler>
      <button appSpoilerToggle>Show the ending</button>
      <div *appSpoilerContent>The butler did it.</div>
    </div>
  `,
})
export class SpoilerExample {}

Connect the other slots the same way, each with usePart. Each part keeps the behavior, state and data attributes of its ready-made counterpart. See Rendering Primitives and Custom Parts for more.

Disabled

Set disabled on the root to stop its trigger from opening or closing the panel. The trigger stays in the tab order and reports aria-disabled="true". A disabled <fieldset> around the collapsible disables it too.

<div uiCollapsible disabled>
  <button uiCollapsibleTrigger>Gift options</button>
  <div uiCollapsiblePanel>Add a gift message and wrapping.</div>
</div>

Keep Mounted

Panels unmount when they close, so Tab skips their contents. Set keepMounted on the panel to keep it mounted and hidden, so a field keeps what you typed into it.

import { Component } from "@angular/core"
import { LucideChevronDown } from "@lucide/angular"
import {
  Collapsible,
  CollapsiblePanel,
  CollapsibleTrigger,
} from "@ui-sdk/angular/collapsible"
import { Field, FieldLabel } from "@ui-sdk/angular/field"
import { Textarea } from "@ui-sdk/angular/textarea"

@Component({
  imports: [
    Collapsible,
    CollapsiblePanel,
    CollapsibleTrigger,
    Field,
    FieldLabel,
    Textarea,
    LucideChevronDown,
  ],
  template: `
    <div uiCollapsible data-variant="card">
      <button uiCollapsibleTrigger>
        <span data-slot="stack">
          <span>Add a delivery note</span>
          <span data-slot="hint">Optional, for the courier</span>
        </span>
        <svg lucideChevronDown></svg>
      </button>
      <div uiCollapsiblePanel keepMounted>
        <div uiField>
          <label uiFieldLabel>Delivery note</label>
          <textarea
            uiTextarea
            placeholder="Leave it with the front desk…"
          ></textarea>
        </div>
      </div>
    </div>
  `,
})
class Example {}

export { Example }
Open Add a delivery note, type a note, then close and reopen the panel. Your note is still there.

Find in Page

Set hiddenUntilFound on the panel to keep it in the DOM as hidden="until-found". Browser Find and links to content inside the panel can reveal it. The panel opens without an animation so the match is visible immediately.

Server rendering keeps hidden="until-found" on the server, so the closed panel is findable before hydration.

import { Component } from "@angular/core"
import { LucideChevronDown } from "@lucide/angular"
import {
  Collapsible,
  CollapsiblePanel,
  CollapsibleTrigger,
} from "@ui-sdk/angular/collapsible"

@Component({
  imports: [
    Collapsible,
    CollapsiblePanel,
    CollapsibleTrigger,
    LucideChevronDown,
  ],
  template: `
    <div uiCollapsible data-variant="card">
      <button uiCollapsibleTrigger>
        <span data-slot="stack">
          <span>Ingredients</span>
          <span data-slot="hint">Morning buns, makes 12</span>
        </span>
        <svg lucideChevronDown></svg>
      </button>
      <div uiCollapsiblePanel hiddenUntilFound>
        <p>
          Flour, butter, brown sugar, eggs, a pinch of cardamom and sea salt.
        </p>
      </div>
    </div>
  `,
})
class Example {}

export { Example }
Leave Ingredients closed. Open your browser's Find command and search for cardamom to reveal the panel.

Controlled

Bind open with [(open)]. (beforeOpenChange) also gets the change details: call $event.cancel() in it to cancel the change.

For a button elsewhere on the page, create a controller with createCollapsibleController() and pass it as controller. Create one controller per mounted collapsible.

Console
import { Component, signal } from "@angular/core"
import { LucideChevronDown } from "@lucide/angular"
import { useLog } from "./log"
import {
  Collapsible,
  CollapsiblePanel,
  CollapsibleTrigger,
  createCollapsibleController,
} from "@ui-sdk/angular/collapsible"

@Component({
  imports: [
    Collapsible,
    CollapsiblePanel,
    CollapsibleTrigger,
    LucideChevronDown,
  ],
  template: `
    <div data-slot="stack">
      <div data-slot="controls">
        <button type="button" (click)="collapsible.toggle()">Toggle</button>
      </div>
      <div
        uiCollapsible
        data-variant="card"
        [(open)]="open"
        [controller]="collapsible"
        (beforeOpenChange)="
          log(($event.value ? 'Opened' : 'Closed') + ' (' + $event.reason + ')')
        "
      >
        <button uiCollapsibleTrigger>
          <span data-slot="stack">
            <span>Recovery codes</span>
            <span data-slot="hint">Each code signs you in once</span>
          </span>
          <svg lucideChevronDown></svg>
        </button>
        <div uiCollapsiblePanel>
          <div data-slot="stack">
            <ul>
              @for (code of recoveryCodes; track code) {
                <li>{{ code }}</li>
              }
            </ul>
            <button type="button" (click)="collapsible.close()">Done</button>
          </div>
        </div>
      </div>
    </div>
  `,
})
class Example {
  readonly recoveryCodes = ["7KQ2-M9XD", "P4TR-8HZC", "W3NB-6JUE", "C5YF-2LSA"]

  readonly log = useLog()

  readonly collapsible = createCollapsibleController()

  readonly open = signal(false)
}

export { Example }
Open Recovery codes with Toggle or its trigger, then press Done. The console logs each change and its reason, and Done returns focus to the trigger.

Animation

Animate the measured --collapsible-panel-height between zero and the content's height. data-starting-style marks the opening frame, and data-ending-style stays on while it closes. The panel stays mounted until its closing animation finishes, and a panel open on its first paint doesn't animate.

[data-slot="collapsible-panel"] {
  height: var(--collapsible-panel-height);
  overflow: hidden;
  transition: height 0.2s ease-out;

  &:is([data-starting-style], [data-ending-style]) {
    height: 0;
  }
}

(openChangeComplete) reports when opening or closing has finished.

Console
import { Component } from "@angular/core"
import { LucideChevronDown } from "@lucide/angular"
import { useLog } from "./log"
import {
  Collapsible,
  CollapsiblePanel,
  CollapsibleTrigger,
} from "@ui-sdk/angular/collapsible"

@Component({
  imports: [
    Collapsible,
    CollapsiblePanel,
    CollapsibleTrigger,
    LucideChevronDown,
  ],
  template: `
    <div
      uiCollapsible
      data-variant="card"
      (openChangeComplete)="
        log($event ? 'Finished opening' : 'Finished closing')
      "
    >
      <button uiCollapsibleTrigger>
        <span data-slot="stack">
          <span>Release notes</span>
          <span data-slot="hint">Version 2.4, released October 28</span>
        </span>
        <svg lucideChevronDown></svg>
      </button>
      <div uiCollapsiblePanel>
        <ul>
          @for (note of notes; track note) {
            <li>{{ note }}</li>
          }
        </ul>
      </div>
    </div>
  `,
})
class Example {
  readonly notes = [
    "Search now matches file contents, not only names.",
    "Shared links remember the folder they were opened from.",
    "Uploads resume after the connection drops.",
  ]

  readonly log = useLog()
}

export { Example }
Open and close Release notes. The console logs when each animation finishes.

Custom Rendering

Each directive goes on an element you write, so you choose the element and read its state from a template reference, like #parcel="uiCollapsible" and then parcel.state().open. The trigger below sits inside a heading, and the panel is an ordered list.

import { Component } from "@angular/core"
import { LucideChevronDown } from "@lucide/angular"
import {
  Collapsible,
  CollapsiblePanel,
  CollapsibleTrigger,
} from "@ui-sdk/angular/collapsible"

@Component({
  imports: [
    Collapsible,
    CollapsiblePanel,
    CollapsibleTrigger,
    LucideChevronDown,
  ],
  template: `
    <div uiCollapsible #parcel="uiCollapsible" data-variant="card">
      <h3>
        <button uiCollapsibleTrigger>
          <span data-slot="stack">
            <span>Track parcel</span>
            <span data-slot="hint">
              {{ parcel.state().open ? "3 updates" : "Arrives Thursday" }}
            </span>
          </span>
          <svg lucideChevronDown></svg>
        </button>
      </h3>
      <ol uiCollapsiblePanel>
        @for (update of updates; track update.label) {
          <li data-slot="row">
            <span>{{ update.label }}</span>
            <span>{{ update.time }}</span>
          </li>
        }
      </ol>
    </div>
  `,
})
class Example {
  readonly updates = [
    { label: "Out for delivery", time: "07:40" },
    { label: "Arrived at the local depot", time: "05:12" },
    { label: "Left the sorting center", time: "Wed 21:03" },
  ]
}

export { Example }
Press Track parcel to open the ordered list. The summary changes from Arrives Thursday to 3 updates.

Styling

Use data-open, data-closed and data-disabled to style each state. The examples turn their chevron from the trigger's data-open.

Each part has a data-slot, such as collapsible-panel, and its state attributes are listed under Data Attributes.

CollapsiblePanel

VariableWhat it holds
--collapsible-panel-heightThe panel's content height in pixels while it animates, auto once open and 0px while closed.
--collapsible-panel-widthThe panel's content width, the same way. For panels that open sideways.

Data Attributes

Collapsible

[data-slot="collapsible"]

AttributeWhat it means
data-openPresent while the panel is open.
data-closedPresent while the panel is closed, including while it animates out.
data-disabledPresent when the collapsible can't open or close.

CollapsibleTrigger

[data-slot="collapsible-trigger"]

AttributeWhat it means
data-openPresent while the panel is open.
data-closedPresent while the panel is closed, including while it animates out.
data-disabledPresent when the collapsible can't open or close.

CollapsiblePanel

[data-slot="collapsible-panel"]

AttributeWhat it means
data-openPresent while the panel is open.
data-closedPresent while the panel is closed, including while it animates out.
data-disabledPresent when the collapsible can't open or close.
data-starting-stylePresent for the panel's first frame. Style the state you animate from.
data-ending-stylePresent while the panel animates closed. Style the state you animate to.

API Reference

Each option below is an input on the directive, and each callback an output. A change callback is two outputs: (beforeOpenChange), with the details and cancel(), then (openChange) with the new value alone. Bind open with [(open)].

Collapsible

open?boolean
Whether the panel is open. Pass it to control the collapsible.
defaultOpen?boolean
Whether the panel is open at first, when uncontrolled.
(beforeOpenChange)
Runs when the panel wants to open or close. details.reason says why, and details.cancel() stops it.
(openChangeComplete)
Runs once the panel has finished opening or closing, after any animation.
disabled?boolean
Stops the trigger from opening or closing the panel. The trigger stays focusable.
controller?object
Opens and closes the panel from anywhere. Create one with createCollapsibleController().
debug?boolean
Logs every state change with its reason to the console, in development builds.

CollapsibleTrigger

The trigger takes no inputs of its own. Everything else stays on your element.

CollapsiblePanel

keepMounted?boolean
Whether the panel stays in the page, hidden, while closed.
hiddenUntilFound?boolean
Keeps the closed panel in the page as hidden="until-found", so the browser's find in page can match its text and open it.

State

What each part's state() signal holds. The trigger and the panel receive the same state as the collapsible.

openboolean
Whether the panel is open.
disabledboolean
Whether the trigger ignores presses, by its own disabled or a disabled fieldset around it. The open option and the controller can still open and close it.

Reasons

$event.reason says why the panel opened or closed.

trigger-pressMouseEvent | KeyboardEvent | TouchEvent
Click, tap, Enter or Space on the part that opens, closes or toggles. Arrow keys that open a menu or submenu count too. A tap on a combobox's input that opens a sheet reports its touchend.
find-in-pageEvent
The browser's find in page matched hidden content.
imperativenone
A controller or api call.

Controller

Create one with createCollapsibleController() and pass it as controller.

Every write method takes an optional { reason, event }, and returns false when the change was canceled or changed nothing.

isOpen()() => boolean
Whether the panel is open.
setOpen(open, options?)(open: boolean, options?: CollapsibleChangeOptions | undefined) => boolean
Opens or closes the panel. Returns false when the change was canceled or not possible.
open(options?)(options?: CollapsibleChangeOptions | undefined) => boolean
Opens the panel.
close(options?)(options?: CollapsibleChangeOptions | undefined) => boolean
Closes the panel.
toggle(options?)(options?: CollapsibleChangeOptions | undefined) => boolean
Opens the panel when it's closed, and closes it when it's open.
focus()() => void
Moves focus to the trigger.

Events

A before output emits { value, reason, event, cancel() } before the change, and its plain output follows with the value alone. A bound open only updates when (beforeOpenChange) wasn't canceled.

BindingOnReceives
[(open)]uiCollapsibleWhether the panel is open
(beforeOpenChange)uiCollapsible{ value, reason, cancel() }: the new state, and the reason
(openChangeComplete)uiCollapsible$event, whether it's open, after the panel's animation

Accessibility

The collapsible follows the WAI-ARIA disclosure pattern. The trigger says whether the panel is open with aria-expanded, and points to the panel with aria-controls while it's mounted. Every line below is an automated test.

Keyboard

KeyDoes
Enter, SpaceOpens or closes the panel. Focus stays on the trigger.
TabMoves from the trigger into an open panel

Closing a panel while focus is inside it moves focus back to the trigger.

Tested Behavior

Opening and closing
  • Starts closed, with the panel out of the page.
  • Clicking the trigger opens the panel, and clicking again closes it.
  • The trigger controls the panel.
  • Generated ids start with ui-sdk-.
  • It can start open.
  • An opening can be canceled.
  • A disabled collapsible doesn't open.
  • A disabled trigger stays focusable and says it's disabled.
  • A controller opens and closes it from anywhere.
Keyboard
  • Enter toggles the panel and keeps focus on the trigger.
  • Space toggles the panel and keeps focus on the trigger.
  • Tab moves from the open trigger into the panel.
  • Tab skips a closed panel.
Mounting
  • With keepMounted, the closed panel stays in the page, hidden.
  • With keepMounted, a class that sets display doesn't show the closed panel.
  • With hiddenUntilFound, the closed panel stays findable.
  • With hiddenUntilFound, a class that sets display keeps the closed panel findable.
  • Finding text in the closed panel opens it.
  • Following a link into the closed panel opens it.
  • The browser's own find opens the closed panel.

Runs in React, Vue and web components.

Beyond the pattern's baseline:

Enhancements
  • Closing the panel while focus is inside it moves focus to the trigger.
  • OnOpenChangeComplete runs once the animation has finished.
  • An inert its owner writes during the exit stays once the exit ends.
  • Find in page reports its own reason and opens without an animation.
  • A canceled find in page keeps the panel hidden until found.
  • An infinite exit animation still unmounts the panel.
  • Parts report open and closed through data attributes.

Runs in React, Vue and web components.

Screen readers hear the same thing in each tested framework, recorded by a virtual screen reader:

  • Announcements while using the collapsible with a keyboard.

Runs in React, Vue and web components.