Skip to content

Commit d73e822

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 ed77ec9 commit d73e822

File tree

5 files changed

+35
-48
lines changed

5 files changed

+35
-48
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
export class MatProgressSpinnerModule {}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ describe('MatProgressSpinner', () => {
2727
ProgressSpinnerWithStringValues,
2828
IndeterminateSpinnerInShadowDom,
2929
IndeterminateSpinnerInShadowDomWithNgIf,
30+
SpinnerWithMode,
3031
],
3132
}).compileComponents();
3233
}));
@@ -428,6 +429,14 @@ describe('MatProgressSpinner', () => {
428429
expect(shadowRoot.querySelector('style[mat-spinner-animation="27"]')).toBeTruthy();
429430
});
430431

432+
it('should be able to change the mode on a mat-spinner', () => {
433+
const fixture = TestBed.createComponent(SpinnerWithMode);
434+
fixture.detectChanges();
435+
436+
const progressElement = fixture.debugElement.query(By.css('mat-spinner')).nativeElement;
437+
expect(progressElement.getAttribute('mode')).toBe('determinate');
438+
});
439+
431440
});
432441

433442

@@ -494,3 +503,7 @@ class IndeterminateSpinnerInShadowDomWithNgIf {
494503
diameter: number;
495504
}
496505

