Skip to content

Commit 5eb5cbd

Browse files
crisbetokara
authored andcommitted
refactor(progress-spinner): re-use keyframe stylesheet element (#7563)
1 parent 66e71c8 commit 5eb5cbd

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ export class MatProgressSpinner extends _MatProgressSpinnerMixinBase implements
9292
_elementSize = this._baseSize;
9393

9494
/** Tracks diameters of existing instances to de-dupe generated styles (default d = 100) */
95-
static diameters = new Set<number>([100]);
95+
private static diameters = new Set<number>([100]);
96+
97+
/** Used for storing all of the generated keyframe animations. */
98+
private static styleTag: HTMLStyleElement;
9699

97100
/** The diameter of the progress spinner (will set width and height of svg). */
98101
@Input()
@@ -130,9 +133,8 @@ export class MatProgressSpinner extends _MatProgressSpinnerMixinBase implements
130133

131134
// On IE and Edge, we can't animate the `stroke-dashoffset`
132135
// reliably so we fall back to a non-spec animation.
133-
const animationClass = this._fallbackAnimation ?
134-
'mat-progress-spinner-indeterminate-fallback-animation' :
135-
'mat-progress-spinner-indeterminate-animation';
136+
const animationClass =
137+
`mat-progress-spinner-indeterminate${this._fallbackAnimation ? '-fallback' : ''}-animation`;
136138

137139
_renderer.addClass(_elementRef.nativeElement, animationClass);
138140
}
@@ -183,9 +185,18 @@ export class MatProgressSpinner extends _MatProgressSpinnerMixinBase implements
183185

184186
/** Dynamically generates a style tag containing the correct animation for this diameter. */
185187
private _attachStyleNode(): void {
186-
const styleTag = this._renderer.createElement('style');
187-
styleTag.textContent = this._getAnimationText();
188-
this._renderer.appendChild(this._document.head, styleTag);
188+
let styleTag = MatProgressSpinner.styleTag;
189+
190+
if (!styleTag) {
191+
styleTag = this._renderer.createElement('style');
192+
this._renderer.appendChild(this._document.head, styleTag);
193+
MatProgressSpinner.styleTag = styleTag;
194+
}
195+
196+
if (styleTag.sheet) {
197+
(styleTag.sheet as CSSStyleSheet).insertRule(this._getAnimationText());
198+
}
199+
189200
MatProgressSpinner.diameters.add(this.diameter);
190201
}
191202

0 commit comments

Comments
 (0)