collapsible

An interactive component which expands and collapses a panel.

PreviousNext

Order #4189

StatusShipped
Shipping address
100 Market St, San Francisco
Items
2x Studio Headphones
import { ChangeDetectionStrategy, Component } from '@angular/core';

import { NgIcon, provideIcons } from '@ng-icons/core';
import { lucideChevronsUpDown } from '@ng-icons/lucide';

import { ZardButtonComponent } from '@/shared/components/button/button.component';
import { ZardCollapsibleImports } from '@/shared/components/collapsible/collapsible.imports';

@Component({
  selector: 'z-demo-collapsible-preview',
  imports: [ZardCollapsibleImports, ZardButtonComponent, NgIcon],
  template: `
    <z-collapsible class="flex w-[350px] flex-col gap-2">
      <div class="flex items-center justify-between gap-4 px-4">
        <h4 class="text-sm font-semibold">Order #4189</h4>

        <button z-button z-collapsible-trigger zType="ghost" zSize="icon-sm">
          <ng-icon name="lucideChevronsUpDown" />
          <span class="sr-only">Toggle details</span>
        </button>
      </div>

      <div class="rounded-md border px-4 py-2 text-sm">
        <div class="flex items-center justify-between">
          <span class="text-muted-foreground">Status</span>
          <span class="font-medium">Shipped</span>
        </div>
      </div>

      <z-collapsible-content>
        <div class="flex flex-col gap-2">
          <div class="rounded-md border px-4 py-2 text-sm">
            <div class="text-muted-foreground">Shipping address</div>
            <div class="font-medium">100 Market St, San Francisco</div>
          </div>
          <div class="rounded-md border px-4 py-2 text-sm">
            <div class="text-muted-foreground">Items</div>
            <div class="font-medium">2x Studio Headphones</div>
          </div>
        </div>
      </z-collapsible-content>
    </z-collapsible>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
  viewProviders: [provideIcons({ lucideChevronsUpDown })],
})
export class ZardDemoCollapsiblePreviewComponent {}

Installation

Copy
npx zard-cli@latest add collapsible

Usage

import { ZardCollapsibleImports } from '@/shared/components/collapsible/collapsible.imports';
Copy
<z-collapsible>
  <button z-collapsible-trigger>Toggle</button>
  <z-collapsible-content>Content</z-collapsible-content>
</z-collapsible>
Copy

Examples

Basic

A minimal [z-collapsible] wrapped in a z-card. The trigger is a full-width ghost z-button whose chevron rotates through group-data-[state=open], driven entirely by the directive's own data-state — no host state needed.

This panel can be expanded or collapsed to reveal additional content.

import { ChangeDetectionStrategy, Component } from '@angular/core';

import { NgIcon, provideIcons } from '@ng-icons/core';
import { lucideChevronDown } from '@ng-icons/lucide';

import { ZardButtonComponent } from '@/shared/components/button/button.component';
import { ZardCardImports } from '@/shared/components/card/card.imports';
import { ZardCollapsibleImports } from '@/shared/components/collapsible/collapsible.imports';

@Component({
  selector: 'z-demo-collapsible-basic',
  imports: [ZardCollapsibleImports, ZardButtonComponent, ZardCardImports, NgIcon],
  template: `
    <z-card class="w-full max-w-sm">
      <div z-card-content>
        <z-collapsible class="data-[state=open]:bg-muted flex flex-col gap-1 rounded-lg p-1 transition-colors">
          <button z-button z-collapsible-trigger zType="ghost" class="group w-full justify-between">
            Product details
            <ng-icon name="lucideChevronDown" class="transition-transform group-data-[state=open]:rotate-180" />
          </button>

          <z-collapsible-content>
            <div class="flex flex-col gap-2 px-2 pt-1 pb-2">
              <p class="text-muted-foreground text-sm">
                This panel can be expanded or collapsed to reveal additional content.
              </p>
              <button z-button zSize="xs" class="w-fit">Learn More</button>
            </div>
          </z-collapsible-content>
        </z-collapsible>
      </div>
    </z-card>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
  viewProviders: [provideIcons({ lucideChevronDown })],
})
export class ZardDemoCollapsibleBasicComponent {}

Settings Panel

A collapsible that grows a form in place: the first two radius fields are always visible, and z-collapsible-content reveals the other two inside the same z-field-group grid. The icon trigger flips between maximize and minimize with the zOpen/zOpenChange pair.
RadiusSet the corner radius of the element.
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';

import { NgIcon, provideIcons } from '@ng-icons/core';
import { lucideMaximize, lucideMinimize } from '@ng-icons/lucide';

