Sass Mixin Themify 不影响身体
我从某人那里继承了一些用于主题化的 SASS 代码,看起来它只是编译成针对具有主题类的元素的子元素的规则。
$themes:(
default:(
background:#CCCCCC
)
);
@mixin themify($themes: $themes) {
@each $theme, $map in $themes {
.theme-#{$theme} & {
$theme-map: () !global;
@each $key, $submap in $map {
$value: map-get(map-get($themes, $theme), '#{$key}');
$theme-map: map-merge($theme-map, ($key: $value)) !global;
}
@content;
$theme-map: null !global;
}
}
}
@function themed($key) {
@return map-get($theme-map, $key);
}
给出一个已编译的CSS规则,
.theme-default body {
background-color: #CCCCCC;
}
代码看起来不错,但是可以拥有主题类的最高元素是主体,但是如何为主体赋予主题属性呢?我尝试添加一个不会使用子项的附加 mixin (只需删除 &
以反转元素关系)
@mixin themifyDirect($themes: $themes) {
@each $theme, $map in $themes {
.theme-#{$theme} {
$theme-map: () !global;
@each $key, $submap in $map {
$value: map-get(map-get($themes, $theme), '#{$key}');
$theme-map: map-merge($theme-map, ($key: $value)) !global;
}
@content;
$theme-map: null !global;
}
}
}
但这会编译成 css 规则
body .theme-default {
background-color: #26282F; }
这很接近,但还不够,因为我需要的规则是
body.theme-default {
background-color: #CCCCCC;
}
具有该类的目标主体
I've inherited some SASS code used for theming from someone and it looks like it is only compiling into rules that target children of the element that has the themed class.
$themes:(
default:(
background:#CCCCCC
)
);
@mixin themify($themes: $themes) {
@each $theme, $map in $themes {
.theme-#{$theme} & {
$theme-map: () !global;
@each $key, $submap in $map {
$value: map-get(map-get($themes, $theme), '#{$key}');
$theme-map: map-merge($theme-map, ($key: $value)) !global;
}
@content;
$theme-map: null !global;
}
}
}
@function themed($key) {
@return map-get($theme-map, $key);
}
Gives a compiled css rule as
.theme-default body {
background-color: #CCCCCC;
}
The code seems fine but the highest element that can have the themed class is the body but then how can the body be given a themed property? I tried to add an additional mixin that will not use the children (just removing the &
to invert the element relation)
@mixin themifyDirect($themes: $themes) {
@each $theme, $map in $themes {
.theme-#{$theme} {
$theme-map: () !global;
@each $key, $submap in $map {
$value: map-get(map-get($themes, $theme), '#{$key}');
$theme-map: map-merge($theme-map, ($key: $value)) !global;
}
@content;
$theme-map: null !global;
}
}
}
But this compiles into a css rule
body .theme-default {
background-color: #26282F; }
This is close but not enough because the rule I need is
body.theme-default {
background-color: #CCCCCC;
}
To target body which has the class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
输出
You we're very close - 注意前面的 '&'第 3 行删除了空格。
outputs
You we're really close - note the leading '&' on line 3 which removes the space.