Skip to content

Commit c97bdb1

Browse files
committed
fix(progress-spinner): unable to change mode on spinner directive
Currently we have the `mat-spinner` directive which is a shortcut to a `mat-progress-spinner` with `mode="indeterminate"`. Since the spinner inherits all of the inputs from the progress spinner, there's nothing stoping people from changing the mode back to `determinate`, however the element will look half-broken because the host bindings assume that the mode won't change. These changes update the host bindings to allow switching between modes. Fixes #14511.
1 parent ecaec18 commit c97bdb1

File tree

4 files changed

+25
-40
lines changed

4 files changed

+25
-40
lines changed

src/lib/progress-spinner/progress-spinner-module.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,17 @@
88
import {NgModule} from '@angular/core';
99
import {CommonModule} from '@angular/common';
1010
import {MatCommonModule} from '@angular/material/core';
11-
import {MatProgressSpinner, MatSpinner} from './progress-spinner';
11+
import {MatProgressSpinner} from './progress-spinner';
1212

1313

1414
@NgModule({
1515
imports: [MatCommonModule, CommonModule],
1616
exports: [
1717
MatProgressSpinner,
18-
MatSpinner,
1918
MatCommonModule
2019
],
2120
declarations: [
2221
MatProgressSpinner,
23-
MatSpinner
2422
],
2523
})
2624
class MatProgressSpinnerModule {}

src/lib/progress-spinner/progress-spinner.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describe('MatProgressSpinner', () => {
2222
ProgressSpinnerCustomDiameter,
2323
SpinnerWithColor,
2424
ProgressSpinnerWithStringValues,
25+
SpinnerWithMode,
2526
],
2627
}).compileComponents();
2728
}));
@@ -296,6 +297,14 @@ describe('MatProgressSpinner', () => {
296297
expect(progressElement.componentInstance.strokeWidth).toBe(7);
297298
});
298299

300+
it('should be able to change the mode on a mat-spinner', () => {
301+
const fixture = TestBed.createComponent(SpinnerWithMode);
302+
fixture.detectChanges();
303+
304+
const progressElement = fixture.debugElement.query(By.css('mat-spinner')).nativeElement;
305+
expect(progressElement.getAttribute('mode')).toBe('determinate');
306+
});
307+
299308
});
300309

301310

@@ -335,3 +344,7 @@ class ProgressSpinnerWithColor { color: string = 'primary'; }
335344
`
336345
})
337346
class ProgressSpinnerWithStringValues { }
347+
348+
@Component({template: '<mat-spinner mode="determinate"></mat-spinner>'})
349+
class SpinnerWithMode { }
350+

src/lib/progress-spinner/progress-spinner.ts

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ const INDETERMINATE_ANIMATION_TEMPLATE = `
104104
*/
105105
@Component({
106106
moduleId: module.id,
107-
selector: 'mat-progress-spinner',
107+
selector: 'mat-progress-spinner, mat-spinner',
108108
exportAs: 'matProgressSpinner',
109109
host: {
110110
'role': 'progressbar',
@@ -186,6 +186,10 @@ export class MatProgressSpinner extends _MatProgressSpinnerMixinBase implements
186186
super(_elementRef);
187187
this._fallbackAnimation = platform.EDGE || platform.TRIDENT;
188188

189+
if (_elementRef.nativeElement.nodeName.toLowerCase() === 'mat-spinner') {
190+
this.mode = 'indeterminate';
191+
}
192+
189193
if (defaults) {
190194
if (defaults.diameter) {
191195
this.diameter = defaults.diameter;
@@ -265,39 +269,3 @@ export class MatProgressSpinner extends _MatProgressSpinnerMixinBase implements
265269
.replace(/DIAMETER/g, `${this.diameter}`);
266270
}
267271
}
268-
269-
270-
/**
271-
* `<mat-spinner>` component.
272-
*
273-
* This is a component definition to be used as a convenience reference to create an
274-
* indeterminate `<mat-progress-spinner>` instance.
275-
*/
276-
@Component({
277-
moduleId: module.id,
278-
selector: 'mat-spinner',
279-
host: {
280-
'role': 'progressbar',
281-
'mode': 'indeterminate',
282-
'class': 'mat-spinner mat-progress-spinner',
283-
'[class._mat-animation-noopable]': `_noopAnimations`,
284-
'[style.width.px]': 'diameter',
285-
'[style.height.px]': 'diameter',
286-
},
287-
inputs: ['color'],
288-
templateUrl: 'progress-spinner.html',
289-
styleUrls: ['progress-spinner.css'],
290-
changeDetection: ChangeDetectionStrategy.OnPush,
291-
encapsulation: ViewEncapsulation.None,
292-
})
293-
export class MatSpinner extends MatProgressSpinner {
294-
constructor(elementRef: ElementRef, platform: Platform,
295-
@Optional() @Inject(DOCUMENT) document: any,
296-
// @breaking-change 8.0.0 animationMode and defaults parameters to be made required.
297-
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string,
298-
@Inject(MAT_PROGRESS_SPINNER_DEFAULT_OPTIONS)
299-
defaults?: MatProgressSpinnerDefaultOptions) {
300-
super(elementRef, platform, document, animationMode, defaults);
301-
this.mode = 'indeterminate';
302-
}
303-
}

src/lib/progress-spinner/public-api.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@
99
export * from './progress-spinner-module';
1010
export * from './progress-spinner';
1111

12+
/**
13+
* @deprecated Import `MatProgressSpinner` instead. Note that the
14+
* `mat-spinner` selector isn't deprecated.
15+
* @breaking-change 8.0.0
16+
*/
17+
export {MatProgressSpinner as MatSpinner} from './progress-spinner';

0 commit comments

Comments
 (0)