Angular Material - Global color variables

前端 未结 3 1086
温柔的废话
温柔的废话 2020-12-30 07:01

Looking at the Angular Material documentation, they recommend using a -theme file per component to manage applying any theme-related styles to a specific class.

3条回答
  •  抹茶落季
    2020-12-30 07:06

    I am working on a project where I used the Material 2 Themes and I used this approach where I use the class name and add colors class globally.

    This is what I did :

    FileName: mytheme-sidemenu.scss:

    // Import all the tools needed to customize the theme and extract parts of it
    @import "~@angular/material/theming";
    // Define a mixin that accepts a theme and outputs the color styles for the component.
    @mixin mytheme-sidemenu($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.
    
      .col-primary {
        color: mat-color($primary, 500) !important;
      }
      .col-accent {
        color: mat-color($accent, 300) !important;
      }
    }
    

    Here is my main theme file: mytheme-theme.scss:

    @import '~@angular/material/theming';
    @import './variables/helper.scss';
    @import './variables/spacemanager.scss';
    @import './mytheme-sidemenu.scss';
    
    // Primary theme
    @include mat-core();
    $mytheme-app-primary: mat-palette($mat-light-blue, 700, 600);
    $mytheme-app-accent: mat-palette($mat-pink, A200, 900, A100);
    $mytheme-app-warn: mat-palette($mat-deep-orange);
    $mytheme-app-theme: mat-light-theme($mytheme-app-primary, $mytheme-app-accent, $mytheme-app-warn);
    @include angular-material-theme($mytheme-app-theme);
    // Secondary Theme
    .mytheme-alt-theme {
        $mytheme-alt-primary: mat-palette($mat-blue-grey, 500);
        $mytheme-alt-accent: mat-palette($mat-pink, 500);
        $mytheme-alt-warn: mat-palette($mat-deep-orange);
        $mytheme-alt-theme: mat-light-theme($mytheme-alt-primary, $mytheme-alt-accent, $mytheme-alt-warn);
        @include angular-material-theme($mytheme-alt-theme);
    }
    // Using the $theme variable from the pre-built theme you can call the theming function
    @include mytheme-sidemenu($mytheme-app-theme);
    

    and in app.module.ts update this :

    export class AppModule {
      constructor(
        @Inject(OverlayContainer) private overlayContainer: OverlayContainer
      ) {
        this.overlayContainer
          .getContainerElement()
          .classList.add("mytheme-alt-theme"); // this for double theme add to the root css class
      }
    }
    

提交回复
热议问题