ipsos-components
Version:
Material Design components for Angular
71 lines (53 loc) • 3.17 kB
Markdown
# Theming your custom components
In order to style your own components with Angular Material's tooling, the component's styles must be defined with Sass.
### Using `` to automatically apply a theme
#### Why using ``
The advantage of using a `` function is that when you change your theme, every file that uses it will be automatically updated.
Calling the `` with a different theme argument allows multiple themes within the app or component.
#### How to use ``
We can better theme our custom components by adding a `` function to its theme file, and then call this function to apply a theme.
All you need is to create a `` function in the custom-component-theme.scss
```scss
// Import all the tools needed to customize the theme and extract parts of it
'~/material/theming';
// Define a mixin that accepts a theme and outputs the color styles for the component.
candy-carousel-theme($theme) {
// Extract whichever individual palettes you need from the theme.
$primary: map-get($theme, primary);
$accent: map-get($theme, accent);
// Use mat-color to extract individual colors from a palette as necessary.
.candy-carousel {
background-color: mat-color($primary);
border-color: mat-color($accent, A400);
}
}
```
Now you just have to call the `` function to apply the theme:
```scss
// Import a pre-built theme
'~/material/prebuilt-themes/deeppurple-amber.css';
// Import your custom input theme file so you can call the custom-input-theme function
'app/candy-carousel/candy-carousel-theme.scss';
// Using the $theme variable from the pre-built theme you can call the theming function
candy-carousel-theme($theme);
```
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).
#### Best practices using ``
When using ``, the theme file should only contain the definitions that are affected by the passed-in theme.
All styles that are not affected by the theme should be placed in a `candy-carousel.scss` file. This file should contain everything that is not affected by the theme like sizes, transitions...
Styles that are affected by the theme should be placed in a separated theming file as `_candy-carousel-theme.scss` and the file should have a `_` before the name. This file should contain the `` function responsible for applying the theme to the component.
### Using colors from a palette
You can consume the theming functions and Material palette variables from `/material/theming`.
You can use the `mat-color` function to extract a specific color from a palette. For example:
```scss
// Import theming functions
'~/material/theming';
// Import your custom theme
'src/unicorn-app-theme.scss';
// Use mat-color to extract individual colors from a palette as necessary.
.candy-carousel {
background-color: mat-color($candy-app-primary);
border-color: mat-color($candy-app-accent, A400);
}
```