55
66import { Direction , Directionality } from '@angular/cdk/bidi' ;
77import {
8+ ANIMATION_MODULE_TYPE ,
9+ AnimationCallbackEvent ,
10+ booleanAttribute ,
811 ChangeDetectionStrategy ,
912 ChangeDetectorRef ,
1013 Component ,
14+ DestroyRef ,
1115 EventEmitter ,
16+ inject ,
1217 Input ,
1318 OnChanges ,
1419 OnInit ,
1520 Output ,
1621 SimpleChanges ,
1722 TemplateRef ,
18- ViewEncapsulation ,
19- booleanAttribute ,
20- inject ,
21- DestroyRef
23+ ViewEncapsulation
2224} from '@angular/core' ;
2325import { takeUntilDestroyed } from '@angular/core/rxjs-interop' ;
2426
25- import { slideAlertMotion } from 'ng-zorro-antd/core/animation' ;
27+ import { NzNoAnimationDirective } from 'ng-zorro-antd/core/animation' ;
2628import { NzConfigKey , onConfigChangeEventForComponent , WithConfig } from 'ng-zorro-antd/core/config' ;
2729import { NzOutletModule } from 'ng-zorro-antd/core/outlet' ;
2830import { NzIconModule } from 'ng-zorro-antd/icon' ;
@@ -33,12 +35,12 @@ export type NzAlertType = 'success' | 'info' | 'warning' | 'error';
3335@Component ( {
3436 selector : 'nz-alert' ,
3537 exportAs : 'nzAlert' ,
36- animations : [ slideAlertMotion ] ,
37- imports : [ NzIconModule , NzOutletModule ] ,
38+ imports : [ NzIconModule , NzOutletModule , NzNoAnimationDirective ] ,
3839 template : `
3940 @if (!closed) {
4041 <div
4142 class="ant-alert"
43+ [nzNoAnimation]="nzNoAnimation"
4244 [class.ant-alert-rtl]="dir === 'rtl'"
4345 [class.ant-alert-success]="nzType === 'success'"
4446 [class.ant-alert-info]="nzType === 'info'"
@@ -48,9 +50,7 @@ export type NzAlertType = 'success' | 'info' | 'warning' | 'error';
4850 [class.ant-alert-banner]="nzBanner"
4951 [class.ant-alert-closable]="nzCloseable"
5052 [class.ant-alert-with-description]="!!nzDescription"
51- [@.disabled]="nzNoAnimation"
52- [@slideAlertMotion]
53- (@slideAlertMotion.done)="onFadeAnimationDone()"
53+ (animate.leave)="onLeaveAnimationDone($event)"
5454 >
5555 @if (nzShowIcon) {
5656 <div class="ant-alert-icon">
@@ -104,6 +104,7 @@ export class NzAlertComponent implements OnChanges, OnInit {
104104 private cdr = inject ( ChangeDetectorRef ) ;
105105 private directionality = inject ( Directionality ) ;
106106 private readonly destroyRef = inject ( DestroyRef ) ;
107+ private readonly animationType = inject ( ANIMATION_MODULE_TYPE , { optional : true } ) ;
107108 readonly _nzModuleName : NzConfigKey = NZ_CONFIG_MODULE_NAME ;
108109
109110 @Input ( ) nzAction : string | TemplateRef < void > | null = null ;
@@ -140,12 +141,32 @@ export class NzAlertComponent implements OnChanges, OnInit {
140141
141142 closeAlert ( ) : void {
142143 this . closed = true ;
144+ // When animations are disabled, emit immediately since animate.leave won't trigger
145+ if ( this . nzNoAnimation || this . animationType === 'NoopAnimations' ) {
146+ this . nzOnClose . emit ( true ) ;
147+ }
143148 }
144149
145- onFadeAnimationDone ( ) : void {
146- if ( this . closed ) {
147- this . nzOnClose . emit ( true ) ;
150+ onLeaveAnimationDone ( event : AnimationCallbackEvent ) : void {
151+ const element = event . target as HTMLElement ;
152+
153+ // If animations are disabled, complete immediately (nzOnClose already emitted in closeAlert)
154+ if ( this . nzNoAnimation || this . animationType === 'NoopAnimations' ) {
155+ event . animationComplete ( ) ;
156+ return ;
148157 }
158+
159+ // Apply animation classes
160+ element . classList . add ( 'ant-alert-motion-leave' , 'ant-alert-motion-leave-active' ) ;
161+
162+ // Listen for transition end to complete the animation
163+ const onTransitionEnd = ( ) : void => {
164+ element . removeEventListener ( 'transitionend' , onTransitionEnd ) ;
165+ this . nzOnClose . emit ( true ) ;
166+ event . animationComplete ( ) ;
167+ } ;
168+
169+ element . addEventListener ( 'transitionend' , onTransitionEnd ) ;
149170 }
150171
151172 ngOnChanges ( changes : SimpleChanges ) : void {
0 commit comments