Skip to content

Commit 9507900

Browse files
committed
fix(material/progress-bar): slow down animations instead of fully stopping them
Reworks the progress bar to slow down its animations, instead of fully stopping them when `prefers-reduced-motion` is enabled. Stopping the animations completely appears to be confusing for users.
1 parent d1083d7 commit 9507900

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/material/progress-bar/progress-bar.scss

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
$fallbacks: m3-progress-bar.get-tokens();
77

88
.mat-mdc-progress-bar {
9+
--mat-progress-bar-animation-multiplier: 1;
10+
911
// Explicitly set to `block` since the browser defaults custom elements to `inline`.
1012
display: block;
1113

@@ -35,6 +37,12 @@ $fallbacks: m3-progress-bar.get-tokens();
3537
}
3638
}
3739

40+
// Slow down the animation by 100% when the user configured their OS to reduce
41+
// motion since some animations like the indeterminate one can be quite dynamic.
42+
.mat-progress-bar-reduced-motion {
43+
--mat-progress-bar-animation-multiplier: 2;
44+
}
45+
3846
.mdc-linear-progress {
3947
position: relative;
4048
width: 100%;
@@ -105,15 +113,17 @@ $fallbacks: m3-progress-bar.get-tokens();
105113
background-repeat: repeat-x;
106114
flex: auto;
107115
transform: rotate(180deg);
108-
animation: mdc-linear-progress-buffering 250ms infinite linear;
116+
animation: mdc-linear-progress-buffering
117+
calc(250ms * var(--mat-progress-bar-animation-multiplier)) infinite linear;
109118
background-color: token-utils.slot(progress-bar-track-color, $fallbacks);
110119

111120
@include cdk.high-contrast {
112121
background-color: ButtonBorder;
113122
}
114123

115124
[dir='rtl'] & {
116-
animation: mdc-linear-progress-buffering-reverse 250ms infinite linear;
125+
animation: mdc-linear-progress-buffering-reverse
126+
calc(250ms * var(--mat-progress-bar-animation-multiplier)) infinite linear;
117127
transform: rotate(0);
118128
}
119129
}
@@ -132,12 +142,14 @@ $fallbacks: m3-progress-bar.get-tokens();
132142
}
133143

134144
.mdc-linear-progress--indeterminate.mdc-linear-progress--animation-ready & {
135-
animation: mdc-linear-progress-primary-indeterminate-translate 2s infinite linear;
145+
animation: mdc-linear-progress-primary-indeterminate-translate
146+
calc(2s * var(--mat-progress-bar-animation-multiplier)) infinite linear;
136147
}
137148

138149
.mdc-linear-progress--indeterminate.mdc-linear-progress--animation-ready & {
139150
> .mdc-linear-progress__bar-inner {
140-
animation: mdc-linear-progress-primary-indeterminate-scale 2s infinite linear;
151+
animation: mdc-linear-progress-primary-indeterminate-scale
152+
calc(2s * var(--mat-progress-bar-animation-multiplier)) infinite linear;
141153
}
142154
}
143155

@@ -160,12 +172,14 @@ $fallbacks: m3-progress-bar.get-tokens();
160172
}
161173

162174
.mdc-linear-progress--indeterminate.mdc-linear-progress--animation-ready & {
163-
animation: mdc-linear-progress-secondary-indeterminate-translate 2s infinite linear;
175+
animation: mdc-linear-progress-secondary-indeterminate-translate
176+
calc(2s * var(--mat-progress-bar-animation-multiplier)) infinite linear;
164177
}
165178

166179
.mdc-linear-progress--indeterminate.mdc-linear-progress--animation-ready & {
167180
> .mdc-linear-progress__bar-inner {
168-
animation: mdc-linear-progress-secondary-indeterminate-scale 2s infinite linear;
181+
animation: mdc-linear-progress-secondary-indeterminate-scale
182+
calc(2s * var(--mat-progress-bar-animation-multiplier)) infinite linear;
169183
}
170184
}
171185

src/material/progress-bar/progress-bar.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
DOCUMENT,
2626
} from '@angular/core';
2727

28-
import {_animationsDisabled, ThemePalette} from '../core';
28+
import {_getAnimationsState, ThemePalette} from '../core';
2929

3030
/** Last animation end data. */
3131
export interface ProgressAnimationEnd {
@@ -121,10 +121,18 @@ export class MatProgressBar implements AfterViewInit, OnDestroy {
121121
constructor(...args: unknown[]);
122122

123123
constructor() {
124+
const animationsState = _getAnimationsState();
125+
124126
const defaults = inject<MatProgressBarDefaultOptions>(MAT_PROGRESS_BAR_DEFAULT_OPTIONS, {
125127
optional: true,
126128
});
127129

130+
this._isNoopAnimation = animationsState === 'di-disabled';
131+
132+
if (animationsState === 'reduced-motion') {
133+
this._elementRef.nativeElement.classList.add('mat-progress-bar-reduced-motion');
134+
}
135+
128136
if (defaults) {
129137
if (defaults.color) {
130138
this.color = this._defaultColor = defaults.color;
@@ -135,7 +143,7 @@ export class MatProgressBar implements AfterViewInit, OnDestroy {
135143
}
136144

137145
/** Flag that indicates whether NoopAnimations mode is set to true. */
138-
_isNoopAnimation = _animationsDisabled();
146+
_isNoopAnimation: boolean;
139147

140148
// TODO: should be typed as `ThemePalette` but internal apps pass in arbitrary strings.
141149
/**

0 commit comments

Comments
 (0)