Skip to content

(docs) update theming.md #1385

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 16 commits into from
Oct 18, 2016
56 changes: 56 additions & 0 deletions docs/theming-your-components.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#Theming your custom components

In order to style your own components with our tooling, the component's styles must be defined with Sass.
Copy link
Member

Choose a reason for hiding this comment

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

Let's change "our tooling" to "Angular Material's tooling"


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.
Copy link
Member

Choose a reason for hiding this comment

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

Wrap lines at 100 columns.

Copy link
Member

@jelbourn jelbourn Oct 5, 2016

Choose a reason for hiding this comment

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

The imports for theming should be
@angular/material/core/theming/theming for functions and
@angular/material/core/theming/palette for Material palette vars

I'd like to also link to the _theming.scss source file with something like

For more details about the theming functions, see the comments in the
[source](https://github.com/angular/material2/blob/master/src/lib/core/theming/_theming.scss).

For example:

app/candy-carousel/candy-carousel-theme.scss

```scss
// Import theming functions and variables
@import '~@angular/material/core/theming/all-theme';
// Import a pre-built theme
@import '~@angular/material/core/theming/prebuilt/deep-purple-amber';
Copy link
Member

@jelbourn jelbourn Oct 5, 2016

Choose a reason for hiding this comment

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

You shouldn't ever import a pre-built theme; those are meant to be leaf nodes in the sass build. Instead you could mention importing the theme variables from your own app's theme definition with something like $candy-app-primary, etc.


// Use md-color to extract individual colors from a palette as necessary.
.candy-carousel {
background-color: md-color($primary);
border-color: md-color($accent, A400);
}
Copy link
Member

Choose a reason for hiding this comment

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

I would move the mixin approach above this example. I want to encourage people to use the mixin approach in order to make it easier to switch between themes in their app.

```

## Using @mixin to automatically apply a theme
Copy link
Member

@jelbourn jelbourn Oct 5, 2016

Choose a reason for hiding this comment

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

@mixin
(here and elsewhere)

We can better theming our custom components adding a @mixin function to its theme file and then calling this function to apply a theme.

All you need is to create a @mixin function in the custom-component-theme.scss

```sass
// Import all the tools needed to customize the theme and extract parts of it
@import '~@angular/material/core/theming/all-theme';

// Define a mixin that accepts a theme and outputs the color styles for the component.
@mixin candy-carousel-theme($theme) {
// Extract whichever individual palettes you need from the theme.
$primary: map-get($theme, primary);
$accent: map-get($theme, accent);

// Use md-color to extract individual colors from a palette as necessary.
.candy-carousel {
background-color: md-color($primary);
border-color: md-color($accent, A400);
}
}
```

Now you just have have to call the @mixin function to apply the theme:
Copy link
Member

Choose a reason for hiding this comment

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

It would be good to explain somewhere in this doc the advantage of using a mixin (calling it with a different theme argument to allow multiple themes within the app).

We should also mention that, when doing this, the theme file should only contain the definitions that are affected by the passed-in theme.


```sass
// Import a pre-built theme
@import '~@angular/material/core/theming/prebuilt/deep-purple-amber';
// Import your custom input theme file so you can call the custom-input-theme function
@import 'app/candy-carousel/candy-carousel-theme.scss';

//Using the $theme variable from the pre-built theme you can call the theming function
Copy link
Member

Choose a reason for hiding this comment

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

Missing space between // and Using

@include candy-carousel-theme($theme);
```
20 changes: 15 additions & 5 deletions docs/theming.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ secondary dark theme:

$dark-theme: md-dark-theme($dark-primary, $dark-accent, $dark-warn);

@include angular-material-theme($dark-theme);
@include angular-material-theme($dark-theme);
}
```

Expand All @@ -109,15 +109,25 @@ dark theme.
In order to style your own components with our tooling, the component's styles must be defined
with Sass.

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:
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:
Copy link
Member

Choose a reason for hiding this comment

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

Rather than making this document much longer, I'd rather create another file theming-your-components.md that covers how to theme your own components with the material tools.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jelbourn Ok, I'll do this! But should I use your tiny as possible example or can we provide the example with all that information to theming custom form components?

Copy link
Member

Choose a reason for hiding this comment

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

I still want to use as small of an example as possible that illustrates the point. Right now, there's just way too much here that distracts from the theming bits.

Copy link
Member

Choose a reason for hiding this comment

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

I would just remove all of the content in this section now and point to the new doc.


```scss
.unicorn-carousel {
// Import theming functions and variables
@import '~@angular/material/core/theming/all-theme';
// Import a pre-built theme
@import '~@angular/material/core/theming/prebuilt/deep-purple-amber';

// Use md-color to extract individual colors from a palette as necessary.
.candy-carousel {
background-color: md-color($primary);
color: md-color($primary, default-contrast);
border-color: md-color($accent, A400);
}
```

You can see a more complete example in [theming-your-components.md][2]

[2]: https://github.com/angular/material2/blob/master/docs/theming-your-components.md

### Future work
* Once CSS variables (custom properties) are available in all the browsers we support,
we will explore how to take advantage of them to make theming even simpler.