Skip to content

Commit a2d0418

Browse files
authored
fix(material-experimental/mdc-slide-toggle): strong focus indication not visible (#19048)
In #18895 a `border-radius: 50%` was added to the ripple host element of the MDC-based slide toggle in order to support different densities. The problem is that the ripple element is also used to draw a rectangle in strong focus indication mode. These changes move the strong focus indication to the `.mdc-switch__thumb-underlay` and switch the base focus indication to use `::after` instead of `::before`.
1 parent 4431442 commit a2d0418

File tree

4 files changed

+15
-16
lines changed

4 files changed

+15
-16
lines changed

src/material-experimental/mdc-slide-toggle/_slide-toggle-theme.scss

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
// MDC's switch doesn't support a `color` property. We add support
3131
// for it by adding a CSS class for accent and warn style.
3232
.mat-mdc-slide-toggle {
33-
.mdc-switch__thumb-underlay::before, .mat-ripple-element {
33+
.mdc-switch__thumb-underlay::after, .mat-ripple-element {
3434
background: $mdc-switch-toggled-off-ripple-color;
3535
}
3636

@@ -51,18 +51,18 @@
5151

5252
// The ripple color matches the palette only when it's checked.
5353
.mat-mdc-slide-toggle-checked {
54-
.mdc-switch__thumb-underlay::before, .mat-ripple-element {
54+
.mdc-switch__thumb-underlay::after, .mat-ripple-element {
5555
background: $primary;
5656
}
5757

5858
&.mat-accent {
59-
.mdc-switch__thumb-underlay::before, .mat-ripple-element {
59+
.mdc-switch__thumb-underlay::after, .mat-ripple-element {
6060
background: $accent;
6161
}
6262
}
6363

6464
&.mat-warn {
65-
.mdc-switch__thumb-underlay::before, .mat-ripple-element {
65+
.mdc-switch__thumb-underlay::after, .mat-ripple-element {
6666
background: $warn;
6767
}
6868
}

src/material-experimental/mdc-slide-toggle/slide-toggle.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
[class.mdc-form-field--align-end]="labelPosition == 'before'">
33
<div class="mdc-switch" #switch>
44
<div class="mdc-switch__track"></div>
5-
<div class="mdc-switch__thumb-underlay">
6-
<div class="mat-mdc-slide-toggle-ripple mat-mdc-focus-indicator" mat-ripple
5+
<div class="mdc-switch__thumb-underlay mat-mdc-focus-indicator">
6+
<div class="mat-mdc-slide-toggle-ripple" mat-ripple
77
[matRippleTrigger]="switch"
88
[matRippleDisabled]="disableRipple || disabled"
99
[matRippleCentered]="true"

src/material-experimental/mdc-slide-toggle/slide-toggle.scss

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
display: inline-block;
1515

1616
// The ripple needs extra specificity so the base ripple styling doesn't override its `position`.
17-
.mat-mdc-slide-toggle-ripple, .mdc-switch__thumb-underlay::before {
17+
.mat-mdc-slide-toggle-ripple, .mdc-switch__thumb-underlay::after {
1818
@include mat-fill;
1919

2020
// Disable pointer events for the ripple container so that it doesn't eat the mouse events meant
@@ -26,19 +26,21 @@
2626
// The MDC switch styles related to the hover state are intertwined with the MDC ripple styles.
2727
// We currently don't use the MDC ripple due to size concerns, therefore we need to add some
2828
// additional styles to restore the hover state.
29-
.mdc-switch__thumb-underlay::before {
29+
// We use `::after`, rather than `::before`, because `::before` is used by the
30+
// generic strong focus indication styles.
31+
.mdc-switch__thumb-underlay::after {
3032
border-radius: 50%;
3133
content: '';
3234
opacity: 0;
3335
}
3436

35-
.mdc-switch:hover .mdc-switch__thumb-underlay::before {
37+
.mdc-switch:hover .mdc-switch__thumb-underlay::after {
3638
opacity: map-get($mdc-ripple-dark-ink-opacities, hover);
3739
transition: mdc-switch-transition-enter(opacity, 0, 75ms);
3840
}
3941

4042
// Needs a little more specificity so the :hover styles don't override it.
41-
&.mat-mdc-slide-toggle-focused .mdc-switch .mdc-switch__thumb-underlay::before {
43+
&.mat-mdc-slide-toggle-focused .mdc-switch .mdc-switch__thumb-underlay::after {
4244
opacity: map-get($mdc-ripple-dark-ink-opacities, focus);
4345
}
4446

@@ -57,7 +59,7 @@
5759
// conditionally add them. Consider the size cost when deciding whether to switch.
5860
&._mat-animation-noopable {
5961
.mdc-switch__thumb-underlay,
60-
.mdc-switch__thumb-underlay::before {
62+
.mdc-switch__thumb-underlay::after {
6163
transition: none;
6264
}
6365
}

src/material-experimental/mdc-slide-toggle/slide-toggle.spec.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,11 +299,8 @@ describe('MDC-based MatSlideToggle without forms', () => {
299299
});
300300

301301
it('should have a focus indicator', () => {
302-
const slideToggleRippleNativeElement =
303-
slideToggleElement.querySelector('.mat-mdc-slide-toggle-ripple')!;
304-
305-
expect(slideToggleRippleNativeElement.classList.contains('mat-mdc-focus-indicator'))
306-
.toBe(true);
302+
const underlayElement = slideToggleElement.querySelector('.mdc-switch__thumb-underlay')!;
303+
expect(underlayElement.classList.contains('mat-mdc-focus-indicator')).toBe(true);
307304
});
308305
});
309306

0 commit comments

Comments
 (0)