Skip to content

Commit ae9f3d5

Browse files
crisbetowagnermaciel
authored andcommitted
fix(material-experimental/mdc-checkbox): switch to non-deprecated styles (#23218)
Switches the MDC-based checkbox to the non-deprecated styles. (cherry picked from commit a5fb8f8)
1 parent 34e4d20 commit ae9f3d5

File tree

5 files changed

+74
-52
lines changed

5 files changed

+74
-52
lines changed

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

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
@use '@material/theme/theme-color' as mdc-theme-color;
55
@use '@material/theme/theme';
66
@use 'sass:map';
7+
@use 'sass:color';
78
@use '../mdc-helpers/mdc-helpers';
89
@use '../../material/core/typography/typography';
910
@use '../../material/core/theming/theming';
@@ -12,15 +13,34 @@
1213

1314
// Mixin that includes the checkbox theme styles with a given palette.
1415
// By default, the MDC checkbox always uses the `secondary` palette.
15-
@mixin private-checkbox-styles-with-color($color) {
16-
@include mdc-checkbox-theme.theme-deprecated(
17-
(
18-
checkmark-color: mdc-theme-color.prop-value(on-#{$color}),
19-
container-checked-color: $color,
20-
container-disabled-color: rgba(mdc-theme-color.prop-value(on-surface), 0.38),
21-
outline-color: rgba(mdc-theme-color.prop-value(on-surface), 0.54),
22-
)
23-
);
16+
@mixin private-checkbox-styles-with-color($color, $mdcColor) {
17+
$on-surface: mdc-theme-color.prop-value(on-surface);
18+
$border-color: rgba($on-surface, color.opacity(mdc-checkbox-theme.$border-color));
19+
$disabled-color: rgba($on-surface, color.opacity(mdc-checkbox-theme.$disabled-color));
20+
21+
@include mdc-checkbox-theme.theme((
22+
selected-checkmark-color: mdc-theme-color.prop-value(on-#{$mdcColor}),
23+
24+
selected-focus-icon-color: $color,
25+
selected-hover-icon-color: $color,
26+
selected-hover-state-layer-color: $color,
27+
selected-icon-color: $color,
28+
selected-pressed-icon-color: $color,
29+
unselected-focus-icon-color: $color,
30+
unselected-hover-icon-color: $color,
31+
32+
selected-focus-state-layer-color: $on-surface,
33+
selected-pressed-state-layer-color: $on-surface,
34+
unselected-focus-state-layer-color: $on-surface,
35+
unselected-hover-state-layer-color: $on-surface,
36+
unselected-pressed-state-layer-color: $on-surface,
37+
38+
disabled-selected-icon-color: $disabled-color,
39+
disabled-unselected-icon-color: $disabled-color,
40+
41+
unselected-icon-color: $border-color,
42+
unselected-pressed-icon-color: $border-color,
43+
));
2444
}
2545

2646
// Apply ripple colors to the MatRipple element and the MDC ripple element when the
@@ -47,21 +67,7 @@
4767
$accent: theming.get-color-from-palette(map.get($config, accent));
4868
$warn: theming.get-color-from-palette(map.get($config, warn));
4969

50-
// Save original values of MDC global variables. We need to save these so we can restore the
51-
// variables to their original values and prevent unintended side effects from using this mixin.
52-
$orig-border-color: mdc-checkbox-theme.$border-color;
53-
$orig-disabled-color: mdc-checkbox-theme.$disabled-color;
54-
5570
@include mdc-helpers.mat-using-mdc-theme($config) {
56-
mdc-checkbox-theme.$border-color: rgba(
57-
mdc-theme-color.prop-value(on-surface),
58-
0.54
59-
);
60-
mdc-checkbox-theme.$disabled-color: rgba(
61-
mdc-theme-color.prop-value(on-surface),
62-
0.26
63-
);
64-
6571
.mat-mdc-checkbox {
6672
@include mdc-form-field.core-styles($query: mdc-helpers.$mat-theme-styles-query);
6773
@include ripple-theme.color((
@@ -78,25 +84,21 @@
7884
// class for accent and warn style, and applying the appropriate overrides below. Since we
7985
// don't use MDC's ripple, we also need to set the color for our replacement ripple.
8086
&.mat-primary {
81-
@include private-checkbox-styles-with-color(primary);
87+
@include private-checkbox-styles-with-color($primary, primary);
8288
@include _selected-ripple-colors($primary, primary);
8389
}
8490

8591
&.mat-accent {
86-
@include private-checkbox-styles-with-color(secondary);
92+
@include private-checkbox-styles-with-color($accent, secondary);
8793
@include _selected-ripple-colors($accent, secondary);
8894
}
8995

9096
&.mat-warn {
91-
@include private-checkbox-styles-with-color(error);
97+
@include private-checkbox-styles-with-color($warn, error);
9298
@include _selected-ripple-colors($warn, error);
9399
}
94100
}
95101
}
96-
97-
// Restore original values of MDC global variables.
98-
mdc-checkbox-theme.$border-color: $orig-border-color;
99-
mdc-checkbox-theme.$disabled-color: $orig-disabled-color;
100102
}
101103

102104
@mixin typography($config-or-theme) {

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

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@use '@material/checkbox' as mdc-checkbox;
2+
@use '@material/checkbox/checkbox-theme' as mdc-checkbox-theme;
23
@use '@material/form-field' as mdc-form-field;
34
@use '@material/ripple' as mdc-ripple;
45
@use '@material/touch-target' as mdc-touch-target;
@@ -23,25 +24,31 @@
2324
// we have to change it in order for margins to work.
2425
display: inline-block;
2526

26-
// The MDC checkbox styles related to the hover state are intertwined with the MDC ripple styles.
27-
// We currently don't use the MDC ripple due to size concerns, therefore we need to add some
28-
// additional styles to restore the hover state.
29-
.mdc-checkbox:hover .mdc-checkbox__native-control:not([disabled]) ~ .mdc-checkbox__ripple {
30-
opacity: map.get(mdc-ripple.$dark-ink-opacities, hover);
31-
transform: scale(1);
32-
transition: mdc-checkbox-transition-enter(opacity, 0, 80ms),
33-
mdc-checkbox-transition-enter(transform, 0, 80ms);
34-
}
27+
.mdc-checkbox {
28+
// MDC theme styles also include structural styles so we have to include the theme at least
29+
// once here. The values will be overwritten by our own theme file afterwards.
30+
@include mdc-checkbox-theme.theme-styles(mdc-checkbox-theme.$light-theme);
31+
32+
// The MDC checkbox styles related to the hover state are intertwined with the MDC ripple
33+
// styles. We currently don't use the MDC ripple due to size concerns, therefore we need to
34+
// add some additional styles to restore the hover state.
35+
&:hover .mdc-checkbox__native-control:not([disabled]) ~ .mdc-checkbox__ripple {
36+
opacity: map.get(mdc-ripple.$dark-ink-opacities, hover);
37+
transform: scale(1);
38+
transition: mdc-checkbox-transition-enter(opacity, 0, 80ms),
39+
mdc-checkbox-transition-enter(transform, 0, 80ms);
40+
}
3541

36-
// Note that the :not([disabled]) here isn't necessary, but we need it for the
37-
// extra specificity so that the hover styles don't override the focus styles.
38-
.mdc-checkbox .mdc-checkbox__native-control:not([disabled]):focus ~ .mdc-checkbox__ripple {
39-
opacity: map.get(mdc-ripple.$dark-ink-opacities, hover) +
40-
map.get(mdc-ripple.$dark-ink-opacities, focus);
42+
// Note that the :not([disabled]) here isn't necessary, but we need it for the
43+
// extra specificity so that the hover styles don't override the focus styles.
44+
.mdc-checkbox__native-control:not([disabled]):focus ~ .mdc-checkbox__ripple {
45+
opacity: map.get(mdc-ripple.$dark-ink-opacities, hover) +
46+
map.get(mdc-ripple.$dark-ink-opacities, focus);
4147

42-
@include a11y.high-contrast(active, off) {
43-
outline: solid 3px;
44-
opacity: 1;
48+
@include a11y.high-contrast(active, off) {
49+
outline: solid 3px;
50+
opacity: 1;
51+
}
4552
}
4653
}
4754

src/material-experimental/mdc-list/_list-option-theme.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
// Mixin that overrides the selected item and checkbox colors for list options. By
77
// default, the MDC list uses the `primary` color for list items. The MDC checkbox
88
// inside list options by default uses the `primary` color too.
9-
@mixin private-list-option-color-override($color) {
9+
@mixin private-list-option-color-override($color, $mdcColor) {
1010
& .mdc-list-item__start, & .mdc-list-item__end {
11-
@include checkbox-theme.private-checkbox-styles-with-color($color);
11+
@include checkbox-theme.private-checkbox-styles-with-color($color, $mdcColor);
1212
}
1313
}
1414

src/material-experimental/mdc-list/_list-theme.scss

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@use 'sass:map';
12
@use '@material/list/evolution-mixins' as mdc-list;
23
@use './interactive-list-theme';
34
@use './list-option-theme';
@@ -11,6 +12,9 @@
1112

1213
@mixin color($config-or-theme) {
1314
$config: theming.get-color-config($config-or-theme);
15+
$primary: theming.get-color-from-palette(map.get($config, primary));
16+
$accent: theming.get-color-from-palette(map.get($config, accent));
17+
$warn: theming.get-color-from-palette(map.get($config, warn));
1418

1519
// MDC's state styles are tied in with their ripple. Since we don't use the MDC
1620
// ripple, we need to add the hover, focus and selected states manually.
@@ -20,13 +24,13 @@
2024
@include mdc-list.without-ripple($query: mdc-helpers.$mat-theme-styles-query);
2125

2226
.mat-mdc-list-option {
23-
@include list-option-theme.private-list-option-color-override(primary);
27+
@include list-option-theme.private-list-option-color-override($primary, primary);
2428
}
2529
.mat-mdc-list-option.mat-accent {
26-
@include list-option-theme.private-list-option-color-override(secondary);
30+
@include list-option-theme.private-list-option-color-override($accent, secondary);
2731
}
2832
.mat-mdc-list-option.mat-warn {
29-
@include list-option-theme.private-list-option-color-override(error);
33+
@include list-option-theme.private-list-option-color-override($warn, error);
3034
}
3135
}
3236
}

src/material-experimental/mdc-list/list-option.scss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@use '@material/checkbox' as mdc-checkbox;
22
@use '@material/list/evolution-variables' as mdc-list-variables;
3+
@use '@material/checkbox/checkbox-theme' as mdc-checkbox-theme;
34
@use '../mdc-helpers/mdc-helpers';
45
@use '../../cdk/a11y';
56
@use './list-option-trailing-avatar-compat';
@@ -18,6 +19,14 @@
1819
@include mdc-checkbox.without-ripple($query: animation);
1920
}
2021

22+
// We can't use the MDC checkbox here directly, because this checkbox is purely
23+
// decorative and including the MDC one will bring in unnecessary JS.
24+
.mdc-checkbox {
25+
// MDC theme styles also include structural styles so we have to include the theme at least
26+
// once here. The values will be overwritten by our own theme file afterwards.
27+
@include mdc-checkbox-theme.theme-styles(mdc-checkbox-theme.$light-theme);
28+
}
29+
2130
// The internal checkbox is purely decorative, but because it's an `input`, the user can still
2231
// focus it by tabbing or clicking. Furthermore, `mat-list-option` has the `option` role which
2332
// doesn't allow a nested `input`. We use `display: none` both to remove it from the tab order

0 commit comments

Comments
 (0)