Skip to content

Commit c945e81

Browse files
authored
feat(module:core): add provideNzNoAnimation (#9555)
1 parent 0e23e80 commit c945e81

44 files changed

Lines changed: 293 additions & 123 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎components/auto-complete/autocomplete.component.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
3333
import { Observable, Subscription, defer, merge } from 'rxjs';
3434
import { filter, switchMap } from 'rxjs/operators';
3535

36-
import { slideMotion } from 'ng-zorro-antd/core/animation';
37-
import { NzNoAnimationDirective } from 'ng-zorro-antd/core/no-animation';
36+
import { slideMotion, NzNoAnimationDirective } from 'ng-zorro-antd/core/animation';
3837
import { NZ_AFTER_NEXT_RENDER$ } from 'ng-zorro-antd/core/render';
3938
import { CompareWith, NzSafeAny } from 'ng-zorro-antd/core/types';
4039
import { numberAttributeWithZeroFallback } from 'ng-zorro-antd/core/util';

‎components/badge/badge-sup.component.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ import {
1616
ViewEncapsulation
1717
} from '@angular/core';
1818

19-
import { zoomBadgeMotion } from 'ng-zorro-antd/core/animation';
20-
import { NzNoAnimationDirective } from 'ng-zorro-antd/core/no-animation';
19+
import { zoomBadgeMotion, NzNoAnimationDirective } from 'ng-zorro-antd/core/animation';
2120
import { NgStyleInterface, NzSafeAny, NzSizeDSType } from 'ng-zorro-antd/core/types';
2221

2322
@Component({

‎components/badge/badge.component.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ import {
1616
inject
1717
} from '@angular/core';
1818

19-
import { zoomBadgeMotion } from 'ng-zorro-antd/core/animation';
19+
import { zoomBadgeMotion, NzNoAnimationDirective } from 'ng-zorro-antd/core/animation';
2020
import { NzConfigKey, NzConfigService, WithConfig } from 'ng-zorro-antd/core/config';
21-
import { NzNoAnimationDirective } from 'ng-zorro-antd/core/no-animation';
2221
import { NzOutletModule } from 'ng-zorro-antd/core/outlet';
2322
import { NgStyleInterface, NzSafeAny, NzSizeDSType } from 'ng-zorro-antd/core/types';
2423

‎components/badge/badge.spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testin
99
import { By } from '@angular/platform-browser';
1010
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';
1111

12-
import { NzNoAnimationDirective } from 'ng-zorro-antd/core/no-animation';
12+
import { NzNoAnimationDirective } from 'ng-zorro-antd/core/animation';
1313
import { NgStyleInterface, NzSizeDSType } from 'ng-zorro-antd/core/types';
1414

1515
import { NzBadgeComponent } from './badge.component';

‎components/cascader/cascader.component.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,9 @@ import { ControlValueAccessor, FormsModule, NG_VALUE_ACCESSOR } from '@angular/f
4545
import { BehaviorSubject, merge, Observable, of } from 'rxjs';
4646
import { distinctUntilChanged, map, startWith, switchMap, withLatestFrom } from 'rxjs/operators';
4747

48-
import { slideMotion } from 'ng-zorro-antd/core/animation';
48+
import { slideMotion, NzNoAnimationDirective } from 'ng-zorro-antd/core/animation';
4949
import { NzConfigKey, onConfigChangeEventForComponent, WithConfig } from 'ng-zorro-antd/core/config';
5050
import { NzFormItemFeedbackIconComponent, NzFormNoStatusService, NzFormStatusService } from 'ng-zorro-antd/core/form';
51-
import { NzNoAnimationDirective } from 'ng-zorro-antd/core/no-animation';
5251
import { NzStringTemplateOutletDirective } from 'ng-zorro-antd/core/outlet';
5352
import {
5453
DEFAULT_CASCADER_POSITIONS,

‎components/collapse/collapse-panel.component.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ import {
2424
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
2525
import { filter } from 'rxjs/operators';
2626

27-
import { collapseMotion } from 'ng-zorro-antd/core/animation';
27+
import { collapseMotion, NzNoAnimationDirective } from 'ng-zorro-antd/core/animation';
2828
import { NzConfigKey, onConfigChangeEventForComponent, WithConfig } from 'ng-zorro-antd/core/config';
29-
import { NzNoAnimationDirective } from 'ng-zorro-antd/core/no-animation';
3029
import { NzOutletModule } from 'ng-zorro-antd/core/outlet';
3130
import { fromEventOutsideAngular } from 'ng-zorro-antd/core/util';
3231
import { NzIconModule } from 'ng-zorro-antd/icon';
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/**
2+
* Use of this source code is governed by an MIT-style license that can be
3+
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
4+
*/
5+
6+
import { ANIMATION_MODULE_TYPE, Component, signal } from '@angular/core';
7+
import { ComponentFixture, TestBed } from '@angular/core/testing';
8+
import { provideNoopAnimations } from '@angular/platform-browser/animations';
9+
10+
import {
11+
NZ_NO_ANIMATION_CLASS,
12+
NzNoAnimationDirective,
13+
provideNzNoAnimation,
14+
withAnimationCheck
15+
} from './no-animation';
16+
17+
@Component({
18+
template: `<div #element [nzNoAnimation]="noAnimation()" data-testid="test-element"></div>`,
19+
imports: [NzNoAnimationDirective]
20+
})
21+
class TestComponent {
22+
noAnimation = signal(false);
23+
animationClass = withAnimationCheck(() => 'animation-class');
24+
}
25+
26+
describe('NzNoAnimationDirective', () => {
27+
let fixture: ComponentFixture<TestComponent>;
28+
let component: TestComponent;
29+
30+
describe('without NoopAnimations', () => {
31+
beforeEach(() => {
32+
TestBed.configureTestingModule({});
33+
fixture = TestBed.createComponent(TestComponent);
34+
component = fixture.componentInstance;
35+
fixture.detectChanges();
36+
});
37+
38+
it('should not add animation disabled class when nzNoAnimation is false', () => {
39+
const element = fixture.nativeElement.querySelector('[data-testid="test-element"]');
40+
expect(element.classList.contains(NZ_NO_ANIMATION_CLASS)).toBeFalse();
41+
});
42+
43+
it('should add animation disabled class when nzNoAnimation is true', async () => {
44+
component.noAnimation.set(true);
45+
await fixture.whenStable();
46+
const element = fixture.nativeElement.querySelector('[data-testid="test-element"]');
47+
expect(element.classList.contains(NZ_NO_ANIMATION_CLASS)).toBeTrue();
48+
});
49+
});
50+
51+
describe('with NoopAnimations', () => {
52+
beforeEach(() => {
53+
TestBed.configureTestingModule({
54+
providers: [provideNoopAnimations()]
55+
});
56+
fixture = TestBed.createComponent(TestComponent);
57+
component = fixture.componentInstance;
58+
fixture.detectChanges();
59+
});
60+
61+
it('should add animation disabled class even when nzNoAnimation is false', () => {
62+
const element = fixture.nativeElement.querySelector('[data-testid="test-element"]');
63+
expect(element.classList.contains(NZ_NO_ANIMATION_CLASS)).toBeTrue();
64+
});
65+
66+
it('should keep animation disabled class when nzNoAnimation is true', async () => {
67+
component.noAnimation.set(true);
68+
await fixture.whenStable();
69+
const element = fixture.nativeElement.querySelector('[data-testid="test-element"]');
70+
expect(element.classList.contains(NZ_NO_ANIMATION_CLASS)).toBeTrue();
71+
});
72+
});
73+
});
74+
75+
describe('provideNzNoAnimation', () => {
76+
beforeEach(() => {
77+
TestBed.configureTestingModule({
78+
providers: [provideNzNoAnimation()]
79+
});
80+
});
81+
82+
it('should provide NoopAnimations animation type', () => {
83+
const animationType = TestBed.inject(ANIMATION_MODULE_TYPE);
84+
expect(animationType).toBe('NoopAnimations');
85+
});
86+
87+
it('should disable animations globally when used', () => {
88+
const fixture = TestBed.createComponent(TestComponent);
89+
fixture.detectChanges();
90+
91+
const element = fixture.nativeElement.querySelector('[data-testid="test-element"]');
92+
expect(element.classList.contains(NZ_NO_ANIMATION_CLASS)).toBeTrue();
93+
});
94+
});
95+
96+
describe('withAnimationCheck', () => {
97+
it('should return dynamic class when animations are enabled', () => {
98+
TestBed.configureTestingModule({});
99+
const fixture = TestBed.createComponent(TestComponent);
100+
fixture.detectChanges();
101+
102+
expect(fixture.componentInstance.animationClass()).toBe('animation-class');
103+
});
104+
105+
it('should return no-animation class with provideNoopAnimations', () => {
106+
TestBed.configureTestingModule({
107+
providers: [provideNoopAnimations()]
108+
});
109+
const fixture = TestBed.createComponent(TestComponent);
110+
fixture.detectChanges();
111+
112+
expect(fixture.componentInstance.animationClass()).toBe(NZ_NO_ANIMATION_CLASS);
113+
});
114+
115+
it('should return no-animation class with provideNzNoAnimation', () => {
116+
TestBed.configureTestingModule({
117+
providers: [provideNzNoAnimation()]
118+
});
119+
const fixture = TestBed.createComponent(TestComponent);
120+
fixture.detectChanges();
121+
122+
expect(fixture.componentInstance.animationClass()).toBe(NZ_NO_ANIMATION_CLASS);
123+
});
124+
125+
describe('injection context validation', () => {
126+
it('should throw error when called outside injection context in dev mode', () => {
127+
const originalDevMode = (globalThis as { ngDevMode?: boolean }).ngDevMode;
128+
(globalThis as { ngDevMode?: boolean }).ngDevMode = true;
129+
130+
expect(() => {
131+
withAnimationCheck(() => 'test-class');
132+
}).toThrow();
133+
134+
(globalThis as { ngDevMode?: boolean }).ngDevMode = originalDevMode;
135+
});
136+
});
137+
});
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Use of this source code is governed by an MIT-style license that can be
3+
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
4+
*/
5+
6+
import {
7+
ANIMATION_MODULE_TYPE,
8+
assertInInjectionContext,
9+
booleanAttribute,
10+
computed,
11+
Directive,
12+
inject,
13+
input,
14+
NgModule,
15+
Provider,
16+
signal,
17+
Signal
18+
} from '@angular/core';
19+
20+
export const NZ_NO_ANIMATION_CLASS = 'nz-animate-disabled';
21+
22+
@Directive({
23+
selector: '[nzNoAnimation]',
24+
exportAs: 'nzNoAnimation',
25+
host: {
26+
[`[class.${NZ_NO_ANIMATION_CLASS}]`]: `nzNoAnimation() || animationType === 'NoopAnimations'`
27+
}
28+
})
29+
export class NzNoAnimationDirective {
30+
readonly animationType = inject(ANIMATION_MODULE_TYPE, { optional: true });
31+
readonly nzNoAnimation = input(false, { transform: booleanAttribute });
32+
}
33+
34+
/**
35+
* @deprecated Will be removed in v23, please use `NzNoAnimationDirective` instead.
36+
*/
37+
@NgModule({
38+
imports: [NzNoAnimationDirective],
39+
exports: [NzNoAnimationDirective]
40+
})
41+
export class NzNoAnimationModule {}
42+
43+
/**
44+
* Returns the set of dependency-injection providers to disable animations in a context.
45+
*/
46+
export function provideNzNoAnimation(): Provider[] {
47+
return [
48+
{
49+
provide: ANIMATION_MODULE_TYPE,
50+
useValue: 'NoopAnimations'
51+
}
52+
];
53+
}
54+
55+
/**
56+
* If the current animation mode is `NoopAnimations`, returns the no-animation class as a signal.
57+
* Otherwise, returns the result of the provided class name getter as a computed signal.
58+
* @param classNameGetter A function that returns the class name string.
59+
*/
60+
export function withAnimationCheck(classNameGetter: () => string): Signal<string> {
61+
if (typeof ngDevMode !== 'undefined' && ngDevMode) {
62+
assertInInjectionContext(withAnimationCheck);
63+
}
64+
65+
const animationType = inject(ANIMATION_MODULE_TYPE, { optional: true });
66+
return animationType === 'NoopAnimations' ? signal(NZ_NO_ANIMATION_CLASS) : computed(classNameGetter);
67+
}

‎components/core/animation/public-api.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ export * from './slide';
1313
export * from './tabs';
1414
export * from './thumb';
1515
export * from './zoom';
16+
export * from './no-animation';

‎components/core/no-animation/index.ts‎

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)