【发布时间】:2020-01-08 22:25:26
【问题描述】:
大家好!
我目前正在开发 CSS 框架的主题功能,遇到了一些问题,希望您能提供帮助。
我创建了一个名为$themes 的 SASS 地图,其中包含不同主题的颜色。我从一篇中等文章(谁没有)中复制粘贴了一些代码,然后我的主题作品就大功告成了!
但是...
这个:
@include themify($themes) {
.btn {
color: themed('blue');
}
}
打开。每一个。零件。比我认为在我将要做的大量样式中可维护的代码更草率。
所以...
我的目标
我想做一些超级 hacky 和很棒的事情:
@include themify($themes) {
$blue: themed(blue);
}
我想对变量进行主题化,所以我所要做的就是添加 $blue 而不是很多调用 mixins 和不必要的胡言乱语。
如果我能得到这样的东西,它会看起来像这样:
.btn {
background: $blue;
}
所有主题都将事先处理好!
但当然它从来没有那么容易,因为它不起作用......如果你们中的一个很棒的sass魔术师可以用这个来发挥一些魔法,那将是天赐之物,我会将你包含在很棒的贡献者的源代码中。
代码
$themes sass-map:
$themes: (
light: (
'blue': #0079FF
),
dark: (
'blue': #0A84FF
)
);
来自这个很棒的Medium Article的copypasta mixin:
@mixin themify($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);
}
对于如何实现这一点的任何建议都将 100% 感激。非常感谢您将您作为出色的贡献者添加到源代码中。
提前致谢!
【问题讨论】:
标签: html css sass themes scss-mixins