question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Theming your own component doc incorrectly references pre-built theme

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:7
  • Comments:23 (8 by maintainers)

github_iconTop GitHub Comments

17reactions
michael-gregsoncommented, Sep 1, 2017

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

@import '~@angular/material/theming';

// Define the palettes for your theme using the Material Design palettes available in palette.scss
$app-primary: mat-palette($mat-blue-grey);
$app-accent:  mat-palette($mat-grey);
$app-warn:    mat-palette($mat-deep-orange);

// Custom Sass colors vars (will be available in all the project)
$primary: mat-color($app-primary);
$accent: mat-color($app-accent);
$warn: mat-color($app-warn);

theme.scss

@import '~@angular/material/theming';
@import "~theme_variables.scss";

@include mat-core();
$app-theme: mat-light-theme($app-primary, $app-accent, $app-warn);
@include angular-material-theme($app-theme);

styles.scss

@import "theme.scss";

...

nav.component.scss

@import "~theme_variables.scss";

.primary {
    color: $primary;
}

.angular-cli.json

"styles": [
   "styles.scss"
]

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.

11reactions
bezysoftwarecommented, Apr 18, 2018

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 13: Angular Material 13 custom theme throwing error
It seems that material needs at least hue: 100, hue 500 and hue 700 (probably for functions like lighter etc). I had the...
Read more >
Theming your own components - Angular Material
Theme your own components with Angular Material's theming system. You can use Angular Material's Sass-based theming system for your own custom components.
Read more >
Angular Material Theming System - Complete Guide
In this article, we will learn about custom themes, modifying typography and much more ... and we will apply custom theme to Angular...
Read more >
Theme your angular application with angular material library
A theme is the set of colors that will be applied to the Angular Material components. In Angular Material, a theme is created...
Read more >
How to make a custom theme in Angular Material
Every single color reference needs a separate .theme.scss file. · custom-theme.scss must know exactly which components provide custom themes.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found