Skip to content

docs(material/theming): rewrite elevation guide #22467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 21, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 30 additions & 24 deletions guides/elevation.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
# Setting Element Elevation
# Applying Elevation

Angular Material's elevation classes and mixins allow you to add separation between elements along
the z-axis. All material design elements have resting elevations. In addition, some elements may
change their elevation in response to user interaction. The
[Material Design spec](https://material.io/design/environment/elevation.html)
explains how to best use elevation.
[The Material Design specification][material-elevation] gives guidance on expressing elevation on
UI elements by adding shadows. Angular Material provides CSS classes and Sass mixins for adding
these shadows.

Angular Material provides two ways to control the elevation of elements: predefined CSS classes
and mixins.
[material-elevation]: https://material.io/design/environment/elevation.html

## Predefined CSS classes
## Elevation CSS classes

The easiest way to add elevation to an element is to simply add one of the predefined CSS classes
`mat-elevation-z#` where `#` is the elevation number you want, 0-24. Dynamic elevation can be
achieved by switching elevation classes:
The `core-theme` Sass mixin, described in the [theming guide][], emits CSS classes for applying
elevation. These classes follow the pattern `mat-elevation-z#`, where `#` is the elevation number
you want, from 0 to 24. These predefined classes use the CSS `box-shadow` settings defined by the
Material Design specification.

You can dynamically change elevation on an element by swapping elevation CSS classes.

```html
<div [class.mat-elevation-z2]="!isActive" [class.mat-elevation-z8]="isActive"></div>
```

<!-- example(elevation-overview) -->

## Mixins

Elevations can also be added in CSS via the `elevation` mixin, which takes a number 0-24
indicating the elevation of the element as well as optional arguments for the elevation shadow's
color tone and opacity.
[theming-guide]: https://material.angular.io/guide/theming#applying-a-theme-to-components

Since an elevation shadow consists of multiple shadow components of varying opacities, the
`$opacity` argument of the mixin is considered a factor by which to scale these initial values
rather than an absolute value.
## Elevation Sass mixins

In order to use the mixin, you must import `~@angular/material`:
In addition to the predefined CSS classes, you can apply elevation styles using the `elevation`
Sass mixin. This mixin accepts a `$zValue` and an optional `$color`. The `$zValue` is a number from
0 to 24, representing the semantic elevation of the element, that controls the intensity of the
box-shadow. You can use the `$color` parameter to further customize the shadow appearance.

```scss
@use '~@angular/material' as mat;
Expand All @@ -47,14 +44,23 @@ In order to use the mixin, you must import `~@angular/material`:
}
```

For convenience, you can use the `elevation-transition` mixin to add a transition when the
elevation changes:
### Overridable elevation

When authoring a component, you may want to specify a default elevation that the component consumer
can override. You can accomplish this by using the `overridable-elevation` Sass mixin. This behaves
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[stretch] adding an example of the override to show this overridable usecase?

I'm not strongly opinionated on this

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll pass on this since I think it's kind of niche and also laziness.

identically to the `elevation` mixin, except that the styles only apply when the element does not
have a CSS class matching the pattern `mat-elevation-z#`, as described in
[Elevation CSS classes](#elevation-css-classes) above.

### Animating elevation

You can use the `elevation-transition` mixin to add a transition when elevation changes.

```scss
@use '~@angular/material' as mat;

.my-class {
@include mat.elevation-transition;
@include mat.elevation-transition();
@include mat.elevation(2);

&:active {
Expand Down