|
1 |
| -# Setting Element Elevation |
| 1 | +# Applying Elevation |
2 | 2 |
|
3 |
| -Angular Material's elevation classes and mixins allow you to add separation between elements along |
4 |
| -the z-axis. All material design elements have resting elevations. In addition, some elements may |
5 |
| -change their elevation in response to user interaction. The |
6 |
| -[Material Design spec](https://material.io/design/environment/elevation.html) |
7 |
| -explains how to best use elevation. |
| 3 | +[The Material Design specification][material-elevation] gives guidance on expressing elevation on |
| 4 | +UI elements by adding shadows. Angular Material provides CSS classes and Sass mixins for adding |
| 5 | +these shadows. |
8 | 6 |
|
9 |
| -Angular Material provides two ways to control the elevation of elements: predefined CSS classes |
10 |
| -and mixins. |
| 7 | +[material-elevation]: https://material.io/design/environment/elevation.html |
11 | 8 |
|
12 |
| -## Predefined CSS classes |
| 9 | +## Elevation CSS classes |
13 | 10 |
|
14 |
| -The easiest way to add elevation to an element is to simply add one of the predefined CSS classes |
15 |
| -`mat-elevation-z#` where `#` is the elevation number you want, 0-24. Dynamic elevation can be |
16 |
| -achieved by switching elevation classes: |
| 11 | +The `core-theme` Sass mixin, described in the [theming guide][], emits CSS classes for applying |
| 12 | +elevation. These classes follow the pattern `mat-elevation-z#`, where `#` is the elevation number |
| 13 | +you want, 0 to 24. These predefined classes use the CSS `box-shadow` settings defined by the |
| 14 | +Material Design specification. |
| 15 | + |
| 16 | +You can dynamically change elevation on an element by swapping elevation CSS classes. |
17 | 17 |
|
18 | 18 | ```html
|
19 | 19 | <div [class.mat-elevation-z2]="!isActive" [class.mat-elevation-z8]="isActive"></div>
|
20 | 20 | ```
|
21 | 21 |
|
22 | 22 | <!-- example(elevation-overview) -->
|
23 | 23 |
|
24 |
| -## Mixins |
25 |
| - |
26 |
| -Elevations can also be added in CSS via the `mat-elevation` mixin, which takes a number 0-24 |
27 |
| -indicating the elevation of the element as well as optional arguments for the elevation shadow's |
28 |
| -color tone and opacity. |
| 24 | +[theming-guide]: https://material.angular.io/guide/theming#applying-a-theme-to-components |
29 | 25 |
|
30 |
| -Since an elevation shadow consists of multiple shadow components of varying opacities, the |
31 |
| -`$opacity` argument of the mixin is considered a factor by which to scale these initial values |
32 |
| -rather than an absolute value. |
| 26 | +## Elevation Sass mixins |
33 | 27 |
|
34 |
| -In order to use the mixin, you must import `~@angular/material/theming`: |
| 28 | +In addition to the predefined CSS classes, you can apply elevation styles using the `elevation` |
| 29 | +Sass mixin. This mixin accepts a `$zValue`, an optional `$color`, and an optional `$opacity`. The |
| 30 | +`$zValue` is number from 0 to 24, representing the semantic elevation of the element, that controls |
| 31 | +the intensity of the box-shadow. You can use the `$color` and `$opacity` parameters to further |
| 32 | +customize the shadow appearance. Because an elevation shadow consists of multiple shadow components |
| 33 | +of varying opacities, the `$opacity` argument of the mixin acts as a factor by which to scale these |
| 34 | +initial values rather than an absolute value. |
35 | 35 |
|
36 | 36 | ```scss
|
37 |
| -@import '~@angular/material/theming'; |
| 37 | +@use '~@angular/material' as mat; |
38 | 38 |
|
39 | 39 | .my-class-with-default-shadow {
|
40 | 40 | // Adds a shadow for elevation level 2 with default color and full opacity:
|
41 |
| - @include mat-elevation(2); |
| 41 | + @include mat.elevation(2); |
42 | 42 | }
|
43 | 43 |
|
44 | 44 | .my-class-with-custom-shadow {
|
45 | 45 | // Adds a shadow for elevation level 2 with color #e91e63 and 80% of the default opacity:
|
46 |
| - @include mat-elevation(2, #e91e63, 0.8); |
| 46 | + @include mat.elevation(2, #e91e63, 0.8); |
47 | 47 | }
|
48 | 48 | ```
|
49 | 49 |
|
50 |
| -For convenience, you can use the `mat-elevation-transition` mixin to add a transition when the |
51 |
| -elevation changes: |
| 50 | +### Overridable elevation |
| 51 | + |
| 52 | +When authoring a component, you may want to specify a default elevation that the component consumer |
| 53 | +can override. You can accomplish this by using the `overridable-elevation` Sass mixin. This behaves |
| 54 | +identically to the `elevation` mixin, except that the styles only apply when the element does not |
| 55 | +have a CSS class matching the pattern `mat-elevation-z#`, as described in |
| 56 | +[Elevation CSS classes](#elevation-css-classes) above. |
| 57 | + |
| 58 | +### Animating elevation |
| 59 | + |
| 60 | +You can use the `elevation-transition` mixin to add a transition when elevation changes. |
52 | 61 |
|
53 | 62 | ```scss
|
54 |
| -@import '~@angular/material/theming'; |
| 63 | +@use '~@angular/material' as mat; |
55 | 64 |
|
56 | 65 | .my-class {
|
57 |
| - @include mat-elevation-transition; |
58 |
| - @include mat-elevation(2); |
| 66 | + @include mat.elevation-transition(); |
| 67 | + @include mat.elevation(2); |
59 | 68 |
|
60 | 69 | &:active {
|
61 |
| - @include mat-elevation(8); |
| 70 | + @include mat.elevation(8); |
62 | 71 | }
|
63 | 72 | }
|
64 | 73 | ```
|
0 commit comments