components: Theming your own component doc incorrectly references pre-built theme
Bug, feature request, or proposal:
Bug/Incorrect Documentation
What is the expected behavior?
According to the theming your components documentation you should be able to use pre-built themes with your custom components
// Import a pre-built theme
@import '~@angular/material/prebuilt-themes/deeppurple-amber.css';
// 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
@include candy-carousel-theme($theme);
What is the current behavior?
The Sass compiler throws an error : Undefined variable: "$theme"
What are the steps to reproduce?
Componet Theme scss:
@import '~@angular/material/theming';
@mixin my-component-theme($theme) {
$primary: map-get($theme, primary);
my-component{
background-color: mat-color($primary);
}
}
Styles.scss
@import '~@angular/material/prebuilt-themes/indigo-pink.css';
@import './path/to/my-component.theme';
@include my-component-theme($theme);
What is the use-case or motivation for changing an existing behavior?
While this isn’t as much of a problem with components that are defined in an application as you can define your own theme, it IS a major issue when creating component libraries that will be consumed by other applications that may choose to use a pre-built theme.
Which versions of Angular, Material, OS, TypeScript, browsers are affected?
Angular: 4.1.3 Material: Beta 6
Is there anything else we should know?
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 7
- Comments: 23 (8 by maintainers)
Thank you all to the contributions to this thread, after fighting with the docs this was a very useful resource. I would appreciate confirmation that my understanding of the approach is correct, and specifically does not import the mat-core and angular-material-theme multiple times.
theme_variables.scss
theme.scss
styles.scss
nav.component.scss
.angular-cli.json
I suppose my concern is that the nav component imports the theme_variables which imports angular/material/theming but I cannot see how to accomplish without.
I must agree that the docs behind theming custom components are pretty confusing.
I want to create a custom component (much like angular button etc.) which would work with the current theme’s
$primary
&$accent
colors. This component would be packaged and then consumed by target app (so the component should not have a theme, but use the theme of target app).So what is the correct way to do this?
This is in addition to @michael-gregson’s solution above, specifically so that we do not have to import
theme_variables.scss
from the custom component’s theme scss file.nav.component.theme.scss
), typically in the same location as your custom component.theme.scss
.nav.component.theme.scss
is NOT declared as the custom component’sstyleUrls
. That was not immediately obvious to me when learning to theme custom components.nav.component.theme.scss
theme.scss:
I would also like to feedback to Angular Material team that the current theming your custom components page is causing more harm than good to those trying to theme custom components.
How about just remove the page until it has been updated to actually work?
my solution just do not use pre-build theme, but we can customize one like it you can find all color in ~@angular/material/_theming.scss
my example:
src\theme_variables.scss
src\theme.scss
src\style.css it’s nothing,it’s empty ,don’t import ~@angular/material/prebuilt-themes/deeppurple-amber.css
src\app\file-card\file-card.component.scss
src\app\file-card\file-card.component.html
<div class="primary"> some text </div>
.angular-cli.json@Parent5446 I don’t know if it’s the correct procedure, but the cleaner way I have found is to create some color variables in theme.scss using mat-color() function. This way I’m able to use the colors through all the project without problems:
Thanks to @willshowell for point me out that importing directly the theme.scss would include
mat-core
andangular-material-theme
in every importing .scss file. As he says, the way to go is create a separate file for this global vars.@e11en As the error says,
$primary
is a map and not a color. You must to usemat-color
function included in angular/material/theming.I recommend you to browse the material components to learn how to use that variables properly. E.g: https://github.com/angular/material2/blob/master/src/lib/button/_button-theme.scss
@michael-gregson you’re good!