Skip to content

Commit c287f5d

Browse files
committed
fix(material-experimental/mdc-button): fix positioning of icons in text buttons (#20963)
* fix(material-experimental/mdc-button): fix positioning of icons in text buttons * fixup! fix(material-experimental/mdc-button): fix positioning of icons in text buttons (cherry picked from commit 933e1e3)
1 parent e001e70 commit c287f5d

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

src/dev-app/button/button-demo.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ <h4 class="demo-section-header">Text Buttons [mat-button]</h4>
2626
<button mat-button color="accent">accent</button>
2727
<button mat-button color="warn">warn</button>
2828
<button mat-button disabled>disabled</button>
29+
<button mat-button>
30+
<mat-icon>home</mat-icon>
31+
with icons
32+
<mat-icon>favorite</mat-icon>
33+
</button>
2934
</section>
3035

3136
<h4 class="demo-section-header">Raised Buttons [mat-raised-button]</h4>
@@ -35,6 +40,11 @@ <h4 class="demo-section-header">Raised Buttons [mat-raised-button]</h4>
3540
<button mat-raised-button color="accent">accent</button>
3641
<button mat-raised-button color="warn">warn</button>
3742
<button mat-raised-button disabled>disabled</button>
43+
<button mat-raised-button>
44+
<mat-icon>home</mat-icon>
45+
with icons
46+
<mat-icon>favorite</mat-icon>
47+
</button>
3848
</section>
3949

4050
<h4 class="demo-section-header">Stroked Buttons [mat-stroked-button]</h4>
@@ -44,6 +54,11 @@ <h4 class="demo-section-header">Stroked Buttons [mat-stroked-button]</h4>
4454
<button mat-stroked-button color="accent">accent</button>
4555
<button mat-stroked-button color="warn">warn</button>
4656
<button mat-stroked-button disabled>disabled</button>
57+
<button mat-stroked-button>
58+
<mat-icon>home</mat-icon>
59+
with icons
60+
<mat-icon>favorite</mat-icon>
61+
</button>
4762
</section>
4863

4964
<h4 class="demo-section-header">Flat Buttons [mat-flat-button]</h4>
@@ -53,6 +68,11 @@ <h4 class="demo-section-header">Flat Buttons [mat-flat-button]</h4>
5368
<button mat-flat-button color="accent">accent</button>
5469
<button mat-flat-button color="warn">warn</button>
5570
<button mat-flat-button disabled>disabled</button>
71+
<button mat-flat-button>
72+
<mat-icon>home</mat-icon>
73+
with icons
74+
<mat-icon>favorite</mat-icon>
75+
</button>
5676
</section>
5777

5878
<h4 class="demo-section-header"> Icon Buttons [mat-icon-button]</h4>

src/dev-app/mdc-button/mdc-button-demo.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ <h4 class="demo-section-header">Text Buttons [mat-button]</h4>
5858
<button mat-button color="accent">accent</button>
5959
<button mat-button color="warn">warn</button>
6060
<button mat-button disabled>disabled</button>
61+
<button mat-button>
62+
<mat-icon>home</mat-icon>
63+
with icons
64+
<mat-icon iconPositionEnd>favorite</mat-icon>
65+
</button>
6166
</section>
6267

6368
<h4 class="demo-section-header">Raised Buttons [mat-raised-button]</h4>
@@ -67,6 +72,11 @@ <h4 class="demo-section-header">Raised Buttons [mat-raised-button]</h4>
6772
<button mat-raised-button color="accent">accent</button>
6873
<button mat-raised-button color="warn">warn</button>
6974
<button mat-raised-button disabled>disabled</button>
75+
<button mat-raised-button>
76+
<mat-icon>home</mat-icon>
77+
with icons
78+
<mat-icon iconPositionEnd>favorite</mat-icon>
79+
</button>
7080
</section>
7181

7282
<h4 class="demo-section-header">Stroked Buttons [mat-stroked-button]</h4>
@@ -76,6 +86,11 @@ <h4 class="demo-section-header">Stroked Buttons [mat-stroked-button]</h4>
7686
<button mat-stroked-button color="accent">accent</button>
7787
<button mat-stroked-button color="warn">warn</button>
7888
<button mat-stroked-button disabled>disabled</button>
89+
<button mat-stroked-button>
90+
<mat-icon>home</mat-icon>
91+
with icons
92+
<mat-icon iconPositionEnd>favorite</mat-icon>
93+
</button>
7994
</section>
8095

8196
<h4 class="demo-section-header">Flat Buttons [mat-flat-button]</h4>
@@ -85,6 +100,11 @@ <h4 class="demo-section-header">Flat Buttons [mat-flat-button]</h4>
85100
<button mat-flat-button color="accent">accent</button>
86101
<button mat-flat-button color="warn">warn</button>
87102
<button mat-flat-button disabled>disabled</button>
103+
<button mat-flat-button>
104+
<mat-icon>home</mat-icon>
105+
with icons
106+
<mat-icon iconPositionEnd>favorite</mat-icon>
107+
</button>
88108
</section>
89109

90110
<h4 class="demo-section-header"> Icon Buttons [mat-icon-button]</h4>

src/material-experimental/mdc-button/button.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55

66
<span class="mdc-button__label"><ng-content></ng-content></span>
77

8+
<ng-content select=".material-icons[iconPositionEnd], mat-icon[iconPositionEnd]">
9+
</ng-content>
10+
811
<!--
912
The indicator can't be directly on the button, because MDC uses ::before for high contrast
1013
indication and it can't be on the ripple, because it has a border radius and overflow: hidden.
1114
-->
1215
<span class="mat-mdc-focus-indicator"></span>
1316

14-
<ng-content select=".material-icons[iconPositionEnd], mat-icon[iconPositionEnd]">
15-
</ng-content>
16-
1717
<span matRipple class="mat-mdc-button-ripple"
1818
[matRippleAnimation]="_rippleAnimation"
1919
[matRippleDisabled]="_isRippleDisabled()"

src/material-experimental/mdc-button/button.scss

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@
1010
.mat-mdc-button, .mat-mdc-unelevated-button, .mat-mdc-raised-button, .mat-mdc-outlined-button {
1111
@include _mat-button-interactive();
1212
@include _mat-button-disabled();
13+
14+
// MDC expects button icons to contain this HTML content:
15+
// ```html
16+
// <span class="mdc-button__icon material-icons">favorite</span>
17+
// ```
18+
// However, Angular Material expects a `mat-icon` instead. The following
19+
// will extend the `mdc-button__icon` styling to the mat icon. Note that
20+
// the extended styles inherently only match icons that nest themselves in
21+
// a parent `mdc-button`.
22+
//
23+
// TODO(mmalerba): Have MDC create a mixin for this so we don't have to rely on extending their
24+
// class.
25+
.mat-icon {
26+
@extend .mdc-button__icon;
27+
}
1328
}
1429

1530
// Add an outline to make buttons more visible in high contrast mode. Stroked buttons and FABs

0 commit comments

Comments
 (0)