Skip to content

Commit 6d1818d

Browse files
benelliottmmalerba
authored andcommitted
feat(elevation): move elevation rules into theme stylesheets
Move all usages of elevation mixins out of component stylesheets and into their respective themes. This allows the elevation color to be changed based on the theme. Add new `mat-theme-elevation` and `mat-theme-overridable-elevation` shorthand mixins to apply the current theme's elevation color. Replace all usages of `mat-elevation` mixins with themed equivalents. Add `elevation` property to the default `foreground` theme palettes. Closes #11343 BREAKING CHANGE: Because `mat-elevation` usages have been moved out of component stylesheets, users who have not invoked a theme mixin will not see any elevation shadows on Material components. However, users that have created a custom theme which lacks the `elevation` property will still see the default black shadows. Additionally, users who want to use themed elevations in their custom components can create their own shorthand mixin: ``` @import '~angular/material/theming'; $myTheme: ... @mixin my-elevation($zValue) { @include mat-theme-elevation($zValue, $myTheme); } ``` and then invoke `angular-material-theme` with the `$myTheme` variable.
1 parent 18d0fa8 commit 6d1818d

31 files changed

+92
-61
lines changed

src/lib/autocomplete/_autocomplete-theme.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
@import '../core/style/elevation';
12
@import '../core/theming/theming';
23

