Skip to content

Commit 7ad684c

Browse files
authored
feat(material-experimental/button): anchor disabled state (#16962)
* feat(material-experimental/button): anchor disabled state * add mixin for applying disabled state * add TODOs for review concerns
1 parent c105754 commit 7ad684c

File tree

7 files changed

+101
-11
lines changed

7 files changed

+101
-11
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ <h4 class="demo-section-header">Buttons</h4>
1212
<mat-icon>check</mat-icon>
1313
</button>
1414
</section>
15+
<section>
16+
<button mat-button disabled>normal</button>
17+
<button mat-raised-button disabled>raised</button>
18+
<button mat-stroked-button disabled>stroked</button>
19+
<button mat-flat-button disabled>flat</button>
20+
<button mat-fab disabled>
21+
<mat-icon>check</mat-icon>
22+
</button>
23+
<button mat-mini-fab disabled>
24+
<mat-icon>check</mat-icon>
25+
</button>
26+
</section>
1527

1628
<h4 class="demo-section-header">Anchors</h4>
1729
<section>
@@ -26,6 +38,18 @@ <h4 class="demo-section-header">Anchors</h4>
2638
<mat-icon>check</mat-icon>
2739
</a>
2840
</section>
41+
<section>
42+
<a href="//www.google.com" disabled mat-button color="primary">SEARCH</a>
43+
<a href="//www.google.com" disabled mat-raised-button>SEARCH</a>
44+
<a href="//www.google.com" disabled mat-stroked-button color="primary">SEARCH</a>
45+
<a href="//www.google.com" disabled mat-flat-button>SEARCH</a>
46+
<a href="//www.google.com" disabled mat-fab>
47+
<mat-icon>check</mat-icon>
48+
</a>
49+
<a href="//www.google.com" disabled mat-mini-fab>
50+
<mat-icon>check</mat-icon>
51+
</a>
52+
</section>
2953

3054
<h4 class="demo-section-header">Text Buttons [mat-button]</h4>
3155
<section>

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,15 @@
1313
@content;
1414
}
1515
}
16+
17+
// MDC's disabled buttons define a default cursor with pointer-events none. However, they select
18+
// :disabled for this, which does not affect anchor tags.
19+
// TODO(andrewseguin): Discuss with the MDC team about a mixin we can call for applying this style,
20+
// and note that having pointer-events may have unintended side-effects, e.g. allowing the user
21+
// to click the target underneath the button.
22+
@mixin _mat-button-disabled() {
23+
&[disabled] {
24+
cursor: default;
25+
pointer-events: none;
26+
}
27+
}

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,37 @@
11
@import '@material/button/mixins';
2+
@import '@material/button/variables';
23
@import '@material/fab/mixins';
34
@import '@material/ripple/mixins';
45
@import '@material/icon-button/mixins';
56
@import '../mdc-helpers/mdc-helpers';
67

8+
// Applies the disabled theme color to the text color.
9+
@mixin _mat-button-disabled-color() {
10+
@include mdc-theme-prop(color,
11+
mdc-theme-ink-color-for-fill_(disabled, $mdc-theme-background));
12+
}
13+
14+
// Wraps the content style in a selector for the disabled state.
15+
// MDC adds theme color by using :not(:disabled), so just using [disabled] once will not
16+
// override this, neither will it apply to anchor tags. This needs to override the
17+
// previously set theme color, so it must be ordered after the theme styles.
18+
// TODO(andrewseguin): Discuss with the MDC team to see if we can avoid the :not(:disabled) by
19+
// manually styling disabled buttons with a [disabled] selector.
20+
@mixin _mat-button-apply-disabled-style() {
21+
&[disabled][disabled] {
22+
@content;
23+
}
24+
}
25+
26+
// Applies the disabled theme background color for raised buttons. Value is taken from
27+
// mixin `mdc-button--filled`.
28+
// TODO(andrewseguin): Discuss with the MDC team about providing a variable for the 0.12 value
29+
// or otherwise have a mixin we can call to apply this style for both button and anchors.
30+
@mixin _mat-button-disabled-background() {
31+
@include mdc-theme-prop(background-color, rgba(mdc-theme-prop-value(on-surface), 0.12));
32+
}
33+
34+
735
@mixin mat-button-theme-mdc($theme) {
836
@include mat-using-mdc-theme($theme) {
937
// Add state interactions for hover, focus, press, active. Colors are changed based on
@@ -39,6 +67,10 @@
3967
@include mdc-button-ink-color(on-error, $query: $mat-theme-styles-query);
4068
@include mdc-states-base-color(on-error, $query: $mat-theme-styles-query);
4169
}
70+
71+
@include _mat-button-apply-disabled-style() {
72+
@include _mat-button-disabled-background();
73+
}
4274
}
4375

