Skip to content

Commit 1937c3e

Browse files
zarendcrisbeto
andcommitted
fix(material-experimental/mdc-radio): switch to new theming API (#23725)
Reworks the MDC-based radio button to use the new theming API based on CSS variables. Also fixes a minor issue where the high contrast styles were emitting slightly more CSS than they need to. Co-authored-by: Kristiyan Kostadinov <crisbeto@abv.bg> (cherry picked from commit a259f22)
1 parent 9df5f4b commit 1937c3e

File tree

2 files changed

+56
-31
lines changed

2 files changed

+56
-31
lines changed

src/material-experimental/mdc-radio/_radio-theme.scss

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@use 'sass:map';
12
@use '@material/theme/theme-color' as mdc-theme-color;
23
@use '@material/radio/radio-theme' as mdc-radio-theme;
34
@use '@material/radio/radio' as mdc-radio;
@@ -6,42 +7,59 @@
67
@use '../../material/core/typography/typography';
78
@use '../../material/core/theming/theming';
89

10+
@mixin _color-palette($color-palette) {
11+
@include mdc-radio-theme.theme((
12+
selected-focus-icon-color: $color-palette,
13+
selected-focus-state-layer-color: $color-palette,
14+
selected-hover-icon-color: $color-palette,
15+
selected-hover-state-layer-color: $color-palette,
16+
selected-icon-color: $color-palette,
17+
selected-pressed-icon-color: $color-palette,
18+
selected-pressed-state-layer-color: $color-palette,
19+
));
20+
21+
// TODO(crisbeto): this should be included by MDC's `theme-styles`, but it isn't currently.
22+
@include mdc-radio-theme.focus-indicator-color($color-palette);
23+
}
24+
925
@mixin color($config-or-theme) {
1026
$config: theming.get-color-config($config-or-theme);
11-
// Save original values of MDC global variables. We need to save these so we can restore the
12-
// variables to their original values and prevent unintended side effects from using this mixin.
13-
$orig-baseline-theme-color: mdc-radio-theme.$baseline-theme-color;
14-
$orig-unchecked-color: mdc-radio-theme.$unchecked-color;
15-
$orig-disabled-circle-color: mdc-radio-theme.$disabled-circle-color;
27+
$primary: theming.get-color-from-palette(map.get($config, primary));
28+
$accent: theming.get-color-from-palette(map.get($config, accent));
29+
$warn: theming.get-color-from-palette(map.get($config, warn));
1630

1731
@include mdc-helpers.mat-using-mdc-theme($config) {
18-
mdc-radio-theme.$baseline-theme-color: primary;
19-
mdc-radio-theme.$unchecked-color: rgba(mdc-theme-color.prop-value(on-surface), 0.54);
20-
mdc-radio-theme.$disabled-circle-color: rgba(mdc-theme-color.prop-value(on-surface), 0.38);
32+
$on-surface: rgba(mdc-theme-color.$on-surface, 0.54);
2133

2234
.mat-mdc-radio-button {
2335
@include mdc-form-field.core-styles($query: mdc-helpers.$mat-theme-styles-query);
36+
@include mdc-radio-theme.theme((
37+
// The disabled colors don't use the `rgba` version, because
38+
// MDC applies a separate opacity to disabled buttons.
39+
disabled-selected-icon-color: mdc-theme-color.$on-surface,
40+
disabled-unselected-icon-color: mdc-theme-color.$on-surface,
41+
unselected-focus-icon-color: $on-surface,
42+
unselected-focus-state-layer-color: $on-surface,
43+
unselected-hover-icon-color: $on-surface,
44+
unselected-hover-state-layer-color: $on-surface,
45+
unselected-icon-color: $on-surface,
46+
unselected-pressed-icon-color: $on-surface,
47+
unselected-pressed-state-layer-color: $on-surface,
48+
));
2449

2550
&.mat-primary {
26-
@include mdc-radio.without-ripple($query: mdc-helpers.$mat-theme-styles-query);
51+
@include _color-palette($primary);
2752
}
2853

2954
&.mat-accent {
30-
mdc-radio-theme.$baseline-theme-color: secondary;
31-
@include mdc-radio.without-ripple($query: mdc-helpers.$mat-theme-styles-query);
55+
@include _color-palette($accent);
3256
}
3357

3458
&.mat-warn {
35-
mdc-radio-theme.$baseline-theme-color: error;
36-
@include mdc-radio.without-ripple($query: mdc-helpers.$mat-theme-styles-query);
59+
@include _color-palette($warn);
3760
}
3861
}
3962
}
40-
41-
// Restore original values of MDC global variables.
42-
mdc-radio-theme.$baseline-theme-color: $orig-baseline-theme-color;
43-
mdc-radio-theme.$unchecked-color: $orig-unchecked-color;
44-
mdc-radio-theme.$disabled-circle-color: $orig-disabled-circle-color;
4563
}
4664

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

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,28 @@
99
@include mdc-radio.without-ripple($query: mdc-helpers.$mat-base-styles-without-animation-query);
1010
@include mdc-form-field.core-styles($query: mdc-helpers.$mat-base-styles-query);
1111

12-
// This is necessary because we do not depend on MDC's ripple, but have our own that should be
13-
// positioned correctly. This can be removed once we start using MDC's ripple implementation.
14-
.mat-mdc-radio-button .mat-radio-ripple {
15-
@include layout-common.fill;
16-
17-
pointer-events: none;
18-
border-radius: 50%;
12+
.mat-mdc-radio-button {
13+
&:not(._mat-animation-noopable) {
14+
@include mdc-radio.without-ripple($query: animation);
15+
}
1916

20-
.mat-ripple-element:not(.mat-radio-persistent-ripple) {
21-
opacity: mdc-radio-theme.$ripple-opacity;
17+
.mdc-radio {
18+
// MDC theme styles also include structural styles so we have to include the theme at least
19+
// once here. The values will be overwritten by our own theme file afterwards.
20+
@include mdc-radio-theme.theme-styles(mdc-radio-theme.$light-theme);
2221
}
23-
}
2422

25-
.mat-mdc-radio-button:not(._mat-animation-noopable) {
26-
@include mdc-radio.without-ripple($query: animation);
23+
// This is necessary because we do not depend on MDC's ripple, but have our own that should be
24+
// positioned correctly. This can be removed once we start using MDC's ripple implementation.
25+
.mat-radio-ripple {
26+
@include layout-common.fill;
27+
pointer-events: none;
28+
border-radius: 50%;
29+
30+
.mat-ripple-element:not(.mat-radio-persistent-ripple) {
31+
opacity: mdc-radio-theme.$ripple-opacity;
32+
}
33+
}
2734
}
2835

2936
// Element used to provide a larger tap target for users on touch devices.
@@ -42,7 +49,7 @@
4249
// Note that this creates a square box around the circle, however it's consistent with
4350
// how IE/Edge treat native radio buttons in high contrast mode. We can't turn the border
4451
// into a dotted one, because it's too thick which causes the circles to look off.
45-
@include a11y.high-contrast {
52+
@include a11y.high-contrast(active, 'off') {
4653
.mat-mdc-radio-button:not(.mat-radio-disabled) {
4754
&.cdk-keyboard-focused,
4855
&.cdk-program-focused {

0 commit comments

Comments
 (0)