Skip to content

Commit 6c7cc8a

Browse files
authored
feat(material/icon): allow multiple classes in setDefaultFontSetClass (#10484)
Allows for the consumer to pass in multiple classes to `MatIconRegistry.setDefaultFontSetClass`. Fixes #10471.
1 parent 02e9ab0 commit 6c7cc8a

File tree

6 files changed

+41
-29
lines changed

6 files changed

+41
-29
lines changed

src/material/icon/icon-registry.ts

+9-11
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,11 @@ export class MatIconRegistry implements OnDestroy {
145145
private _resolvers: IconResolver[] = [];
146146

147147
/**
148-
* The CSS class to apply when an `<mat-icon>` component has no icon name, url, or font specified.
149-
* The default 'material-icons' value assumes that the material icon font has been loaded as
150-
* described at http://google.github.io/material-design-icons/#icon-font-for-the-web
148+
* The CSS classes to apply when an `<mat-icon>` component has no icon name, url, or font
149+
* specified. The default 'material-icons' value assumes that the material icon font has been
150+
* loaded as described at http://google.github.io/material-design-icons/#icon-font-for-the-web
151151
*/
152-
private _defaultFontSetClass = 'material-icons';
152+
private _defaultFontSetClass = ['material-icons'];
153153

154154
constructor(
155155
@Optional() private _httpClient: HttpClient,
@@ -302,21 +302,19 @@ export class MatIconRegistry implements OnDestroy {
302302
}
303303

304304
/**
305-
* Sets the CSS class name to be used for icon fonts when an `<mat-icon>` component does not
305+
* Sets the CSS classes to be used for icon fonts when an `<mat-icon>` component does not
306306
* have a fontSet input value, and is not loading an icon by name or URL.
307-
*
308-
* @param className
309307
*/
310-
setDefaultFontSetClass(className: string): this {
311-
this._defaultFontSetClass = className;
308+
setDefaultFontSetClass(...classNames: string[]): this {
309+
this._defaultFontSetClass = classNames;
312310
return this;
313311
}
314312

315313
/**
316-
* Returns the CSS class name to be used for icon fonts when an `<mat-icon>` component does not
314+
* Returns the CSS classes to be used for icon fonts when an `<mat-icon>` component does not
317315
* have a fontSet input value, and is not loading an icon by name or URL.
318316
*/
319-
getDefaultFontSetClass(): string {
317+
getDefaultFontSetClass(): string[] {
320318
return this._defaultFontSetClass;
321319
}
322320

src/material/icon/icon.spec.ts

+18
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,24 @@ describe('MatIcon', () => {
223223

224224
expect(matIconElement.textContent.trim()).toBe('house');
225225
});
226+
227+
it('should be able to provide multiple alternate icon set classes', () => {
228+
iconRegistry.setDefaultFontSetClass('myfont', 'myfont-48x48');
229+
230+
let fixture = TestBed.createComponent(IconWithLigature);
231+
232+
const testComponent = fixture.componentInstance;
233+
const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon');
234+
testComponent.iconName = 'home';
235+
fixture.detectChanges();
236+
expect(sortedClassNames(matIconElement)).toEqual([
237+
'mat-icon',
238+
'mat-icon-no-color',
239+
'myfont',
240+
'myfont-48x48',
241+
'notranslate',
242+
]);
243+
});
226244
});
227245

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

src/material/icon/icon.ts

+10-14
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ export class MatIcon extends _MatIconBase implements OnInit, AfterViewChecked, C
209209
}
210210
private _fontIcon: string;
211211

212-
private _previousFontSetClass: string;
212+
private _previousFontSetClass: string[] = [];
213213
private _previousFontIconClass: string;
214214

215215
_svgName: string | null;
@@ -366,21 +366,17 @@ export class MatIcon extends _MatIconBase implements OnInit, AfterViewChecked, C
366366
}
367367

368368
const elem: HTMLElement = this._elementRef.nativeElement;
369-
const fontSetClass = this.fontSet
370-
? this._iconRegistry.classNameForFontAlias(this.fontSet)
371-
: this._iconRegistry.getDefaultFontSetClass();
369+
const fontSetClasses = (
370+
this.fontSet
371+
? [this._iconRegistry.classNameForFontAlias(this.fontSet)]
372+
: this._iconRegistry.getDefaultFontSetClass()
373+
).filter(className => className.length > 0);
372374

373-
if (fontSetClass != this._previousFontSetClass) {
374-
if (this._previousFontSetClass) {
375-
elem.classList.remove(this._previousFontSetClass);
376-
}
377-
if (fontSetClass) {
378-
elem.classList.add(fontSetClass);
379-
}
380-
this._previousFontSetClass = fontSetClass;
381-
}
375+
this._previousFontSetClass.forEach(className => elem.classList.remove(className));
376+
fontSetClasses.forEach(className => elem.classList.add(className));
377+
this._previousFontSetClass = fontSetClasses;
382378

383-
if (this.fontIcon != this._previousFontIconClass) {
379+
if (this.fontIcon !== this._previousFontIconClass) {
384380
if (this._previousFontIconClass) {
385381
elem.classList.remove(this._previousFontIconClass);
386382
}

src/material/icon/testing/fake-icon-registry.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class FakeMatIconRegistry implements PublicApi<MatIconRegistry>, OnDestro
6161
}
6262

6363
getDefaultFontSetClass() {
64-
return 'material-icons';
64+
return ['material-icons'];
6565
}
6666

6767
getSvgIconFromUrl(): Observable<SVGElement> {

tools/public_api_guard/material/icon-testing.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class FakeMatIconRegistry implements PublicApi<MatIconRegistry>, OnDestro
3535
// (undocumented)
3636
classNameForFontAlias(alias: string): string;
3737
// (undocumented)
38-
getDefaultFontSetClass(): string;
38+
getDefaultFontSetClass(): string[];
3939
// (undocumented)
4040
getNamedSvgIcon(): Observable<SVGElement>;
4141
// (undocumented)

tools/public_api_guard/material/icon.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,13 @@ export class MatIconRegistry implements OnDestroy {
128128
addSvgIconSetLiteral(literal: SafeHtml, options?: IconOptions): this;
129129
addSvgIconSetLiteralInNamespace(namespace: string, literal: SafeHtml, options?: IconOptions): this;
130130
classNameForFontAlias(alias: string): string;
131-
getDefaultFontSetClass(): string;
131+
getDefaultFontSetClass(): string[];
132132
getNamedSvgIcon(name: string, namespace?: string): Observable<SVGElement>;
133133
getSvgIconFromUrl(safeUrl: SafeResourceUrl): Observable<SVGElement>;
134134
// (undocumented)
135135
ngOnDestroy(): void;
136136
registerFontClassAlias(alias: string, className?: string): this;
137-
setDefaultFontSetClass(className: string): this;
137+
setDefaultFontSetClass(...classNames: string[]): this;
138138
// (undocumented)
139139
static ɵfac: i0.ɵɵFactoryDeclaration<MatIconRegistry, [{ optional: true; }, null, { optional: true; }, null]>;
140140
// (undocumented)

0 commit comments

Comments
 (0)