Skip to content

Commit 88878b3

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 03a9a39 commit 88878b3

File tree

5 files changed

+29
-42
lines changed

5 files changed

+29
-42
lines changed

src/material/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/material/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
}));
@@ -321,6 +322,14 @@ describe('MatProgressSpinner', () => {
321322
expect(progressElement.nativeElement.hasAttribute('aria-valuenow')).toBe(false);
322323
});
323324

325+
it('should be able to change the mode on a mat-spinner', () => {
326+
const fixture = TestBed.createComponent(SpinnerWithMode);
327+
fixture.detectChanges();
328+
329+
const progressElement = fixture.debugElement.query(By.css('mat-spinner')).nativeElement;
330+
expect(progressElement.getAttribute('mode')).toBe('determinate');
331+
});
332+
324333
});
325334

326335

@@ -360,3 +369,7 @@ class ProgressSpinnerWithColor { color: string = 'primary'; }
360369
`
361370
})
362371
class ProgressSpinnerWithStringValues { }
372+
373+
@Component({template: '<mat-spinner mode="determinate"></mat-spinner>'})
374+
class SpinnerWithMode { }
375+

src/material/progress-spinner/progress-spinner.ts

Lines changed: 5 additions & 36 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
this._noopAnimations = animationMode === 'NoopAnimations' &&
187187
(!!defaults && !defaults._forceAnimations);
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,38 +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<HTMLElement>, platform: Platform,
295-
@Optional() @Inject(DOCUMENT) document: any,
296-
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode: string,
297-
@Inject(MAT_PROGRESS_SPINNER_DEFAULT_OPTIONS)
298-
defaults?: MatProgressSpinnerDefaultOptions) {
299-
super(elementRef, platform, document, animationMode, defaults);
300-
this.mode = 'indeterminate';
301-
}
302-
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import {MatProgressSpinner} from './progress-spinner';
10+
911
export * from './progress-spinner-module';
1012
export * from './progress-spinner';
1113

14+
/**
15+
* @deprecated Import `MatProgressSpinner` instead. Note that the
16+
* `mat-spinner` selector isn't deprecated.
17+
* @breaking-change 8.0.0
18+
*/
19+
// tslint:disable-next-line:variable-name
20+
export const MatSpinner = MatProgressSpinner;

tools/public_api_guard/material/progress-spinner.d.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ export interface MatProgressSpinnerDefaultOptions {
2323
strokeWidth?: number;
2424
}
2525

26-
export declare class MatSpinner extends MatProgressSpinner {
27-
constructor(elementRef: ElementRef<HTMLElement>, platform: Platform, document: any, animationMode: string, defaults?: MatProgressSpinnerDefaultOptions);
28-
}
26+
export declare const MatSpinner: typeof MatProgressSpinner;
2927

3028
export declare type ProgressSpinnerMode = 'determinate' | 'indeterminate';

0 commit comments

Comments
 (0)