Accordion

Accordion groups a collection of contents in tabs.


import { AccordionModule } from 'primeng/accordion';

Accordion consists of one or more AccordionTab elements which are collapsed by default. Tab to expand initially can be defined with the activeIndex property.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.


<p-accordion [activeIndex]="0">
    <p-accordionTab header="Header I">
        <p>
            Lorem ipsum dolor sit amet...
        </p>
    </p-accordionTab>
    <p-accordionTab header="Header II">
        <p>
            Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium...
        </p>
    </p-accordionTab>
    <p-accordionTab header="Header III">
        <p>
            At vero eos et accusamus et iusto odio dignissimos...
        </p>
    </p-accordionTab>
</p-accordion>

AccordionTabs can be generated dynamically using the standard ngFor directive.

Content 1


<p-accordion [activeIndex]="0">
    <p-accordionTab [header]="tab.title" *ngFor="let tab of tabs">
        <p class="m-0">
            {{ tab.content }}
        </p>
    </p-accordionTab>
</p-accordion>

Only one tab at a time can be active by default, enabling multiple property changes this behavior to allow multiple tabs. In this case activeIndex needs to be an array.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.


<p-accordion [multiple]="true" [activeIndex]="[0]">
    <p-accordionTab header="Header I">
        <p>
            Lorem ipsum dolor sit amet...
        </p>
    </p-accordionTab>
    <p-accordionTab header="Header II">
        <p>
            Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium...
        </p>
    </p-accordionTab>
    <p-accordionTab header="Header III">
        <p>
            At vero eos et accusamus et iusto odio dignissimos...
        </p>
    </p-accordionTab>
</p-accordion>

Enabling disabled property of an AccordionTab prevents user interaction.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.


<p-accordion [activeIndex]="0">
    <p-accordionTab header="Header I">
        <p>
            Lorem ipsum dolor sit amet...
        </p>
    </p-accordionTab>
    <p-accordionTab header="Header II">
        <p>
            Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium...
        </p>
    </p-accordionTab>
    <p-accordionTab header="Header III">
        <p>
            At vero eos et accusamus et iusto odio dignissimos...
        </p>
    </p-accordionTab>
    <p-accordionTab header="Header IV" [disabled]="true" />
</p-accordion>

Tabs can be controlled programmatically using the activeIndex property of the accordion in general or the selected property of p-accordionTab individually.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.


<div class="flex mb-3 gap-2 justify-content-end">
    <p-button 
        (click)="activeIndex = 0" 
        rounded="true" 
        label="1" 
        styleClass="w-2rem h-2rem p-0" 
        [outlined]="activeIndex !== 0" />
    <p-button 
        (click)="activeIndex = 1" 
        rounded="true" 
        label="2" 
        styleClass="w-2rem h-2rem p-0" 
        [outlined]="activeIndex !== 1" />
    <p-button 
        (click)="activeIndex = 2" 
        rounded="true" 
        label="3" 
        styleClass="w-2rem h-2rem p-0" 
        [outlined]="activeIndex !== 2" />
    </div>
    <p-accordion (activeIndexChange)="activeIndexChange($event)" [activeIndex]="activeIndex">
        <p-accordionTab header="Header I">
            <p class="m-0">
                Lorem ipsum dolor sit amet...
            </p>
        </p-accordionTab>
        <p-accordionTab header="Header II">
            <p class="m-0">
                Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium...
            </p>
        </p-accordionTab>
        <p-accordionTab header="Header III">
            <p class="m-0">
                At vero eos et accusamus et iusto odio dignissimos...
            </p>
        </p-accordionTab>
    </p-accordion>

AccordionTab is customized header and content templates.


<p-accordion class="w-full" expandIcon="pi pi-plus" collapseIcon="pi pi-minus">
        <p-accordionTab>
            <ng-template pTemplate="header">
                <span class="flex align-items-center gap-2 w-full">
                    <p-avatar 
                        image="https://primefaces.org/cdn/primeng/images/demo/avatar/amyelsner.png" 
                        shape="circle" />
                    <span class="font-bold white-space-nowrap">
                        Amy Elsner
                    </span>
                    <p-badge value="3" class="ml-auto mr-2" />
                </span>
            </ng-template>
            <p class="m-0">
                Lorem ipsum dolor sit amet...
            </p>
        </p-accordionTab>
        <p-accordionTab>
            <ng-template pTemplate="header">
                <span class="flex align-items-center gap-2 w-full">
                    <p-avatar 
                        image="https://primefaces.org/cdn/primeng/images/demo/avatar/onyamalimba.png" 
                        shape="circle" />
                    <span class="font-bold white-space-nowrap">
                        Onyama Limba
                    </span>
                    <p-badge value="4" class="ml-auto mr-2" />
                </span>
            </ng-template>
            <p class="m-0">
                Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium...
            </p>
        </p-accordionTab>
        <p-accordionTab>
            <ng-template pTemplate="header">
                <span class="flex align-items-center gap-2 w-full">
                    <p-avatar 
                        image="https://primefaces.org/cdn/primeng/images/demo/avatar/ionibowcher.png" 
                        shape="circle" />
                    <span class="font-bold white-space-nowrap">
                        Ioni Bowcher
                    </span>
                    <p-badge value="2" class="ml-auto mr-2" />
                </span>
            </ng-template>
            <p class="m-0">
                At vero eos et accusamus et iusto odio dignissimos...
            </p>
        </p-accordionTab>
</p-accordion>

Following is the list of structural style classes, for theming classes visit theming page.

NameElement
p-accordionContainer element
p-accordion-headerHeader of a tab.
p-accordion-contentContent of a tab.

Screen Reader

Accordion header elements have a button role and use aria-controls to define the id of the content section along with aria-expanded for the visibility state. The value to read a header element defaults to the value of the header property and can be customized by defining an aria-label or aria-labelledby property. Each header has a heading role, for which the level is customized by headerAriaLevel and has a default level of 2 as per W3C specifications.

The content uses region role, defines an id that matches the aria-controls of the header and aria-labelledby referring to the id of the header.

Header Keyboard Support

KeyFunction
tabMoves focus to the next the focusable element in the page tab sequence.
shift + tabMoves focus to the previous the focusable element in the page tab sequence.
enterToggles the visibility of the content.
spaceToggles the visibility of the content.
down arrowMoves focus to the next header.
up arrowMoves focus to the previous header.
homeMoves focus to the first header.
endMoves focus to the last header.