简体   繁体   English

使用SASS为颜色主题创建混入

[英]Creating a mixin for color themes with SASS

I would like to create various themes in my project (I work with SASS 3.2) : 我想在我的项目中创建各种主题(我使用SASS 3.2):

I defined a variable: 我定义了一个变量:

$themeName: Z;

I tried to create a mixin like : 我试图创建一个像这样的mixin:

@mixin theme($themeName) {
  @if $themeName == Z {
    @content;
  }

  @if $themeName == Y {
    @content;
  }
}

To be able to use like that: 为了能够像这样使用:

.wrapper {
  @include theme(Y) {
    border-top: 5px solid blue;
  }
  @include theme(Z) {
    border-top: 10px solid red;
  }
}

But in my CSS file, I have something like that : 但是在我的CSS文件中,我有类似以下内容:

.wrapper {
  border-top: 10px solid red;
  border-top: 5px solid blue;
 }

I'll appreciate a lot some help, I spend hours trying to find a solution to easily develop various themes for my platform. 我会非常感谢您的帮助,我花了数小时试图找到一个解决方案,以轻松地为我的平台开发各种主题。

You are calling mixin two times, each time for a different theme. 您两次调用mixin,每次调用一个不同的主题。 Mixin remaining unchanged, try this: Mixin保持不变,请尝试以下操作:

$currentTheme: Z;

.wrapper {
    @include theme($currentTheme) {
        border-top: 10px solid red;
     }
}

And alter $currentTheme at the beginning of .scss file to which theme you are currently using (Y, Z, A, B, X...), meaning: which theme do you want to see in generated .css file. 并在.scss文件的开头更改$ currentTheme(当前,当前使用的主题(Y,Z,A,B,X ...)),这意味着:要在生成的.css文件中看到哪个主题。

EDIT: Thanks for clearing everything up in the comment, now the solution: 编辑:感谢您清除注释中的所有内容,现在解决方案:

$currentTheme: "Z";

@mixin isTheme($theme) {
    @if $theme == $currentTheme {
        @content;
    }
}

.wrapper {
    @include isTheme("Z") {
    border-top: 10px solid green;
}  
    @include isTheme("Y") {
        border-top: 10px solid blue;
    }
}

Just change $currentTheme variable as you see fit (in this case to "Y", just to see that it actually works). 只需在您认为合适的情况下更改$ currentTheme变量即可(在本例中为“ Y”,只是看它是否确实有效)。

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

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