506+
507+
@Component({template: '<mat-spinner mode="determinate"></mat-spinner>'})
508+
class SpinnerWithMode { }
509+

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

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,12 @@ const INDETERMINATE_ANIMATION_TEMPLATE = `
104104
* `<mat-progress-spinner>` component.
105105
*/
106106
@Component({
107-
selector: 'mat-progress-spinner',
107+
selector: 'mat-progress-spinner, mat-spinner',
108108
exportAs: 'matProgressSpinner',
109109
host: {
110110
'role': 'progressbar',
111-
'class': 'mat-progress-spinner',
111+
// `mat-spinner` is here for backward compatibility.
112+
'class': 'mat-progress-spinner mat-spinner',
112113
'[class._mat-animation-noopable]': `_noopAnimations`,
113114
'[style.width.px]': 'diameter',
114115
'[style.height.px]': 'diameter',
@@ -201,6 +202,10 @@ export class MatProgressSpinner extends _MatProgressSpinnerMixinBase implements
201202
this._noopAnimations = animationMode === 'NoopAnimations' &&
202203
(!!defaults && !defaults._forceAnimations);
203204

205+
if (_elementRef.nativeElement.nodeName.toLowerCase() === 'mat-spinner') {
206+
this.mode = 'indeterminate';
207+
}
208+
204209
if (defaults) {
205210
if (defaults.diameter) {
206211
this.diameter = defaults.diameter;
@@ -300,41 +305,6 @@ export class MatProgressSpinner extends _MatProgressSpinnerMixinBase implements
300305
static ngAcceptInputType_value: NumberInput;
301306
}
302307

303-
304-
/**
305-
* `<mat-spinner>` component.
306-
*
307-
* This is a component definition to be used as a convenience reference to create an
308-
* indeterminate `<mat-progress-spinner>` instance.
309-
*/
310-
@Component({
311-
selector: 'mat-spinner',
312-
host: {
313-
'role': 'progressbar',
314-
'mode': 'indeterminate',
315-
'class': 'mat-spinner mat-progress-spinner',
316-
'[class._mat-animation-noopable]': `_noopAnimations`,
317-
'[style.width.px]': 'diameter',
318-
'[style.height.px]': 'diameter',
319-
},
320-
inputs: ['color'],
321-
templateUrl: 'progress-spinner.html',
322-
styleUrls: ['progress-spinner.css'],
323-
changeDetection: ChangeDetectionStrategy.OnPush,
324-
encapsulation: ViewEncapsulation.None,
325-
})
326-
export class MatSpinner extends MatProgressSpinner {
327-
constructor(elementRef: ElementRef<HTMLElement>, platform: Platform,
328-
@Optional() @Inject(DOCUMENT) document: any,
329-
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode: string,
330-
@Inject(MAT_PROGRESS_SPINNER_DEFAULT_OPTIONS)
331-
defaults?: MatProgressSpinnerDefaultOptions) {
332-
super(elementRef, platform, document, animationMode, defaults);
333-
this.mode = 'indeterminate';
334-
}
335-
}
336-
337-
338308
/** Gets the shadow root of an element, if supported and the element is inside the Shadow DOM. */
339309
export function _getShadowRoot(element: HTMLElement, _document: Document): Node | null {
340310
// TODO(crisbeto): see whether we should move this into the CDK

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,22 @@
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 {
1113
MatProgressSpinner,
12-
MatSpinner,
1314
MAT_PROGRESS_SPINNER_DEFAULT_OPTIONS,
1415
ProgressSpinnerMode,
1516
MatProgressSpinnerDefaultOptions,
1617
MAT_PROGRESS_SPINNER_DEFAULT_OPTIONS_FACTORY,
1718
} from './progress-spinner';
19+
20+
21+
/**
22+
* @deprecated Import `MatProgressSpinner` instead. Note that the
23+
* `mat-spinner` selector isn't deprecated.
24+
* @breaking-change 8.0.0
25+
*/
26+
// tslint:disable-next-line:variable-name
27+
export const MatSpinner = MatProgressSpinner;

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export declare class MatProgressSpinner extends _MatProgressSpinnerMixinBase imp
2222
static ngAcceptInputType_diameter: NumberInput;
2323
static ngAcceptInputType_strokeWidth: NumberInput;
2424
static ngAcceptInputType_value: NumberInput;
25-
static ɵcmp: i0.ɵɵComponentDefWithMeta<MatProgressSpinner, "mat-progress-spinner", ["matProgressSpinner"], { "color": "color"; "diameter": "diameter"; "strokeWidth": "strokeWidth"; "mode": "mode"; "value": "value"; }, {}, never>;
25+
static ɵcmp: i0.ɵɵComponentDefWithMeta<MatProgressSpinner, "mat-progress-spinner, mat-spinner", ["matProgressSpinner"], { "color": "color"; "diameter": "diameter"; "strokeWidth": "strokeWidth"; "mode": "mode"; "value": "value"; }, {}, never>;
2626
static ɵfac: i0.ɵɵFactoryDef<MatProgressSpinner>;
2727
}
2828

@@ -34,13 +34,9 @@ export interface MatProgressSpinnerDefaultOptions {
3434

3535
export declare class MatProgressSpinnerModule {
3636
static ɵinj: i0.ɵɵInjectorDef<MatProgressSpinnerModule>;
37-
static ɵmod: i0.ɵɵNgModuleDefWithMeta<MatProgressSpinnerModule, [typeof i1.MatProgressSpinner, typeof i1.MatSpinner], [typeof i2.MatCommonModule, typeof i3.CommonModule], [typeof i1.MatProgressSpinner, typeof i1.MatSpinner, typeof i2.MatCommonModule]>;
37+
static ɵmod: i0.ɵɵNgModuleDefWithMeta<MatProgressSpinnerModule, [typeof i1.MatProgressSpinner], [typeof i2.MatCommonModule, typeof i3.CommonModule], [typeof i1.MatProgressSpinner, typeof i2.MatCommonModule]>;
3838
}
3939

40-
export declare class MatSpinner extends MatProgressSpinner {
41-
constructor(elementRef: ElementRef<HTMLElement>, platform: Platform, document: any, animationMode: string, defaults?: MatProgressSpinnerDefaultOptions);
42-
static ɵcmp: i0.ɵɵComponentDefWithMeta<MatSpinner, "mat-spinner", never, { "color": "color"; }, {}, never>;
43-
static ɵfac: i0.ɵɵFactoryDef<MatSpinner>;
44-
}
40+
export declare const MatSpinner: typeof MatProgressSpinner;
4541

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

0 commit comments

Comments
 (0)