Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically change background color of mat-expansion-panel-header based on whether or not it is expanded

I've been struggling to find a way to dynamically change the background color of a material design expansion panel header based on whether or not the panel is expanded.

When the panel is closed I'd like the background color to be white, but when it is expanded I want to change that color to something else. I'm not seeing something I can base this change off of, maybe I'm missing something. I'm new to angular and was thinking I would just be able to base it off of [expanded]'s setting but it appears that is not the case.

mat-accordion
mat-expansion-panel
mat-expansion-panel-header
mat-expanded{
  font-size: 1.25em;
  background-color: white;
  background: white;
  color: #00838f;
  font-weight: 500;
}



mat-accordion
mat-expansion-panel
mat-expansion-panel-header {
  font-size: 1.25em;
  background-color: white;
  background: white;

  color: #00838f;
  font-weight: 500;
}


  mat-accordion
  mat-expansion-panel
  .mat-expansion-panel-body {
  background-color: white;
  padding-top: 5px;
}
.mat-expansion-indicator::after {
  color: #00838f;
}
<mat-accordion multi="true">
<mat-expansion-panel [expanded]="selectedBenefitCategories.indexOf(cat)!=-1" [hidden]="selectedBenefitCategories.indexOf(cat)===-1">
  <mat-expansion-panel-header>
    <i class="search-category-icon far fa-star"></i> &nbsp; {{cat}}
  </mat-expansion-panel-header>
  <ng-template matExpansionPanelContent>

    some content

  </ng-template>
</mat-expansion-panel>
</mat-accordion>
like image 646
Brandon Jerz Avatar asked May 24 '18 17:05

Brandon Jerz


People also ask

What is the role of the expansion panel header?

The expansion panel header has role="button" and also the attribute aria-controls with the expansion panel's id as value. The expansion panel headers are buttons. Users can use the keyboard to activate the expansion panel header to switch between expanded state and collapsed state.

What is the current behavior of the expansion-panel-header background color?

I want to change the expansion-panel-header background color to gray or something else. What is the current behavior? Currently, its color is by default white and on mouse hover it changes to gray.

How to add background to the panel header in Angular Material?

In order to fix this, you can simply combine it with the .mat-expansion-panel-header class selector. Notice: Angular Material does also add background: inherit on the hover state. Show activity on this post. You have to provide a CSS selector that is stronger than the default ones that are provided by angular material.

How do I use the <mat-expansion-panel-header>?

The <mat-expansion-panel-header> shows a summary of the panel content and acts as the control for expanding and collapsing. This header may optionally contain an <mat-panel-title> and an <mat-panel-description>, which format the content of the header to align with Material Design specifications.


1 Answers

Angular Material does add dynamic classes based on the expanding state.

mat-expanded would be the one you are looking for. The problem is this class gets also attached to the panel-content. In order to fix this, you can simply combine it with the .mat-expansion-panel-header class selector.

All summed up this would look like this:

.mat-expansion-panel-header.mat-expanded {
  background: red;
}

Notice: Angular Material does also add background: inherit on the hover state.

If that's not the behavior you want, simply overwrite it like this:

.mat-expansion-panel-header.mat-expanded,
.mat-expansion-panel-header.mat-expanded:hover {
  background: red;
}
like image 139
Orlandster Avatar answered Sep 21 '22 07:09

Orlandster