【发布时间】:2018-03-07 09:31:15
【问题描述】:
我正在创建一个 UI 库,我想在其中提供一种机制来主题化所有 UI 组件,例如 button、cards、slider和所有。我对 variables 和 mixins 感到困惑。
一种方法是提供号码。用户可以更新的变量和基于该变量的组件类将被派生。 materialzecss 库中使用了相同的概念。并且用户会使用喜欢
//variables that are used to create component css classes
$primary : "blue";
$btn-primary :"green";
//then include the ui library
@import "_ui-variables";
@import "ui-library";
_ui-variables.scss
$primary : "red" !default;
$btn-primary: $primary !default;
// and other variables
_btn.scss 会像
.btn {
// other rule sets
color:$btn-primary;
}
其他方式可能是使用mixins。每个组件都有一个主题文件,其中包含该组件的主题混合,在库级别,将有一个主题混合,其中包含单个组件的所有混合。正如 angular-material 所做的那样
_btn.scss
@import "_btn-theme.scss";
.btn {
// some rules
}
_btn-theme.scss
@mixin btn-theme($theme) {
// if user has added the btn-primary then use btn-primary otherwise primary
@if map-has-key($theme,btn-primary) {
$btn-primary : map-get($theme,primary);
} @else {
$btn-primary : map-get($theme,primary);
}
.btn {
color:$btn-primary;
}
}
和 ui-library.scss
@import "_btn.scss";
@import "_card.scss";
@mixin ui-theme($theme) {
@include btn-theme($theme);
@include card-theme($theme); // include all component theme
}
消费者会将其称为
consumer-theme.scss
@import "ui-library";
$theme :(primary:"blue",accent:"yellow");
@include ui-theme($theme);
这些方法的优缺点是什么?有没有其他方法可以做到这一点?
【问题讨论】:
标签: css design-patterns sass mixins