Skip to content

Commit 7006b48

Browse files
authored
fix(material-experimental/mdc-checkbox): remove background state styles (#21748)
1 parent c791d6a commit 7006b48

File tree

4 files changed

+57
-75
lines changed

4 files changed

+57
-75
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
<h1> Themed Checkboxes </h1>
2+
3+
<div>
4+
<mat-checkbox>Default</mat-checkbox>
5+
</div>
6+
7+
<div>
8+
<mat-checkbox color="primary">Primary</mat-checkbox>
9+
</div>
10+
11+
<div>
12+
<mat-checkbox color="accent">Accent</mat-checkbox>
13+
</div>
14+
15+
<div>
16+
<mat-checkbox color="warn">Warn</mat-checkbox>
17+
</div>
18+
19+
120
<h1>mat-checkbox: Basic Example</h1>
221
<form>
322
<mat-checkbox [(ngModel)]="isChecked"

src/material-experimental/mdc-checkbox/_checkbox-theme.scss

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
@use 'sass:map';
55
@use '../mdc-helpers/mdc-helpers';
66
@use '../../material/core/theming/theming';
7+
@use '../../material/core/ripple/ripple';
78

89
@import '@material/checkbox/mixins.import';
910
@import '@material/checkbox/variables.import';
@@ -29,23 +30,23 @@
2930
ripple-checked-opacity: ripple-theme.$dark-ink-opacities,
3031
)
3132
);
32-
@include _background-focus-indicator-color(mdc-theme-prop-value(on-surface));
33-
@include _background-focus-indicator-checked-color($color);
3433
}
3534

