Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change toggle icon of mat-expansion-panel?

The default icon for toggling a mat-expansion-panel is >. Setting hideToggle true just hides the toggle icon. Is there any way to change it? I found nothing in the official documentation. I want to use + or - icon if the state is closed or opened respectively.

like image 832
Samiul Alam Avatar asked Jun 16 '19 13:06

Samiul Alam


People also ask

How do I change my mat-expansion-Panel icon?

panel. expanded">add</mat-icon><mat-icon *ngIf="panel. expanded">remove</mat-icon></mat-panel-description> . By defining panel as a template reference, you can access the expanded state of the mat-expansion-panel directly, removing the need for a variable in the component.

How do I expand my mat-expansion-panel on button click?

This means the (click) event handler can now use mep to interact with the panel. The default value for expanded is false, so to toggle the expansion panel, you reassign the inverse (using "!") to the expanded property on that expansion panel.

How do I know if my mat expansion is expanded?

By default, the expansion-panel header includes a toggle icon at the end of the header to indicate the expansion state.

How do I open my mat-expansion-panel by default?

By default, the expansion panel content will be initialized even when the panel is closed. To instead defer initialization until the panel is open, the content should be provided as an ng-template: <mat-expansion-panel> <mat-expansion-panel-header>

How to change the icon of a mat-expanded panel?

There is a class .mat-expandedwhich is applied to a <mat-expansion-panel> So you can just use CSS to change the icon, no JS required. To change arrow down to up: .mat-expanded .mat-expansion-panel-header-title .material-icons{ transform: rotate(180deg); }

How to show/hide the icons in the expansion panel?

Show activity on this post. Give your expansion panel a reference id example and set the hideToggle property as true. place your icons in <mat-panel-description> and use the expanded property of the panel to show or hide the relevant icons.

How to toggle the expanded part of a panel?

The default value for expanded is false, so to toggle the expansion panel, you reassign the inverse (using "!") to the expanded property on that expansion panel. using disabled greys out the entire panel. probably not desired. Use two-way binding on the expanded attribute of mat-expansion-panel. Here is an example live in StackBlitz:

What do I do if I have multiple expansion panels?

What to do if I have multiple expansion panels? The panelOpenStateis a variable that will be shared by every expansion panels. One panel expands, all the icon changes from + to zero because every expansion panels share the same variable panelOpenState.


3 Answers

As stated on the Angular Material Expansion Panel documentation, we can customise the stylings of the mat-extension-panel-header, which will in turn allow you to customise the icons.

First and foremost, you hide the original icon by setting the hideToggle attribute to false. In addition, we assign the event bindings (opened) and (closed) to the panelOpenState property. You may check out the other properties of mat-expansion-panel over here.

<mat-expansion-panel (opened)="panelOpenState = true" (closed)="panelOpenState = false"hideToggle="true">

Next, on your mat-panel-description, you include the custom icons required. The icons shown will be dependent on the state of the panel. For this example, we are using the icons from Material Icons.

<mat-panel-description>
  <mat-icon *ngIf="!panelOpenState">add</mat-icon>
  <mat-icon *ngIf="panelOpenState">remove</mat-icon>
</mat-panel-description> 

I have edited the original sample from the Angular documentation, such that it is using the custom + and minus icons to denote the expanded/collapsed state of the mat-extension-panel. You may access the demo over here.

like image 150
wentjun Avatar answered Sep 29 '22 22:09

wentjun


There is a class .mat-expanded which is applied to a <mat-expansion-panel>

So you can just use CSS to change the icon, no JS required.

  • To change arrow down to up:
.mat-expanded .mat-expansion-panel-header-title .material-icons{
  transform: rotate(180deg);
}
    <mat-panel-title>
      <span class="material-icons">expand_more</span>
      Panel with icon `transform`
    </mat-panel-title>
  • To swap icons:
.mat-expansion-panel-header-title .open,
.mat-expanded .mat-expansion-panel-header-title .close{
  display: inline-block;
}
.mat-expanded .mat-expansion-panel-header-title .open,
.mat-expansion-panel-header-title .close{
  display: none;
}
    <mat-panel-title>
      <span class="material-icons open">open_in_full</span>
      <span class="material-icons close">close_fullscreen</span>
      Panel with open/close
    </mat-panel-title>

Here is a link to a demo https://stackblitz.com/edit/angular-e2fnlm

CSS solution works when you have multiple Material Expansion Panels

like image 44
v-andrew Avatar answered Sep 29 '22 23:09

v-andrew


Give the name to the mat-expansion-panel say panel in the example. Use expanded property of the mat-expansion-panel to show or hide the + or - icon.

 <mat-expansion-panel #panel  hideToggle>
        <mat-expansion-panel-header>
             <mat-panel-title>
                        Buttons
             </mat-panel-title>
             <mat-icon >{{panel.expanded? 'remove' : 'add'}}</mat-icon>
                        
        </mat-expansion-panel-header>
                   
  </mat-expansion-panel>
like image 30
LN Agrawal Avatar answered Sep 30 '22 00:09

LN Agrawal