Skip to content

Commit 36476b3

Browse files
committed
refactor(material-experimental/mdc-snack-bar): switch to new theming API
Reworks the MDC-based snack bar to use the new CSS-variable-based theming APIs.
1 parent 039f532 commit 36476b3

File tree

2 files changed

+89
-41
lines changed

2 files changed

+89
-41
lines changed

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

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,32 @@
11
@use '@angular/material' as mat;
22
@use '@material/theme/theme-color' as mdc-theme-color;
33
@use '@material/snackbar' as mdc-snackbar;
4+
@use '@material/snackbar/snackbar-theme' as mdc-snackbar-theme;
45
@use 'sass:color';
56
@use 'sass:map';
67

78

89
@mixin color($config-or-theme) {
910
$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;
11+
$is-dark-theme: map.get($config, is-dark);
12+
$accent: map.get($config, accent);
1413

1514
@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-
}
15+
.mat-mdc-snack-bar-container {
16+
$button-color: if($is-dark-theme, currentColor, mat.get-color-from-palette($accent, text));
17+
--mat-mdc-snack-bar-button-color: #{$button-color};
3218

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;
19+
@include mdc-snackbar-theme.theme((
20+
container-color: color.mix(
21+
mdc-theme-color.prop-value(on-surface),
22+
mdc-theme-color.prop-value(surface),
23+
80%
24+
),
25+
supporting-text-color: rgba(
26+
mdc-theme-color.prop-value(surface),
27+
mdc-theme-color.text-emphasis(high)
28+
)
29+
));
4830
}
4931
}
5032
}
@@ -53,7 +35,14 @@
5335
$config: mat.private-typography-to-2018-config(
5436
mat.get-typography-config($config-or-theme));
5537
@include mat.private-using-mdc-typography($config) {
56-
@include mdc-snackbar.core-styles($query: mat.$private-mdc-typography-styles-query);
38+
.mat-mdc-snack-bar-container {
39+
@include mdc-snackbar-theme.theme((
40+
supporting-text-font: mat.font-family($config, body-2),
41+
supporting-text-line-height: mat.line-height($config, body-2),
42+
supporting-text-size: mat.font-size($config, body-2),
43+
supporting-text-weight: mat.font-weight($config, body-2),
44+
));
45+
}
5746
}
5847
}
5948

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

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,82 @@
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+
41+
// Removed to match the previous appearance.
42+
supporting-text-tracking: null,
43+
44+
// We're not using any of these since the button styling goes through `mat-button`.
45+
action-label-text-color: null,
46+
action-focus-state-layer-opacity: null,
47+
action-hover-state-layer-opacity: null,
48+
action-pressed-state-layer-opacity: null,
49+
icon-focus-state-layer-opacity: null,
50+
icon-hover-state-layer-opacity: null,
51+
icon-pressed-state-layer-opacity: null,
52+
)));
53+
}
54+
55+
// MDC sets the position as fixed and sets the container on the bottom center of the page (or
56+
// otherwise can be set to be "leading"). Our overlay handles a more advanced configuration
57+
// of positions, so we'll defer logic there.
1658
position: static;
1759

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

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

0 commit comments

Comments
 (0)