Skip to content

Commit d36d541

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 3255cf3 commit d36d541

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

src/lib/icon/icon-registry.ts

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

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

126126
constructor(
127127
@Optional() private _httpClient: HttpClient,
@@ -238,21 +238,19 @@ export class MatIconRegistry {
238238
}
239239

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

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

src/lib/icon/icon.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,18 @@ describe('MatIcon', () => {
128128
fixture.detectChanges();
129129
expect(sortedClassNames(matIconElement)).toEqual(['mat-icon', 'myfont']);
130130
});
131+
132+
it('should be able to provide multiple alternate icon set classes', () => {
133+
iconRegistry.setDefaultFontSetClass('myfont', 'myfont-48x48');
134+
135+
let fixture = TestBed.createComponent(IconWithLigature);
136+
137+
const testComponent = fixture.componentInstance;
138+
const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon');
139+
testComponent.iconName = 'home';
140+
fixture.detectChanges();
141+
expect(sortedClassNames(matIconElement)).toEqual(['mat-icon', 'myfont', 'myfont-48x48']);
142+
});
131143
});
132144

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

src/lib/icon/icon.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export class MatIcon extends _MatIconMixinBase implements OnChanges, OnInit, Can
106106
}
107107
private _fontIcon: string;
108108

109-
private _previousFontSetClass: string;
109+
private _previousFontSetClass: string[] = [];
110110
private _previousFontIconClass: string;
111111

112112
constructor(
@@ -217,19 +217,13 @@ export class MatIcon extends _MatIconMixinBase implements OnChanges, OnInit, Can
217217
}
218218

219219
const elem: HTMLElement = this._elementRef.nativeElement;
220-
const fontSetClass = this.fontSet ?
221-
this._iconRegistry.classNameForFontAlias(this.fontSet) :
220+
const fontSetClasses = this.fontSet ?
221+
[this._iconRegistry.classNameForFontAlias(this.fontSet)] :
222222
this._iconRegistry.getDefaultFontSetClass();
223223

224-
if (fontSetClass != this._previousFontSetClass) {
225-
if (this._previousFontSetClass) {
226-
elem.classList.remove(this._previousFontSetClass);
227-
}
228-
if (fontSetClass) {
229-
elem.classList.add(fontSetClass);
230-
}
231-
this._previousFontSetClass = fontSetClass;
232-
}
224+
this._previousFontSetClass.forEach(className => elem.classList.remove(className));
225+
fontSetClasses.forEach(className => elem.classList.add(className));
226+
this._previousFontSetClass = fontSetClasses;
233227

234228
if (this.fontIcon != this._previousFontIconClass) {
235229
if (this._previousFontIconClass) {

0 commit comments

Comments
 (0)