Sass Mixin Themify 不影响身体

发布于 2025-01-11 02:31:28 字数 1505 浏览 0 评论 0原文

我从某人那里继承了一些用于主题化的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦毁影碎の 2025-01-18 02:31:28
@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;
        }
    }
}

输出

body.theme-default {
   background-color: #CCCCCC;
}

You we're very close - 注意前面的 '&'第 3 行删除了空格。

@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;
        }
    }
}

outputs

body.theme-default {
   background-color: #CCCCCC;
}

You we're really close - note the leading '&' on line 3 which removes the space.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文