sass multiple themes in angular

廉价感情. 提交于 2019-12-11 05:49:19

问题


Let's say I have two themes (light & dark) in an angular app > 2, and a toggle button which is already configured to add the theme class to the body tag

variables.scss

$themes: (
  light: (
    backgroundColor: #fff,
    textColor: #408bbd,
    borderColor: #254fe5,
  ),
  dark: (
    backgroundColor: #222,
    textColor: #ddd,
    borderColor: #2e4dd2,
  ),
);

I tried different approaches I found (here and here), but none worked.

I also tried the simplest approach

about.component.scss

.theme-dark .page-title{
  color: white;
}

.theme-light .page-title{
  color: red;
}

I know the class .theme-dark on body tag is not recognized from a component style, but I can't find any solution.

What approach can I use to get multiple themes in such modules system?


回答1:


You can use :host-context in a scoped angular component. Here is your example:

about.component.scss

:host-context(.theme-dark) .page-title{
  color: white;
}

:host-context(.theme-light) .page-title{
  color: red;
}

Then you need an instance e.g. a service that can change the CSS class at top level to switch the theme.

When you don't need to support IE11 or other legacy browser take a look at CSS Custom Properties (CSS Variables) to change the values at runtime. This reduces your CSS files.



来源:https://stackoverflow.com/questions/43035538/sass-multiple-themes-in-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!