Skip to content

Commit 4fc5ee1

Browse files
jelbournvivian-hu-zz
authored andcommitted
docs: clean up and clarify theming docs (#13435)
This makes some minor wording changes to the main theming guide and mostly rewrites the theming-your-own-component guide. Edited for clarify, brevity, and language. Fixes #13206
1 parent b7de6be commit 4fc5ee1

File tree

2 files changed

+72
-58
lines changed

2 files changed

+72
-58
lines changed

guides/theming-your-components.md

Lines changed: 69 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,99 @@
1-
# Theming your custom components
2-
In order to style your own components with Angular Material's tooling, the component's styles must be defined with Sass.
1+
### Theming your custom component with Angular Material's theming system
2+
In order to style your own components with Angular Material's tooling, the component's styles must
3+
be defined with Sass.
34

4-
### Using `@mixin` to automatically apply a theme
5-
6-
#### Why using `@mixin`
7-
The advantage of using a `@mixin` function is that when you change your theme, every file that uses it will be automatically updated.
8-
Calling the `@mixin` with a different theme argument allows multiple themes within the app or component.
9-
10-
#### How to use `@mixin`
11-
We can better theme our custom components by adding a `@mixin` function to its theme file, and then call this function to apply a theme.
12-
13-
All you need is to create a `@mixin` function in the custom-component-theme.scss
5+
#### 1. Define all color and typography styles in a "theme file" for the component
6+
First, create a Sass mixin that accepts an Angular Material theme and outputs the color-specific
7+
styles for the component. An Angular Material theme definition is a Sass map.
148

9+
For example, if building a custom carousel component:
1510
```scss
16-
// Import all the tools needed to customize the theme and extract parts of it
11+
// Import library functions for theme creation.
1712
@import '~@angular/material/theming';
1813

19-
// Define a mixin that accepts a theme and outputs the color styles for the component.
14+
// Define a mixin that accepts a theme and outputs the theme-specific styles.
2015
@mixin candy-carousel-theme($theme) {
21-
// Extract whichever individual palettes you need from the theme.
16+
// Extract the palettes you need from the theme definition.
2217
$primary: map-get($theme, primary);
2318
$accent: map-get($theme, accent);
24-
25-
// Use mat-color to extract individual colors from a palette as necessary.
19+
20+
// Define any styles affected by the theme.
2621
.candy-carousel {
22+
// Use mat-color to extract individual colors a palette.
2723
background-color: mat-color($primary);
2824
border-color: mat-color($accent, A400);
2925
}
3026
}
3127
```
32-
Now you just have to call the `@mixin` function to apply the theme:
3328

34-
```scss
35-
// Import a pre-built theme
36-
@import '~@angular/material/prebuilt-themes/deeppurple-amber.css';
37-
// Import your custom input theme file so you can call the custom-input-theme function
38-
@import 'app/candy-carousel/candy-carousel-theme.scss';
29+
Second, create another Sass mixin that accepts an Angular Material typography definition and outputs
30+
typographic styles. For example:
3931

40-
// Using the $theme variable from the pre-built theme you can call the theming function
41-
@include candy-carousel-theme($theme);
32+
```scss
33+
@mixin candy-caroursel-typography($config) {
34+
.candy-carousel {
35+
font: {
36+
family: mat-font-family($config, body-1);
37+
size: mat-font-size($config, body-1);
38+
weight: mat-font-weight($config, body-1);
39+
}
40+
}
41+
}
4242
```
4343

44-
For more details about the theming functions, see the comments in the
45-
[source](https://github.com/angular/material2/blob/master/src/lib/core/theming/_theming.scss).
44+
See the [typography guide](https://material.angular.io/guide/typography) for more information on
45+
typographic customization.
4646

47-
#### Best practices using `@mixin`
48-
When using `@mixin`, the theme file should only contain the definitions that are affected by the passed-in theme.
47+
#### 2. Define all remaining styles in a normal component stylesheet.
48+
Define all styles unaffected by the theme in a separate file referenced directly in the component's
49+
`styleUrl`. This generally includes everything except for color and typography styles.
4950

50-
All styles that are not affected by the theme should be placed in a `candy-carousel.scss` file.
51-
This file should contain everything that is not affected by the theme like sizes, transitions...
5251

53-
Styles that are affected by the theme should be placed in a separated theming file as
54-
`_candy-carousel-theme.scss` and the file should have a `_` before the name. This file should
55-
contain the `@mixin` function responsible for applying the theme to the component.
52+
#### 3. Include the theme mixin in your application
53+
Use the Sass `@include` keyword to include a component's theme mixin wherever you're already
54+
including Angular Material's built-in theme mixins.
5655

56+
```scss
57+
// Import library functions for theme creation.
58+
@import '~@angular/material/theming';
59+
60+
// Include non-theme styles for core.
61+
@include mat-core();
62+
63+
// Define your application's custom theme.
64+
$primary: mat-palette($mat-indigo);
65+
$accent: mat-palette($mat-pink, A200, A100, A400);
66+
$theme: mat-light-theme($primary, $accent);
67+
68+
// Include theme styles for Angular Material components.
69+
@include angular-material-theme($theme);
70+
71+
// Include theme styles for your custom components.
72+
@include candy-carousel-theme($theme);
73+
```
5774

58-
### Using colors from a palette
59-
You can consume the theming functions and Material palette variables from
60-
`@angular/material/theming`. You can use the `mat-color` function to extract a specific color
61-
from a palette. For example:
75+
76+
#### Note: using the `mat-color` function to extract colors from a palette
77+
You can consume the theming functions and Material Design color palettes from
78+
`@angular/material/theming`. The `mat-color` Sass function extracts a specific color from a palette.
79+
For example:
6280

6381
```scss
6482
// Import theming functions
6583
@import '~@angular/material/theming';
66-
// Import your custom theme
67-
@import 'src/unicorn-app-theme.scss';
68-
69-
// Use mat-color to extract individual colors from a palette as necessary.
70-
// The hue can be one of the standard values (500, A400, etc.), one of the three preconfigured
71-
// hues (default, lighter, darker), or any of the aforementioned prefixed with "-contrast".
72-
// For example a hue of "darker-contrast" gives a light color to contrast with a "darker" hue.
73-
// Note that quotes are needed when using a numeric hue with the "-contrast" modifier.
74-
// Available color palettes: https://material.io/design/color/
84+
7585
.candy-carousel {
76-
background-color: mat-color($candy-app-primary);
77-
border-color: mat-color($candy-app-accent, A400);
78-
color: mat-color($candy-app-primary, '100-contrast');
86+
// Get the default hue for a palette.
87+
color: mat-color($primary);
88+
89+
// Get a specific hue for a palette.
90+
// See https://material.io/archive/guidelines/style/color.html#color-color-palette for hues.
91+
background-color: mat-color($accent, 300);
92+
93+
// Get a relative color for a hue ('lighter' or 'darker')
94+
outline-color: mat-color($accent, lighter);
95+
96+
// Get a constrast color for a hue by adding `-contrast` to any other key.
97+
border-color: mat-color($primary, '100-constrast');
7998
}
8099
```

guides/theming.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ ensures that the proper theme background is applied to your page.
5454
When you want more customization than a pre-built theme offers, you can create your own theme file.
5555

5656
A custom theme file does two things:
57-
1. Imports the `mat-core()` sass mixin. This includes all common styles that are used by multiple
57+
1. Imports the `mat-core()` Sass mixin. This includes all common styles that are used by multiple
5858
components. **This should only be included once in your application.** If this mixin is included
5959
multiple times, your application will end up with multiple copies of these common styles.
6060
2. Defines a **theme** data structure as the composition of multiple palettes. This object can be
@@ -104,8 +104,8 @@ node-sass src/unicorn-app-theme.scss dist/unicorn-app-theme.css
104104
```
105105
and then include the output file in your index.html.
106106

107-
The theme file **should not** be imported into other SCSS files. This will cause duplicate styles
108-
to be written into your CSS output. If you want to consume the theme definition object
107+
Your custom theme file **should not** be imported into other SCSS files. This will duplicate styles
108+
in your CSS output. If you want to consume your theme definition object
109109
(e.g., `$candy-app-theme`) in other SCSS files, then the definition of the theme object should be
110110
broken into its own file, separate from the inclusion of the `mat-core` and
111111
`angular-material-theme` mixins.
@@ -214,8 +214,3 @@ $candy-app-theme: mat-light-theme($candy-app-primary, $candy-app-accent);
214214
### Theming your own components
215215
For more details about theming your own components,
216216
see [theming-your-components.md](./theming-your-components.md).
217-
218-
### Future work
219-
* Once CSS variables (custom properties) are available in all the browsers we support,
220-
we will explore how to take advantage of them to make theming even simpler.
221-
* More prebuilt themes will be added as development continues.

0 commit comments

Comments
 (0)