Skip to content

Commit

Permalink
refactor: simplify no-animation directive (#8486)
Browse files Browse the repository at this point in the history
  • Loading branch information
HyperLife1119 committed Apr 16, 2024
1 parent 28ff3ac commit 55cbf18
Showing 1 changed file with 8 additions and 38 deletions.
46 changes: 8 additions & 38 deletions components/core/no-animation/nz-no-animation.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,18 @@
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/

import { coerceElement } from '@angular/cdk/coercion';
import { AfterViewInit, Directive, ElementRef, Inject, Input, OnChanges, Optional, Renderer2 } from '@angular/core';
import { Directive, Input, booleanAttribute, inject } from '@angular/core';
import { ANIMATION_MODULE_TYPE } from '@angular/platform-browser/animations';

import { BooleanInput } from 'ng-zorro-antd/core/types';
import { InputBoolean } from 'ng-zorro-antd/core/util';

const DISABLED_CLASSNAME = 'nz-animate-disabled';

@Directive({
selector: '[nzNoAnimation]',
exportAs: 'nzNoAnimation',
standalone: true
})
export class NzNoAnimationDirective implements OnChanges, AfterViewInit {
static ngAcceptInputType_nzNoAnimation: BooleanInput;

@Input() @InputBoolean() nzNoAnimation: boolean = false;

constructor(
private element: ElementRef,
private renderer: Renderer2,
@Optional() @Inject(ANIMATION_MODULE_TYPE) private animationType: string
) {}

ngOnChanges(): void {
this.updateClass();
}

ngAfterViewInit(): void {
this.updateClass();
}

private updateClass(): void {
const element = coerceElement(this.element);
if (!element) {
return;
}
if (this.nzNoAnimation || this.animationType === 'NoopAnimations') {
this.renderer.addClass(element, DISABLED_CLASSNAME);
} else {
this.renderer.removeClass(element, DISABLED_CLASSNAME);
}
standalone: true,
host: {
'[class.nz-animate-disabled]': `nzNoAnimation || animationType === 'NoopAnimations'`
}
})
export class NzNoAnimationDirective {
animationType = inject(ANIMATION_MODULE_TYPE, { optional: true });
@Input({ transform: booleanAttribute }) nzNoAnimation: boolean = false;
}

0 comments on commit 55cbf18

Please sign in to comment.