Skip to content

Commit 1c7b940

Browse files
authored
refactor(material-experimental/mdc-snack-bar): switch to new theming API (#25273)
Reworks the MDC-based snack bar to use the new CSS-variable-based theming APIs.
1 parent df2eb8b commit 1c7b940

File tree

2 files changed

+93
-42
lines changed

2 files changed

+93
-42
lines changed

src/material-experimental/mdc-snack-bar/_snack-bar-theme.scss

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,31 @@
11
@use '@angular/material' as mat;
22
@use '@material/theme/theme-color' as mdc-theme-color;
3-
@use '@material/snackbar' as mdc-snackbar;
3+
@use '@material/snackbar/snackbar-theme' as mdc-snackbar-theme;
44
@use 'sass:color';
55
@use 'sass:map';
66

77

88
@mixin color($config-or-theme) {
99
$config: mat.get-color-config($config-or-theme);
10-
11-
$orig-fill-color: mdc-snackbar.$fill-color;
12-
$orig-label-ink-color: mdc-snackbar.$label-ink-color;
13-
$orig-dismiss-ink-color: mdc-snackbar.$dismiss-ink-color;
10+
$is-dark-theme: map.get($config, is-dark);
11+
$accent: map.get($config, accent);
1412

1513
@include mat.private-using-mdc-theme($config) {
16-
mdc-snackbar.$fill-color: color.mix(
17-
mdc-theme-color.prop-value(on-surface),
18-
mdc-theme-color.prop-value(surface),
19-
80%
20-
);
21-
mdc-snackbar.$label-ink-color: rgba(
22-
mdc-theme-color.prop-value(surface),
23-
mdc-theme-color.text-emphasis(high)
24-
);
25-
mdc-snackbar.$dismiss-ink-color: rgba(
26-
mdc-theme-color.prop-value(surface),
27-
mdc-theme-color.text-emphasis(high)
28-
);
29-
30-
@include mdc-snackbar.core-styles($query: mat.$private-mdc-theme-styles-query);
31-
}
14+
.mat-mdc-snack-bar-container {
15+
$button-color: if($is-dark-theme, currentColor, mat.get-color-from-palette($accent, text));
16+
--mat-mdc-snack-bar-button-color: #{$button-color};
3217

33-
mdc-snackbar.$fill-color: $orig-fill-color;
34-
mdc-snackbar.$label-ink-color: $orig-label-ink-color;
35-
mdc-snackbar.$dismiss-ink-color: $orig-dismiss-ink-color;
36-
37-
// Four classes is necessary to make it higher specificity than the mdc-button theme styles.
38-
// This ensures the correct styles will be applied regardless of the order te mixins are
39-
// @included.
40-
.mat-mdc-simple-snack-bar .mat-mdc-snack-bar-actions .mdc-snackbar__action.mdc-snackbar__action {
41-
$is-dark-theme: map.get($config, is-dark);
42-
$accent: map.get($config, accent);
43-
color: if($is-dark-theme, inherit, mat.get-color-from-palette($accent, text));
44-
45-
.mat-ripple-element {
46-
background-color: currentColor;
47-
opacity: 0.1;
18+
@include mdc-snackbar-theme.theme((
19+
container-color: color.mix(
20+
mdc-theme-color.prop-value(on-surface),
21+
mdc-theme-color.prop-value(surface),
22+
80%
23+
),
24+
supporting-text-color: rgba(
25+
mdc-theme-color.prop-value(surface),
26+
mdc-theme-color.text-emphasis(high)
27+
)
28+
));
4829
}
4930
}
5031
}
@@ -53,7 +34,14 @@
5334
$config: mat.private-typography-to-2018-config(
5435
mat.get-typography-config($config-or-theme));
5536
@include mat.private-using-mdc-typography($config) {
56-
@include mdc-snackbar.core-styles($query: mat.$private-mdc-typography-styles-query);
37+
.mat-mdc-snack-bar-container {
38+
@include mdc-snackbar-theme.theme((
39+
supporting-text-font: mat.font-family($config, body-2),
40+
supporting-text-line-height: mat.line-height($config, body-2),
41+
supporting-text-size: mat.font-size($config, body-2),
42+
supporting-text-weight: mat.font-weight($config, body-2),
43+
));
44+
}
5745
}
5846
}
5947

src/material-experimental/mdc-snack-bar/snack-bar-container.scss

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,86 @@
1+
@use 'sass:map';
12
@use '@angular/material' as mat;
23
@use '@angular/cdk';
3-
@use '@material/snackbar' as mdc-snackbar;
4+
@use '@material/snackbar/snackbar' as mdc-snackbar;
5+
@use '@material/snackbar/snackbar-theme' as mdc-snackbar-theme;
46

57

68
@include mat.private-disable-mdc-fallback-declarations {
79
// Include the styles without the animations since we
810
// reuse the same animation as the non-MDC version.
9-
@include mdc-snackbar.core-styles($query: mat.$private-mdc-base-styles-without-animation-query);
11+
@include mdc-snackbar.static-styles($query: mat.$private-mdc-base-styles-without-animation-query);
1012
}
1113

12-
// MDC sets the position as fixed and sets the container on the bottom center of the page (or
13-
// otherwise can be set to be "leading"). Our overlay handles a more advanced configuration
14-
// of positions, so we'll defer logic there.
1514
.mat-mdc-snack-bar-container {
15+
@include mat.private-disable-mdc-fallback-declarations {
16+
// The styles weren't included in `static-styles` so we need to add them ourselves.
17+
@include mdc-snackbar-theme.min-width(
18+
mdc-snackbar-theme.$min-width,
19+
$query: mat.$private-mdc-base-styles-query
20+
);
21+
@include mdc-snackbar-theme.max-width(
22+
mdc-snackbar-theme.$max-width,
23+
$query: mat.$private-mdc-base-styles-query
24+
);
25+
@include mdc-snackbar-theme.viewport-margin(
26+
mdc-snackbar-theme.$viewport-margin-narrow,
27+
$query: mat.$private-mdc-base-styles-query
28+
);
29+
30+
// MDC has the `container-elevation` which sounds like it should work, but it ends up applying
31+
// the shadow on the outer container instead of the `.mdc-snackbar__surface` which causes
32+
// a white background behind the rounded corners, because the `border-radius` is on the
33+
// surface element.
34+
@include mdc-snackbar-theme.elevation(mdc-snackbar-theme.$elevation);
35+
36+
@include mdc-snackbar-theme.theme-styles(map.merge(mdc-snackbar-theme.$light-theme, (
37+
container-color: inherit,
38+
supporting-text-color: inherit,
39+
container-shape: mdc-snackbar-theme.$shape-radius,
40+
supporting-text-font: inherit,
41+
supporting-text-line-height: inherit,
42+
supporting-text-size: inherit,
43+
supporting-text-weight: inherit,
44+
45+
// Removed to match the previous appearance.
46+
supporting-text-tracking: null,
47+
48+
// We're not using any of these since the button styling goes through `mat-button`.
49+
action-label-text-color: null,
50+
action-focus-state-layer-opacity: null,
51+
action-hover-state-layer-opacity: null,
52+
action-pressed-state-layer-opacity: null,
53+
icon-focus-state-layer-opacity: null,
54+
icon-hover-state-layer-opacity: null,
55+
icon-pressed-state-layer-opacity: null,
56+
)));
57+
}
58+
59+
// MDC sets the position as fixed and sets the container on the bottom center of the page (or
60+
// otherwise can be set to be "leading"). Our overlay handles a more advanced configuration
61+
// of positions, so we'll defer logic there.
1662
position: static;
1763

1864
@include cdk.high-contrast(active, off) {
1965
border: solid 1px;
2066
}
67+
68+
// The `mat-mdc-button` and `:not(:disabled)` here are redundant, but we need them to increase
69+
// the specificity over the button styles that may bleed in from the rest of the app.
70+
.mat-mdc-button.mat-mdc-snack-bar-action:not(:disabled) {
71+
// MDC's `action-label-text-color` should be able to do this, but the button theme has a
72+
// higher specificity so it ends up overriding it. Define our own variable that we can
73+
// use to control the color instead.
74+
color: var(--mat-mdc-snack-bar-button-color, transparent);
75+
76+
// Darken the ripples in the button so they're visible against the dark background.
77+
--mat-mdc-button-persistent-ripple-color: currentColor;
78+
79+
.mat-ripple-element {
80+
background-color: currentColor;
81+
opacity: 0.1;
82+
}
83+
}
2184
}
2285

2386
// These elements need to have full width using flex layout.

0 commit comments

Comments
 (0)