Skip to content

fix(menu): Set menu-item icon color only when not set on mat-icon #8614

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 6 commits into from
Dec 1, 2017
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion src/lib/menu/_menu-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}
}

.mat-menu-item .mat-icon,
.mat-menu-item .mat-icon:not([color]),
.mat-menu-item-submenu-trigger::after {
color: mat-color($foreground, 'icon');
}
Expand Down
48 changes: 47 additions & 1 deletion src/lib/menu/menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
MenuPositionY,
MatMenuItem,
} from './index';
import { MatIconModule } from '../icon/index';
import {MENU_PANEL_TOP_PADDING} from './menu-trigger';
import {MatRipple} from '@angular/material/core';
import {
Expand All @@ -44,7 +45,7 @@ describe('MatMenu', () => {
beforeEach(async(() => {
dir = 'ltr';
TestBed.configureTestingModule({
imports: [MatMenuModule, NoopAnimationsModule],
imports: [MatIconModule, MatMenuModule, NoopAnimationsModule],
Copy link
Member

Choose a reason for hiding this comment

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

In general we try to avoid having dependencies between components like this. It shouldn't be an issue after the previous comment about removing the tests is addressed.

declarations: [
SimpleMenu,
PositionedMenu,
Expand All @@ -54,6 +55,7 @@ describe('MatMenu', () => {
NestedMenu,
NestedMenuCustomElevation,
NestedMenuRepeater,
ItemIconColorsMenu,
FakeIcon
],
providers: [
Expand Down Expand Up @@ -223,6 +225,32 @@ describe('MatMenu', () => {
expect(fixture.componentInstance.items.first.getLabel()).toBe('Item');
});

it('should have an icon with the default theme color if mat-icon has no color attribute set',
Copy link
Member

Choose a reason for hiding this comment

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

You can get rid of these tests since they're basically testing the CSS engine.

() => {
const fixture = TestBed.createComponent(ItemIconColorsMenu);
fixture.detectChanges();

fixture.componentInstance.trigger.openMenu();
fixture.detectChanges();

const matIconElement =
overlayContainerElement.querySelector('mat-icon:not([color])') as HTMLElement;
const computedIconStyle = window.getComputedStyle(matIconElement);
expect(computedIconStyle.color).toBe('rgba(0, 0, 0, 0.54)');
});

it('should have an icon with the provided color if mat-icon has the color attribute set', () => {
const fixture = TestBed.createComponent(ItemIconColorsMenu);
fixture.detectChanges();

fixture.componentInstance.trigger.openMenu();
fixture.detectChanges();

const matIconElement = overlayContainerElement.querySelector('mat-icon[color]') as HTMLElement;
const computedIconStyle = window.getComputedStyle(matIconElement);
expect(computedIconStyle.color).toBe('rgb(255, 64, 129)');
});

it('should filter out non-text nodes when figuring out the label', () => {
const fixture = TestBed.createComponent(SimpleMenu);
fixture.detectChanges();
Expand Down Expand Up @@ -1380,6 +1408,24 @@ class NestedMenuRepeater {
items = ['one', 'two', 'three'];
}

@Component({
template: `
<button [matMenuTriggerFor]="menu" #triggerEl>Toggle menu</button>
<mat-menu #menu="matMenu">
<button mat-menu-item> <mat-icon>notifications_off</mat-icon> Disable alerts </button>
<button mat-menu-item disabled>
<mat-icon color="accent">bookmark</mat-icon> Bookmark
</button>
</mat-menu>
`
})
class ItemIconColorsMenu {
@ViewChild(MatMenuTrigger) trigger: MatMenuTrigger;
@ViewChild('triggerEl') triggerEl: ElementRef;
@ViewChild(MatMenu) menu: MatMenu;
@ViewChildren(MatMenuItem) items: QueryList<MatMenuItem>;
}

@Component({
selector: 'fake-icon',
template: '<ng-content></ng-content>'
Expand Down