import { ZardButtonComponent } from '@/shared/components/button/button.component';
import { ZardCardImports } from '@/shared/components/card/card.imports';
import { ZardCollapsibleImports } from '@/shared/components/collapsible/collapsible.imports';
import { ZardFieldImports } from '@/shared/components/field/field.imports';
import { ZardInputComponent } from '@/shared/components/input/input.component';

@Component({
  selector: 'z-demo-collapsible-settings-panel',
  imports: [ZardCollapsibleImports, ZardCardImports, ZardFieldImports, ZardButtonComponent, ZardInputComponent, NgIcon],
  template: `
    <z-card zSize="sm" class="mx-auto w-full min-w-xs">
      <z-card-header>
        <z-card-title zTitle="Radius" />
        <z-card-description zDescription="Set the corner radius of the element." />
      </z-card-header>
      <z-card-content>
        <z-collapsible class="flex items-start gap-2" [zOpen]="isOpen()" (zOpenChange)="isOpen.set($event)">
          <div z-field-group class="grid w-full grid-cols-2 gap-2">
            <div z-field>
              <label z-field-label for="radius-top-left" class="sr-only">Radius top left</label>
              <input z-input id="radius-top-left" placeholder="0" value="0" />
            </div>
            <div z-field>
              <label z-field-label for="radius-top-right" class="sr-only">Radius top right</label>
              <input z-input id="radius-top-right" placeholder="0" value="0" />
            </div>
            <z-collapsible-content class="col-span-full">
              <div class="grid grid-cols-2 gap-2">
                <div z-field>
                  <label z-field-label for="radius-bottom-left" class="sr-only">Radius bottom left</label>
                  <input z-input id="radius-bottom-left" placeholder="0" value="0" />
                </div>
                <div z-field>
                  <label z-field-label for="radius-bottom-right" class="sr-only">Radius bottom right</label>
                  <input z-input id="radius-bottom-right" placeholder="0" value="0" />
                </div>
              </div>
            </z-collapsible-content>
          </div>
          <button z-button z-collapsible-trigger zType="outline" zSize="icon" aria-label="Toggle all corners">
            <ng-icon [name]="isOpen() ? 'lucideMinimize' : 'lucideMaximize'" />
          </button>
        </z-collapsible>
      </z-card-content>
    </z-card>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
  viewProviders: [provideIcons({ lucideMaximize, lucideMinimize })],
})
export class ZardDemoCollapsibleSettingsPanelComponent {
  protected readonly isOpen = signal(false);
}

File Tree

A recursive file explorer: every folder is its own z-collapsible whose content renders the same template again, so nested folders open and close independently of one another.
import { NgTemplateOutlet } from '@angular/common';
import { ChangeDetectionStrategy, Component } from '@angular/core';

import { NgIcon, provideIcons } from '@ng-icons/core';
import { lucideChevronRight, lucideFile, lucideFolder } from '@ng-icons/lucide';

import { ZardButtonComponent } from '@/shared/components/button/button.component';
import { ZardCardImports } from '@/shared/components/card/card.imports';
import { ZardCollapsibleImports } from '@/shared/components/collapsible/collapsible.imports';
import { ZardTabsImports } from '@/shared/components/tabs/tabs.imports';

interface FileTreeItem {
  name: string;
  items?: FileTreeItem[];
}

@Component({
  selector: 'z-demo-collapsible-file-tree',
  imports: [ZardCollapsibleImports, ZardCardImports, ZardTabsImports, ZardButtonComponent, NgIcon, NgTemplateOutlet],
  template: `
    <z-card zSize="sm" class="mx-auto w-full max-w-[16rem] gap-2">
      <z-card-header>
        <z-tab-group class="w-full **:[[role=tablist]]:w-full">
          <z-tab label="Explorer" />
          <z-tab label="Outline" />
        </z-tab-group>
      </z-card-header>
      <z-card-content>
        <div class="flex flex-col gap-1">
          @for (item of fileTree; track item.name) {
            <ng-container *ngTemplateOutlet="node; context: { $implicit: item }" />
          }
        </div>
      </z-card-content>
    </z-card>

    <ng-template #node let-item>
      @if (item.items) {
        <z-collapsible>
          <button
            z-button
            z-collapsible-trigger
            zType="ghost"
            zSize="sm"
            class="hover:bg-accent hover:text-accent-foreground group w-full justify-start transition-none"
          >
            <ng-icon name="lucideChevronRight" class="transition-transform group-data-[state=open]:rotate-90" />
            <ng-icon name="lucideFolder" />
            {{ item.name }}
          </button>
          <z-collapsible-content class="ms-5 data-[state=open]:mt-1">
            <div class="flex flex-col gap-1">
              @for (child of item.items; track child.name) {
                <ng-container *ngTemplateOutlet="node; context: { $implicit: child }" />
              }
            </div>
          </z-collapsible-content>
        </z-collapsible>
      } @else {
        <button z-button zType="link" zSize="sm" class="text-foreground w-full justify-start gap-2">
          <ng-icon name="lucideFile" />
          <span>{{ item.name }}</span>
        </button>
      }
    </ng-template>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
  viewProviders: [provideIcons({ lucideChevronRight, lucideFolder, lucideFile })],
})
export class ZardDemoCollapsibleFileTreeComponent {
  protected readonly fileTree: FileTreeItem[] = [
    {
      name: 'components',
      items: [
        {
          name: 'ui',
          items: [
            { name: 'button.component.ts' },
            { name: 'card.component.ts' },
            { name: 'dialog.component.ts' },
            { name: 'input.component.ts' },
            { name: 'select.component.ts' },
            { name: 'table.component.ts' },
          ],
        },
        { name: 'login-form.component.ts' },
        { name: 'register-form.component.ts' },
      ],
    },
    {
      name: 'services',
      items: [{ name: 'auth.service.ts' }, { name: 'api.service.ts' }, { name: 'storage.service.ts' }],
    },
    {
      name: 'utils',
      items: [{ name: 'merge-classes.ts' }, { name: 'debounce.ts' }, { name: 'media-query.ts' }],
    },
    {
      name: 'types',
      items: [{ name: 'index.d.ts' }, { name: 'api.d.ts' }],
    },
    {
      name: 'public',
      items: [{ name: 'favicon.ico' }, { name: 'logo.svg' }, { name: 'images' }],
    },
    { name: 'app.component.ts' },
    { name: 'app.config.ts' },
    { name: 'app.routes.ts' },
    { name: 'styles.css' },
    { name: 'project.json' },
    { name: 'nx.json' },
    { name: 'tsconfig.json' },
    { name: 'README.md' },
    { name: '.gitignore' },
  ];
}