34
@mixin mat-autocomplete-theme($theme) {
45
$foreground: map-get($theme, foreground);
56
$background: map-get($theme, background);
67

78
.mat-autocomplete-panel {
9+
@include _mat-theme-overridable-elevation(4, $theme);
810
background: mat-color($background, card);
911
color: mat-color($foreground, text);
1012

src/lib/autocomplete/autocomplete.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ $mat-autocomplete-panel-max-height: 256px !default;
88
$mat-autocomplete-panel-border-radius: 4px !default;
99

1010
.mat-autocomplete-panel {
11-
@include mat-menu-base(4);
11+
@include mat-menu-base();
1212

1313
visibility: hidden;
1414
max-width: none;

src/lib/bottom-sheet/_bottom-sheet-theme.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@import '../core/style/elevation';
12
@import '../core/typography/typography-utils';
23
@import '../core/theming/palette';
34

@@ -6,6 +7,7 @@
67
$foreground: map-get($theme, foreground);
78

89
.mat-bottom-sheet-container {
10+
@include _mat-theme-elevation(16, $theme);
911
background: mat-color($background, dialog);
1012
color: mat-color($foreground, text);
1113
}

src/lib/bottom-sheet/bottom-sheet-container.scss

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
@import '../core/style/elevation';
21
@import '../../cdk/a11y/a11y';
32

43
// The bottom sheet minimum width on larger screen sizes is based
@@ -9,8 +8,6 @@ $mat-bottom-sheet-container-vertical-padding: 8px !default;
98
$mat-bottom-sheet-container-horizontal-padding: 16px !default;
109

1110
.mat-bottom-sheet-container {
12-
@include mat-elevation(16);
13-
1411
padding: $mat-bottom-sheet-container-vertical-padding
1512
$mat-bottom-sheet-container-horizontal-padding;
1613
min-width: 100vw;

src/lib/button-toggle/_button-toggle-theme.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@import '../core/style/elevation';
12
@import '../core/theming/palette';
23
@import '../core/theming/theming';
34
@import '../core/typography/typography-utils';
@@ -6,6 +7,10 @@
67
$foreground: map-get($theme, foreground);
78
$background: map-get($theme, background);
89

10+
.mat-button-toggle-standalone, .mat-button-toggle-group {
11+
@include _mat-theme-elevation(2, $theme);
12+
}
13+
914
.mat-button-toggle {
1015
color: mat-color($foreground, hint-text);
1116

src/lib/button-toggle/button-toggle.scss

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
@import '../core/style/elevation';
21
@import '../core/style/vendor-prefixes';
32
@import '../core/style/layout-common';
43
@import '../../cdk/a11y/a11y';
@@ -9,7 +8,6 @@ $mat-button-toggle-border-radius: 2px !default;
98

109
.mat-button-toggle-standalone,
1110
.mat-button-toggle-group {
12-
@include mat-elevation(2);
1311
position: relative;
1412
display: inline-flex;
1513
flex-direction: row;

src/lib/button/_button-base.scss

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ $mat-mini-fab-padding: 8px !default;
7676
// Applies styles to buttons with backgrounds: raised, fab, and mini-fab
7777
@mixin mat-raised-button {
7878
@include mat-button-base;
79-
@include mat-overridable-elevation(2);
8079
@include _noop-animation();
8180

8281
// Force hardware acceleration.
@@ -86,10 +85,6 @@ $mat-mini-fab-padding: 8px !default;
8685
transition: background $swift-ease-out-duration $swift-ease-out-timing-function,
8786
mat-elevation-transition-property-value();
8887

89-
&:not([disabled]):active {
90-
@include mat-overridable-elevation(8);
91-
}
92-
9388
&[disabled] {
9489
box-shadow: none;
9590
}
@@ -98,7 +93,6 @@ $mat-mini-fab-padding: 8px !default;
9893
// Applies styles to fab and mini-fab button types only
9994
@mixin mat-fab($size, $padding) {
10095
@include mat-raised-button;
101-
@include mat-overridable-elevation(6);
10296

10397
// Reset the min-width from the button base.
10498
min-width: 0;
@@ -110,10 +104,6 @@ $mat-mini-fab-padding: 8px !default;
110104

111105
flex-shrink: 0;
112106

113-
&:not([disabled]):active {
114-
@include mat-overridable-elevation(12);
115-
}
116-
117107
.mat-button-wrapper {
118108
padding: $padding 0;
119109
display: inline-block;

src/lib/button/_button-theme.scss

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@import '../core/theming/theming';
2+
@import '../core/style/elevation';
23
@import '../core/typography/typography-utils';
34

45
// Applies a focus style to an mat-button element for each of the supported palettes.
@@ -106,6 +107,26 @@
106107
.mat-icon-button {
107108
@include _mat-button-ripple-color($theme, default, 0.2);
108109
}
110+
111+
.mat-stroked-button, .mat-flat-button {
112+
@include _mat-theme-overridable-elevation(0, $theme);
113+
}
114+
115+
.mat-raised-button {
116+
@include _mat-theme-overridable-elevation(2, $theme);
117+
118+
&:not([disabled]):active {
119+
@include _mat-theme-overridable-elevation(8, $theme);
120+
}
121+
}
122+
123+
.mat-fab, .mat-mini-fab {
124+
@include _mat-theme-overridable-elevation(6, $theme);
125+
126+
&:not([disabled]):active {
127+
@include _mat-theme-overridable-elevation(12, $theme);
128+
}
129+
}
109130
}
110131

111132
@mixin mat-button-typography($config) {

src/lib/button/button.scss

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,11 @@
3737
}
3838

3939
.mat-stroked-button {
40-
@include mat-overridable-elevation(0);
41-
42-
border: 1px solid currentColor;
40+
border: 1px solid currentColor;
4341
padding: $mat-stroked-button-padding;
4442
line-height: $mat-stroked-button-line-height;
4543
}
4644

47-
.mat-flat-button {
48-
@include mat-overridable-elevation(0);
49-
}
50-
5145
.mat-fab {
5246
@include mat-fab($mat-fab-size, $mat-fab-padding);
5347
}

src/lib/card/_card-theme.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@import '../core/theming/palette';
22
@import '../core/theming/theming';
3+
@import '../core/style/elevation';
34
@import '../core/typography/typography-utils';
45

56

@@ -8,6 +9,7 @@
89
$foreground: map-get($theme, foreground);
910

1011
.mat-card {
12+
@include _mat-theme-overridable-elevation(2, $theme);
1113
background: mat-color($background, card);
1214
color: mat-color($foreground, text);
1315
}

src/lib/card/card.scss

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ $mat-card-header-size: 40px !default;
1010

1111
.mat-card {
1212
@include mat-elevation-transition;
13-
@include mat-overridable-elevation(2);
1413
display: block;
1514
position: relative;
1615
padding: $mat-card-default-padding;

src/lib/chips/_chips-theme.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@import '../core/style/elevation';
12
@import '../core/theming/palette';
23
@import '../core/theming/theming';
34
@import '../core/typography/typography-utils';
@@ -39,6 +40,10 @@ $mat-chip-remove-font-size: 18px;
3940

4041
.mat-chip.mat-standard-chip {
4142
@include mat-chips-color($unselected-foreground, $unselected-background);
43+
44+
&:focus {
45+
@include _mat-theme-elevation(3, $theme);
46+
}
4247
}
4348

4449
.mat-chip.mat-standard-chip.mat-chip-selected {

src/lib/chips/chips.scss

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ $mat-chip-remove-size: 18px;
4141
}
4242

4343
&:focus {
44-
@include mat-elevation(3);
4544
outline: none;
4645
}
4746

src/lib/core/_core.scss

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,6 @@
1414

1515
// Mixin that renders all of the core styles that are not theme-dependent.
1616
@mixin mat-core($typography-config: null) {
17-
// Provides external CSS classes for each elevation value. Each CSS class is formatted as
18-
// `mat-elevation-z$zValue` where `$zValue` corresponds to the z-space to which the element is
19-
// elevated.
20-
@for $zValue from 0 through 24 {
21-
.#{$_mat-elevation-prefix}#{$zValue} {
22-
@include mat-elevation($zValue);
23-
}
24-
}
25-
2617
@include angular-material-typography($typography-config);
2718
@include mat-ripple();
2819
@include cdk-a11y();
@@ -37,6 +28,15 @@
3728
@include mat-optgroup-theme($theme);
3829
@include mat-pseudo-checkbox-theme($theme);
3930

31+
// Provides external CSS classes for each elevation value. Each CSS class is formatted as
32+
// `mat-elevation-z$zValue` where `$zValue` corresponds to the z-space to which the element is
33+
// elevated.
34+
@for $zValue from 0 through 24 {
35+
.#{$_mat-elevation-prefix}#{$zValue} {
36+
@include _mat-theme-elevation($zValue, $theme);
37+
}
38+
}
39+
4040
// Wrapper element that provides the theme background when the user's content isn't
4141
// inside of a `mat-sidenav-container`. Note that we need to exclude the ampersand
4242
// selector in case the mixin is included at the top level.

src/lib/core/style/_elevation.scss

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,14 @@ $_mat-elevation-prefix: 'mat-elevation-z';
151151
#{map-get(_get-ambient-map($color, $opacity), $zValue)};
152152
}
153153

154+
@mixin _mat-theme-elevation($zValue, $theme, $opacity: $mat-elevation-opacity) {
155+
$foreground: map-get($theme, foreground);
156+
$elevation-color: map-get($foreground, elevation);
157+
$elevation-color-or-default: if($elevation-color == null, $mat-elevation-color, $elevation-color);
158+
159+
@include mat-elevation($zValue, $elevation-color-or-default, $opacity);
160+
}
161+
154162
// Applies the elevation to an element in a manner that allows
155163
// consumers to override it via the Material elevation classes.
156164
@mixin mat-overridable-elevation(
@@ -162,6 +170,14 @@ $_mat-elevation-prefix: 'mat-elevation-z';
162170
}
163171
}
164172

173+
@mixin _mat-theme-overridable-elevation($zValue, $theme, $opacity: $mat-elevation-opacity) {
174+
$foreground: map-get($theme, foreground);
175+
$elevation-color: map-get($foreground, elevation);
176+
$elevation-color-or-default: if($elevation-color == null, $mat-elevation-color, $elevation-color);
177+
178+
@include mat-overridable-elevation($zValue, $elevation-color-or-default, $opacity);
179+
}
180+
165181
// Returns a string that can be used as the value for a transition property for elevation.
166182
// Calling this function directly is useful in situations where a component needs to transition
167183
// more than one property.

src/lib/core/style/_menu-common.scss

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
@import './variables';
2-
@import './elevation';
32
@import './list-common';
43
@import './vendor-prefixes';
54

@@ -14,9 +13,7 @@ $mat-menu-side-padding: 16px !default;
1413
$mat-menu-icon-margin: 16px !default;
1514

1615

17-
@mixin mat-menu-base($default-elevation) {
18-
@include mat-overridable-elevation($default-elevation);
19-
16+
@mixin mat-menu-base() {
2017
min-width: $mat-menu-overlay-min-width;
2118
max-width: $mat-menu-overlay-max-width;
2219
overflow: auto;

src/lib/core/theming/_palette.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,7 @@ $mat-light-theme-foreground: (
714714
disabled: $dark-disabled-text,
715715
disabled-button: rgba(black, 0.26),
716716
disabled-text: $dark-disabled-text,
717+
elevation: black,
717718
hint-text: $dark-disabled-text,
718719
secondary-text: $dark-secondary-text,
719720
icon: rgba(black, 0.54),
@@ -732,6 +733,7 @@ $mat-dark-theme-foreground: (
732733
disabled: $light-disabled-text,
733734
disabled-button: rgba(white, 0.3),
734735
disabled-text: $light-disabled-text,
736+
elevation: black,
735737
hint-text: $light-disabled-text,
736738
secondary-text: $light-secondary-text,
737739
icon: white,

src/lib/datepicker/_datepicker-theme.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@import '../core/style/elevation';
12
@import '../core/theming/palette';
23
@import '../core/theming/theming';
34
@import '../core/typography/typography-utils';
@@ -83,6 +84,7 @@ $mat-calendar-weekday-table-font-size: 11px !default;
8384
@include _mat-datepicker-color(map-get($theme, primary));
8485

8586
.mat-datepicker-content {
87+
@include _mat-theme-elevation(4, $theme);
8688
background-color: mat-color($background, card);
8789
color: mat-color($foreground, text);
8890

@@ -95,6 +97,10 @@ $mat-calendar-weekday-table-font-size: 11px !default;
9597
}
9698
}
9799

100+
.mat-datepicker-content-touch {
101+
@include _mat-theme-elevation(0, $theme);
102+
}
103+
98104
.mat-datepicker-toggle-active {
99105
color: mat-color(map-get($theme, primary));
100106

src/lib/datepicker/datepicker-content.scss

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
@import '../core/style/elevation';
2-
3-
41
$mat-datepicker-calendar-padding: 8px;
52
$mat-datepicker-non-touch-calendar-cell-size: 40px;
63
$mat-datepicker-non-touch-calendar-width:
@@ -25,8 +22,6 @@ $mat-datepicker-touch-max-height: 788px;
2522

2623

2724
.mat-datepicker-content {
28-
@include mat-elevation(4);
29-
3025
display: block;
3126
border-radius: 4px;
3227

@@ -37,8 +32,6 @@ $mat-datepicker-touch-max-height: 788px;
3732
}
3833

3934
.mat-datepicker-content-touch {
40-
@include mat-elevation(0);
41-
4235
display: block;
4336
// make sure the dialog scrolls rather than being cropped on ludicrously small screens
4437
max-height: 80vh;

src/lib/dialog/_dialog-theme.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@import '../core/style/elevation';
12
@import '../core/theming/palette';
23
@import '../core/theming/theming';
34
@import '../core/typography/typography-utils';
@@ -8,6 +9,7 @@
89
$foreground: map-get($theme, foreground);
910

1011
.mat-dialog-container {
12+
@include _mat-theme-elevation(24, $theme);
1113
background: mat-color($background, dialog);
1214
color: mat-color($foreground, text);
1315
}

src/lib/dialog/dialog.scss

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
@import '../core/style/elevation';
21
@import '../core/style/vendor-prefixes';
32
@import '../../cdk/a11y/a11y';
43

@@ -9,8 +8,6 @@ $mat-dialog-max-height: 65vh !default;
98
$mat-dialog-button-margin: 8px !default;
109

1110
.mat-dialog-container {
12-
@include mat-elevation(24);
13-
1411
display: block;
1512
padding: $mat-dialog-padding;
1613
border-radius: $mat-dialog-border-radius;

0 commit comments

Comments
 (0)