Skip to content

Commit 74e945a

Browse files
crisbetommalerba
authored andcommittedDec 5, 2018
fix(menu): reduce specificity of icon selector (#14389)
In #14161 we had to tweak the selector for an icon inside a `mat-menu-item`, in order to fix an issue, however the new selector's specificity is a lot higher which can break people's overrides. These changes rework the approach to have a lower specificity selector.
1 parent 31f0e6d commit 74e945a

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed
 

‎src/lib/icon/icon.spec.ts

+29-9
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,19 @@ describe('MatIcon', () => {
8787
expect(sortedClassNames(matIconElement)).toEqual(['mat-icon', 'mat-primary', 'material-icons']);
8888
});
8989

90+
it('should apply a class if there is no color', () => {
91+
let fixture = TestBed.createComponent(IconWithColor);
92+
93+
const testComponent = fixture.componentInstance;
94+
const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon');
95+
testComponent.iconName = 'home';
96+
testComponent.iconColor = '';
97+
fixture.detectChanges();
98+
99+
expect(sortedClassNames(matIconElement))
100+
.toEqual(['mat-icon', 'mat-icon-no-color', 'material-icons']);
101+
});
102+
90103
it('should mark mat-icon as aria-hidden by default', () => {
91104
const fixture = TestBed.createComponent(IconWithLigature);
92105
const iconElement = fixture.debugElement.nativeElement.querySelector('mat-icon');
@@ -121,7 +134,8 @@ describe('MatIcon', () => {
121134
const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon');
122135
testComponent.iconName = 'home';
123136
fixture.detectChanges();
124-
expect(sortedClassNames(matIconElement)).toEqual(['mat-icon', 'material-icons']);
137+
expect(sortedClassNames(matIconElement))
138+
.toEqual(['mat-icon', 'mat-icon-no-color', 'material-icons']);
125139
});
126140

127141
it('should use alternate icon font if set', () => {
@@ -133,7 +147,7 @@ describe('MatIcon', () => {
133147
const matIconElement = fixture.debugElement.nativeElement.querySelector('mat-icon');
134148
testComponent.iconName = 'home';
135149
fixture.detectChanges();
136-
expect(sortedClassNames(matIconElement)).toEqual(['mat-icon', 'myfont']);
150+
expect(sortedClassNames(matIconElement)).toEqual(['mat-icon', 'mat-icon-no-color', 'myfont']);
137151
});
138152
});
139153

@@ -682,17 +696,20 @@ describe('MatIcon', () => {
682696
testComponent.fontSet = 'f1';
683697
testComponent.fontIcon = 'house';
684698
fixture.detectChanges();
685-
expect(sortedClassNames(matIconElement)).toEqual(['font1', 'house', 'mat-icon']);
699+
expect(sortedClassNames(matIconElement))
700+
.toEqual(['font1', 'house', 'mat-icon', 'mat-icon-no-color']);
686701

687702
testComponent.fontSet = 'f2';
688703
testComponent.fontIcon = 'igloo';
689704
fixture.detectChanges();
690-
expect(sortedClassNames(matIconElement)).toEqual(['f2', 'igloo', 'mat-icon']);
705+
expect(sortedClassNames(matIconElement))
706+
.toEqual(['f2', 'igloo', 'mat-icon', 'mat-icon-no-color']);
691707

692708
testComponent.fontSet = 'f3';
693709
testComponent.fontIcon = 'tent';
694710
fixture.detectChanges();
695-
expect(sortedClassNames(matIconElement)).toEqual(['f3', 'mat-icon', 'tent']);
711+
expect(sortedClassNames(matIconElement))
712+
.toEqual(['f3', 'mat-icon', 'mat-icon-no-color', 'tent']);
696713
});
697714

698715
it('should handle values with extraneous spaces being passed in to `fontSet`', () => {
@@ -704,14 +721,15 @@ describe('MatIcon', () => {
704721
fixture.detectChanges();
705722
}).not.toThrow();
706723

707-
expect(sortedClassNames(matIconElement)).toEqual(['font', 'mat-icon']);
724+
expect(sortedClassNames(matIconElement)).toEqual(['font', 'mat-icon', 'mat-icon-no-color']);
708725

709726
expect(() => {
710727
fixture.componentInstance.fontSet = ' changed';
711728
fixture.detectChanges();
712729
}).not.toThrow();
713730

714-
expect(sortedClassNames(matIconElement)).toEqual(['changed', 'mat-icon']);
731+
expect(sortedClassNames(matIconElement))
732+
.toEqual(['changed', 'mat-icon', 'mat-icon-no-color']);
715733
});
716734

717735
it('should handle values with extraneous spaces being passed in to `fontIcon`', () => {
@@ -723,14 +741,16 @@ describe('MatIcon', () => {
723741
fixture.detectChanges();
724742
}).not.toThrow();
725743

726-
expect(sortedClassNames(matIconElement)).toEqual(['font', 'mat-icon', 'material-icons']);
744+
expect(sortedClassNames(matIconElement))
745+
.toEqual(['font', 'mat-icon', 'mat-icon-no-color', 'material-icons']);
727746

728747
expect(() => {
729748
fixture.componentInstance.fontIcon = ' changed';
730749
fixture.detectChanges();
731750
}).not.toThrow();
732751

733-
expect(sortedClassNames(matIconElement)).toEqual(['changed', 'mat-icon', 'material-icons']);
752+
expect(sortedClassNames(matIconElement))
753+
.toEqual(['changed', 'mat-icon', 'mat-icon-no-color', 'material-icons']);
734754
});
735755

736756
});

‎src/lib/icon/icon.ts

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ const funcIriPattern = /^url\(['"]?#(.*?)['"]?\)$/;
129129
'role': 'img',
130130
'class': 'mat-icon',
131131
'[class.mat-icon-inline]': 'inline',
132+
'[class.mat-icon-no-color]': 'color !== "primary" && color !== "accent" && color !== "warn"',
132133
},
133134
encapsulation: ViewEncapsulation.None,
134135
changeDetection: ChangeDetectionStrategy.OnPush,

‎src/lib/menu/_menu-theme.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
}
2525
}
2626

27-
.mat-menu-item .mat-icon:not(.mat-primary):not(.mat-accent):not(.mat-warn),
27+
.mat-menu-item .mat-icon-no-color,
2828
.mat-menu-item-submenu-trigger::after {
2929
color: mat-color($foreground, 'icon');
3030
}

0 commit comments

Comments
 (0)
Please sign in to comment.