36-
// TODO(b/175233410): Use ripple element to show focus indicator.
37-
@mixin _background-focus-indicator-color($color) {
38-
.mdc-checkbox__background::before {
39-
@include theme.property(background-color, on-surface);
40-
}
41-
}
35+
// Apply ripple colors to the MatRipple element and the MDC ripple element when the
36+
// checkbox is selected.
37+
@mixin _selected-ripple-colors($theme, $mdcColor) {
38+
.mdc-checkbox--selected ~ {
39+
.mat-mdc-checkbox-ripple {
40+
@include ripple.theme((
41+
foreground: (
42+
base: mdc-theme-prop-value($mdcColor)
43+
),
44+
));
45+
}
4246

43-
// TODO(b/175233410): Use ripple element to show focus indicator.
44-
@mixin _background-focus-indicator-checked-color($color) {
45-
.mdc-checkbox__native-control:checked ~ .mdc-checkbox__background::before,
46-
.mdc-checkbox__native-control:indeterminate ~ .mdc-checkbox__background::before,
47-
.mdc-checkbox__native-control[data-indeterminate='true'] ~ .mdc-checkbox__background::before {
48-
@include theme.property(background-color, $color);
47+
.mdc-checkbox__ripple {
48+
background: $theme;
49+
}
4950
}
5051
}
5152

@@ -73,35 +74,32 @@
7374
.mat-mdc-checkbox {
7475
@include private-checkbox-styles-with-color(primary);
7576
@include mdc-form-field-core-styles($query: mdc-helpers.$mat-theme-styles-query);
76-
}
77+
@include ripple.theme((
78+
foreground: (
79+
base: mdc-theme-prop-value(on-surface)
80+
),
81+
));
7782

78-
// MDC's checkbox doesn't support a `color` property. We add support for it by adding a CSS
79-
// class for accent and warn style, and applying the appropriate overrides below. Since we don't
80-
// use MDC's ripple, we also need to set the color for our replacement ripple.
81-
.mat-mdc-checkbox {
82-
.mat-ripple-element,
83-
.mdc-checkbox__background::before {
83+
.mdc-checkbox__ripple {
8484
background: mdc-theme-prop-value(on-surface);
8585
}
8686

87-
.mdc-checkbox--selected ~ .mat-mdc-checkbox-ripple .mat-ripple-element {
88-
background: $primary;
87+
// MDC's checkbox doesn't support a `color` property. We add support for it by adding a CSS
88+
// class for accent and warn style, and applying the appropriate overrides below. Since we
89+
// don't use MDC's ripple, we also need to set the color for our replacement ripple.
90+
&.mat-primary {
91+
@include private-checkbox-styles-with-color(primary);
92+
@include _selected-ripple-colors($primary, primary);
8993
}
9094

9195
&.mat-accent {
9296
@include private-checkbox-styles-with-color(secondary);
93-
94-
.mdc-checkbox--selected ~ .mat-mdc-checkbox-ripple .mat-ripple-element {
95-
background: $accent;
96-
}
97+
@include _selected-ripple-colors($accent, secondary);
9798
}
9899

99100
&.mat-warn {
100101
@include private-checkbox-styles-with-color(error);
101-
102-
.mdc-checkbox--selected ~ .mat-mdc-checkbox-ripple .mat-ripple-element {
103-
background: $warn;
104-
}
102+
@include _selected-ripple-colors($warn, error);
105103
}
106104
}
107105
}
@@ -119,27 +117,13 @@
119117
}
120118
}
121119

122-
// TODO(b/175233410): Use ripple element to show focus indicator.
123-
@mixin _background-focus-density($density-scale) {
124-
$ripple-size: checkbox-theme.get-ripple-size($density-scale);
125-
$checkbox-padding: ($ripple-size - checkbox-theme.$icon-size) / 2;
126-
127-
.mdc-checkbox__background::before {
128-
top: -(checkbox-theme.$border-width) - $checkbox-padding;
129-
left: -(checkbox-theme.$border-width) - $checkbox-padding;
130-
width: $ripple-size;
131-
height: $ripple-size;
132-
}
133-
}
134-
135120
@mixin density($config-or-theme) {
136121
$density-scale: theming.get-density-config($config-or-theme);
137122
.mat-mdc-checkbox .mdc-checkbox {
138123
@include mdc-checkbox-density(
139124
$density-scale,
140125
$query: mdc-helpers.$mat-base-styles-query
141126
);
142-
@include _background-focus-density($density-scale);
143127
}
144128
}
145129

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
(blur)="_onBlur()"
1919
(click)="_onClick()"
2020
(change)="$event.stopPropagation()"/>
21+
<div class="mdc-checkbox__ripple"></div>
2122
<div class="mdc-checkbox__background">
2223
<svg class="mdc-checkbox__checkmark"
2324
focusable="false"

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

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,10 @@
1212
@include mdc-checkbox-without-ripple($query: mdc-helpers.$mat-base-styles-query);
1313
@include mdc-form-field-core-styles($query: mdc-helpers.$mat-base-styles-query);
1414

15-
// TODO(b/175233410): Use ripple element to show focus indicator.
16-
@mixin _background-focus-styles() {
17-
// The frame's ::before element is used as a focus indicator for the checkbox
18-
.mdc-checkbox__background::before {
19-
position: absolute;
20-
transform: scale(0, 0);
21-
border-radius: 50%;
15+
// Apply base styles to the MDC ripple to adjust appearance for state changes (hover, focus, press)
16+
@mixin _ripple-base-styles() {
17+
.mdc-checkbox__ripple {
2218
opacity: 0;
23-
pointer-events: none;
24-
content: '';
25-
will-change: opacity, transform;
26-
transition: checkbox.transition-exit(opacity),
27-
checkbox.transition-exit(transform);
28-
}
29-
30-
.mdc-checkbox__native-control:focus ~ .mdc-checkbox__background::before {
31-
transform: scale(1);
32-
opacity: checkbox-theme.$focus-indicator-opacity;
33-
transition: checkbox.transition-enter(opacity, 0ms, 80ms),
34-
checkbox.transition-enter(transform, 0ms, 80ms);
3519
}
3620
}
3721

@@ -44,27 +28,21 @@
4428
// We currently don't use the MDC ripple due to size concerns, therefore we need to add some
4529
// additional styles to restore the hover state.
4630
.mdc-checkbox:hover
47-
.mdc-checkbox__native-control:not([disabled]) ~ .mdc-checkbox__background::before {
31+
.mdc-checkbox__native-control:not([disabled]) ~ .mdc-checkbox__ripple {
4832
opacity: map.get($mdc-ripple-dark-ink-opacities, hover);
4933
transform: scale(1);
5034
transition: mdc-checkbox-transition-enter(opacity, 0, 80ms),
51-
mdc-checkbox-transition-enter(transform, 0, 80ms);
35+
mdc-checkbox-transition-enter(transform, 0, 80ms);
5236
}
5337

5438
// Note that the :not([disabled]) here isn't necessary, but we need it for the
5539
// extra specificity so that the hover styles don't override the focus styles.
5640
.mdc-checkbox
57-
.mdc-checkbox__native-control:not([disabled]):focus ~ .mdc-checkbox__background::before {
41+
.mdc-checkbox__native-control:not([disabled]):focus ~ .mdc-checkbox__ripple {
5842
opacity: map.get($mdc-ripple-dark-ink-opacities, hover) +
5943
map.get($mdc-ripple-dark-ink-opacities, focus);
6044
}
6145

62-
// We use an Angular Material ripple rather than an MDC ripple due to size concerns, so we need to
63-
// style it appropriately.
64-
.mat-ripple-element {
65-
opacity: map.get($mdc-ripple-dark-ink-opacities, press);
66-
}
67-
6846
// Angular Material supports disabling all animations when NoopAnimationsModule is imported.
6947
// TODO(mmalerba): Look into using MDC's Sass queries to separate the animation styles and
7048
// conditionally add them. Consider the size cost when deciding whether to switch.
@@ -82,10 +60,10 @@
8260
display: none;
8361
}
8462

85-
@include _background-focus-styles();
63+
@include _ripple-base-styles();
8664
}
8765

88-
.mat-mdc-checkbox-ripple {
66+
.mat-mdc-checkbox-ripple, .mdc-checkbox__ripple {
8967
@include layout-common.fill();
9068

9169
// Usually the ripple radius would be specified through the MatRipple input, but

0 commit comments

Comments
 (0)