components icon indicating copy to clipboard operation
components copied to clipboard

[Tabs] Hidden tabs don't render expansion panels correctly

Open AlanCrevon opened this issue 9 years ago • 109 comments

Bug, feature request, or proposal:

Bug.

What is the expected behavior?

A <md-expansion-panel [expanded]="false"> should not be expanded until explicitally opened. When inside a hidden tab, .mat-expansion-panel-content should have height : 0px

What is the current behavior?

Panel is collapsed but panetl's content is visible. Problem occurs only if the accordion is in a hidden tab of a tab-group.

What are the steps to reproduce?

http://plnkr.co/edit/q6kXPH?p=preview

  <md-tab label="Working">
    <md-accordion>
        <md-expansion-panel [expanded]="false">
        <md-expansion-panel-header>
          You should see me
        </md-expansion-panel-header>
        <p>Yous should not see me</p>
      </md-expansion-panel>
    </md-accordion>
  </md-tab>
  <md-tab label="Not working">
    <md-accordion>
        <md-expansion-panel [expanded]="false">
        <md-expansion-panel-header>
          You should see me
        </md-expansion-panel-header>
        <p>Yous should not see me</p>
      </md-expansion-panel>
    </md-accordion>
  </md-tab>
</md-tab-group>

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Material2 master Angular 4.2.3

AlanCrevon avatar Jun 21 '17 08:06 AlanCrevon

It seems to be working correctly here: http://plnkr.co/edit/q6kXPH?p=preview

julianobrasil avatar Jun 21 '17 10:06 julianobrasil

correct. It's also working in another page of mine. Currently trying to isolate the pb.

AlanCrevon avatar Jun 21 '17 11:06 AlanCrevon

Ok, i edit the issue : the problem occurs only if the element is in a hidden tab. Thanks @julianobrasil

AlanCrevon avatar Jun 21 '17 11:06 AlanCrevon

OK. Updated the plunker to reproduce it: http://plnkr.co/edit/q6kXPH?p=preview

Put it in the issue.

julianobrasil avatar Jun 21 '17 12:06 julianobrasil

This actually happens to anything that contains an animation when set inside a hidden md-tab.

emoralesb05 avatar Jun 21 '17 15:06 emoralesb05

This seems to be caused by angular 4.2 changes. I use Teradata Covalent expansion panels in my app which have the same problem since the new angular version: https://gitter.im/Teradata/covalent?at=593e58f06549436c7d3c8360

mzernetsch avatar Jun 21 '17 21:06 mzernetsch

