Search code examples
angularangular-animations

Angular animation leave transition on child div


Enter animation is working fine, but the leave animation is not working. if I move the @modalFadeZoom to parent both transitions works, but the problem with that is the scaling is not happening from center of the modal, which is giving weird animation.

If i move @modalFadeZoom to the child the fade in zoom out is working fine. but no fade out and zoom in on closing the model.

component html

    <div class="modal-dialog" *ngIf="showModal">
            <div class="modal-content" [@modalFadeZoom]>
                <div class="modal-header">
                    <h5 class="modal-title">SOME TITLE</h5>
                    <button type="button" (click)="showModal=false" class="close" aria-label="Close">
              <span aria-hidden="true">×</span>
            </button>
                </div>
                <div class="modal-body">
                    lorem ipsum etc etc
                </div>
            </div>
        </div>
        <div class="modal-backdrop fade show" *ngIf="showModal"></div>

ts file

    import { Component, OnInit, trigger, transition, style, animate } from '@angular/core';
    
        @Component({
            templateUrl: 'modal.template.html',
            animations: [
                trigger(
                    'modalFadeZoom',
                    [
                        transition(
                            ':enter', [
                                style({ transform: 'scale(.7)', opacity: 0 }),
                                animate('0.3s', style({ opacity: 1, transform: 'scale(1)' })),
                            ]
                        ),
                        transition(
                            ':leave', [
                                style({ opacity: 1, transform: 'scale(1)' }),
                                animate('5.3s', style({ opacity: 0, transform: 'scale(.7)' })),
                            ]
                        ),
                    ])
            ]
        })
        
        export class ModalComponent implements OnInit {
        
            private showModal = false;;
        
            ngOnInit(): void {
                this.showModal = true;
            }
        }

css for modal-dialog

    .modal-dialog {
        position: fixed;
        top: 50%;
        left: 50%;
        height: auto;
        z-index: 2000;
        transform: translateX(-50%) translateY(-50%);
    }

plnkr link

notice that i have moved animation to parent in the plnkr demo animation starts off from the center, because of scaling.

https://plnkr.co/Ldn4wJwuZMaaunVWuYpx


Solution

  • Change your animation declaration to this:

    trigger(
      'modalFadeZoom',
      [
        transition(
          ':enter', [
            style({ transform: 'translateX(-50%) translateY(-50%) scale(.7)', opacity: 0 }),
            animate('0.3s', style({ opacity: 1, transform: 'translateX(-50%) translateY(-50%) scale(1)' })),
          ]
        ),
        transition(
          ':leave', [
            style({ opacity: 1, transform: 'translateX(-50%) translateY(-50%) scale(1)' }),
            animate('5.3s', style({ opacity: 0, transform: 'translateX(-50%) translateY(-50%) scale(.7)' })),
          ]
        ),
      ])
    

    And move the animation to the outermost element(model-dialog).

    Angular doesn't play leaving animations when they're children inside an *ngIf.