简体   繁体   English

Angular 材料 Sass 通过全局 scss 文件上的 map-get 和深色主题动态获取 styles

[英]Angular material Sass get styles dynamically via map-get and dark theme on global scss files

I have implemented dark theme in my Angular application.我在我的 Angular 应用程序中实现了深色主题。 It's done via Service and adds or remove a class to the document body.它通过服务完成,并向文档正文添加或删除 class。

this.renderer.addClass(document.body, 'color-scheme-dark');

Styles.scss is including the material theme according to the class: Styles.scss 包含根据 class 的材料主题:

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

.color-scheme-dark {
  @include angular-material-theme($dark-theme);
}

Everything is working and angular sets the dark theme only if i'm passing the dark scheme class.一切正常,只有当我通过黑暗方案 class 时,angular 才会设置黑暗主题。 However, Issue is that i have global styles scss folders, Which applying the styles according to the light theme:但是,问题是我有全局 styles scss 文件夹,根据轻主题应用 styles:

.app-hr {
  color: map-get($app-foreground, border);
}

But the correct result is to take the (app-dark-doreground,border) if the class is dark.但正确的结果是如果 class 是暗的,则采用 (app-dark-doreground,border)。 I have tried to do something like the code below,But failed:我试图做类似下面代码的事情,但失败了:

.app-hr {
  color: map-get($app-foreground, border);
}

.color-scheme-dark {
  color: map-get($app-dark-foreground, border);

}

So I have few Scss files that gets the colors from the pallet but i can't change them dynamically by the class i have added.因此,我很少有从托盘获取 colors 的 Scss 文件,但我无法通过我添加的 class 动态更改它们。 What can i do next to change them?接下来我该怎么做才能改变它们? Thanks.谢谢。

So here is how i've solved this.所以这就是我解决这个问题的方法。 Inside the global-styles.scss, I made a mixin with a variable $theme:在 global-styles.scss 中,我使用变量 $theme 制作了一个 mixin:

@mixin toolbar-style($theme) {
  mat-toolbar {
    background-color: map-get($theme, toolbar-background)
  }
}

And inside the styles.scss, I'm including the relevant mixin with the correct theme, According to the class.在 styles.scss 中,根据 class,我包含了具有正确主题的相关 mixin。

@include toolbar-style($app-foreground);

.color-scheme-dark {
  @include toolbar-style($mat-dark-theme-foreground);
}

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

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