Skip to content

Commit fb1dda9

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 c28549d commit fb1dda9

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
@@ -101,11 +101,11 @@ export class MatIconRegistry {
101101
private _fontCssClassesByAlias = new Map<string, string>();
102102

103103
/**
104-
* The CSS class to apply when an `<mat-icon>` component has no icon name, url, or font specified.
105-
* The default 'material-icons' value assumes that the material icon font has been loaded as
106-
* described at http://google.github.io/material-design-icons/#icon-font-for-the-web
104+
* The CSS classes to apply when an `<mat-icon>` component has no icon name, url, or font
105+
* specified. The default 'material-icons' value assumes that the material icon font has been
106+
* loaded as described at http://google.github.io/material-design-icons/#icon-font-for-the-web
107107
*/
108-
private _defaultFontSetClass = 'material-icons';
108+
private _defaultFontSetClass = ['material-icons'];
109109

110110
constructor(
111111
@Optional() private _httpClient: HttpClient,
@@ -182,21 +182,19 @@ export class MatIconRegistry {
182182
}
183183

184184
/**
185-
* Sets the CSS class name to be used for icon fonts when an `<mat-icon>` component does not
185+
* Sets the CSS classes to be used for icon fonts when an `<mat-icon>` component does not
186186
* have a fontSet input value, and is not loading an icon by name or URL.
187-
*
188-
* @param className
189187
*/
190-
setDefaultFontSetClass(className: string): this {
191-
this._defaultFontSetClass = className;
188+
setDefaultFontSetClass(...classNames: string[]): this {
189+
this._defaultFontSetClass = classNames;
192190
return this;
193191
}
194192

195193
/**
196-
* Returns the CSS class name to be used for icon fonts when an `<mat-icon>` component does not
194+
* Returns the CSS classes to be used for icon fonts when an `<mat-icon>` component does not
197195
* have a fontSet input value, and is not loading an icon by name or URL.
198196
*/
199-
getDefaultFontSetClass(): string {
197+
getDefaultFontSetClass(): string[] {
200198
return this._defaultFontSetClass;
201199
}
202200

src/lib/icon/icon.spec.ts

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

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

src/lib/icon/icon.ts

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

110-
private _previousFontSetClass: string;
110+
private _previousFontSetClass: string[] = [];
111111
private _previousFontIconClass: string;
112112

113113
constructor(
@@ -202,19 +202,13 @@ export class MatIcon extends _MatIconMixinBase implements OnChanges, OnInit, Can
202202
}
203203

204204
const elem: HTMLElement = this._elementRef.nativeElement;
205-
const fontSetClass = this.fontSet ?
206-
this._iconRegistry.classNameForFontAlias(this.fontSet) :
205+
const fontSetClasses = this.fontSet ?
206+
[this._iconRegistry.classNameForFontAlias(this.fontSet)] :
207207
this._iconRegistry.getDefaultFontSetClass();
208208

209-
if (fontSetClass != this._previousFontSetClass) {
210-
if (this._previousFontSetClass) {
211-
elem.classList.remove(this._previousFontSetClass);
212-
}
213-
if (fontSetClass) {
214-
elem.classList.add(fontSetClass);
215-
}
216-
this._previousFontSetClass = fontSetClass;
217-
}
209+
this._previousFontSetClass.forEach(className => elem.classList.remove(className));
210+
fontSetClasses.forEach(className => elem.classList.add(className));
211+
this._previousFontSetClass = fontSetClasses;
218212

219213
if (this.fontIcon != this._previousFontIconClass) {
220214
if (this._previousFontIconClass) {

0 commit comments

Comments
 (0)