Skip to content

update theming docs to be more clear about how to structure your theme file to be usable across custom components #1786

Closed

Description

@fxck

If I took this code https://github.com/angular/material2/blob/master/docs/theming.md#defining-a-custom-theme and put it into _custom-theme.scss and included it in my main styles file and then imported it into my components like as shown here https://github.com/angular/material2/blob/master/docs/theming-your-components.md#using-colors-from-a-pallete

it would generate unnecessary(since they are already in the main file) classes, like elevation, inside my components's encapsulated css.. which is bad.

The way I see it, your _custom-theme.scss should import and contain something along these lines

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

$custom-color-primary: md-palette($md-blue);
$custom-color-accent:  md-palette($md-pink, A200, A100, A400);
$custom-color-warn:    md-palette($md-red);

$custom-theme: md-light-theme($color-primary, $color-accent, $color-warn);

the main style file should have

@import '@angular/material/core/theming/all-theme';

@include md-core();

@import './custom-theme';

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

thanks to this, you'd be able to import _custom-theme.scss anywhere and be able to use palette functions as well as your variables without it generating any duplicated material classes

Activity

  1. wbhob commented on Dec 23, 2016

    @wbhob

    This is fixed here: https://material.angular.io/guide/theming. This guide is really easy and I got started in 60 seconds.

    Happy holidays!

  2. dehypnosis commented on Feb 9, 2017

    @dehypnosis

    why builder say 'No mixin named md-core'?

  3. wbhob commented on Feb 9, 2017

    @wbhob
  4. dehypnosis commented on Feb 9, 2017

    @dehypnosis

    i am on latest of master branch, and it seemes that the prefix of names are changed from 'md' to 'mat'.
    is it right?

  5. dehypnosis commented on Feb 9, 2017

    @dehypnosis
    /* angular material theming */
    @import '~@angular/material/core/theming/all-theme';
    @include mat-core();
    
    $app-primary: mat-palette($mat-blue, A400, A100, A700);
    $app-accent:  mat-palette($mat-pink, A200, A100, A400);
    $app-warn:    mat-palette($mat-red);
    $app-theme: mat-light-theme($app-primary, $app-accent, $app-warn);
    @include angular-material-theme($app-theme);

    now it works!

  6. wbhob commented on Feb 9, 2017

    @wbhob
  7. dehypnosis commented on Feb 9, 2017

    @dehypnosis

    really? thank you for your help!

  8. wbhob commented on Feb 9, 2017

    @wbhob
  9. artworkad commented on Feb 14, 2017

    @artworkad

    For me mat-* does not work, but md-* does

  10. fxck commented on Feb 14, 2017

    @fxck
    ContributorAuthor

    That's because beta.1 still uses md-*, only the latest *-builds releases have mat-*.

  11. fxck commented on Feb 14, 2017

    @fxck
    ContributorAuthor

    Also @wbhob no, it's not "fixed", still if you took the code from https://material.angular.io/guide/theming and put it in _my-theme.scss and then tried importing it in your custom component(to be able to use the colors you just defined, or md-*()/mat-*() functions generally), I would generate the whole angular theme encapsulated for the said component, that's like 30kb of styles inside EACH of your components.

  12. wbhob commented on Feb 15, 2017

    @wbhob

    @fxck Yeah, we are all waiting for @angular to cut a new version. Their tests are passing, and they haven't released a new beta in TWO MONTHS. I, personally, am getting pretty furious at the lack of transparency.

  13. fxck commented on Feb 15, 2017

    @fxck
    ContributorAuthor

    Trust me, the situation is amazing compared to what it was couple of months ago. Also now you have the material2-builds repo, which helps a ton. #3075 (comment)

  14. ccheraa commented on Feb 16, 2017

    @ccheraa

    I updated to beta-2 and md-* is not working anymore, had to use mat-*.

  15. wbhob commented on Feb 16, 2017

    @wbhob

    That is correct and by design.

  16. 1 remaining item

  17. wbhob commented on Mar 7, 2017

    @wbhob

    The tilde ~ represents the path and doesn't actually work. You'll have to build in the absolute path (./node_modules/@angular/...). Also, I don't think remote origins are supported, so just include them in index.htnl.

    Finally, this is not the place for support requests, please post on stack overflow or elsewhere @sumnercreations

  18. jelbourn commented on Apr 9, 2017

    @jelbourn
    Contributor

    Closing this as it's drifted a bit off-topic.

    AFAIK the two guides on theming on the docs site should provide decent guidance on custom theming and for theming your own components. Feel free to open a new issue if there are any specific problems.

  19. fxck commented on Apr 10, 2017

    @fxck
    ContributorAuthor

    @jelbourn I still think it's misleading, wrong, and unnecessarily increasing people's bundle sizes, the specific problem I initially described still stands

    if I follow the directions of https://material.angular.io/guide/theming, that is:

    1. create my theme.scss, with this content
    @import '~@angular/material/theming';
    // Plus imports for other components in your app.
    
    // Include the base styles for Angular Material core. We include this here so that you only
    // have to load a single css file for Angular Material in your app.
    @include mat-core();
    
    // Define the palettes for your theme using the Material Design palettes available in palette.scss
    // (imported above). For each palette, you can optionally specify a default, lighter, and darker
    // hue.
    $candy-app-primary: mat-palette($mat-indigo);
    $candy-app-accent:  mat-palette($mat-pink, A200, A100, A400);
    
    // The warn palette is optional (defaults to red).
    $candy-app-warn:    mat-palette($mat-red);
    
    // Create the theme object (a Sass map containing all of the palettes).
    $candy-app-theme: mat-light-theme($candy-app-primary, $candy-app-accent, $candy-app-warn);
    
    // Include theme styles for core and each component used in your app.
    // Alternatively, you can import and @include the theme mixins for each component
    // that you are using.
    @include angular-material-theme($candy-app-theme);
    
    1. import it in my component's scss file with @import
    // or whatever is the path to my theme
    @import '../../assets/theme';

    I do not need to do literally anything else, just this single line is gonna generate all the general m2 styles (elevation, ripple, cdk stuff)

    image

    and this is gonna happen for every single component you import your theme in!!

    that's why it's important to separate

    @import '~@angular/material/theming';
    @include mat-core();
    $candy-app-primary: mat-palette($mat-indigo);
    $candy-app-accent:  mat-palette($mat-pink, 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);

    which is what you can safely import in any component, because it itself doesn't generate anything, from

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

    which is what you want to include only once in your root component

  20. wbhob commented on Apr 10, 2017

    @wbhob
  21. fxck commented on Apr 10, 2017

    @fxck
    ContributorAuthor

    No, that is not true.

    I have two empty components generated with ng-cli, FooComponent, BarComponent,

    import { Component, OnInit } from '@angular/core';
    
    @Component({
      selector: 'app-foo', // bar-foo
      templateUrl: './foo.component.html', // bar.component.html
      styleUrls: ['./foo.component.scss'] // bar.component.scss
    })
    export class FooComponent implements OnInit { // BarComponent
    
      constructor() { }
    
      ngOnInit() {
      }
    
    }

    *.component.scss only includes this line @import '../../assets/theme';

    theme.scss is c&p from theming guide

    @import '~@angular/material/theming';
    // Plus imports for other components in your app.
    
    // Include the base styles for Angular Material core. We include this here so that you only
    // have to load a single css file for Angular Material in your app.
    @include mat-core();
    
    // Define the palettes for your theme using the Material Design palettes available in palette.scss
    // (imported above). For each palette, you can optionally specify a default, lighter, and darker
    // hue.
    $candy-app-primary: mat-palette($mat-indigo);
    $candy-app-accent:  mat-palette($mat-pink, A200, A100, A400);
    
    // The warn palette is optional (defaults to red).
    $candy-app-warn:    mat-palette($mat-red);
    
    // Create the theme object (a Sass map containing all of the palettes).
    $candy-app-theme: mat-light-theme($candy-app-primary, $candy-app-accent, $candy-app-warn);
    
    // Include theme styles for core and each component used in your app.
    // Alternatively, you can import and @include the theme mixins for each component
    // that you are using.
    @include angular-material-theme($candy-app-theme);

    both are printed in the root component

    image

    since I've removed all other ng-cli styles (the default app.component.css and styles.css), there are only two <style> tags appended to head, for app-foo and for app-bar

    image

    this is content of the first one
    image

    this is content of the second one
    image

  22. wbhob commented on Apr 10, 2017

    @wbhob
  23. fxck commented on Apr 10, 2017

    @fxck
    ContributorAuthor

    ........... that's the whole point, you need to import your theme.scss, if you want to utilise ANY material2 sass helper functions, color palettes, your theme

    https://material.angular.io/guide/theming-your-components
    image

    if you do this, where @import 'src/unicorn-app-theme.scss' is this
    image

    you are gonna 28kb of unnecessary styles (after minification) included in your component

  24. fxck commented on Apr 10, 2017

    @fxck
    ContributorAuthor

    the guide should say that you should define your theme in theme.scss, but instead doing @include angular-material-theme($candy-app-theme); in the same file, your should import this theme.scss in your main/global style and call the angular-material-theme function there, only then you can safely import your theme.scss in any component without it generating duplicated files

  25. wbhob commented on Apr 10, 2017

    @wbhob
  26. wbhob commented on Apr 10, 2017

    @wbhob
  27. fxck commented on Apr 10, 2017

    @fxck
    ContributorAuthor

    I don't understand why are you commenting if you don't understand. :)

    If you use webpack (or ng-cli), there's no such thing as globally available sass functions and/or variables, you HAVE to @import them in every single component you need them:

    webpack/sass-loader#218
    webpack/sass-loader#211

    ..and million other issues.

    The only thing you can use, are the already generated css classes (ie. elevation), if they are either in your head tag, or in your root component with disabled encapsulation. But that still only allows you to do:

    <div class="mat-elevation-z1"></div>

    and not:

    <div class="foo"></div>
    .foo {
      @include mat-elevation(1);
    }
  28. codercatdev commented on May 7, 2018

    @codercatdev

    @fxck thank you so much!!! Same functionality
    Old
    image
    New
    image

  29. angular-automatic-lock-bot commented on Sep 8, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  30. locked and limited conversation to collaborators on Sep 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions