Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing primary color which is defined custom theme inside component's scss

Tags:

angular

I have defined a custom theme in angular 4 and want to use this in one of my component's scss file

Basically I want background of specific mat-grid-tile to be primary color

Below is my custom theme

@import '~@angular/material/theming';
@include mat-core();
$candy-app-primary: mat-palette($mat-red);
$candy-app-accent:  mat-palette($mat-orange, A200, A100, A400);
$candy-app-warn:    mat-palette($mat-red);
$candy-app-theme: mat-light-theme($candy-app-primary, $candy-app-accent, $candy-app-warn);
@include angular-material-theme($candy-app-theme);

Below is code of my component's scss where i am trying to access this

@import '/src/styles.css';


:host {             // host targets selector: 'home'

        display: block;
        left: 0;

        width: 100%;  


        height: 100%;
}

.selected {
    background: map-get($candy-app-theme,primary);
    color:"white";
}

Compilation is failing static error that variable "$candy-app-theme" is not defined, but it is defined in styles.scss

I have just started Angular 4, hence i'm very unclear about it. Please let me know if any further code is required

Note: path from /src/styles.scss and '../../../styles.scss' both resolves correctly but none of them resolves variable

like image 288
Kumar Gaurav Avatar asked Oct 08 '17 14:10

Kumar Gaurav


People also ask

How would you include a prebuilt theme in angular application without using angular CLI?

You can find the pre-built theme files in the "prebuilt-themes" directory of Angular Material's npm package ( @angular/material/prebuilt-themes ). To include the pre-built theme in your application, add your chosen CSS file to the styles array of your project's angular.json file.

How do I use angular color materials?

To read color values from a theme, you can use the get-color-config Sass function. This function returns a Sass map containing the theme's primary, accent, and warn palettes, as well as a flag indicating whether dark mode is set. @use 'sass:map'; @use '@angular/material' as mat; $color-config: mat.

Can I change angular material theme?

it's very easy to create a custom angular material theme. This will allow you to specify primary, accent and warning colors that will be used on Angular Material components. Your custom theme will be a Sass file and in our case, we'll call it theme. scss and place it in our app's /src folder.

Which of the following is not a default theme in angular material?

Angular doesn't have a default theme.


1 Answers

Edit : in fact, this is not the recommended way...

Instead of importing your theme in your components, you should use mixins as described in the documentation : https://material.angular.io/guide/theming-your-components

Original answer :

First, defines your custom theme in a separate file (recommended) :

Be careful to define your theme in a less or sass/scss file and not just simple css file :

theme.scss

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

@include mat-core();

$primary: mat-palette($mat-indigo);
$accent:  mat-palette($mat-yellow);
$warn:    mat-palette($mat-red);

$theme: mat-light-theme($primary, $accent, $warn);

@include angular-material-theme($theme);

Then, in the file where you want using your custom theme :

  1. Import the theme file :

@import '../../../theme.scss';

  1. You get the primary palette like that :

$primary: map-get($theme, primary);

  1. Then, you can use color like this :

background: mat-color($primary);

like image 191
eroak Avatar answered Oct 03 '22 21:10

eroak