Twitter LogoFacebook Logo
Angular Material UI: Expansion Panel
The expansion panel is a component that we can toggle on and off to show more or less of the content.
By: King
Image from Codeible.com

The expansion panel is a component that we can toggle on and off to show more or less of the content.


There are 3 parts to each panel. 

(1) The header
(2) The content area
(3) The action bar

For each header, there is the title, the description, and the toggle icon.

Image from Codeible.com

Implementation

To begin, add the MatExpansionModule to the project. Go into the app module file and add the import statement. Then add it to the app.

...
import MatExpansionModule from '@angular/material/expansion';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    ...
    MatExpansionModule

  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Using the Panel

To create the panel, use the mat-expansion-panel element.

<mat-expansion-panel></mat-expansion-panel>

Then add the header, use the mat-expansion-panel-header element.

<mat-expansion-panel>

   <mat-expansion-panel-header>

   </mat-expansion-panel-header>
</mat-expansion-panel>

Inside the header element, we can define a title using the mat-panel-title element and the description using the mat-panel-description element.

<mat-expansion-panel>

   <mat-expansion-panel-header>

         <mat-panel-title>
              Codeible
         </mat-panel-title>
         <mat-panel-description>
              Expansion Panel Component
         </mat-panel-description>

   </mat-expansion-panel-header>
</mat-expansion-panel>

The toggle icon is shown by default. To hide it, add the hideToggle property to the panel.

<mat-expansion-panel hideToggle>

   ...
</mat-expansion-panel>

Image from Codeible.com

Adding Content

To add content, place your elements inside the panel element, but outside of the header element. 

<mat-expansion-panel>


   <mat-expansion-panel-header>
      ...
   </mat-expansion-panel-header>

   <div>
       <img src="https://codeible.com/assets/images/icons/codible_256.png">
   </div>

</mat-expansion-panel>

Image from Codeible.com

Adding the Action bar

For the action bar, use the mat-action-row element. 

<mat-expansion-panel>


   <mat-expansion-panel-header>
      ...
   </mat-expansion-panel-header>

   <div>
       ...
   <div>

   <mat-action-row>
        <button>Subscribe</button>
   </mat-action-row>

</mat-expansion-panel>

Image from Codeible.com

Disabling the panel

To disable the panel, add the disabled directive to the panel element and set it to true. To enabled it, set it to false.

<mat-expansion-panel [disabled]="true">


   ...

</mat-expansion-panel>

Grouping Panels

We can also group multiple panels by using the mat-accordion element. With this, only 1 panel can be opened within the same group.

<mat-accordion>

   
   <mat-expansion-panel>
      ...
   </mat-expansion-panel>

   <mat-expansion-panel>
      ...
   </mat-expansion-panel>

   <mat-expansion-panel>
      ...
   </mat-expansion-panel>

</mat-accordion>

Image from Codeible.com

To allow multiple panels to be opened at once inside the accordion element, apply the multi directive to it and set it to true. 

<mat-accordion [multi]="true">

   
   <mat-expansion-panel>
      ...
   </mat-expansion-panel>

   ...

</mat-accordion>

Manually opening the Panels

Instead of using the default behavior to expand the panels we can expand it manually by setting the expanded attribute.

HTML

<mat-expansion-panel [expanded]="shouldOpenPanel1">


   ...

</mat-expansion-panel>

TypeScript

shouldOpenPanel1 = true;


Sign In