Skip to content

Commit 1bf2f21

Browse files
committed
feat(material-experimental/mdc-checkbox): switch to non-deprecated styles
Switches the MDC-based checkbox to the non-deprecated styles.
1 parent b17ed9d commit 1bf2f21

File tree

5 files changed

+55
-44
lines changed

5 files changed

+55
-44
lines changed

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

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
@use '@material/checkbox/checkbox' as mdc-checkbox;
22
@use '@material/checkbox/checkbox-theme' as mdc-checkbox-theme;
3-
@use '@material/ripple/ripple-theme' as mdc-ripple-theme;
43
@use '@material/form-field' as mdc-form-field;
54
@use '@material/theme/theme-color' as mdc-theme-color;
65
@use '@material/theme/theme';
76
@use 'sass:map';
7+
@use 'sass:color';
88
@use '../mdc-helpers/mdc-helpers';
99
@use '../../material/core/typography/typography';
1010
@use '../../material/core/theming/theming';
@@ -13,22 +13,34 @@
1313

1414
// Mixin that includes the checkbox theme styles with a given palette.
1515
// By default, the MDC checkbox always uses the `secondary` palette.
16-
@mixin private-checkbox-styles-with-color($color) {
17-
@include mdc-checkbox-theme.theme-deprecated(
18-
(
19-
density-scale: null,
20-
checkmark-color: mdc-theme-color.prop-value(on-#{$color}),
21-
container-checked-color: $color,
22-
container-checked-hover-color: null,
23-
container-disabled-color: rgba(mdc-theme-color.prop-value(on-surface), 0.38),
24-
outline-color: rgba(mdc-theme-color.prop-value(on-surface), 0.54),
25-
outline-hover-color: null,
26-
ripple-color: mdc-theme-color.prop-value(on-surface),
27-
ripple-opacity: mdc-ripple-theme.$dark-ink-opacities,
28-
ripple-checked-color: $color,
29-
ripple-checked-opacity: mdc-ripple-theme.$dark-ink-opacities,
30-
)
31-
);
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+
));
3244
}
3345

3446
// Apply ripple colors to the MatRipple element and the MDC ripple element when the
@@ -55,23 +67,8 @@
5567
$accent: theming.get-color-from-palette(map.get($config, accent));
5668
$warn: theming.get-color-from-palette(map.get($config, warn));
5769

58-
// Save original values of MDC global variables. We need to save these so we can restore the
59-
// variables to their original values and prevent unintended side effects from using this mixin.
60-
$orig-border-color: mdc-checkbox-theme.$border-color;
61-
$orig-disabled-color: mdc-checkbox-theme.$disabled-color;
62-
6370
@include mdc-helpers.mat-using-mdc-theme($config) {
64-
mdc-checkbox-theme.$border-color: rgba(
65-
mdc-theme-color.prop-value(on-surface),
66-
0.54
67-
);
68-
mdc-checkbox-theme.$disabled-color: rgba(
69-
mdc-theme-color.prop-value(on-surface),
70-
0.26
71-
);
72-
7371
.mat-mdc-checkbox {
74-
@include private-checkbox-styles-with-color(primary);
7572
@include mdc-form-field.core-styles($query: mdc-helpers.$mat-theme-styles-query);
7673
@include ripple-theme.theme((
7774
foreground: (
@@ -87,25 +84,21 @@
8784
// class for accent and warn style, and applying the appropriate overrides below. Since we
8885
// don't use MDC's ripple, we also need to set the color for our replacement ripple.
8986
&.mat-primary {
90-
@include private-checkbox-styles-with-color(primary);
87+
@include private-checkbox-styles-with-color($primary, primary);
9188
@include _selected-ripple-colors($primary, primary);
9289
}
9390

9491
&.mat-accent {
95-
@include private-checkbox-styles-with-color(secondary);
92+
@include private-checkbox-styles-with-color($accent, secondary);
9693
@include _selected-ripple-colors($accent, secondary);
9794
}
9895

9996
&.mat-warn {
100-
@include private-checkbox-styles-with-color(error);
97+
@include private-checkbox-styles-with-color($warn, error);
10198
@include _selected-ripple-colors($warn, error);
10299
}
103100
}
104101
}
105-
106-
// Restore original values of MDC global variables.
107-
mdc-checkbox-theme.$border-color: $orig-border-color;
108-
mdc-checkbox-theme.$disabled-color: $orig-disabled-color;
109102
}
110103

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

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

Lines changed: 5 additions & 0 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;
@@ -19,6 +20,10 @@
1920
}
2021

2122
.mat-mdc-checkbox {
23+
// MDC theme styles also include structural styles so we have to include the theme at least
24+
// once here. The values will be overwritten by our own theme file afterwards.
25+
@include mdc-checkbox-theme.theme-styles(mdc-checkbox-theme.$light-theme);
26+
2227
// The host node defaults to `diplay: inline` so
2328
// we have to change it in order for margins to work.
2429
display: inline-block;

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)