Skip to content

Commit d1083d7

Browse files
committed
refactor(material/core): granular animations disabled state
Adds a function that provides more granular information about why animations are disabled.
1 parent 0d26886 commit d1083d7

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

goldens/material/core/index.api.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ export class _ErrorStateTracker {
132132
updateErrorState(): void;
133133
}
134134

135+
// @public
136+
export function _getAnimationsState(): 'enabled' | 'di-disabled' | 'reduced-motion';
137+
135138
// @public
136139
export function _getOptionScrollPosition(optionOffset: number, optionHeight: number, currentScrollPosition: number, panelHeight: number): number;
137140

src/material/core/animation/animation.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,28 @@ export class AnimationDurations {
4141
static EXITING = '195ms';
4242
}
4343

44+
let reducedMotion: boolean | null = null;
45+
4446
/**
45-
* Returns whether animations have been disabled by DI. Must be called in a DI context.
47+
* Gets the the configured animations state.
4648
* @docs-private
4749
*/
48-
export function _animationsDisabled(): boolean {
50+
export function _getAnimationsState(): 'enabled' | 'di-disabled' | 'reduced-motion' {
4951
if (
5052
inject(MATERIAL_ANIMATIONS, {optional: true})?.animationsDisabled ||
5153
inject(ANIMATION_MODULE_TYPE, {optional: true}) === 'NoopAnimations'
5254
) {
53-
return true;
55+
return 'di-disabled';
5456
}
5557

56-
const mediaMatcher = inject(MediaMatcher);
57-
return mediaMatcher.matchMedia('(prefers-reduced-motion)').matches;
58+
reducedMotion ??= inject(MediaMatcher).matchMedia('(prefers-reduced-motion)').matches;
59+
return reducedMotion ? 'reduced-motion' : 'enabled';
60+
}
61+
62+
/**
63+
* Returns whether animations have been disabled by DI. Must be called in a DI context.
64+
* @docs-private
65+
*/
66+
export function _animationsDisabled(): boolean {
67+
return _getAnimationsState() !== 'enabled';
5868
}

0 commit comments

Comments
 (0)