Skip to content

mat-expansion-panel "opens" with angular enter-leave animations, although current state is closed. #11765

Closed

Description

@bencbradshaw

Bug, feature request, or proposal:

Bug

What is the expected behavior?

Mat-expansion-panel, when animating in or out, will stay in current state of opened or closed and render as such.

What is the current behavior?

mat-expansion-panel, when animating out, will open and display all hidden panel content, while still showing the current state as closed.

What are the steps to reproduce?

https://stackblitz.com/edit/angular-ykxmcr?file=src%2Fapp%2Fhero-list-multistep.component.ts

What is the use-case or motivation for changing an existing behavior?

Keeping the panel closed when animating out provides a much cleaner experience for the UI by preventing jumpiness and inconsistencies.

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

@angular/core 6.0.4
@angular/material 6.2.1
@angular/cdk 6.2.1
Chrome 67.0.3396.79
Firefox 60.0.2
Safari 11.1

Is there anything else we should know?

This happens with all animations I have tested, not just the one provided in the stackblitz example.

Activity

  1. self-assigned this
    on Jun 13, 2018
  2. added
    P3
    on Jun 13, 2018
  3. added a commit that references this issue on Jun 13, 2018
    a019463
  4. added a commit that references this issue on Aug 20, 2018
    39ddff9
  5. added 2 commits that reference this issue on Aug 23, 2018
    e96991b
    df406cc
  6. added a commit that references this issue on Sep 22, 2018
    53341e5
  7. added a commit that references this issue on Oct 8, 2018
    fb72c01
  8. added a commit that references this issue on Nov 6, 2018
    fdfdcd5
  9. aeslinger0 commented on Nov 8, 2018

    @aeslinger0

    Possibly related... I'm seeing something similar where the expansion panel is visible in a dialog as the dialog animates open. So it may appear that the animation does not have to applied directly to the expansion panel to see this behavior.

  10. kiwikern commented on Nov 11, 2018

    @kiwikern

    I have created an example for a router transition which illustrates the same problem:

    https://stackblitz.com/edit/angular-expansion-panel-with-route-transition-animation?file=app%2Fapp%2Fapp.component.html

    checkin-eurowings

  11. thw0rted commented on Nov 25, 2018

    @thw0rted

    Any updates on this? I thought maybe I had to specify something in my transition animations to cause everything to trigger in the right order, but if that's the case, I can't figure out what. I'd like to see an example of transitions that don't cause this behavior, if anybody has it working.

  12. virtuoushub commented on Nov 28, 2018

    @virtuoushub

    @aeslinger0

    Possibly related... I'm seeing something similar where the expansion panel is visible in a dialog as the dialog animates open. So it may appear that the animation does not have to applied directly to the expansion panel to see this behavior.

    See #13870. I am not sure they are related either, but what you describe seems similar to that issue.

  13. thw0rted commented on Nov 29, 2018

    @thw0rted

    That sounds likely, yeah. For now I have a workaround in my global styles. I'll keep an eye out for the linked patch landing and see if that fixes things.

  14. 25 remaining items

  15. OlliHolli commented on Aug 8, 2022

    @OlliHolli

    I've found another workaround that might help a few people.
    See my comment here.

  16. antoci-alin commented on Feb 22, 2023

    @antoci-alin

    I have also fixed using:

    .ng-animating .mat-accordion .mat-expansion-panel .mat-expansion-panel-content {
      height: 0px;
      visibility: hidden;
    }
    
  17. Vestelth commented on Oct 20, 2023

    @Vestelth

    similar to above, works for me:

    .mat-expansion-panel .mat-expansion-panel-content {
        &.ng-animating {
           height: 0;
        }
    }
    
  18. Nividica commented on Oct 30, 2023

    @Nividica

    TL;DR

    Changing

    • query(':enter', animateChild(), { optional: true })

    to

    • query(':enter', query('@*', animateChild(), { optional: true }), { optional: true })

    fixed my issues.


    Also ran into this issue today, and stumbled into a solution that seems to fix my issues.

    My Setup

    Dependencies:

    "@angular/core": "16.2.0",
    "@angular/material": "16.2.4",
    

    Two router animations

    1. Fade in the first page load.
    2. Slide/Swipe when navigating from any page to any other page.

    <mat-expansion-panel [expanded]="false" on the target page.

    All components use OnPush change detection.

    Issue

    For both situations the expansion panel starts as expanded, the page fades/slides in, then a moment later the expansion panel closes.
    I also sometimes have to click the header twice to get the panel to expand.

    Research

    I tried some of the solutions given above, with [@.disabled]="true" being the simplest fix for me, but I was left unsatisfied as it all felt like work-arounds for whatever the root problems was, and I was hoping for a solution that only changed the animation sequence/config.

    After spending some time debugging in and out of the animation code and the components, I came to the conclusion that Angular wasn't animating the components on-page until some time after the router animation had finished.
    In other words the animation triggers were not getting triggered until the page was done animating and what appeared to be a new change detection cycle had started.

    This is obviously undesired, but even more concerning is this doesn't match my expectations, as I have an animateChild() query at the very end of my sequence that I expected to link up the child animations with the route animation.

    transition('* => *', [
      // Prep for animation
      group([
        query(':enter', style(...), { optional: true }),
        query(':leave', style(...), { optional: true }),
      ]),
      // Slide in
      group([
        query(':enter', animate(...), { optional: true }),
        query(':leave', animate(...), { optional: true }),
      ]),
      // Animate children
      query(':enter', animateChild(), { optional: true }),
    ]);

    I admit I know very little about Angular animations, this is the first project I have used them, so I thought if I moved the animateChild() higher up in the sequence it would do all the on-page animations first, then slide the page in.
    However, no matter where I put the animateChild() the results were the same. Even if it was the first element in the sequence, the animations on-page always ran some time after the route animation had finished.

    Stumbling into a solution

    What stood out to me at this point was the Child of animateChild(), I had assumed that it animated all descendants not just direct children. Maybe that was a bad assumption?
    I didn't find anything like animateChildRecursive() or animateDescendants().

    So, is there a way I can target all descendants?
    A quick read of the query function, I spotted some potential candidates

    `query(":animating")` : Query all currently animating elements.
    `query("@*")` : Query all elements that contain an animation triggers.

    I tried with query(":animating") and got the same results as before, so I tried query("@*") and unexpectedly everything worked!

    Working Solution

    In the end my animation sequence ended up being

    transition('* => *', [
      // Prep for animation
      group([
        query(':enter', style(...), { optional: true }),
        query(':leave', style(...), { optional: true }),
      ]),
      // Slide in
      group([
        query(':enter', animate(...), { optional: true }),
        query(':leave', animate(...), { optional: true }),
      ]),
      // Animate :enter descendants
    query(':enter', query('@*', animateChild(), { optional: true }), { optional: true }),
    ]);

    I think query(':enter', query('@*' is targeting all descendants of the page entering and animating them.

    I still had the .ng-animating red border @simeyla suggested from before, and with the fix, the expansion panel no longer has a red border while the page is sliding in.

    Final Thoughts

    While this did fix all the issues I was having, I still don't fully understand why it fixed them. It seems like Angular was not animating the full descendant tree with animateChild() and I don't know if that was my fault or just a bad expectation.
    I hope this helps someone else having similar issues, but I will say I don't know the full scope of the @* selector so it is entirely possible this will trigger animations deep in the page undesirably.

    My remaining unanswered questions are

    1. Why query(':enter', animateChild() doesn't seem to make it all the way down to the elements on the page.
      • I assume this is a failure of my understanding of Angular animations, and I have a broken "chain" of animation links
    2. Why does placing query(':enter', query('@*' as the last step in the route animation fix the problem?
      • I would have expected that placing it at the end would yield the same results, but no, the expansion panel is collapsed before the route animation slides the page over.
  19. self-assigned this
    on Dec 5, 2024
  20. added 2 commits that reference this issue on Dec 5, 2024
    bcfd6c3
    b35c757
  21. added 2 commits that reference this issue on Dec 5, 2024
    7b64c45
    aafa151
  22. angular-automatic-lock-bot commented on Jan 5, 2025

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  23. locked and limited conversation to collaborators on Jan 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P3area: material/expansion

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions