Skip to content

Commit 3899a52

Browse files
committed
feat(elevation): Add elevation core styles
This commit replaces `_shadows.scss` within `src/core/styles` with `_elevation.scss`, according to the proposed design outlined [here](https://goo.gl/Kq0k9Z). All components that were dependent on the old shadows have been modified to use elevation. Note however that none of the values in those old components have been changed; it's simply a to the new API. Part of #48.
1 parent abefaf5 commit 3899a52

File tree

6 files changed

+206
-20
lines changed

6 files changed

+206
-20
lines changed

src/components/button/_button-base.scss

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
@import "variables";
3-
@import "shadows";
3+
@import "elevation";
44

55
// TODO(jelbourn): This goes away.
66
@import "default-theme";
@@ -70,20 +70,20 @@ $md-mini-fab-padding: 8px !default;
7070
%md-raised-button {
7171
@extend %md-button-base;
7272

73+
@include md-elevation(1);
7374
@include md-button-theme('color', default-contrast);
7475
@include md-button-theme('background-color');
7576

7677
background-color: md-color($md-background, background);
77-
box-shadow: $md-shadow-bottom-z-1;
7878
// Force hardware acceleration.
7979
transform: translate3d(0, 0, 0);
8080

8181
// Animation.
8282
transition: background $swift-ease-out-duration $swift-ease-out-timing-function,
83-
box-shadow $swift-ease-out-duration $swift-ease-out-timing-function;
83+
md-elevation-transition-property-value();
8484

8585
&:active {
86-
box-shadow: $md-shadow-bottom-z-2;
86+
@include md-elevation(2);
8787
}
8888

8989
&.md-button-focus {

src/components/card/card.scss

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
@import "variables";
3-
@import "shadows";
3+
@import "elevation";
44
@import "default-theme"; // TODO: Remove this
55

66
$md-card-default-padding: 24px !default;
@@ -9,18 +9,18 @@ $md-card-border-radius: 2px !default;
99
$md-card-header-size: 40px !default;
1010

1111
md-card {
12+
@include md-elevation(1);
13+
@include md-elevation-transition;
1214
display: block;
1315
position: relative;
1416
padding: $md-card-default-padding;
1517
border-radius: $md-card-border-radius;
16-
box-shadow: $md-shadow-bottom-z-1;
1718
font-family: $md-font-family;
1819
background: md-color($md-background, card);
1920
}
2021

2122
md-card:hover {
22-
box-shadow: $md-shadow-bottom-z-2;
23-
transition: $swift-ease-in;
23+
@include md-elevation(2);
2424
}
2525

2626
.md-card-flat {
@@ -30,7 +30,7 @@ md-card:hover {
3030
// base styles for each card section preset (md-card-content, etc)
3131
%md-card-section-base {
3232
display: block;
33-
margin-bottom: 16px;
33+
margin-bottom: 16px;
3434
}
3535

3636
md-card-title {
@@ -76,7 +76,7 @@ md-card-footer {
7676
md-card-actions {
7777
[md-button], [md-raised-button] {
7878
margin: 0 4px;
79-
}
79+
}
8080
}
8181

8282
/* HEADER STYLES */
@@ -175,7 +175,7 @@ md-card-title-group {
175175
margin-bottom: 0;
176176
}
177177

178-
// if main image is on top, need to place it flush against top
178+
// if main image is on top, need to place it flush against top
179179
// (by stripping card's default 24px padding)
180180
[md-card-image]:first-child {
181181
margin-top: -24px;
@@ -208,7 +208,7 @@ md-card-header md-card-subtitle:not(:first-child) {
208208
margin-top: -8px;
209209
}
210210

211-
// xl image should always have 16px on top.
211+
// xl image should always have 16px on top.
212212
// when it's the first el, it'll need to remove 8px from default card padding of 24px
213213
.md-card > [md-card-xl-image]:first-child{
214214
margin-top: -8px;

src/components/sidenav/sidenav.scss

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@import "default-theme";
22
@import "mixins";
33
@import "variables";
4-
@import "shadows";
4+
@import "elevation";
55

66

77
// We use invert() here to have the darken the background color expected to be used. If the
@@ -31,14 +31,14 @@ $md-sidenav-push-background-color: md-color($md-background, dialog) !default;
3131
will-change: transform;
3232
}
3333
&.md-sidenav-opening {
34+
@include md-elevation(1);
3435
visibility: visible;
3536
transform: translateX($open);
3637
will-change: transform;
37-
box-shadow: $md-shadow-bottom-z-1;
3838
}
3939
&.md-sidenav-opened {
40+
@include md-elevation(1);
4041
transform: translateX($open);
41-
box-shadow: $md-shadow-bottom-z-1;
4242
}
4343
}
4444

src/core/style/_elevation.scss

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/**
2+
* A collection of mixins and CSS classes that can be used to apply elevation to a material
3+
* element.
4+
* See: https://www.google.com/design/spec/what-is-material/elevation-shadows.html
5+
* Examples:
6+
*
7+
*
8+
* .md-foo {
9+
* @include $md-elevation(2);
10+
*
11+
* &:active {
12+
* @include $md-elevation(8);
13+
* }
14+
* }
15+
*
16+
* <div id="external-card" class="md-elevation-z2"><p>Some content</p></div>
17+
*
18+
* For an explanation of the design behind how elevation is implemented, see the design doc at
19+
* https://goo.gl/Kq0k9Z.
20+
*/
21+
22+
// Colors for umbra, penumbra, and ambient shadows. As described in the design doc, each elevation
23+
// level is created using a set of 3 shadow values, one for umbra (the shadow representing the
24+
// space completely obscured by an object relative to its light source), one for penumbra (the
25+
// space partially obscured by an object), and one for ambient (the space which contains the object
26+
// itself). For a further explanation of these terms and their meanings, see
27+
// https://en.wikipedia.org/wiki/Umbra,_penumbra_and_antumbra.
28+
29+
$_umbra-color: rgba(black, 0.2);
30+
$_penumbra-color: rgba(black, 0.14);
31+
$_ambient-color: rgba(black, 0.12);
32+
33+
// Maps for the different shadow sets and their values within each z-space. These values were
34+
// created by taking a few reference shadow sets created by Google's Designers and interpolating
35+
// all of the values between them.
36+
37+
$_umbra-elevation-map: (
38+
0: '0px 0px 0px 0px #{$_umbra-color}',
39+
1: '0px 2px 1px -1px #{$_umbra-color}',
40+
2: '0px 3px 1px -2px #{$_umbra-color}',
41+
3: '0px 3px 3px -2px #{$_umbra-color}',
42+
4: '0px 2px 4px -1px #{$_umbra-color}',
43+
5: '0px 3px 5px -1px #{$_umbra-color}',
44+
6: '0px 3px 5px -1px #{$_umbra-color}',
45+
7: '0px 4px 5px -2px #{$_umbra-color}',
46+
8: '0px 5px 5px -3px #{$_umbra-color}',
47+
9: '0px 5px 6px -3px #{$_umbra-color}',
48+
10: '0px 6px 6px -3px #{$_umbra-color}',
49+
11: '0px 6px 7px -4px #{$_umbra-color}',
50+
12: '0px 7px 8px -4px #{$_umbra-color}',
51+
13: '0px 7px 8px -4px #{$_umbra-color}',
52+
14: '0px 7px 9px -4px #{$_umbra-color}',
53+
15: '0px 8px 9px -5px #{$_umbra-color}',
54+
16: '0px 8px 10px -5px #{$_umbra-color}',
55+
17: '0px 8px 11px -5px #{$_umbra-color}',
56+
18: '0px 9px 11px -5px #{$_umbra-color}',
57+
19: '0px 9px 12px -6px #{$_umbra-color}',
58+
20: '0px 10px 13px -6px #{$_umbra-color}',
59+
21: '0px 10px 13px -6px #{$_umbra-color}',
60+
22: '0px 10px 14px -6px #{$_umbra-color}',
61+
23: '0px 11px 14px -7px #{$_umbra-color}',
62+
24: '0px 11px 15px -7px #{$_umbra-color}'
63+
);
64+
65+
$_penumbra-elevation-map: (
66+
0: '0px 0px 0px 0px #{$_penumbra-color}',
67+
1: '0px 1px 1px 0px #{$_penumbra-color}',
68+
2: '0px 2px 2px 0px #{$_penumbra-color}',
69+
3: '0px 3px 4px 0px #{$_penumbra-color}',
70+
4: '0px 4px 5px 0px #{$_penumbra-color}',
71+
5: '0px 5px 8px 0px #{$_penumbra-color}',
72+
6: '0px 6px 10px 0px #{$_penumbra-color}',
73+
7: '0px 7px 10px 1px #{$_penumbra-color}',
74+
8: '0px 8px 10px 1px #{$_penumbra-color}',
75+
9: '0px 9px 12px 1px #{$_penumbra-color}',
76+
10: '0px 10px 14px 1px #{$_penumbra-color}',
77+
11: '0px 11px 15px 1px #{$_penumbra-color}',
78+
12: '0px 12px 17px 2px #{$_penumbra-color}',
79+
13: '0px 13px 19px 2px #{$_penumbra-color}',
80+
14: '0px 14px 21px 2px #{$_penumbra-color}',
81+
15: '0px 15px 22px 2px #{$_penumbra-color}',
82+
16: '0px 16px 24px 2px #{$_penumbra-color}',
83+
17: '0px 17px 26px 2px #{$_penumbra-color}',
84+
18: '0px 18px 28px 2px #{$_penumbra-color}',
85+
19: '0px 19px 29px 2px #{$_penumbra-color}',
86+
20: '0px 20px 31px 3px #{$_penumbra-color}',
87+
21: '0px 21px 33px 3px #{$_penumbra-color}',
88+
22: '0px 22px 35px 3px #{$_penumbra-color}',
89+
23: '0px 23px 36px 3px #{$_penumbra-color}',
90+
24: '0px 24px 38px 3px #{$_penumbra-color}'
91+
);
92+
93+
$_ambient-elevation-map: (
94+
0: '0px 0px 0px 0px #{$_ambient-color}',
95+
1: '0px 1px 3px 0px #{$_ambient-color}',
96+
2: '0px 1px 5px 0px #{$_ambient-color}',
97+
3: '0px 1px 8px 0px #{$_ambient-color}',
98+
4: '0px 1px 10px 0px #{$_ambient-color}',
99+
5: '0px 1px 14px 0px #{$_ambient-color}',
100+
6: '0px 1px 18px 0px #{$_ambient-color}',
101+
7: '0px 2px 16px 1px #{$_ambient-color}',
102+
8: '0px 3px 14px 2px #{$_ambient-color}',
103+
9: '0px 3px 16px 2px #{$_ambient-color}',
104+
10: '0px 4px 18px 3px #{$_ambient-color}',
105+
11: '0px 4px 20px 3px #{$_ambient-color}',
106+
12: '0px 5px 22px 4px #{$_ambient-color}',
107+
13: '0px 5px 24px 4px #{$_ambient-color}',
108+
14: '0px 5px 26px 4px #{$_ambient-color}',
109+
15: '0px 6px 28px 5px #{$_ambient-color}',
110+
16: '0px 6px 30px 5px #{$_ambient-color}',
111+
17: '0px 6px 32px 5px #{$_ambient-color}',
112+
18: '0px 7px 34px 6px #{$_ambient-color}',
113+
19: '0px 7px 36px 6px #{$_ambient-color}',
114+
20: '0px 8px 38px 7px #{$_ambient-color}',
115+
21: '0px 8px 40px 7px #{$_ambient-color}',
116+
22: '0px 8px 42px 7px #{$_ambient-color}',
117+
23: '0px 9px 44px 8px #{$_ambient-color}',
118+
24: '0px 9px 46px 8px #{$_ambient-color}'
119+
);
120+
121+
/**
122+
* The css property used for elevation. In most cases this should not be changed. It is exposed
123+
* as a variable for abstraction / easy use when needing to reference the property directly, for
124+
* example in a will-change rule.
125+
*/
126+
$md-elevation-property: box-shadow !default;
127+
128+
/** The default duration value for elevation transitions. */
129+
$md-elevation-transition-duration: 280ms !default;
130+
131+
/** The default easing value for elevation transitions. */
132+
$md-elevation-transition-timing-function: $md-fast-out-slow-in-timing-function;
133+
134+
/**
135+
* Applies the correct css rules to an element to give it the elevation specified by $zValue.
136+
* The $zValue must be between 0 and 24.
137+
*/
138+
@mixin md-elevation($zValue) {
139+
@if type-of($zValue) != number or not unitless($zValue) {
140+
@error "$zValue must be a unitless number";
141+
}
142+
@if $zValue < 0 or $zValue > 24 {
143+
@error "$zValue must be between 0 and 24";
144+
}
145+
146+
#{$md-elevation-property}: #{map-get($_umbra-elevation-map, $zValue)},
147+
#{map-get($_penumbra-elevation-map, $zValue)},
148+
#{map-get($_ambient-elevation-map, $zValue)};
149+
}
150+
151+
/**
152+
* Returns a string that can be used as the value for a transition property for elevation.
153+
* Calling this function directly is useful in situations where a component needs to transition
154+
* more than one property.
155+
*
156+
* .foo {
157+
* transition: md-elevation-transition-property-value(), opacity 100ms ease;
158+
* will-change: $md-elevation-property, opacity;
159+
* }
160+
*/
161+
@function md-elevation-transition-property-value(
162+
$duration: $md-elevation-transition-duration,
163+
$easing: $md-elevation-transition-timing-function) {
164+
@return #{$md-elevation-property} #{$duration} #{$easing};
165+
}
166+
167+
/**
168+
* Applies the correct css rules needed to have an element transition between elevations.
169+
* This mixin should be applied to elements whose elevation values will change depending on their
170+
* context (e.g. when active or disabled).
171+
*/
172+
// NOTE(traviskaufman): Both this mixin and the above function use default parameters so they can
173+
// be used in the same way by clients.
174+
@mixin md-elevation-transition(
175+
$duration: $md-elevation-transition-duration,
176+
$easing: $md-elevation-transition-timing-function) {
177+
transition: md-elevation-transition-property-value($duration, $easing);
178+
will-change: $md-elevation-property;
179+
}

src/core/style/_shadows.scss

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/core/style/core.scss

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Core styles that can be used to apply material design treatments to any element.
2+
3+
@import 'elevation';
4+
5+
// Provides external CSS classes for each elevation value. Each CSS class is formatted as
6+
// `md-elevation-z$zValue` where `$zValue` corresponds to the z-space to which the element is
7+
// elevated.
8+
@for $zValue from 0 through 24 {
9+
.md-elevation-z#{$zValue} {
10+
@include md-elevation($zValue);
11+
}
12+
}

0 commit comments

Comments
 (0)