Skip to content

Commit cdb8286

Browse files
committed
feat(icon): allow multiple classes in setDefaultFontSetClass
Allows for the consumer to pass in multiple classes to `MatIconRegistry.setDefaultFontSetClass`. Fixes #10471.
1 parent 2adf629 commit cdb8286

File tree

3 files changed

+26
-23
lines changed

3 files changed

+26
-23
lines changed

src/material/icon/icon-registry.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ export class MatIconRegistry implements OnDestroy {
118118
private _fontCssClassesByAlias = new Map<string, string>();
119119

120120
/**
121-
* The CSS class to apply when an `<mat-icon>` component has no icon name, url, or font specified.
122-
* The default 'material-icons' value assumes that the material icon font has been loaded as
123-
* described at http://google.github.io/material-design-icons/#icon-font-for-the-web
121+
* The CSS classes to apply when an `<mat-icon>` component has no icon name, url, or font
122+
* specified. The default 'material-icons' value assumes that the material icon font has been
123+
* loaded as described at http://google.github.io/material-design-icons/#icon-font-for-the-web
124124
*/
125-
private _defaultFontSetClass = 'material-icons';
125+
private _defaultFontSetClass = ['material-icons'];
126126

127127
constructor(
128128
@Optional() private _httpClient: HttpClient,
@@ -239,21 +239,19 @@ export class MatIconRegistry implements OnDestroy {
239239
}
240240

241241
/**
242-
* Sets the CSS class name to be used for icon fonts when an `<mat-icon>` component does not
242+
* Sets the CSS classes to be used for icon fonts when an `<mat-icon>` component does not
243243
* have a fontSet input value, and is not loading an icon by name or URL.
244-
*
245-
* @param className
246244
*/
247-
setDefaultFontSetClass(className: string): this {
248-
this._defaultFontSetClass = className;
245+
setDefaultFontSetClass(...classNames: string[]): this {
246+
this._defaultFontSetClass = classNames;
249247
return this;
250248
}
251249

252250
/**
253-
* Returns the CSS class name to be used for icon fonts when an `<mat-icon>` component does not
251+
* Returns the CSS classes to be used for icon fonts when an `<mat-icon>` component does not
254252
* have a fontSet input value, and is not loading an icon by name or URL.
255253
*/
256-
getDefaultFontSetClass(): string {
254+
getDefaultFontSetClass(): string[] {
257255
return this._defaultFontSetClass;
258256
}
259257

src/material/icon/icon.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,17 @@ describe('MatIcon', () => {
175175
expect(matIconElement.textContent.trim()).toBe('house');
176176
});
177177

178+
it('should be able to provide multiple alternate icon set classes', () => {
179+
iconRegistry.setDefaultFontSetClass('myfont', 'myfont-48x48');
180+
181+
let fixture = TestBed.createComponent(IconWithLigature);
182+
183+
const testComponent = fixture.componentInstance;
184+
const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon');
185+
testComponent.iconName = 'home';
186+
fixture.detectChanges();
187+
expect(sortedClassNames(matIconElement)).toEqual(['mat-icon', 'myfont', 'myfont-48x48']);
188+
});
178189
});
179190

180191
describe('Icons from URLs', () => {

src/material/icon/icon.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export class MatIcon extends _MatIconMixinBase implements OnChanges, OnInit, Aft
168168
}
169169
private _fontIcon: string;
170170

171-
private _previousFontSetClass: string;
171+
private _previousFontSetClass: string[] = [];
172172
private _previousFontIconClass: string;
173173

174174
/** Keeps track of the current page path. */
@@ -330,19 +330,13 @@ export class MatIcon extends _MatIconMixinBase implements OnChanges, OnInit, Aft
330330
}
331331

332332
const elem: HTMLElement = this._elementRef.nativeElement;
333-
const fontSetClass = this.fontSet ?
334-
this._iconRegistry.classNameForFontAlias(this.fontSet) :
333+
const fontSetClasses = this.fontSet ?
334+
[this._iconRegistry.classNameForFontAlias(this.fontSet)] :
335335
this._iconRegistry.getDefaultFontSetClass();
336336

337-
if (fontSetClass != this._previousFontSetClass) {
338-
if (this._previousFontSetClass) {
339-
elem.classList.remove(this._previousFontSetClass);
340-
}
341-
if (fontSetClass) {
342-
elem.classList.add(fontSetClass);
343-
}
344-
this._previousFontSetClass = fontSetClass;
345-
}
337+
this._previousFontSetClass.forEach(className => elem.classList.remove(className));
338+
fontSetClasses.forEach(className => elem.classList.add(className));
339+
this._previousFontSetClass = fontSetClasses;
346340

347341
if (this.fontIcon != this._previousFontIconClass) {
348342
if (this._previousFontIconClass) {

0 commit comments

Comments
 (0)