Skip to content

Commit c3899cf

Browse files
crisbetommalerba
authored andcommitted
feat(progress-spinner): add injection token for configuring the diameter and stroke globally (#11493)
Adds the `MAT_PROGRESS_SPINNER_DEFAULT_OPTIONS` injection token that can be used to globally configure the diameter and stroke (and any other options we decide to add) for all progress spinners. Fixes #11490.
1 parent 69e4ca7 commit c3899cf

File tree

2 files changed

+79
-7
lines changed

2 files changed

+79
-7
lines changed

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import {TestBed, async} from '@angular/core/testing';
22
import {Component} from '@angular/core';
33
import {By} from '@angular/platform-browser';
4-
import {MatProgressSpinnerModule, MatProgressSpinner} from './index';
4+
import {
5+
MatProgressSpinnerModule,
6+
MatProgressSpinner,
7+
MAT_PROGRESS_SPINNER_DEFAULT_OPTIONS,
8+
} from './index';
59

610

711
describe('MatProgressSpinner', () => {
@@ -232,6 +236,47 @@ describe('MatProgressSpinner', () => {
232236
expect(spinner.nativeElement.style.width).toBe('32px');
233237
expect(spinner.nativeElement.style.height).toBe('32px');
234238
});
239+
240+
it('should be able to set a default diameter', () => {
241+
TestBed
242+
.resetTestingModule()
243+
.configureTestingModule({
244+
imports: [MatProgressSpinnerModule],
245+
declarations: [BasicProgressSpinner],
246+
providers: [{
247+
provide: MAT_PROGRESS_SPINNER_DEFAULT_OPTIONS,
248+
useValue: {diameter: 23}
249+
}]
250+
})
251+
.compileComponents();
252+
253+
const fixture = TestBed.createComponent(BasicProgressSpinner);
254+
fixture.detectChanges();
255+
256+
const progressElement = fixture.debugElement.query(By.css('mat-progress-spinner'));
257+
expect(progressElement.componentInstance.diameter).toBe(23);
258+
});
259+
260+
it('should be able to set a default stroke width', () => {
261+
TestBed
262+
.resetTestingModule()
263+
.configureTestingModule({
264+
imports: [MatProgressSpinnerModule],
265+
declarations: [BasicProgressSpinner],
266+
providers: [{
267+
provide: MAT_PROGRESS_SPINNER_DEFAULT_OPTIONS,
268+
useValue: {strokeWidth: 7}
269+
}]
270+
})
271+
.compileComponents();
272+
273+
const fixture = TestBed.createComponent(BasicProgressSpinner);
274+
fixture.detectChanges();
275+
276+
const progressElement = fixture.debugElement.query(By.css('mat-progress-spinner'));
277+
expect(progressElement.componentInstance.strokeWidth).toBe(7);
278+
});
279+
235280
});
236281

237282

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

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
ElementRef,
1515
ViewEncapsulation,
1616
Optional,
17+
InjectionToken,
1718
} from '@angular/core';
1819
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
1920
import {CanColor, mixinColor} from '@angular/material/core';
@@ -43,6 +44,26 @@ export class MatProgressSpinnerBase {
4344
}
4445
export const _MatProgressSpinnerMixinBase = mixinColor(MatProgressSpinnerBase, 'primary');
4546

47+
/** Default `mat-progress-spinner` options that can be overridden. */
48+
export interface MatProgressSpinnerDefaultOptions {
49+
/** Diameter of the spinner. */
50+
diameter?: number;
51+
/** Width of the spinner's stroke. */
52+
strokeWidth?: number;
53+
}
54+
55+
/** Injection token to be used to override the default options for `mat-progress-spinner`. */
56+
export const MAT_PROGRESS_SPINNER_DEFAULT_OPTIONS =
57+
new InjectionToken<MatProgressSpinnerDefaultOptions>('mat-progress-spinner-default-options', {
58+
providedIn: 'root',
59+
factory: MAT_PROGRESS_SPINNER_DEFAULT_OPTIONS_FACTORY,
60+
});
61+
62+
/** @docs-private */
63+
export function MAT_PROGRESS_SPINNER_DEFAULT_OPTIONS_FACTORY(): MatProgressSpinnerDefaultOptions {
64+
return {diameter: BASE_SIZE};
65+
}
66+
4667
// .0001 percentage difference is necessary in order to avoid unwanted animation frames
4768
// for example because the animation duration is 4 seconds, .1% accounts to 4ms
4869
// which are enough to see the flicker described in
@@ -98,7 +119,7 @@ const INDETERMINATE_ANIMATION_TEMPLATE = `
98119
export class MatProgressSpinner extends _MatProgressSpinnerMixinBase implements CanColor {
99120

100121
private _value = 0;
101-
private _strokeWidth: number;
122+
private _strokeWidth = this._defaults ? this._defaults.strokeWidth : undefined;
102123
private _fallbackAnimation = false;
103124

104125
/** Tracks diameters of existing instances to de-dupe generated styles (default d = 100) */
@@ -120,7 +141,8 @@ export class MatProgressSpinner extends _MatProgressSpinnerMixinBase implements
120141
this._attachStyleNode();
121142
}
122143
}
123-
private _diameter = BASE_SIZE;
144+
private _diameter = this._defaults && this._defaults.diameter ?
145+
this._defaults.diameter : BASE_SIZE;
124146

125147
/** Stroke width of the progress spinner. */
126148
@Input()
@@ -131,7 +153,6 @@ export class MatProgressSpinner extends _MatProgressSpinnerMixinBase implements
131153
this._strokeWidth = coerceNumberProperty(value);
132154
}
133155

134-
135156
/** Mode of the progress circle */
136157
@Input() mode: ProgressSpinnerMode = 'determinate';
137158

@@ -147,7 +168,10 @@ export class MatProgressSpinner extends _MatProgressSpinnerMixinBase implements
147168
constructor(public _elementRef: ElementRef,
148169
platform: Platform,
149170
@Optional() @Inject(DOCUMENT) private _document: any,
150-
@Optional() @Inject(ANIMATION_MODULE_TYPE) public _animationMode?: string) {
171+
// @deletion-target 7.0.0 _animationMode and _defaults parameters to be made required.
172+
@Optional() @Inject(ANIMATION_MODULE_TYPE) public _animationMode?: string,
173+
@Inject(MAT_PROGRESS_SPINNER_DEFAULT_OPTIONS)
174+
private _defaults?: MatProgressSpinnerDefaultOptions) {
151175

152176
super(_elementRef);
153177
this._fallbackAnimation = platform.EDGE || platform.TRIDENT;
@@ -249,8 +273,11 @@ export class MatProgressSpinner extends _MatProgressSpinnerMixinBase implements
249273
export class MatSpinner extends MatProgressSpinner {
250274
constructor(elementRef: ElementRef, platform: Platform,
251275
@Optional() @Inject(DOCUMENT) document: any,
252-
@Optional() @Inject(ANIMATION_MODULE_TYPE) _animationMode?: string) {
253-
super(elementRef, platform, document, _animationMode);
276+
// @deletion-targets 7.0.0 animationMode and defaults parameters to be made required.
277+
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string,
278+
@Inject(MAT_PROGRESS_SPINNER_DEFAULT_OPTIONS)
279+
defaults?: MatProgressSpinnerDefaultOptions) {
280+
super(elementRef, platform, document, animationMode, defaults);
254281
this.mode = 'indeterminate';
255282
}
256283
}

0 commit comments

Comments
 (0)