feat(animations): add support for disabling animations through BrowserAnimationsModule.withConfig (#40731)
Currently the only way to disable animations is by providing the `NoopAnimationsModule` which doesn't allow for it to be disabled based on runtime information. These changes add support for disabling animations based on runtime information by using `BrowserAnimationsModule.withConfig({disableAnimations: true})`. PR Close #40731
This commit is contained in:
parent
3c24136b98
commit
29d8a0ab09
|
@ -1,6 +1,11 @@
|
||||||
export declare const ANIMATION_MODULE_TYPE: InjectionToken<"NoopAnimations" | "BrowserAnimations">;
|
export declare const ANIMATION_MODULE_TYPE: InjectionToken<"NoopAnimations" | "BrowserAnimations">;
|
||||||
|
|
||||||
export declare class BrowserAnimationsModule {
|
export declare class BrowserAnimationsModule {
|
||||||
|
static withConfig(config: BrowserAnimationsModuleConfig): ModuleWithProviders<BrowserAnimationsModule>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export declare interface BrowserAnimationsModuleConfig {
|
||||||
|
disableAnimations?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export declare class NoopAnimationsModule {
|
export declare class NoopAnimationsModule {
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
"master": {
|
"master": {
|
||||||
"uncompressed": {
|
"uncompressed": {
|
||||||
"runtime-es2015": 3033,
|
"runtime-es2015": 3033,
|
||||||
"main-es2015": 447894,
|
"main-es2015": 448055,
|
||||||
"polyfills-es2015": 52493
|
"polyfills-es2015": 52493
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@
|
||||||
"master": {
|
"master": {
|
||||||
"uncompressed": {
|
"uncompressed": {
|
||||||
"runtime-es2015": 3153,
|
"runtime-es2015": 3153,
|
||||||
"main-es2015": 432647,
|
"main-es2015": 433285,
|
||||||
"polyfills-es2015": 52493
|
"polyfills-es2015": 52493
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,8 +46,7 @@ describe('animation tests', function() {
|
||||||
{declarations: [SharedAnimationCmp], imports: [BrowserAnimationsModule]});
|
{declarations: [SharedAnimationCmp], imports: [BrowserAnimationsModule]});
|
||||||
|
|
||||||
const fixture = TestBed.createComponent(SharedAnimationCmp);
|
const fixture = TestBed.createComponent(SharedAnimationCmp);
|
||||||
const cmp = fixture.componentInstance;
|
expect(fixture.componentInstance.animationType).toEqual('BrowserAnimations');
|
||||||
expect(cmp.animationType).toEqual('BrowserAnimations');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should hint at NoopAnimationsModule being used', () => {
|
it('should hint at NoopAnimationsModule being used', () => {
|
||||||
|
@ -56,8 +55,19 @@ describe('animation tests', function() {
|
||||||
{declarations: [SharedAnimationCmp], imports: [NoopAnimationsModule]});
|
{declarations: [SharedAnimationCmp], imports: [NoopAnimationsModule]});
|
||||||
|
|
||||||
const fixture = TestBed.createComponent(SharedAnimationCmp);
|
const fixture = TestBed.createComponent(SharedAnimationCmp);
|
||||||
const cmp = fixture.componentInstance;
|
expect(fixture.componentInstance.animationType).toEqual('NoopAnimations');
|
||||||
expect(cmp.animationType).toEqual('NoopAnimations');
|
});
|
||||||
|
|
||||||
|
it('should hint at NoopAnimationsModule being used when BrowserAnimationsModule is provided with disabled animations',
|
||||||
|
() => {
|
||||||
|
TestBed.resetTestingModule();
|
||||||
|
TestBed.configureTestingModule({
|
||||||
|
declarations: [SharedAnimationCmp],
|
||||||
|
imports: [BrowserAnimationsModule.withConfig({disableAnimations: true})]
|
||||||
|
});
|
||||||
|
|
||||||
|
const fixture = TestBed.createComponent(SharedAnimationCmp);
|
||||||
|
expect(fixture.componentInstance.animationType).toEqual('NoopAnimations');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
* @description
|
* @description
|
||||||
* Entry point for all animation APIs of the animation browser package.
|
* Entry point for all animation APIs of the animation browser package.
|
||||||
*/
|
*/
|
||||||
export {BrowserAnimationsModule, NoopAnimationsModule} from './module';
|
export {BrowserAnimationsModule, BrowserAnimationsModuleConfig, NoopAnimationsModule} from './module';
|
||||||
|
|
||||||
export {ANIMATION_MODULE_TYPE} from './providers';
|
export {ANIMATION_MODULE_TYPE} from './providers';
|
||||||
|
|
||||||
|
|
|
@ -5,11 +5,23 @@
|
||||||
* Use of this source code is governed by an MIT-style license that can be
|
* Use of this source code is governed by an MIT-style license that can be
|
||||||
* found in the LICENSE file at https://angular.io/license
|
* found in the LICENSE file at https://angular.io/license
|
||||||
*/
|
*/
|
||||||
import {NgModule} from '@angular/core';
|
import {ModuleWithProviders, NgModule} from '@angular/core';
|
||||||
import {BrowserModule} from '@angular/platform-browser';
|
import {BrowserModule} from '@angular/platform-browser';
|
||||||
|
|
||||||
import {BROWSER_ANIMATIONS_PROVIDERS, BROWSER_NOOP_ANIMATIONS_PROVIDERS} from './providers';
|
import {BROWSER_ANIMATIONS_PROVIDERS, BROWSER_NOOP_ANIMATIONS_PROVIDERS} from './providers';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Object used to configure the behavior of {@link BrowserAnimationsModule}
|
||||||
|
* @publicApi
|
||||||
|
*/
|
||||||
|
export interface BrowserAnimationsModuleConfig {
|
||||||
|
/**
|
||||||
|
* Whether animations should be disabled. Passing this is identical to providing the
|
||||||
|
* `NoopAnimationsModule`, but it can be controlled based on a runtime value.
|
||||||
|
*/
|
||||||
|
disableAnimations?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Exports `BrowserModule` with additional [dependency-injection providers](guide/glossary#provider)
|
* Exports `BrowserModule` with additional [dependency-injection providers](guide/glossary#provider)
|
||||||
* for use with animations. See [Animations](guide/animations).
|
* for use with animations. See [Animations](guide/animations).
|
||||||
|
@ -20,6 +32,30 @@ import {BROWSER_ANIMATIONS_PROVIDERS, BROWSER_NOOP_ANIMATIONS_PROVIDERS} from '.
|
||||||
providers: BROWSER_ANIMATIONS_PROVIDERS,
|
providers: BROWSER_ANIMATIONS_PROVIDERS,
|
||||||
})
|
})
|
||||||
export class BrowserAnimationsModule {
|
export class BrowserAnimationsModule {
|
||||||
|
/**
|
||||||
|
* Configures the module based on the specified object.
|
||||||
|
*
|
||||||
|
* @param config Object used to configure the behavior of the `BrowserAnimationsModule`.
|
||||||
|
* @see `BrowserAnimationsModuleConfig`
|
||||||
|
*
|
||||||
|
* @usageNotes
|
||||||
|
* When registering the `BrowserAnimationsModule`, you can use the `withConfig`
|
||||||
|
* function as follows:
|
||||||
|
* ```
|
||||||
|
* @NgModule({
|
||||||
|
* imports: [BrowserAnimationsModule.withConfig(config)]
|
||||||
|
* })
|
||||||
|
* class MyNgModule {}
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
static withConfig(config: BrowserAnimationsModuleConfig):
|
||||||
|
ModuleWithProviders<BrowserAnimationsModule> {
|
||||||
|
return {
|
||||||
|
ngModule: BrowserAnimationsModule,
|
||||||
|
providers: config.disableAnimations ? BROWSER_NOOP_ANIMATIONS_PROVIDERS :
|
||||||
|
BROWSER_ANIMATIONS_PROVIDERS
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -9,15 +9,34 @@ import {animate, style, transition, trigger} from '@angular/animations';
|
||||||
import {ɵAnimationEngine} from '@angular/animations/browser';
|
import {ɵAnimationEngine} from '@angular/animations/browser';
|
||||||
import {Component} from '@angular/core';
|
import {Component} from '@angular/core';
|
||||||
import {TestBed} from '@angular/core/testing';
|
import {TestBed} from '@angular/core/testing';
|
||||||
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
|
import {BrowserAnimationsModule, NoopAnimationsModule} from '@angular/platform-browser/animations';
|
||||||
|
|
||||||
{
|
|
||||||
describe('NoopAnimationsModule', () => {
|
describe('NoopAnimationsModule', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
TestBed.configureTestingModule({imports: [NoopAnimationsModule]});
|
TestBed.configureTestingModule({imports: [NoopAnimationsModule]});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
noopAnimationTests();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('BrowserAnimationsModule with disableAnimations = true', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
TestBed.configureTestingModule(
|
||||||
|
{imports: [BrowserAnimationsModule.withConfig({disableAnimations: true})]});
|
||||||
|
});
|
||||||
|
|
||||||
|
noopAnimationTests();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
function noopAnimationTests() {
|
||||||
it('should flush and fire callbacks when the zone becomes stable', (async) => {
|
it('should flush and fire callbacks when the zone becomes stable', (async) => {
|
||||||
|
// This test is only meant to be run inside the browser.
|
||||||
|
if (isNode) {
|
||||||
|
async();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'my-cmp',
|
selector: 'my-cmp',
|
||||||
template:
|
template:
|
||||||
|
@ -56,6 +75,12 @@ import {NoopAnimationsModule} from '@angular/platform-browser/animations';
|
||||||
|
|
||||||
it('should handle leave animation callbacks even if the element is destroyed in the process',
|
it('should handle leave animation callbacks even if the element is destroyed in the process',
|
||||||
(async) => {
|
(async) => {
|
||||||
|
// This test is only meant to be run inside the browser.
|
||||||
|
if (isNode) {
|
||||||
|
async();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'my-cmp',
|
selector: 'my-cmp',
|
||||||
template:
|
template:
|
||||||
|
@ -101,5 +126,4 @@ import {NoopAnimationsModule} from '@angular/platform-browser/animations';
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue