Skip to content

Commit 82f0fa6

Browse files
authored
feat(material/core): handle prefers-reduced-motion automatically (#30796)
Adds some logic so that animations automatically get disabled if the `prefers-reduced-motion` media query matches.
1 parent 4e41ce7 commit 82f0fa6

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

goldens/material/core/index.api.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class AnimationDurations {
5353

5454
// @public
5555
export interface AnimationsConfig {
56-
animationsDisabled: boolean;
56+
animationsDisabled?: boolean;
5757
}
5858

5959
// @public

src/material/core/animation/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ ng_project(
77
srcs = ["animation.ts"],
88
deps = [
99
"//:node_modules/@angular/core",
10+
"//src/cdk/layout",
1011
],
1112
)

src/material/core/animation/animation.ts

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

9+
import {MediaMatcher} from '@angular/cdk/layout';
910
import {ANIMATION_MODULE_TYPE, inject, InjectionToken} from '@angular/core';
1011

1112
/** Object used to configure the animation in Angular Material. */
1213
export interface AnimationsConfig {
1314
/** Whether all animations should be disabled. */
14-
animationsDisabled: boolean;
15+
animationsDisabled?: boolean;
1516
}
1617

1718
/** Injection token used to configure the animations in Angular Material. */
@@ -45,11 +46,13 @@ export class AnimationDurations {
4546
* @docs-private
4647
*/
4748
export function _animationsDisabled(): boolean {
48-
const customToken = inject(MATERIAL_ANIMATIONS, {optional: true});
49-
50-
if (customToken) {
51-
return customToken.animationsDisabled;
49+
if (
50+
inject(MATERIAL_ANIMATIONS, {optional: true})?.animationsDisabled ||
51+
inject(ANIMATION_MODULE_TYPE, {optional: true}) === 'NoopAnimations'
52+
) {
53+
return true;
5254
}
5355

54-
return inject(ANIMATION_MODULE_TYPE, {optional: true}) === 'NoopAnimations';
56+
const mediaMatcher = inject(MediaMatcher);
57+
return mediaMatcher.matchMedia('(prefers-reduced-motion)').matches;
5558
}

0 commit comments

Comments
 (0)