4476
.mat-mdc-outlined-button {
@@ -49,6 +81,23 @@
4981
&.mat-warn {
5082
@include mdc-button-outline-color(error, $query: $mat-theme-styles-query);
5183
}
84+
85+
@include _mat-button-apply-disabled-style() {
86+
@include mdc-theme-prop(border-color,
87+
mdc-theme-ink-color-for-fill_(disabled, $mdc-theme-background));
88+
}
89+
}
90+
91+
.mat-mdc-raised-button {
92+
@include _mat-button-apply-disabled-style() {
93+
@include mdc-elevation(0, $query: $mat-theme-styles-query);
94+
}
95+
}
96+
97+
.mat-mdc-button, .mat-mdc-raised-button, .mat-mdc-unelevated-button, .mat-mdc-outlined-button {
98+
@include _mat-button-apply-disabled-style() {
99+
@include _mat-button-disabled-color();
100+
}
52101
}
53102

54103
@include mdc-button-without-ripple($query: $mat-theme-styles-query);
@@ -78,6 +127,12 @@
78127
@include mdc-fab-container-color(error, $query: $mat-theme-styles-query);
79128
@include mdc-fab-ink-color(on-error, $query: $mat-theme-styles-query);
80129
}
130+
131+
@include _mat-button-apply-disabled-style() {
132+
@include _mat-button-disabled-color();
133+
@include _mat-button-disabled-background();
134+
@include mdc-elevation(0, $query: $mat-theme-styles-query);
135+
}
81136
}
82137

83138
@include mdc-fab-without-ripple($query: $mat-theme-styles-query);
@@ -106,6 +161,10 @@
106161
@include mdc-states-base-color(error, $query: $mat-theme-styles-query);
107162
@include mdc-icon-button-ink-color(error, $query: $mat-theme-styles-query);
108163
}
164+
165+
@include _mat-button-apply-disabled-style() {
166+
@include _mat-button-disabled-color();
167+
}
109168
}
110169

111170
@include mdc-icon-button-without-ripple($query: $mat-theme-styles-query);

src/material-experimental/mdc-button/button-base.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ export const MAT_ANCHOR_INPUTS = ['disabled', 'disableRipple', 'color', 'tabInde
122122

123123
/** Shared host configuration for buttons using the `<a>` tag. */
124124
export const MAT_ANCHOR_HOST = {
125-
...MAT_BUTTON_HOST,
125+
'[attr.disabled]': 'disabled || null',
126+
'[class._mat-animation-noopable]': '_animationMode === "NoopAnimations"',
127+
126128
// Note that we ignore the user-specified tabindex when it's disabled for
127129
// consistency with the `mat-button` applied on native buttons where even
128130
// though they have an index, they're not tabbable.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@
1616
}
1717

1818
@include _mat-button-interactive();
19+
@include _mat-button-disabled();
1920
}
20-

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,7 @@
88

99
.mat-mdc-fab, .mat-mdc-mini-fab {
1010
@include _mat-button-interactive();
11-
12-
// Spec does not define disabled FAB buttons but Angular Material does support it.
13-
&:disabled {
14-
background-color: rgba(mdc-theme-prop-value(on-surface), 0.12);
15-
color: $mdc-button-disabled-ink-color;
16-
cursor: default;
17-
pointer-events: none;
18-
box-shadow: none;
19-
}
11+
@include _mat-button-disabled();
2012
}
2113

2214
.mat-mdc-fab, .mat-mdc-mini-fab {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
// Border radius is inherited by ripple to know its shape. Set to 50% so the ripple is round.
1313
border-radius: 50%;
1414

15+
@include _mat-button-disabled();
1516
}

0 commit comments

Comments
 (0)