如何使用angular5和angular材质创建自定义颜色主题

Dan*_*Dan 16 material-design angular-material angular

我一直在关注如何创建自定义主题的角度/材料文档,跟随其他博客,并检查各种堆栈溢出类似的问题,但似乎无法使这工作.我有以下styles.css,angular-cli.json,theme.scss和另一个sass文件,其中我的主题颜色来自super-styles.sass.

styles.css的

...
@import 'assets/styles/theme.scss';
...
Run Code Online (Sandbox Code Playgroud)

角cli.json

...
"styles": [
   "styles.css",
    "src/assets/styles/theme.scss"
],
...
Run Code Online (Sandbox Code Playgroud)

theme.scss

@import '~@angular/material/theming';
@import "super-styles";

// Plus imports for other components in your app.

// Include the common styles for Angular Material. We include this here so that you only
// have to load a single css file for Angular Material in your app.
// Be sure that you only ever include this mixin once!
@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($darkblue, A400);
$candy-app-accent:  mat-palette($orange, A400);

// The warn palette is optional (defaults to red).
$candy-app-warn:    mat-palette($alert);

// 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);
Run Code Online (Sandbox Code Playgroud)

超级styles.sass

...
$darkblue: #7faedd
$mediumblue: #85ceef
$lightblue: #c5e8f1
$yellow: #f4ef5f
$alert: #f37652
$orange: #fbb03c
...
Run Code Online (Sandbox Code Playgroud)

根据教程,我觉得这应该工作,但角度没有编译,我得到一个错误.

错误在./node_modules/css-loader?{"sourceMap":false,"importLoaders":1}!./node_modules/postcss-loader?{"ident":"postcss"}!./src/assets/styles/ theme.scss模块构建失败:未知单词(23:1)

21 | $ candy-app-theme:mat-light-theme($ candy-app-primary,$ candy-app-accent,$ candy-app-warn); 22 |

23 | //包括核心的主题样式以及应用中使用的每个组件.| ^ 24 | //或者,您可以为每个组件导入和@include主题mixins 25 | //你正在使用

任何有关如何构建自定义主题并在我的角度应用程序中使用它的帮助将非常有帮助.谢谢!

Z. *_*ley 36

为了使用角度 - 材质的自定义六角形调色板,您需要为调色板定义不同的阴影和对比色,即使您只需要一种颜色.我建议使用至少3种颜色(浅色,普通,深色),以便使用Material的内置动画完美无瑕:

// below defines your custom color to build a theme palette from
$my-blue: (
  50: #7fdddd,
  100: #7faedd,
  200: #7f7fdd,
  300: #7faedd,
  400: #7faedd,
  500: #7faedd,
  600: #7faedd,
  700: #7faedd,
  800: #7faedd,
  900: #7faedd,
  A100: #7faedd,
  A200: #7faedd,
  A400: #7faedd,
  A700: #7faedd,
  contrast: (
    50: white,
    100: white,
    200: white,
    300: white,
    400: white,
    500: white,
    600: white,
    700: white,
    800: white,
    900: white,
    A100: white,
    A200: white,
    A400: white,
    A700: white,
  )
);
// below creates a primary palette with three shades of blue
$my-primary: mat-palette($my-blue, 100, 50, 200);
Run Code Online (Sandbox Code Playgroud)

  • 对于那些懒惰并且不想手动计算不同颜色的人来说,有一个很酷的在线工具可以完成这项工作 - http://mcg.mbitson.com (9认同)
  • @Dan从styles.css中删除`@import'res assets/styles/theme.scss';` (2认同)

Jan*_*rju 9

正如 Z. Bagley 建议制作自己的调色板,但我认为您不需要将所有这些颜色都制作成调色板。例如,这工作正常。

$custom-collection: (
    warning :  #FFC116,
    success :  #0a630f,
    danger:    #c00000,
    contrast: (
        warning :  #000000,
        success :  #FFFFFF,
        danger:    #FFFFFF,
    )
);
Run Code Online (Sandbox Code Playgroud)

然后你按照建议制作调色板

$my-app-custom: mat-palette($custom-collection, custom);
Run Code Online (Sandbox Code Playgroud)

然后像这样在 mat-light-theme 行之后将其合并到主题

$my-app-theme: mat-light-theme($my-app-primary, $my-app-accent, $my-app-warn);
$my-app-theme: map_merge($my-app-theme, (custom: $my-app-custom));
Run Code Online (Sandbox Code Playgroud)

在此之后,您将拥有一个包含每种颜色的对象。

我可以建议你像这样制作通用的自定义对象吗

$custom: map-get($my-app-theme, custom);
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样在你的组件中使用它

background-color: mat-color($custom, validation-invalid);
color: mat-color($custom, validation-invalid-contrast);
Run Code Online (Sandbox Code Playgroud)

还有一个建议。您可以将 mat-success 添加到您的全局样式文件中

.mat-success {
  background-color: mat-color($custom, success);
  color: mat-color($custom, success-contrast);
}
Run Code Online (Sandbox Code Playgroud)

现在您可以像使用主色和强调色一样使用颜色属性。

<button mat-flat-button color="success" >Success</button>
Run Code Online (Sandbox Code Playgroud)

这是有效的,因为颜色指令将 mat-*-class 添加到元素,其中 * 是颜色值。所以 color="foo" 生成 class="mat-foo" 对应的元素。


小智 5

为了供将来参考,有一些工具可以根据起始颜色为您创建主题,例如http://mcg.mbitson.com/#!?mcgpalette0=%233f51b5 。

  • 给它起个名字
  • 选择基色
  • 单击剪贴板图标
  • 选择框架
  • 复制粘贴到您的 .scss 中
  • 将变量向下传递