简体   繁体   English

如何将材质主题颜色用于自定义内容?

[英]How can I use Material Theme colors for custom things?

I'm trying to use a specific (BACKGROUND) color from my theme to make other elements that color 我正在尝试使用我的主题中的特定(背景)颜色来制作其他颜色的元素

What is the designed way to do that? 设计方法是什么? Is there a designed way to do this? 有没有设计的方法来做到这一点?

For example, Is there a way to say this custom element should use the current theme's -> background -> disabled color? 例如,有没有办法说这个自定义元素应该使用当前主题的 - >背景 - >禁用颜色? etc 等等

I would love to be able to say, for example, this specific element should be whatever color the current theme's -> background palette's -> disabled-list-option. 我希望能够说,例如,这个特定的元素应该是当前主题的所有颜色 - >背景调色板 - > disabled-list-option。

Thanks!! 谢谢!!

在此输入图像描述

I searched for reuse same color scss and came across this post , which I highly recommend so you can understand reusing values throughout your SCSS files, in order to make them more maintainable. 我搜索了重复使用相同颜色的scss并且遇到了这篇文章 ,我强烈推荐这样你可以理解整个SCSS文件中的重用值,以使它们更易于维护。

To sum it up, you can define SCSS variables by defining something like this: 总结一下,您可以通过定义如下内容来定义SCSS变量:

$important-color: rgb(1, 2, 3);

And then reuse it like so: 然后重复使用它:

class-1 {
    background-color: $important-color;
}

...

class-2 > span {
    color: $important-color;
}

So what you should probably do is to define one of these constants in a SCSS file higher up in the hierarchy (such as app.scss, for instance, if this is an application-wide variable) and use this variable in your components. 因此,您应该做的是在层次结构中较高的SCSS文件中定义其中一个常量(例如app.scss,如果这是一个应用程序范围的变量)并在组件中使用此变量。 Please, make sure to read Aloke's answer as well for a more in-depth analysis. 请务必阅读Aloke的答案,以便进行更深入的分析。

Always use the search, and be sure to include your code when asking code-related questions (use websites such as JSFiddle to make things easier). 始终使用搜索,并确保在询问与代码相关的问题时包含您的代码(使用JSFiddle等网站来简化操作)。

I think you want a .scss file where you put all your custom color variables and use them to generic CSSclass to apply all over your apps. 我想你想要一个.scss文件,你可以在其中放置所有自定义颜色变量,并将它们用于通用CSSclass以应用所有应用程序。

Then you need to do this things. 然后你需要做这件事。

  1. Uses a theme.scss file which holds current material theme/ dynamic themes scss 使用theme.scss文件,其中包含当前材质主题/动态主题scss

@import '~@angular/material/theming';
@import './mytheme-sidemenu.scss';

// Primary theme
@include mat-core();
$mytheme-app-primary: mat-palette($mat-deep-purple, 700, 600);
$mytheme-app-accent: mat-palette($mat-red, 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-teal, 500);
    $mytheme-alt-accent: mat-palette($mat-orange, 500);
    $mytheme-alt-warn: mat-palette($mat-red);
    $mytheme-alt-theme: mat-light-theme($mytheme-alt-primary, $mytheme-alt-accent, $mytheme-alt-warn);
    @include angular-material-theme($mytheme-alt-theme);
}

.mytheme-another-alt-theme{
   $mytheme-another-alt-primary: mat-palette($mat-blue, 500);
    $mytheme-another-alt-accent: mat-palette($mat-yellow, 500);
    $mytheme-another-alt-warn: mat-palette($mat-green);
    $mytheme-another-alt-theme: mat-light-theme($mytheme-another-alt-primary, $mytheme-another-alt-accent, $mytheme-another-alt-warn);
    @include angular-material-theme($mytheme-another-alt-theme);
}
// Using the $theme variable from the pre-built theme you can call the theming function
@include mytheme-sidemenu($mytheme-app-theme);
  1. Uses separate scss file to hold your custom css class 使用单独的scss文件来保存自定义css类

// 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;
  }
}
  1. Now you can use your custom css classes over the app and don't forget to put the scss file link within your angular.json/angular-cli.json 现在,您可以在应用程序上使用自定义css类,并且不要忘记将scss文件链接放在angular.json / angular-cli.json中。

You can also use this classes too:
.col-primary {
    & .hue {
      &-50 {
        color: mat-color($primary, 50) !important;
      }
      &-100 {
        color: mat-color($primary, 100) !important;
      }
      .
      .
      .
     &-800 {
        color: mat-color($primary, 800) !important;
      }
    }
 }

Also, you can see the running Example of this code here 此外,您可以在此处查看此代码的运行示例

The answer to THIS: question is actually what I was looking for: It was much easier than I thought: 对此的回答:问题实际上是我在寻找的东西:它比我想象的容易得多:

.mat-option {
  color : mat-color($foreground, text);
}

Angular Material - change color of clicked mat-list-option 角度材质 - 更改单击的mat-list-option的颜色

Why don't use Angular Material Theming built-in functionnalities ? 为什么不使用Angular Material Theming内置函数?

That way, you only have to create a custom Color Palette and affect it to either primary/accent/warn color (or use a pre-defined theme) with a bit of scss (to customize base colors) 这样,您只需创建一个自定义调色板,并将其影响为主要/重音/警告color (或使用预定义的主题)与一点scss(以自定义基色)

Then the rest is done automatically by selecting the good color while using angular material components : 然后通过在使用角度材料组件时选择好的颜色自动完成其余部分:

<button mat-raised-button color="primary">Primary</button>
<button mat-raised-button color="accent">Accent</button>
<button mat-raised-button color="warn">Warn</button>

In your components you can also use theses palettes quite easily if needed : 在您的组件中,如果需要,您还可以非常轻松地使用这些调色板:

button {
    background-color: mat-color($accent);
}

If your themes have consistent color variable names in them, you can use those variables. 如果主题中包含一致的颜色变量名称,则可以使用这些变量。

But using javascript you can get the color of the element and store it in a variable and use it elsewhere. 但是使用javascript你可以获得元素的颜色并将其存储在变量中并在其他地方使用它。

document.getElementById("element").style.backgroundColor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM