Skip to content

Commit 44c8821

Browse files
authored
refactor(material/card): switch to new theming API (#25454)
Reworks the card component to use the new CSS-variable-based theming API from MDC.
1 parent 1fcdc27 commit 44c8821

File tree

3 files changed

+45
-18
lines changed

3 files changed

+45
-18
lines changed

src/material/card/_card-theme.scss

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
@use '../core/theming/theming';
22
@use '../core/mdc-helpers/mdc-helpers';
33
@use '../core/typography/typography';
4-
@use '@material/card' as mdc-card;
4+
@use '@material/card/elevated-card-theme' as mdc-elevated-card-theme;
5+
@use '@material/card/outlined-card-theme' as mdc-outlined-card-theme;
56
@use '@material/typography' as mdc-typography;
67
@use '@material/theme/theme-color' as mdc-theme-color;
78
@use 'sass:color';
@@ -10,36 +11,31 @@
1011
@mixin color($config-or-theme) {
1112
$config: theming.get-color-config($config-or-theme);
1213
$foreground: map.get($config, foreground);
13-
$is-dark-theme: map.get($config, is-dark);
14-
15-
$orig-mdc-card-action-icon-color: mdc-card.$action-icon-color;
16-
$orig-mdc-card-outline-color: mdc-card.$outline-color;
1714

1815
@include mdc-helpers.using-mdc-theme($config) {
19-
mdc-card.$action-icon-color: rgba(mdc-theme-color.prop-value(on-surface),
20-
mdc-theme-color.text-emphasis(medium));
21-
mdc-card.$outline-color: color.mix(mdc-theme-color.prop-value(on-surface),
22-
mdc-theme-color.prop-value(surface), 12%);
16+
.mat-mdc-card {
17+
@include mdc-elevated-card-theme.theme((
18+
container-color: mdc-theme-color.prop-value(surface),
19+
));
2320

24-
@include mdc-card.without-ripple($query: mdc-helpers.$mdc-theme-styles-query);
21+
@include mdc-outlined-card-theme.theme((
22+
outline-color: color.mix(mdc-theme-color.prop-value(on-surface),
23+
mdc-theme-color.prop-value(surface), 12%)
24+
));
25+
}
2526

2627
// Card subtitles are an Angular Material construct (not MDC), so we explicitly set their
2728
// color to secondary text here.
2829
.mat-mdc-card-subtitle {
2930
color: theming.get-color-from-palette($foreground, secondary-text);
3031
}
3132
}
32-
33-
mdc-card.$action-icon-color: $orig-mdc-card-action-icon-color;
34-
mdc-card.$outline-color: $orig-mdc-card-outline-color;
3533
}
3634

3735
@mixin typography($config-or-theme) {
3836
$config: typography.private-typography-to-2018-config(
3937
theming.get-typography-config($config-or-theme));
4038
@include mdc-helpers.using-mdc-typography($config) {
41-
@include mdc-card.without-ripple($query: mdc-helpers.$mdc-typography-styles-query);
42-
4339
// Card subtitles and titles are an Angular Material construct (not MDC), so we explicitly
4440
// set their typographic styles here.
4541
.mat-mdc-card-title {

src/material/card/card.scss

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
@use '../core/mdc-helpers/mdc-helpers';
21
@use '@material/card' as mdc-card;
2+
@use '@material/card/variables' as mdc-card-variables;
3+
@use '@material/card/elevated-card-theme' as mdc-elevated-card-theme;
4+
@use '@material/card/outlined-card-theme' as mdc-outlined-card-theme;
5+
@use '../core/mdc-helpers/mdc-helpers';
6+
@use '../core/style/elevation';
37

48
// TODO(jelbourn): move header and title-group styles to their own files.
59

@@ -11,7 +15,33 @@ $mat-card-default-padding: 16px !default;
1115

1216
@include mdc-helpers.disable-mdc-fallback-declarations {
1317
// Include all MDC card styles except for color and typography.
14-
@include mdc-card.without-ripple($query: mdc-helpers.$mdc-base-styles-query);
18+
@include mdc-card.static-styles($query: mdc-helpers.$mdc-base-styles-query);
19+
}
20+
21+
.mat-mdc-card {
22+
position: relative;
23+
24+
@include mdc-helpers.disable-mdc-fallback-declarations {
25+
// MDC's theme has `container-elevation` and `container-shadow-color` tokens, but we can't
26+
// use them because they output under a `.mdc-card` selector whereas the rest of the theme
27+
// isn't under any selector. Even if the mixin is pulled out of the selector, it throws a
28+
// different error.
29+
@include elevation.elevation(1);
30+
@include mdc-elevated-card-theme.theme-styles((
31+
container-color: transparent,
32+
container-shape: mdc-card-variables.$shape-radius,
33+
));
34+
}
35+
}
36+
37+
.mat-mdc-card-outlined {
38+
@include mdc-helpers.disable-mdc-fallback-declarations {
39+
@include elevation.elevation(0);
40+
@include mdc-outlined-card-theme.theme-styles((
41+
outline-color: transparent,
42+
outline-width: 1px,
43+
));
44+
}
1545
}
1646

1747
// Title text and subtitles text within a card. MDC doesn't have pre-made title sections for cards.

src/material/card/card.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ export const MAT_CARD_CONFIG = new InjectionToken<MatCardConfig>('MAT_CARD_CONFI
4040
styleUrls: ['card.css'],
4141
host: {
4242
'class': 'mat-mdc-card mdc-card',
43-
'[class.mdc-card--outlined]': 'appearance == "outlined"',
43+
'[class.mat-mdc-card-outlined]': 'appearance === "outlined"',
44+
'[class.mdc-card--outlined]': 'appearance === "outlined"',
4445
},
4546
exportAs: 'matCard',
4647
encapsulation: ViewEncapsulation.None,

0 commit comments

Comments
 (0)