Skip to content

Commit c2abcad

Browse files
authored
refactor: style disabled button with class instead of :disabled (#19406)
Uses a new class, `.mat-button-disabled`, for applying disabled button styles. This change request comes from GCP, which wants to avoid _actually_ disabling buttons so that they can have tooltips.
1 parent 3d03bf3 commit c2abcad

File tree

4 files changed

+20
-15
lines changed

4 files changed

+20
-15
lines changed

src/material/button/_button-base.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ $mat-mini-fab-padding: 8px !default;
5858
// on most browsers except on IE11 where it defaults to `hidden`.
5959
overflow: visible;
6060

61-
&[disabled] {
61+
&.mat-button-disabled {
6262
cursor: default;
6363
}
6464

src/material/button/_button-theme.scss

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ $_mat-button-ripple-opacity: 0.1;
2424
background-color: mat-color($warn);
2525
}
2626

27-
&[disabled] .mat-button-focus-overlay {
27+
&.mat-button-disabled .mat-button-focus-overlay {
2828
background-color: transparent;
2929
}
3030
}
@@ -76,8 +76,8 @@ $_mat-button-ripple-opacity: 0.1;
7676
#{$property}: mat-color($warn, $hue);
7777
}
7878

79-
&.mat-primary, &.mat-accent, &.mat-warn, &[disabled] {
80-
&[disabled] {
79+
&.mat-primary, &.mat-accent, &.mat-warn, &.mat-button-disabled {
80+
&.mat-button-disabled {
8181
$palette: if($property == 'color', $foreground, $background);
8282
#{$property}: mat-color($palette, disabled-button);
8383
}
@@ -118,7 +118,7 @@ $_mat-button-ripple-opacity: 0.1;
118118

119119
// Note: this needs a bit extra specificity, because we're not guaranteed the inclusion
120120
// order of the theme styles and the button reset may end up resetting this as well.
121-
.mat-stroked-button:not([disabled]) {
121+
.mat-stroked-button:not(.mat-button-disabled) {
122122
border-color: mat-color($foreground, divider);
123123
}
124124

@@ -139,23 +139,23 @@ $_mat-button-ripple-opacity: 0.1;
139139
.mat-raised-button {
140140
@include _mat-theme-overridable-elevation(2, $config);
141141

142-
&:not([disabled]):active {
142+
&:not(.mat-button-disabled):active {
143143
@include _mat-theme-overridable-elevation(8, $config);
144144
}
145145

146-
&[disabled] {
146+
&.mat-button-disabled {
147147
@include _mat-theme-overridable-elevation(0, $config);
148148
}
149149
}
150150

151151
.mat-fab, .mat-mini-fab {
152152
@include _mat-theme-overridable-elevation(6, $config);
153153

154-
&:not([disabled]):active {
154+
&:not(.mat-button-disabled):active {
155155
@include _mat-theme-overridable-elevation(12, $config);
156156
}
157157

158-
&[disabled] {
158+
&.mat-button-disabled {
159159
@include _mat-theme-overridable-elevation(0, $config);
160160
}
161161
}

src/material/button/button.scss

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
// Only basic and stroked buttons (not raised, FABs or icon buttons) have a hover style.
1414
// Use the same visual treatment for hover as for focus.
15-
.mat-button:hover,
16-
.mat-stroked-button:hover {
17-
.mat-button-focus-overlay {
15+
.mat-button,
16+
.mat-stroked-button {
17+
&:hover:not(.mat-button-disabled) .mat-button-focus-overlay {
1818
opacity: 0.04;
1919
}
2020
}
@@ -23,9 +23,9 @@
2323
// enhancement and not all desktop browsers support this kind of media query, we can't
2424
// use something like `@media (hover)`.
2525
@media (hover: none) {
26-
.mat-button:hover,
27-
.mat-stroked-button:hover {
28-
.mat-button-focus-overlay {
26+
.mat-button,
27+
.mat-stroked-button {
28+
&:hover:not(.mat-button-disabled) .mat-button-focus-overlay {
2929
opacity: 0;
3030
}
3131
}

src/material/button/button.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ const _MatButtonMixinBase: CanDisableRippleCtor & CanDisableCtor & CanColorCtor
7171
host: {
7272
'[attr.disabled]': 'disabled || null',
7373
'[class._mat-animation-noopable]': '_animationMode === "NoopAnimations"',
74+
// Add a class for disabled button styling instead of the using attribute
75+
// selector or pseudo-selector. This allows users to create focusabled
76+
// disabled buttons without recreating the styles.
77+
'[class.mat-button-disabled]': 'disabled',
7478
'class': 'mat-focus-indicator',
7579
},
7680
templateUrl: 'button.html',
@@ -160,6 +164,7 @@ export class MatButton extends _MatButtonMixinBase
160164
'[attr.aria-disabled]': 'disabled.toString()',
161165
'(click)': '_haltDisabledEvents($event)',
162166
'[class._mat-animation-noopable]': '_animationMode === "NoopAnimations"',
167+
'[class.mat-button-disabled]': 'disabled',
163168
'class': 'mat-focus-indicator',
164169
},
165170
inputs: ['disabled', 'disableRipple', 'color'],

0 commit comments

Comments
 (0)