简体   繁体   English

来自Mixins的Sass占位符

[英]Sass Placeholders from Mixins

I want to create Sass placeholders on the fly using arbitrary values passed from a style block: 我想使用从样式块传递的任意值来动态创建Sass占位符:

@mixin example-mixin($arg) {
    %placeholder-#{$arg} {
        property: $arg;
    }
    @extend %placeholder-#{$arg};
}

Calling the mixin: 调用mixin:

.classname {
    @include example-mixin('value');
}

This almost works, but for some reason in the CSS output the .classname is given twice as though it's a descendant selector: 这几乎可以工作,但由于某些原因,在CSS输出中, .classname被赋予两次,就好像它是一个后代选择器:

.classname .classname {
    property: value;
}

I'm not seeing the logic behind the duplicate class names - can anyone see what I'm doing wrong and/or suggest a workaround? 我没有看到重复类名背后的逻辑 - 任何人都可以看到我做错了什么和/或建议解决方法吗?

Let's look at what happens if you use real classes instead of extend classes 让我们看一下如果使用真实类而不是扩展类会发生什么

.a {
    .b {
        color: blue;
    }

    @extend .b;
}

Output: 输出:

.a .b, .a .a {
  color: blue;
}

The only reason I could imagine you wanting to do this is so you can use the extend class for purposes of extending instead of .classname like so: 我可以想象你想要这样做的唯一原因是你可以使用extend类来扩展而不是.classname如下所示:

.c {
    @extend .b;
}

You'll see that the output probably isn't what you want at all: 你会看到输出可能根本不是你想要的:

.a .b, .a .a, .a .c {
  color: blue;
}

The .a .a doesn't make a whole lot of sense to me either, but it's harmless. .a .a对我来说也没有多大意义,但它是无害的。 What you're actually wanting to do is something like this: 你真正想要做的是这样的事情:

%placeholder-name, .classname {
    property: name;
}

.foo {
    @extend %placeholder-name;
}

And the output will be like this: 输出将是这样的:

.foo, .classname {
  property: name;
}

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

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