Css 什么最适合用于在sass中设置变量主题或混合
我正在创建一个UI库,我想在其中提供一种机制来为所有UI组件设置主题,如按钮、卡片、滑块等等。我混淆了变量和混合变量 一种方法是提供用户可以更新的变量数量,并根据这些变量派生组件类。materialzecss库中使用了相同的概念。用户将使用Css 什么最适合用于在sass中设置变量主题或混合,css,design-patterns,sass,mixins,Css,Design Patterns,Sass,Mixins,我正在创建一个UI库,我想在其中提供一种机制来为所有UI组件设置主题,如按钮、卡片、滑块等等。我混淆了变量和混合变量 一种方法是提供用户可以更新的变量数量,并根据这些变量派生组件类。materialzecss库中使用了相同的概念。用户将使用 //variables that are used to create component css classes $primary : "blue"; $btn-primary :"green"; //then include the ui library
//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";
\u ui-variables.scss
$primary : "red" !default;
$btn-primary: $primary !default;
// and other variables
@import "_btn-theme.scss";
.btn {
// some rules
}
@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;
}
}
@import "_btn.scss";
@import "_card.scss";
@mixin ui-theme($theme) {
@include btn-theme($theme);
@include card-theme($theme); // include all component theme
}
@import "ui-library";
$theme :(primary:"blue",accent:"yellow");
@include ui-theme($theme);
而\u btn.scs将如下
.btn {
// other rule sets
color:$btn-primary;
}
另一种方法是使用mixins
。每个组件都将有一个主题文件,其中包含该组件的主题混合,在库级别,将有一个主题混合,其中包含单个组件的所有混合。正如角材质所做的那样
\u btn.scss
$primary : "red" !default;
$btn-primary: $primary !default;
// and other variables
@import "_btn-theme.scss";
.btn {
// some rules
}
@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;
}
}
@import "_btn.scss";
@import "_card.scss";
@mixin ui-theme($theme) {
@include btn-theme($theme);
@include card-theme($theme); // include all component theme
}
@import "ui-library";
$theme :(primary:"blue",accent:"yellow");
@include ui-theme($theme);
\u btn-theme.scss
$primary : "red" !default;
$btn-primary: $primary !default;
// and other variables
@import "_btn-theme.scss";
.btn {
// some rules
}
@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;
}
}
@import "_btn.scss";
@import "_card.scss";
@mixin ui-theme($theme) {
@include btn-theme($theme);
@include card-theme($theme); // include all component theme
}
@import "ui-library";
$theme :(primary:"blue",accent:"yellow");
@include ui-theme($theme);
以及ui library.scss
$primary : "red" !default;
$btn-primary: $primary !default;
// and other variables
@import "_btn-theme.scss";
.btn {
// some rules
}
@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;
}
}
@import "_btn.scss";
@import "_card.scss";
@mixin ui-theme($theme) {
@include btn-theme($theme);
@include card-theme($theme); // include all component theme
}
@import "ui-library";
$theme :(primary:"blue",accent:"yellow");
@include ui-theme($theme);
消费者会称之为
消费者主题。scss
$primary : "red" !default;
$btn-primary: $primary !default;
// and other variables
@import "_btn-theme.scss";
.btn {
// some rules
}
@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;
}
}
@import "_btn.scss";
@import "_card.scss";
@mixin ui-theme($theme) {
@include btn-theme($theme);
@include card-theme($theme); // include all component theme
}
@import "ui-library";
$theme :(primary:"blue",accent:"yellow");
@include ui-theme($theme);
这些方法的优缺点是什么?还有其他方法吗?如果您可以使用CSS自定义属性(CSS变量),这将非常简单。您只需要在主体中添加一个类,同时更改所有变量。所以你只需要一个默认的主题,然后一些类改变你的主题 我在我的一个项目中有一个小例子,如果单击“反转主题”,它会将页面主题更改为反转: CSS自定义属性的问题在于,并非每个兄弟都支持它:/ 否则,我强烈建议使用sass映射。当主题较少时,更容易维护,并且可以使用
@每个
循环快速生成组件
例如,如果要生成背景色类:
$color-themes: (
primary:
(
base: #4c5c8c,
dark: darken(#4c5c8c, 15%),
light: lighten(#4c5c8c, 15%),
transparent: transparentize(#4c5c8c, 0.5),
contrast: #ffffff
),
secondary:
(
base: #212529,
dark: darken(#212529, 15%),
light: lighten(#212529, 15%),
transparent: transparentize(#212529, 0.5),
contrast: #ffffff
)
}
@each $name, $theme in $color-themes {
.has-bg-#{$name} {
background-color: map-get($name, base);
color: map-get($name, contrast);
}
}
因此,这里我们将得到两个新类。has bg primary
,。has bg secondary
如果向地图添加新条目,它将自动生成新类:)
我已经创建了一个使用CSS自定义属性(这个可以禁用)和Sass变量的Scss样板。它针对主题创建进行了优化。大多数组件都链接到变量(使用映射)。如果你能使用CSS自定义属性(CSS变量),那就很容易了。您只需要在主体中添加一个类,同时更改所有变量。所以你只需要一个默认的主题,然后一些类改变你的主题 我在我的一个项目中有一个小例子,如果单击“反转主题”,它会将页面主题更改为反转: CSS自定义属性的问题在于,并非每个兄弟都支持它:/ 否则,我强烈建议使用sass映射。当主题较少时,更容易维护,并且可以使用
@每个
循环快速生成组件
例如,如果要生成背景色类:
$color-themes: (
primary:
(
base: #4c5c8c,
dark: darken(#4c5c8c, 15%),
light: lighten(#4c5c8c, 15%),
transparent: transparentize(#4c5c8c, 0.5),
contrast: #ffffff
),
secondary:
(
base: #212529,
dark: darken(#212529, 15%),
light: lighten(#212529, 15%),
transparent: transparentize(#212529, 0.5),
contrast: #ffffff
)
}
@each $name, $theme in $color-themes {
.has-bg-#{$name} {
background-color: map-get($name, base);
color: map-get($name, contrast);
}
}
因此,这里我们将得到两个新类。has bg primary
,。has bg secondary
如果向地图添加新条目,它将自动生成新类:)
我已经创建了一个使用CSS自定义属性(这个可以禁用)和Sass变量的Scss样板。它针对主题创建进行了优化。大多数组件都链接到变量(使用映射)。看看吧变量将是你最初的最佳选择 创建一个主题故事不是你应该匆忙完成的事情,而是花时间为一组基本颜色整合坚实的、经过深思熟虑的变量。之后,您可以使用
lighte()
、darken()
,以及其他内置于SASS中的工具来扩展它们
然后,使用该基本变量集建立特定于组件的变量,以根据需要缩放主题故事。变量将是您最初的最佳选择 创建一个主题故事不是你应该匆忙完成的事情,而是花时间为一组基本颜色整合坚实的、经过深思熟虑的变量。之后,您可以使用
lighte()
、darken()
,以及其他内置于SASS中的工具来扩展它们
然后,使用该基本变量集建立特定于组件的变量,以根据需要缩放主题故事