Skip to content

Commit 3daa360

Browse files
authored
feat(material/core): introduce custom token for controlling animations (#30749)
Even though Material is currently decoupled from the animations module, users still need to import the `NoopAnimationsModule` to disable them. These changes introduce a custom token so that users don't have to depend on the animations-related packages at all.
1 parent 66fe9f5 commit 3daa360

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

goldens/material/core/index.api.md

+8
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ export class AnimationDurations {
5151
static EXITING: string;
5252
}
5353

54+
// @public
55+
export interface AnimationsConfig {
56+
animationsDisabled: boolean;
57+
}
58+
5459
// @public
5560
export function _animationsDisabled(): boolean;
5661

@@ -189,6 +194,9 @@ export type MatDateFormats = {
189194
};
190195
};
191196

197+
// @public
198+
export const MATERIAL_ANIMATIONS: InjectionToken<AnimationsConfig>;
199+
192200
// @public @deprecated
193201
export const MATERIAL_SANITY_CHECKS: InjectionToken<SanityChecks>;
194202

src/dev-app/main.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,21 @@ import '@angular/localize/init';
1111

1212
import {provideHttpClient} from '@angular/common/http';
1313
import {
14-
importProvidersFrom,
1514
provideExperimentalZonelessChangeDetection,
1615
// tslint:disable-next-line:no-zone-dependencies -- Allow manual testing of dev-app with zones
1716
provideZoneChangeDetection,
1817
} from '@angular/core';
1918
import {bootstrapApplication} from '@angular/platform-browser';
20-
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
21-
import {RouterModule} from '@angular/router';
19+
import {provideRouter} from '@angular/router';
2220

2321
import {Directionality} from '@angular/cdk/bidi';
2422
import {FullscreenOverlayContainer, OverlayContainer} from '@angular/cdk/overlay';
25-
import {MAT_RIPPLE_GLOBAL_OPTIONS, provideNativeDateAdapter} from '@angular/material/core';
23+
import {
24+
MAT_RIPPLE_GLOBAL_OPTIONS,
25+
MATERIAL_ANIMATIONS,
26+
provideNativeDateAdapter,
27+
AnimationsConfig,
28+
} from '@angular/material/core';
2629

2730
import {DevApp} from './dev-app';
2831
import {DevAppDirectionality} from './dev-app/dev-app-directionality';
@@ -46,14 +49,15 @@ document.head.appendChild(theme);
4649
function bootstrap(): void {
4750
bootstrapApplication(DevApp, {
4851
providers: [
49-
importProvidersFrom(
50-
BrowserAnimationsModule.withConfig({
51-
disableAnimations: !cachedAppState.animations,
52-
}),
53-
RouterModule.forRoot(DEV_APP_ROUTES),
54-
),
52+
provideRouter(DEV_APP_ROUTES),
5553
provideNativeDateAdapter(),
5654
provideHttpClient(),
55+
{
56+
provide: MATERIAL_ANIMATIONS,
57+
useValue: {
58+
animationsDisabled: !cachedAppState.animations,
59+
} as AnimationsConfig,
60+
},
5761
{provide: OverlayContainer, useClass: FullscreenOverlayContainer},
5862
{provide: MAT_RIPPLE_GLOBAL_OPTIONS, useExisting: DevAppRippleOptions},
5963
{provide: Directionality, useClass: DevAppDirectionality},

src/material/core/animation/animation.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,16 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {ANIMATION_MODULE_TYPE, inject} from '@angular/core';
9+
import {ANIMATION_MODULE_TYPE, inject, InjectionToken} from '@angular/core';
10+
11+
/** Object used to configure the animation in Angular Material. */
12+
export interface AnimationsConfig {
13+
/** Whether all animations should be disabled. */
14+
animationsDisabled: boolean;
15+
}
16+
17+
/** Injection token used to configure the animations in Angular Material. */
18+
export const MATERIAL_ANIMATIONS = new InjectionToken<AnimationsConfig>('MATERIAL_ANIMATIONS');
1019

1120
/**
1221
* @deprecated No longer used, will be removed.
@@ -36,5 +45,11 @@ export class AnimationDurations {
3645
* @docs-private
3746
*/
3847
export function _animationsDisabled(): boolean {
48+
const customToken = inject(MATERIAL_ANIMATIONS, {optional: true});
49+
50+
if (customToken) {
51+
return customToken.animationsDisabled;
52+
}
53+
3954
return inject(ANIMATION_MODULE_TYPE, {optional: true}) === 'NoopAnimations';
4055
}

0 commit comments

Comments
 (0)