:wink: A very unacceptable workaround (but useful if you can't wait for the next release, possibly without this bug) is using a *ngIf and just show the accordion/expansion panel when it's needed (if it's needed when the page is loaded, put a variable in a setTimeout to trigger *ngIf in about 4 to 5 seconds after the tab-group has been rendered, or use the tab change event). In this case the expansion is shown in its expected state (collapsed or expanded).

julianobrasil avatar Jun 28 '17 23:06 julianobrasil

Hey! that bug is really annoying, is there any hope to see it's fix?) Maybe applying @julianobrasil solution with *ngIf=isActive on tabs content?

lebeker avatar Aug 31 '17 06:08 lebeker

@lebeker, I've been using something like that in a project: http://plnkr.co/edit/YAi6Xb?p=preview

I tried isActive (*ngIf="mdTab.isActive" where mdTab is a reference template variable), but it causes a "changed after checked" error.

julianobrasil avatar Aug 31 '17 09:08 julianobrasil

Hello! @julianobrasil, following @lebeker advice that's working for me as a workaround:

<md-tab-group [(selectedIndex)]="selectedTabIndex">
  <md-tab label="First">
    <div>...</div>
  </md-tab>
  <md-tab label="Second">
    <div *ngIf="selectedTabIndex == 1">...</div>
  <md-tab>
  <md-tab label="Third">
    <div *ngIf="selectedTabIndex == 2">...</div>
  <md-tab>
</md-tab-group>

lula avatar Sep 01 '17 21:09 lula

@lula worked for me, thanks!

Puresoft avatar Sep 05 '17 19:09 Puresoft

@lula Can you show your component.ts file?

michalmw avatar Sep 11 '17 13:09 michalmw

@michalmw there's nothing to show in my component.ts concerning with my workaround. Variable selectedTabIndex is directly used in the view template. Angular should take care of instantiating it. Should you need to start with another tab than the first, then you can also add selectedTabIndex to component.ts and set the tab index you prefer. Let me know if this is what you wanted to know or you needed something else :)

lula avatar Sep 11 '17 14:09 lula

Just a little tweak of @lula's solution - you don't need to actually store the selectedIndex. Instead you could just reference the md-tab-group:

<md-tab-group #myTabGroup>
    <md-tab label="First">
        <div>First</div>
    </md-tab>
    <md-tab label="Second">
        <md-accordion *ngIf="myTabGroup.selectedIndex == 1">
            <md-expansion-panel *ngFor="let i of [1,2,3];">
                <md-expansion-panel-header>
                    <md-panel-title>Test {{ i }}</md-panel-title>
                </md-expansion-panel-header>

                <p>Testing content {{ i }}</p>
            </md-expansion-panel>
        </md-accordion>
    </md-tab>
    <md-tab label="Third">
        <div>Third</div>
    </md-tab>
</md-tab-group>

steve-todorov avatar Sep 11 '17 21:09 steve-todorov

Or use css ? It worked for me ! :)

div.mat-expansion-panel-content:not(.mat-expanded) {
    height: 0px !important;
    visibility: hidden !important;
}

.mat-expansion-panel-header  {
    height: 48px;
}

bastienlemaitre avatar Sep 12 '17 13:09 bastienlemaitre

The CSS solution by @bastienlemaitre is not working for me in the content of hidden tabs.

Tried the solution of mdTab.isActive based on @julianobrasil suggestion with ngIf but resulting in ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'false'. Current value: 'true'.

I have to generate tabs dynamically, so storing selected index won't work. Here is what I am trying.

<mat-tab-group #tabGroup>
    <mat-tab #currTab *ngFor="let label of labels">
        <ng-template mat-tab-label>
          <span>{{label}}</span>
        </ng-template>
        <div *ngIf="currTab.isActive" >
          <mat-expansion-panel [expanded]="false" *ngFor="let panel of panels">
            <mat-expansion-panel-header>
              <mat-panel-title>{{panel.title}}</mat-panel-title>
              <mat-panel-description>{{panel.desc}}</mat-panel-description>
            </mat-expansion-panel-header>
            <div>{{panel.content}}</div>
          </mat-expansion-panel>
        </div>
    </mat-tab>
</mat-tab-group>

Is there a better way to operate on this one?

rayabhagis avatar Sep 24 '17 08:09 rayabhagis

@rayabhagis, try @lula solution above (https://github.com/angular/material2/issues/5269#issuecomment-326692430). It's simple and dosen't throw the "changed after checked" error.

julianobrasil avatar Sep 24 '17 09:09 julianobrasil

Thanks, I updated my code to follow similar approach by comparing index of *ngFor on tabs with tab-group's selectedIndex and there is no "change after checked" error.

rayabhagis avatar Sep 24 '17 16:09 rayabhagis

@rayabhagis, did you use this into your component ?

encapsulation: ViewEncapsulation.None

Updated CSS:

div.mat-expansion-panel-content:not(.mat-expanded) {
    height: 0px;
    visibility: hidden;
}

.mat-expansion-panel-header:not(.mat-expanded)  {
    height: 48px;
}

.mat-expansion-panel-header  {
    height: 64px;
    .mat-content {
        font-weight: 100;
        font-size: 18px;
    }
    &.mat-expanded {        
        .mat-expansion-indicator {
            transform: rotate(180deg);
        }
    }
}

bastienlemaitre avatar Oct 06 '17 10:10 bastienlemaitre

Apparently the problem is on trigger animation. The trigger isn't call when component is inside a false *ngIf. I don't know if this is a problem in Material or Angular.

tiaguinho avatar Oct 16 '17 09:10 tiaguinho