Skip to content

Commit 379a57f

Browse files
authored
docs(theming): add initial guide for theming. (#1246)
1 parent e0726e1 commit 379a57f

File tree

2 files changed

+127
-45
lines changed

2 files changed

+127
-45
lines changed

docs/scss-theming.md

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

docs/theming.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# Theming your Angular Material app
2+
3+
4+
### What is a theme?
5+
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].
7+
8+
In Angular Material, a theme is created by composing multiple palettes. In particular,
9+
a theme consists of:
10+
* A primary palette: colors most widely used across all screens and components.
11+
* An accent palette: colors used for the floating action button and interactive elements.
12+
* A warn palette: colors used to convey error state.
13+
* A foreground palette: colors for text and icons.
14+
* A background palette: colors used for element backgrounds.
15+
16+
In Angular Material 2, all theme styles are generated _statically_ at build-time so that your
17+
app doesn't have to spend cycles generating theme styles on startup.
18+
19+
[1]: https://material.google.com/style/color.html#color-color-palette
20+
21+
### Using a pre-built theme
22+
Angular Material comes prepackaged with several pre-built theme css files. These theme files also
23+
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.
25+
26+
You can include a theme file directly into your application from
27+
`@angular2-material/core/theming/prebuilt`
28+
29+
If you're using Angular CLI, this is as simple as including one line
30+
in your `style.css` file:
31+
```css
32+
@import '~@angular2-material/core/theming/prebuilt/deeppurple-amber.css';
33+
```
34+
35+
Alternatively, you can just reference the file directly. This would look something like
36+
```html
37+
<link href="node_modules/@angular2-material/core/theming/prebuilt/indigo-pink.css" rel="stylesheet">
38+
```
39+
The actual path will depend on your server setup.
40+
41+
You can also concatenate the file with the rest of your application's css.
42+
43+
### Defining a custom theme
44+
When you want more customization than a pre-built theme offers, you can create your own theme file.
45+
46+
A theme file is a simple Sass file that defines your palettes and passes them to mixins that output
47+
the corresponding styles. A typical theme file will look something like this:
48+
```scss
49+
@import '~@angular2-material/core/theming/theming';
50+
@import '~@angular2-material/core/theming/palette';
51+
@import '~@angular2-material/core/core';
52+
@import '~@angular2-material/button/button-theme';
53+
// Plus imports for other components in your app.
54+
55+
// Include the base styles for Angular Material core. We include this here so that you only
56+
// have to load a single css file for Angular Material in your app.
57+
@include md-core();
58+
59+
// Define the palettes for your theme using the Material Design palettes available in palette.scss
60+
// (imported above). For each palette, you can optionally specify a default, lighter, and darker
61+
// hue.
62+
$primary: md-palette($md-indigo);
63+
$accent: md-palette($md-pink, A200, A100, A400);
64+
$warn: md-palette($md-red);
65+
66+
// Create the theme object (a Sass map containing all of the palettes).
67+
$theme: md-light-theme($primary, $accent, $warn);
68+
69+
// Include theme styles for core and each component used in your app.
70+
@include md-core-theme($theme);
71+
@include md-button-theme($theme);
72+
// Plus includes for other components in your app.
73+
```
74+
75+
You only need this single Sass file; you do not need to use Sass to style the rest of your app.
76+
77+
If you are using the Angular CLI, support for compiling Sass to css is built-in; you only have to
78+
add a new entry to the `"styles"` list in `angular-cli.json` pointing to the theme
79+
file (e.g., `unicorn-app-theme.scss`).
80+
81+
If you're not using the Angular CLI, you can use any existing Sass tooling to build the file (such
82+
as gulp-sass or grunt-sass). The simplest approach is to use the `node-sass` CLI; you simply run:
83+
```
84+
node-sass src/unicorn-app-theme.scss dist/unicorn-app-theme.css
85+
```
86+
and then include the output file in your application.
87+
88+
The theme file can be concatenated and minified with the rest of the application's css.
89+
90+
#### Multiple themes
91+
You can extend the example above to define a second (or third or fourth) theme that is gated by
92+
some selector. For example, we could append the following to the example above to define a
93+
secondary dark theme:
94+
```scss
95+
.unicorn-dark-theme {
96+
$dark-primary: md-palette($md-blue-grey);
97+
$dark-accent: md-palette($md-amber, A200, A100, A400);
98+
$dark-warn: md-palette($md-deep-orange);
99+
100+
$dark-theme: md-dark-theme($dark-primary, $dark-accent, $dark-warn);
101+
102+
@include md-core-theme($dark-theme);
103+
@include md-button-theme($dark-theme);
104+
}
105+
```
106+
107+
With this, any element inside of a parent with the `unicorn-dark-theme` class will use this
108+
dark theme.
109+
110+
### Styling your own components
111+
In order to style your own components with our tooling, the component's styles must be defined
112+
with Sass.
113+
114+
You can consume the theming functions and variables from the `@angular2-material/core/theming`.
115+
You can use the `md-color` function to extract a specific color from a palette. For example:
116+
```scss
117+
.unicorn-carousel {
118+
background-color: md-color($primary);
119+
color: md-color($primary, default-contrast);
120+
}
121+
```
122+
123+
### Future work
124+
* When the "all" package is released, there will be a mixin that captures all of the component's
125+
theme styles so that you don't have to include them all individually.
126+
* Once CSS variables (custom properties) are available in all the browsers we support,
127+
we will explore how to take advantage of them to make theming even simpler.

0 commit comments

Comments
 (0)