Skip to content

Commit bdc78ad

Browse files
Bruno Joaobrunonp
authored andcommitted
Cleaned up and changed as @jelbourn suggested
1 parent e3f5ff1 commit bdc78ad

File tree

2 files changed

+55
-62
lines changed

2 files changed

+55
-62
lines changed

docs/theming-your-components.md

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,16 @@
11
#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.
23

3-
In order to style your own components with our tooling, the component's styles must be defined with Sass.
4+
## Using `@mixin` to automatically apply a theme
45

5-
You can consume the theming functions from the `@angular/material/core/theming/all-theme` and theming variables from a pre-built theme or a custom one. You can use the `md-color` function to extract a specific color from a palette.
6-
For example:
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 updated automatically.
8+
Calling it with a different theme argument allow multiple themes within the app or component.
79

8-
app/candy-carousel/candy-carousel-theme.scss
10+
### How to use `@mixin`
11+
We can better theming our custom components adding a `@mixin` function to its theme file and then calling this function to apply a theme.
912

10-
```scss
11-
// Import theming functions and variables
12-
@import '~@angular/material/core/theming/all-theme';
13-
// Import a pre-built theme
14-
@import '~@angular/material/core/theming/prebuilt/deep-purple-amber';
15-
16-
// Use md-color to extract individual colors from a palette as necessary.
17-
.candy-carousel {
18-
background-color: md-color($primary);
19-
border-color: md-color($accent, A400);
20-
}
21-
```
22-
23-
## Using @mixin to automatically apply a theme
24-
We can better theming our custom components adding a @mixin function to its theme file and then calling this function to apply a theme.
25-
26-
All you need is to create a @mixin function in the custom-component-theme.scss
13+
All you need is to create a `@mixin` function in the custom-component-theme.scss
2714

2815
```sass
2916
// Import all the tools needed to customize the theme and extract parts of it
@@ -42,15 +29,41 @@ All you need is to create a @mixin function in the custom-component-theme.scss
4229
}
4330
}
4431
```
45-
46-
Now you just have have to call the @mixin function to apply the theme:
32+
Now you just have have to call the `@mixin` function to apply the theme:
4733

4834
```sass
4935
// Import a pre-built theme
5036
@import '~@angular/material/core/theming/prebuilt/deep-purple-amber';
5137
// Import your custom input theme file so you can call the custom-input-theme function
5238
@import 'app/candy-carousel/candy-carousel-theme.scss';
5339
54-
//Using the $theme variable from the pre-built theme you can call the theming function
40+
// Using the $theme variable from the pre-built theme you can call the theming function
5541
@include candy-carousel-theme($theme);
5642
```
43+
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).
46+
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.
49+
50+
All styles that are not affected by the theme should be placed in a `component.scss` file. This file should contain sizes, transitions, positioning and everything that is not affected by the theme.
51+
52+
Styles that are affected by the theme should be placed in a separated theming file as `component-theme.scss`. This file should contain the `@mixin` function responsible for applying the theme to the component.
53+
54+
55+
## Using colors from a pallete
56+
You can consume the theming functions from the `@angular/material/core/theming/theming` and Material pallete vars from `@angular/material/core/theming/palette`. You can use the `md-color` function to extract a specific color from a palette. For example:
57+
58+
```scss
59+
// Import theming functions
60+
@import '~@angular/material/core/theming/theming';
61+
// Import your custom theme
62+
@import 'src/unicorn-app-theme.scss';
63+
64+
// Use md-color to extract individual colors from a palette as necessary.
65+
.candy-carousel {
66+
background-color: md-color($candy-app-primary);
67+
border-color: md-color($candy-app-accent, A400);
68+
}
69+
```

docs/theming.md

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
### What is a theme?
55
A **theme** is the set of colors that will be applied to the Angular Material components. The
6-
library's approach to theming is based on the guidance from the [Material Design spec][1].
6+
library's approach to theming is based on the guidance from the [Material Design spec][1].
77

8-
In Angular Material, a theme is created by composing multiple palettes. In particular,
8+
In Angular Material, a theme is created by composing multiple palettes. In particular,
99
a theme consists of:
1010
* A primary palette: colors most widely used across all screens and components.
1111
* An accent palette: colors used for the floating action button and interactive elements.
@@ -21,9 +21,9 @@ app doesn't have to spend cycles generating theme styles on startup.
2121
### Using a pre-built theme
2222
Angular Material comes prepackaged with several pre-built theme css files. These theme files also
2323
include all of the styles for core (styles common to all components), so you only have to include a
24-
single css file for Angular Material in your app.
24+
single css file for Angular Material in your app.
2525

