【问题标题】:How can I use Material Theme colors for custom things?如何将 Material Theme 颜色用于自定义内容?
【发布时间】:2023-03-22 02:45:01
【问题描述】:

我正在尝试使用我的主题中的特定(背景)颜色来制作其他颜色的元素

这样做的设计方法是什么?有没有设计好的方法来做到这一点?

例如, 有没有办法说这个自定义元素应该使用当前主题的 -> 背景 -> 禁用颜色?等等

我很想能够说,例如,这个特定元素应该是当前主题 -> 背景调色板 -> disabled-list-option 的任何颜色。

谢谢!!

【问题讨论】:

  • 欢迎来到stackoverflow,一个问答网站,您必须知道您的问题的答案。不需要,但最好分享定义材质主题的 scss 部分。这可能是投反对票的原因。
  • 我有...我看到它提到了主要、重音和警告调色板,我希望使用背景调色板中的特定颜色,你有这样的例子吗? @RobinDijkhof

标签: angular sass themes angular-material


【解决方案1】:

为什么不使用Angular Material Theming 内置函数?

这样,您只需要创建一个自定义调色板并将其影响到主要/重音/警告color(或使用预定义主题)和一点 scss(自定义基色)

然后在使用角材料组件时通过选择好的颜色自动完成其余的工作:

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

如果需要,您还可以在您的组件中轻松使用这些调色板:

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

【讨论】:

  • 我正在尝试设置自定义组件的一部分,而不是标准按钮或类似的东西..?我该怎么做?甚至在某些情况下覆盖颜色,我们也试图坚持一个与材质主题不同的特定主题,所以我想要一种从我的主题设置自定义颜色的简单方法。
  • 我提供的链接包含您需要的参考:material.angular.io/guide/theming-your-components
  • 再次,我对此很陌生..所以我查看了链接,这看起来确实很有帮助。我认为这让我的头脑更清楚了,我会尝试进一步检查这些混合。我想我可能会迷路,看起来你只能使用原色,重音,警告颜色吗?我想使用自定义“背景”调色板中的特定颜色等。这可能吗?
【解决方案2】:

我认为您需要一个 .scss 文件,您可以在其中放置所有自定义颜色变量并将它们用于通用 CSSclass 以应用于您的所有应用程序。

那么你需要做这些事情。

  1. 使用包含当前材质主题/动态主题 scss 的 theme.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. 使用单独的 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. 现在您可以在应用程序上使用您的自定义 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;
      }
    }
 }

另外,你可以看到这段代码的运行示例here

【讨论】:

  • 我认为这离我想去的地方很近。我认为这就是我设置所有内容的方式,减去我不完全理解的 mixin。我也在尝试设置自定义组件的一部分,而不是标准按钮或类似的东西..?我该怎么做?甚至在某些情况下覆盖颜色,我们也试图坚持一个与材质主题不同的特定主题,所以我想要一种从我的主题设置自定义颜色的简单方法。
  • 想想我可能会迷路,看起来你只能这样使用主要颜色、重音颜色、警告颜色吗?我想使用自定义“背景”调色板中的特定颜色等。这可能吗?
  • 看你是否使用了材料部分,我认为你需要遵循他们的指导方针,你可以遵循。您还可以通过使用封装覆盖 Angular 样式在材质组件中使用自己的 CSS:ViewEncapsulation.None。如果您可以使用 stackblitz 重现演示,它可以帮助我理解您的要求。
  • 我可以发布任何有帮助的内容,但我认为您的理解是正确的。我们只是有一个非常具体的主题,我要坚持下去,我需要做一些不同于材料指南的事情。我想以最“设计”的方式在必要时覆盖材料主题
  • 看看这是网络技术,你可以改变任何你想要的东西。但是这里@mixin 由材质主题提供所以你不能使用材质主题之外的数据,我认为你需要在任何地方覆盖它们以应用你自己的特定主题。
【解决方案3】:

我搜索了reuse same color scss 并遇到了this post,我强烈建议您这样做,以便您了解在整个 SCSS 文件中重用值,以使它们更易于维护。

总结起来,你可以通过定义这样的东西来定义 SCSS 变量:

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

然后像这样重用它:

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

...

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

因此,您可能应该做的是在层次结构中较高的 SCSS 文件中定义这些常量之一(例如 app.scss,如果这是一个应用程序范围的变量)并在您的成分。请务必阅读 Aloke 的回答,以便进行更深入的分析。

始终使用搜索,并确保在询问与代码相关的问题时包含您的代码(使用JSFiddle 等网站让事情变得更容易)。

【讨论】:

    【解决方案4】:

    这个问题的答案:问题实际上是我一直在寻找的: 这比我想象的要容易得多:

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

    Angular Material - change color of clicked mat-list-option

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-04
      • 2018-07-24
      • 2017-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多