1

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

raaaay
  • 496
  • 7
  • 14
vito
  • 473
  • 1
  • 7
  • 17
  • @Vega i have updated question with the css also – vito Jul 13 '17 at 18:03
  • Angular doesn't play leaving animations for elements that are children in an *ngIf template for whatever reason. Try to move the animation to the same element that has the *ngIf template on it. – smoyer Jul 13 '17 at 18:31

1 Answers1

3

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.

smoyer
  • 7,932
  • 3
  • 17
  • 26
  • so there is no way to perform leave animation ? – Raas Masood Nov 17 '17 at 01:48
  • 1
    @RaasMasood there is, you just need to make sure your animation is declared on the element you have your *ngIf on. – smoyer Nov 17 '17 at 15:13
  • https://stackoverflow.com/questions/47341321/angular-2-enter-animation-on-child-component-works-but-leave-does-not i asked the question here and looks like Angular just does not support leaving – Raas Masood Nov 17 '17 at 19:09