26-
You can include a theme file directly into your application from
26+
You can include a theme file directly into your application from
2727
`@angular/material/core/theming/prebuilt`
2828

2929
If you're using Angular CLI, this is as simple as including one line
@@ -35,8 +35,8 @@ in your `style.css` file:
3535
Alternatively, you can just reference the file directly. This would look something like
3636
```html
3737
<link href="node_modules/@angular/material/core/theming/prebuilt/indigo-pink.css" rel="stylesheet">
38-
```
39-
The actual path will depend on your server setup.
38+
```
39+
The actual path will depend on your server setup.
4040

4141
You can also concatenate the file with the rest of your application's css.
4242

@@ -56,25 +56,25 @@ the corresponding styles. A typical theme file will look something like this:
5656
// Define the palettes for your theme using the Material Design palettes available in palette.scss
5757
// (imported above). For each palette, you can optionally specify a default, lighter, and darker
5858
// hue.
59-
$primary: md-palette($md-indigo);
60-
$accent: md-palette($md-pink, A200, A100, A400);
59+
$candy-app-primary: md-palette($md-indigo);
60+
$candy-app-accent: md-palette($md-pink, A200, A100, A400);
6161

6262
// The warn palette is optional (defaults to red).
63-
$warn: md-palette($md-red);
63+
$candy-app-warn: md-palette($md-red);
6464

6565
// Create the theme object (a Sass map containing all of the palettes).
66-
$theme: md-light-theme($primary, $accent, $warn);
66+
$candy-app-theme: md-light-theme($candy-app-primary, $candy-app-accent, $candy-app-warn);
6767

6868
// Include theme styles for core and each component used in your app.
6969
// Alternatively, you can import and @include the theme mixins for each component
7070
// that you are using.
71-
@include angular-material-theme($theme);
71+
@include angular-material-theme($candy-app-theme);
7272
```
7373

7474
You only need this single Sass file; you do not need to use Sass to style the rest of your app.
7575

76-
If you are using the Angular CLI, support for compiling Sass to css is built-in; you only have to
77-
add a new entry to the `"styles"` list in `angular-cli.json` pointing to the theme
76+
If you are using the Angular CLI, support for compiling Sass to css is built-in; you only have to
77+
add a new entry to the `"styles"` list in `angular-cli.json` pointing to the theme
7878
file (e.g., `unicorn-app-theme.scss`).
7979

8080
If you're not using the Angular CLI, you can use any existing Sass tooling to build the file (such
@@ -87,8 +87,8 @@ and then include the output file in your application.
8787
The theme file can be concatenated and minified with the rest of the application's css.
8888

8989
#### Multiple themes
90-
You can extend the example above to define a second (or third or fourth) theme that is gated by
91-
some selector. For example, we could append the following to the example above to define a
90+
You can extend the example above to define a second (or third or fourth) theme that is gated by
91+
some selector. For example, we could append the following to the example above to define a
9292
secondary dark theme:
9393
```scss
9494
.unicorn-dark-theme {
@@ -97,36 +97,16 @@ secondary dark theme:
9797
$dark-warn: md-palette($md-deep-orange);
9898

9999
$dark-theme: md-dark-theme($dark-primary, $dark-accent, $dark-warn);
100-
100+
101101
@include angular-material-theme($dark-theme);
102102
}
103103
```
104104

105105
With this, any element inside of a parent with the `unicorn-dark-theme` class will use this
106106
dark theme.
107107

108-
### Styling your own components
109-
In order to style your own components with our tooling, the component's styles must be defined
110-
with Sass.
111-
112-
You can consume the theming functions and variables from the @angular/material/core/theming. You can use the `md-color` function to extract a specific color from a palette. For example:
113-
114-
```scss
115-
// Import theming functions and variables
116-
@import '~@angular/material/core/theming/all-theme';
117-
// Import a pre-built theme
118-
@import '~@angular/material/core/theming/prebuilt/deep-purple-amber';
119-
120-
// Use md-color to extract individual colors from a palette as necessary.
121-
.candy-carousel {
122-
background-color: md-color($primary);
123-
border-color: md-color($accent, A400);
124-
}
125-
```
126-
127-
You can see a more complete example in [theming-your-components.md][2]
128-
129-
[2]: https://github.com/angular/material2/blob/master/docs/theming-your-components.md
108+
### Theming your own components
109+
For more details about theming your own components, see [theming-your-components.md](https://github.com/angular/material2/blob/master/docs/theming-your-components.md)
130110

131111
### Future work
132112
* Once CSS variables (custom properties) are available in all the browsers we support,

0 commit comments

Comments
 (0)