Skip to content

Commit d319e56

Browse files
authored
fix(material-experimental/mdc-button): strong focus indication always visible on fab buttons (#19169)
Our generic strong focus indication uses `::before` which is also used by MDC's FAB to draw a transparent border for high contrast mode. The result is that our indication is always visible and not positioned properly. These changes move the indicator into an inner element whose `::before` isn't styled by MDC.
1 parent d321d45 commit d319e56

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ export const MAT_BUTTON_HOST = {
3838
// Add a class that applies to all buttons. This makes it easier to target if somebody
3939
// wants to target all Material buttons.
4040
'[class.mat-mdc-button-base]': 'true',
41-
'class': 'mat-mdc-focus-indicator',
4241
};
4342

4443
/** Configuration for the ripple animation. */
@@ -154,7 +153,6 @@ export const MAT_ANCHOR_HOST = {
154153
// an unthemed version. If color is undefined, apply a CSS class that makes it easy to
155154
// select and style this "theme".
156155
'[class.mat-unthemed]': '!color',
157-
'class': 'mat-mdc-focus-indicator',
158156
// Add a class that applies to all buttons. This makes it easier to target if somebody
159157
// wants to target all Material buttons.
160158
'[class.mat-mdc-button-base]': 'true',

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

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

8+
<!--
9+
The indicator can't be directly on the button, because MDC uses ::before for high contrast
10+
indication and it can't be on the ripple, because it has a border radius and overflow: hidden.
11+
-->
12+
<div class="mat-mdc-focus-indicator"></div>
13+
814
<ng-content select=".material-icons[iconPositionEnd], mat-icon[iconPositionEnd]">
915
</ng-content>
1016

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ describe('MDC-based MatButton', () => {
264264
[...fixture.debugElement.nativeElement.querySelectorAll('a, button')];
265265

266266
expect(buttonNativeElements
267-
.every(element => element.classList.contains('mat-mdc-focus-indicator'))).toBe(true);
267+
.every(element => !!element.querySelector('.mat-mdc-focus-indicator'))).toBe(true);
268268
});
269269
});
270270

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,8 @@ $mat-typography-level-mappings: (
235235
// values are necessary to ensure that the focus indicator is sufficiently
236236
// contrastive and renders appropriately.
237237

238-
.mat-mdc-focus-indicator.mdc-button::before,
239-
.mat-mdc-focus-indicator.mdc-chip::before,
240-
.mat-mdc-focus-indicator.mdc-fab::before,
241-
.mat-mdc-focus-indicator.mdc-icon-button::before {
238+
.mat-mdc-button-base .mat-mdc-focus-indicator::before,
239+
.mat-mdc-focus-indicator.mdc-chip::before {
242240
margin: $mat-focus-indicator-border-width * -2;
243241
}
244242

@@ -260,6 +258,7 @@ $mat-typography-level-mappings: (
260258
.mdc-checkbox__native-control:focus ~ .mat-mdc-focus-indicator::before,
261259
.mat-mdc-slide-toggle-focused .mat-mdc-focus-indicator::before,
262260
.mat-mdc-radio-button.cdk-focused .mat-mdc-focus-indicator::before,
261+
.mat-mdc-button-base:focus .mat-mdc-focus-indicator::before,
263262

264263
// For all other components, render the focus indicator on focus.
265264
.mat-mdc-focus-indicator:focus::before {

0 commit comments

Comments
 (0)