Controlled

Bind zOpen to a signal and listen to zOpenChange to drive the panel from outside the component.
The panel is open
Both buttons drive the same signal, so the component stays in sync with the host state.
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';

import { ZardButtonComponent } from '@/shared/components/button/button.component';
import { ZardCollapsibleImports } from '@/shared/components/collapsible/collapsible.imports';

@Component({
  selector: 'z-demo-collapsible-controlled',
  imports: [ZardCollapsibleImports, ZardButtonComponent],
  template: `
    <div class="flex w-[350px] flex-col gap-3">
      <div class="flex items-center justify-between gap-4">
        <span class="text-muted-foreground text-sm">The panel is {{ open() ? 'open' : 'closed' }}</span>

        <button z-button zType="outline" zSize="sm" (click)="open.set(!open())">
          {{ open() ? 'Close' : 'Open' }} from outside
        </button>
      </div>

      <z-collapsible class="flex flex-col gap-2" [zOpen]="open()" (zOpenChange)="open.set($event)">
        <button z-button z-collapsible-trigger zType="secondary" zSize="sm">Toggle from inside</button>

        <z-collapsible-content>
          <div class="text-muted-foreground rounded-md border px-4 py-3 text-sm">
            Both buttons drive the same signal, so the component stays in sync with the host state.
          </div>
        </z-collapsible-content>
      </z-collapsible>
    </div>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ZardDemoCollapsibleControlledComponent {
  readonly open = signal(true);
}

Disabled

Use zDisabled to block the trigger. The panel keeps whatever state it was rendered with.
The trigger is disabled, so this panel stays exactly as it was rendered.
import { ChangeDetectionStrategy, Component } from '@angular/core';

import { ZardButtonComponent } from '@/shared/components/button/button.component';
import { ZardCollapsibleImports } from '@/shared/components/collapsible/collapsible.imports';

@Component({
  selector: 'z-demo-collapsible-disabled',
  imports: [ZardCollapsibleImports, ZardButtonComponent],
  template: `
    <z-collapsible class="flex w-[350px] flex-col gap-2" zDisabled zOpen>
      <button z-button z-collapsible-trigger zType="outline" zSize="sm">Cannot be toggled</button>

      <z-collapsible-content>
        <div class="text-muted-foreground rounded-md border px-4 py-3 text-sm">
          The trigger is disabled, so this panel stays exactly as it was rendered.
        </div>
      </z-collapsible-content>
    </z-collapsible>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ZardDemoCollapsibleDisabledComponent {}

RTL

To enable RTL support in zard/ui, see the RTL configuration guide.

الطلب #4189

الحالةتم الشحن

عنوان الشحن

100 Market St, San Francisco

العناصر

2x سماعات الاستوديو

// You will notice this example is rendered inside a wrapper that sets the dir and data-lang attributes, and
// reads its text from a translations helper. This is because this site is not RTL by default.
// In your application you won't need either: set dir="rtl" on <html> (or use the CDK Dir directive on a subtree)
// and write the text of your own language. See the RTL guide: https://zardui.com/docs/rtl

import { ChangeDetectionStrategy, Component } from '@angular/core';

import { NgIcon, provideIcons } from '@ng-icons/core';
import { lucideChevronsUpDown } from '@ng-icons/lucide';

import { ZardButtonComponent } from '@/shared/components/button/button.component';
import { ZardCardImports } from '@/shared/components/card/card.imports';
import { ZardCollapsibleImports } from '@/shared/components/collapsible/collapsible.imports';
import { injectRtlTranslation, type ZardRtlTranslations } from '@/shared/demo-utils/rtl-translation';

interface CollapsibleRtlValues {
  orderNumber: string;
  status: string;
  shipped: string;
  shippingAddress: string;
  address: string;
  items: string;
  itemsDescription: string;
  toggle: string;
}

const translations: ZardRtlTranslations<CollapsibleRtlValues> = {
  en: {
    dir: 'ltr',
    values: {
      orderNumber: 'Order #4189',
      status: 'Status',
      shipped: 'Shipped',
      shippingAddress: 'Shipping address',
      address: '100 Market St, San Francisco',
      items: 'Items',
      itemsDescription: '2x Studio Headphones',
      toggle: 'Toggle details',
    },
  },
  ar: {
    dir: 'rtl',
    values: {
      orderNumber: 'الطلب #4189',
      status: 'الحالة',
      shipped: 'تم الشحن',
      shippingAddress: 'عنوان الشحن',
      address: '100 Market St, San Francisco',
      items: 'العناصر',
      itemsDescription: '2x سماعات الاستوديو',
      toggle: 'عرض التفاصيل',
    },
  },
  he: {
    dir: 'rtl',
    values: {
      orderNumber: 'הזמנה #4189',
      status: 'סטטוס',
      shipped: 'נשלח',
      shippingAddress: 'כתובת משלוח',
      address: '100 Market St, San Francisco',
      items: 'פריטים',
      itemsDescription: '2x אוזניות סטודיו',
      toggle: 'הצג פרטים',
    },
  },
};

@Component({
  selector: 'z-demo-collapsible-rtl',
  imports: [ZardCollapsibleImports, ZardButtonComponent, ZardCardImports, NgIcon],
  template: `
    <z-card class="w-full max-w-sm" zSize="sm">
      <div z-card-content>
        <z-collapsible class="flex flex-col gap-2">
          <div class="flex items-center justify-between gap-4">
            <h4 class="text-sm font-semibold">{{ i18n.t().orderNumber }}</h4>
            <button z-button z-collapsible-trigger zType="ghost" zSize="icon-sm" [attr.aria-label]="i18n.t().toggle">
              <ng-icon name="lucideChevronsUpDown" />
            </button>
          </div>
          <div class="flex items-center justify-between rounded-md border px-4 py-2 text-sm">
            <span class="text-muted-foreground">{{ i18n.t().status }}</span>
            <span class="font-medium">{{ i18n.t().shipped }}</span>
          </div>
          <z-collapsible-content>
            <div class="flex flex-col gap-2">
              <div class="rounded-md border px-4 py-2 text-sm">
                <p class="text-muted-foreground">{{ i18n.t().shippingAddress }}</p>
                <p class="font-medium">{{ i18n.t().address }}</p>
              </div>
              <div class="rounded-md border px-4 py-2 text-sm">
                <p class="text-muted-foreground">{{ i18n.t().items }}</p>
                <p class="font-medium">{{ i18n.t().itemsDescription }}</p>
              </div>
            </div>
          </z-collapsible-content>
        </z-collapsible>
      </div>
    </z-card>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
  viewProviders: [provideIcons({ lucideChevronsUpDown })],
})
export class ZardDemoCollapsibleRtlComponent {
  protected readonly i18n = injectRtlTranslation(translations);
}

API Reference

z-collapsible, [z-collapsible]

Expands and collapses a panel.

Prop
[zOpen]

Open state; supports [(zOpen)]

Type
boolean
Default
false
[zDisabled]

Stops the trigger from toggling

Type
boolean
Default
false
[class]

Custom CSS classes

Type
ClassValue
Default
''
(zOpenChange)

Emitted with the new open state

Type
EventEmitter<boolean>
Default
—

[z-collapsible-trigger]

Toggles the panel; unstyled, apply it to your own button.

z-collapsible-content

The panel revealed by the trigger.

Prop
[class]

Custom CSS classes

Type
ClassValue
Default
''
github iconwhatsapp icondiscord iconX icon

Made with ❤ in Brazil. Open source and available on GitHub .