Skip to content

Commit

Permalink
feat(icon): allow multiple classes in setDefaultFontSetClass
Browse files Browse the repository at this point in the history
Allows for the consumer to pass in multiple classes to `MatIconRegistry.setDefaultFontSetClass`.

Fixes #10471.
  • Loading branch information
crisbeto authored and andrewseguin committed Jun 7, 2018
1 parent 7652c87 commit 931127d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
20 changes: 9 additions & 11 deletions src/lib/icon/icon-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ export class MatIconRegistry {
private _fontCssClassesByAlias = new Map<string, string>();

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

constructor(
@Optional() private _httpClient: HttpClient,
Expand Down Expand Up @@ -238,21 +238,19 @@ export class MatIconRegistry {
}

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

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

Expand Down
12 changes: 12 additions & 0 deletions src/lib/icon/icon.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ describe('MatIcon', () => {
fixture.detectChanges();
expect(sortedClassNames(matIconElement)).toEqual(['mat-icon', 'myfont']);
});

it('should be able to provide multiple alternate icon set classes', () => {
iconRegistry.setDefaultFontSetClass('myfont', 'myfont-48x48');

let fixture = TestBed.createComponent(IconWithLigature);

const testComponent = fixture.componentInstance;
const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon');
testComponent.iconName = 'home';
fixture.detectChanges();
expect(sortedClassNames(matIconElement)).toEqual(['mat-icon', 'myfont', 'myfont-48x48']);
});
});

describe('Icons from URLs', () => {
Expand Down
18 changes: 6 additions & 12 deletions src/lib/icon/icon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class MatIcon extends _MatIconMixinBase implements OnChanges, OnInit, Can
}
private _fontIcon: string;

private _previousFontSetClass: string;
private _previousFontSetClass: string[] = [];
private _previousFontIconClass: string;

constructor(
Expand Down Expand Up @@ -202,19 +202,13 @@ export class MatIcon extends _MatIconMixinBase implements OnChanges, OnInit, Can
}

const elem: HTMLElement = this._elementRef.nativeElement;
const fontSetClass = this.fontSet ?
this._iconRegistry.classNameForFontAlias(this.fontSet) :
const fontSetClasses = this.fontSet ?
[this._iconRegistry.classNameForFontAlias(this.fontSet)] :
this._iconRegistry.getDefaultFontSetClass();

if (fontSetClass != this._previousFontSetClass) {
if (this._previousFontSetClass) {
elem.classList.remove(this._previousFontSetClass);
}
if (fontSetClass) {
elem.classList.add(fontSetClass);
}
this._previousFontSetClass = fontSetClass;
}
this._previousFontSetClass.forEach(className => elem.classList.remove(className));
fontSetClasses.forEach(className => elem.classList.add(className));
this._previousFontSetClass = fontSetClasses;

if (this.fontIcon != this._previousFontIconClass) {
if (this._previousFontIconClass) {
Expand Down

0 comments on commit 931127d

Please sign in to comment.