Skip to content

feat(icon): add input for inline styling of icons #9984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/demo-app/icon/icon-demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@
Custom icon font and CSS:
<mat-icon fontSet="fontawesome" fontIcon="fa-birthday-cake"></mat-icon>
</p>

<p>
Inline styling <mat-icon inline="true">favorite</mat-icon> icons appear as
the same <mat-icon inline="true">directions_car</mat-icon>size as text around them.</p>
</div>
4 changes: 4 additions & 0 deletions src/demo-app/tabs/tabs-demo.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
}
}

.mat-tab-label.mat-tab-label.mat-tab-label {
min-width: 0;
}

.demo-tab-group {
flex-grow: 1;
}
Expand Down
7 changes: 7 additions & 0 deletions src/lib/icon/icon.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ $mat-icon-size: 24px !default;
fill: currentColor;
height: $mat-icon-size;
width: $mat-icon-size;

&.mat-icon-inline {
font-size: inherit;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does IE11 support inherit? Any reason for not using 1em?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: inherit is supported since IE8.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I was thinking of initial

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use 1em instead of inherit if you would like, I don't have a super strong preference at this point.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inherit should be fine, I mixed up the IE support in my head

height: inherit;
line-height: inherit;
width: inherit;
}
}

.mat-form-field:not(.mat-form-field-appearance-legacy) {
Expand Down
24 changes: 21 additions & 3 deletions src/lib/icon/icon.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ describe('MatIcon', () => {
IconFromSvgName,
IconWithAriaHiddenFalse,
IconWithBindingAndNgIf,
InlineIcon,
]
});

Expand Down Expand Up @@ -82,14 +83,26 @@ describe('MatIcon', () => {
const fixture = TestBed.createComponent(IconWithLigature);
const iconElement = fixture.debugElement.nativeElement.querySelector('mat-icon');
expect(iconElement.getAttribute('aria-hidden'))
.toBe('true', 'Expected the mat-icon element has aria-hidden="true" by default');
.toBe('true', 'Expected the mat-icon element has aria-hidden="true" by default');
});

it('should not override a user-provided aria-hidden attribute', () => {
const fixture = TestBed.createComponent(IconWithAriaHiddenFalse);
const iconElement = fixture.debugElement.nativeElement.querySelector('mat-icon');
expect(iconElement.getAttribute('aria-hidden'))
.toBe('false', 'Expected the mat-icon element has the user-provided aria-hidden value');
.toBe('false', 'Expected the mat-icon element has the user-provided aria-hidden value');
});

it('should apply inline styling', () => {
const fixture = TestBed.createComponent(InlineIcon);
const iconElement = fixture.debugElement.nativeElement.querySelector('mat-icon');
expect(iconElement.classList.contains('mat-icon-inline'))
.toBeFalsy('Expected the mat-icon element to not include the inline styling class');

fixture.debugElement.componentInstance.inline = true;
fixture.detectChanges();
expect(iconElement.classList.contains('mat-icon-inline'))
.toBeTruthy('Expected the mat-icon element to include the inline styling class');
});

describe('Ligature icons', () => {
Expand Down Expand Up @@ -500,10 +513,15 @@ class IconFromSvgName {
}

@Component({template: '<mat-icon aria-hidden="false">face</mat-icon>'})
class IconWithAriaHiddenFalse { }
class IconWithAriaHiddenFalse {}

@Component({template: `<mat-icon [svgIcon]="iconName" *ngIf="showIcon">{{iconName}}</mat-icon>`})
class IconWithBindingAndNgIf {
iconName = 'fluffy';
showIcon = true;
}

@Component({template: `<mat-icon [inline]="inline">{{iconName}}</mat-icon>`})
class InlineIcon {
inline = false;
}
15 changes: 15 additions & 0 deletions src/lib/icon/icon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
ViewEncapsulation,
} from '@angular/core';
import {CanColor, mixinColor} from '@angular/material/core';
import {coerceBooleanProperty} from '@angular/cdk/coercion';
import {MatIconRegistry} from './icon-registry';


Expand Down Expand Up @@ -67,12 +68,26 @@ export const _MatIconMixinBase = mixinColor(MatIconBase);
host: {
'role': 'img',
'class': 'mat-icon',
'[class.mat-icon-inline]': 'inline',
},
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MatIcon extends _MatIconMixinBase implements OnChanges, OnInit, CanColor {

/**
* Whether the icon should be inlined, automatically sizing the icon to match the font size of
* the element the icon is contained in.
*/
@Input()
get inline(): boolean {
return this._inline;
}
set inline(inline: boolean) {
this._inline = coerceBooleanProperty(inline);
}
private _inline: boolean = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to have one unit test that uses this.


/** Name of the icon in the SVG icon set. */
@Input() svgIcon: string;

Expand Down