SCSS 使用
架构
css 样式
bash
│ │ ├── src
│ │ │ ├── styles
│ │ │ │ ├── index.scss
│ │ │ │ ├── chooseButton
│ │ │ │ │ ├── chooseButton.scss
│ │ │ │ ├── font
│ │ │ │ │ ├── Aileron.otf
│ │ │ │ ├── common
│ │ │ │ │ ├── common.scss
│ │ │ │ │ ├── isLoading.scss
│ │ │ │ │ ├── iconfont.scss
│ │ │ │ │ ├── initRoot.scss
│ │ │ │ │ ├── theme
│ │ │ │ │ │ ├── componentVar.scss
│ │ │ │ │ │ ├── config.scss
│ │ │ │ │ │ ├── function.scss
│ │ │ │ │ │ ├── mixins.scss
│ │ │ │ │ │ ├── var.scssvar.scss
deep-merge 合并
color.mix(#ffffff, #3069ff, 10%) 颜色混合, 10%是白色
@debug map.get($colors, "primary", "light-1");打印出数值
scss
@use "sass:map";
@use "sass:math";
@use "sass:color";
// type
$types: primary, success, warning, danger;
// sizes
$sizes: small, default, large;
/** colors */
$colors: () !default;
// 合并
$colors: map.deep-merge(
(
// 白色
"white": #ffffff,
// 黑色
"black": #000000,
// 主要
"primary": (
"base": #3069ff,
),
// 成功
"success": (
"base": #14cd70,
),
// 警告
"warning": (
"base": #ffa81a,
),
// 错误
"danger": (
"base": #ff4a5b,
)
),
$colors
);
// 色调
$color-white: map.get($colors, "white") !default;
$color-black: map.get($colors, "black") !default;
$color-primary: map.get($colors, "primary", "base") !default;
$color-success: map.get($colors, "success", "base") !default;
$color-warning: map.get($colors, "warning", "base") !default;
$color-error: map.get($colors, "error", "base") !default;
/**
* mixin 设置颜色层次
* $type 类型:primary, success, warning, error
* $number 百分比计算值
* $mode 标识
* $mix-color 混合种颜色
*/
@mixin set-color-level($type, $number, $mode: "light", $mix-color) {
$colors: map.deep-merge(
(
$type: (
"#{$mode}-#{$number}": color.mix(
$mix-color,
map.get($colors, $type, "base"),
math.percentage(math.div($number, 10))
),
),
),
$colors
) !global;
}
@each $type in $types {
@for $i from 1 through 9 {
@include set-color-level($type, $i, "light", $color-white);
}
}
/** 中性色 */
$neutral-color: () !default;
$neutral-color: map.deep-merge(
(
"light-1": #212226,
"light-2": #4d5059,
"light-3": #7c808c,
"light-4": #abb1bf,
"light-5": #c9cdd9,
"light-6": #e2e6f1,
"light-7": #ebeef5,
"light-8": #f5f6fa,
"light-9": #fafbff,
),
$neutral-color
);
/** 文字颜色 */
$text-color: () !default;
$text-color: map.deep-merge(
(
"primary": #4d5059,
// 文字主色
"secondary": #7c808c,
// 文字辅色
"disabled": #abb1bf,
// 文字禁用
"placeholder": #c9cdd9,
// 文字占位
"light-8": #f5f6fa,
// 文字占位
"light-9": #fafbff,
// 文字占位
),
$text-color
);
/** 文字尺寸 */
$font-size: () !default;
$font-size: map.deep-merge(
(
"small": 12px,
// 小的
"default": 14px,
// 默认的
"large": 16px,
// 大的
),
$font-size
);
/** 边框 */
$border-color: () !default;
$border-color: map.deep-merge(
(
"default": #e2e6f1,
// 边框主色
"disabled": #ebeef5,
// 边框禁用
"hover": #c9cdd9,
// 鼠标悬停
),
$border-color
);
/** 组件尺寸 */
$component-size: () !default;
$component-size: map.deep-merge(
(
"small": 24px,
// 小的
"default": 32px,
// 默认的
"large": 40px,
// 大的
),
$component-size
);
/** 颜色 */
$color: () !default;
$color: map.deep-merge(
(
"white": $color-white,
// 白色
"black": $color-black,
// 黑色
"disabled-bg": "#F5F6FA",
),
$color
);
/** 颜色 */
$component-round: () !default;
$component-round: map.deep-merge(
(
"small": 10px,
// 小的
"default": 12px,
// 默认的
"large": 12px,
// 大的
"round": 100px,
// 圆的
),
$component-round
);
/** 边距 */
$padding: () !default;
$padding: map.deep-merge(
(
"small": 6px,
// 小的
"default": 10px,
// 默认的
"large": 10px,
// 大的
),
$padding
);
/** 圆角 */
$round: () !default;
$round: map.deep-merge(
(
"default": 4px,
"round": 100px,
),
$round
);
/** checkbox */
$checkbox-size: () !default;
$checkbox-size: map.deep-merge(
(
"small": 16px,
"default": 20px,
"large": 24px,
),
$checkbox-size
);
$checkbox-round: () !default;
$checkbox-round: map.deep-merge(
(
"small": 7px,
"default": 8.5px,
"large": 10.5px,
),
$checkbox-round
);
$checkbox-icon-size: () !default;
$checkbox-icon-size: map.deep-merge(
(
"small": 8px,
"default": 10px,
"large": 14px,
),
$checkbox-icon-size
);
$checkbox-indeterminate-size: () !default;
$checkbox-indeterminate-size: map.deep-merge(
(
"small": 8px,
"default": 10px,
"large": 12px,
),
$checkbox-indeterminate-size
);
$checkbox-indeterminate-round: () !default;
$checkbox-indeterminate-round: map.deep-merge(
(
"small": 3.5px,
"default": 4px,
"large": 5px,
),
$checkbox-indeterminate-round
);
/** 全局配置 */
$global: (
"neutral-color": $neutral-color,
// 文字颜色
"text-color": $text-color,
// 文字颜色
"font-size": $font-size,
// 文字尺寸
"border-color": $border-color,
// 边框颜色
"component-size": $component-size,
// 组件尺寸
"color": $color,
// 颜色
"component-round": $component-round,
// 圆角
"padding": $padding,
"checkbox-size": $checkbox-size,
"checkbox-round": $checkbox-round,
"checkbox-icon-size": $checkbox-icon-size,
"checkbox-indeterminate-size": $checkbox-indeterminate-size,
"checkbox-indeterminate-round": $checkbox-indeterminate-round,
);config.scss
scss
$namespace: "y" !default; // 前缀
$connect: "-" !default; // 块、子级
$element-connect: "__" !default; // 元素
$modifier-connect: "--" !default; // 修改器
$modifier-value-connect: "_" !default; // 修改器的值
$state-prefix: "is-" !default; // 状态前缀,如禁用、圆角、加载等
$b: null !default; // 基础值function.scss
scss
@use "config.scss" as *;
/** 组合变量名称 */
@function createVarName($list) {
$name: "--" + $namespace; // --拼接前缀
@each $item in $list {
// each循环
@if $item != "" {
// 判断不为空时执行
$name: $name + "-" + $item; // 开始拼接
}
}
@return $name; // 返回结果
}
/** 获取变量名称 */
@function getVarName($args...) {
@return createVarName(($args));
}componentVar.scss
scss
@use "function" as *;
// 按钮组件配置
@function buttonVar($type: "") {
$button: (
// 默认
"border-color": (
"default": getVarName("border-color", "default"),
"type": getVarName("color", $type),
),
"text-color": (
"default": getVarName("text-color", "primary"),
"type": getVarName("color", "white"),
),
"bg-color": (
"default": getVarName("color", "white"),
"type": getVarName("color", $type),
),
"is-link-color": (
"default": getVarName("text-color", "primary"),
"type": getVarName("color", $type),
),
"is-text-color": (
"default": getVarName("text-color", "primary"),
"type": getVarName("color", $type),
),
"is-text-bg-color": (
"default": getVarName("color", "white"),
"type": getVarName("color", "white"),
),
// hover
"hover-text-color": (
"default": getVarName("color", "primary"),
"type": getVarName("color", "white"),
),
"hover-bg-color": (
"default": getVarName("color", "primary", "light-9"),
"type": getVarName("color", $type, "light-3"),
),
"hover-border-color": (
"default": getVarName("color", "primary", "light-7"),
"type": getVarName("color", $type, "light-3"),
),
"hover-is-link-color": (
"default": getVarName("text-color", "secondary"),
"type": getVarName("color", $type, "light-4"),
),
"hover-is-text-color": (
"default": getVarName("text-color", "primary"),
"type": getVarName("color", $type),
),
"hover-is-text-bg-color": (
"default": getVarName("text-color", "light-8"),
"type": getVarName("color", $type, "light-9"),
),
// disabled
"disabled-text-color": (
"default": getVarName("text-color", "disabled"),
"type": getVarName("color", "white"),
),
"disabled-bg-color": (
"default": getVarName("color", "disabled-bg"),
"type": getVarName("color", $type, "light-5"),
),
"disabled-border-color": (
"default": getVarName("border-color", "disabled"),
"type": getVarName("color", $type, "light-5"),
),
"disabled-is-link-color": (
"default": getVarName("text-color", "disabled"),
"type": getVarName("color", $type, "light-5"),
),
"disabled-is-text-color": (
"default": getVarName("text-color", "disabled"),
"type": getVarName("color", $type, "light-5"),
),
"disabled-is-text-bg-color": (
"default": getVarName("color", "white"),
"type": getVarName("color", "white"),
),
"size": (
"default": getVarName("component-size", "default"),
),
"font-size": (
"default": getVarName("font-size", "default"),
)
);
@return $button;
}
// input输入框组件配置
@function inputVar() {
$input: (
"text-color": (
// 字体颜色
"default": getVarName("text-color", "primary"),
),
"text-color-aside": (
// 字体颜色
"default": getVarName("text-color", "secondary"),
),
"border-color": (
// 边框颜色
"default": getVarName("border-color", "default"),
),
"bg-color": (
// 背景颜色
"default": getVarName("color", "white"),
),
"bg-color-aside": (
// 背景颜色
"default": getVarName("color", "disabled-bg"),
),
"size": (
// 组件尺寸
"default": getVarName("component-size", "default"),
),
"font-size": (
// 文字大小
"default": getVarName("font-size", "default"),
),
"round": (
// 圆角
"default": getVarName("component-round", "default"),
),
"padding": (
// 边距
"default": getVarName("padding", "default"),
),
);
@return $input;
}
// textarea输入框组件配置
@function textareaVar() {
$textarea: (
"text-color": (
// 字体颜色
"default": getVarName("text-color", "primary"),
),
"text-color-aside": (
// 字体颜色
"default": getVarName("text-color", "secondary"),
),
"border-color": (
// 边框颜色
"default": getVarName("border-color", "default"),
),
"bg-color": (
// 背景颜色
"default": getVarName("color", "white"),
),
"bg-color-aside": (
// 背景颜色
"default": getVarName("color", "disabled-bg"),
),
"size": (
// 组件尺寸
"default": getVarName("component-size", "default"),
),
"font-size": (
// 文字大小
"default": getVarName("font-size", "default"),
),
"padding": (
// 文字大小
"default": getVarName("padding", "default"),
),
"round": (
// 圆角
"default": getVarName("component-round", "default"),
),
);
@return $textarea;
}
// checkbox输入框组件配置
@function checkboxVar($type: "") {
$checkbox: (
/** 默认状态 **********************************/ // label文本颜色
"text-color": (
"default": getVarName("text-color", "primary"),
"type": getVarName("color", $type),
),
// 边框颜色
"border-color": (
"default": getVarName("border-color", "default"),
"type": getVarName("color", $type),
),
// 背景颜色
"bg-color": (
"default": getVarName("color", "white"),
"type": getVarName("color", $type),
),
// 勾选的图标颜色
"icon-color": (
"default": getVarName("color", "white"),
"type": getVarName("color", "white"),
),
/** 禁用状态 **********************************/ // label文本颜色
"disabled-text-color": (
"default": getVarName("text-color", "disabled"),
"type": getVarName("color", $type, "light-5"),
),
// 边框颜色
"disabled-border-color": (
"default": getVarName("border-color", "disabled"),
"type": getVarName("color", $type, "light-5"),
),
// 背景颜色
"disabled-bg-color": (
"default": getVarName("color", "disabled-bg"),
"type": getVarName("color", $type, "light-5"),
),
// 勾选的图标颜色
"disabled-icon-color": (
"default": getVarName("text-color", "disabled"),
"type": getVarName("color", "white"),
),
/** 勾选状态 **********************************/ // label文本颜色
"checked-text-color": (
"default": getVarName("text-color", "primary"),
"type": getVarName("color", $type),
),
// 边框颜色
"checked-border-color": (
"default": getVarName("color", "primary"),
"type": getVarName("color", $type),
),
// 背景颜色
"checked-bg-color": (
"default": getVarName("color", "primary"),
"type": getVarName("color", $type),
),
// 勾选的图标颜色
"checked-icon-color": (
"default": getVarName("color", "white"),
"type": getVarName("color", "white"),
),
// 选择框尺寸
"check-size": (
"default": getVarName("checkbox-size", "default"),
),
// 选择框圆角
"check-round": (
"default": getVarName("checkbox-round", "default"),
),
// 图标尺寸
"check-icon-size": (
"default": getVarName("checkbox-icon-size", "default"),
),
// 部分选中状态尺寸
"check-indeterminate-size": (
"default": getVarName("checkbox-indeterminate-size", "default"),
),
// 半选圆角
"check-indeterminate-round": (
"default": getVarName("checkbox-indeterminate-round", "default"),
),
// 组件尺寸
"size": (
"default": getVarName("component-size", "default"),
),
// label文本尺寸
"font-size": (
"default": getVarName("font-size", "default"),
)
);
@return $checkbox;
}
// badge输入框组件配置
@function badgeVar($type: "") {
$badge: (
// 背景颜色
"bg-color": (
"default": getVarName("color", "danger"),
"type": getVarName("color", $type),
),
// 文字尺寸
"font-size": (
"default": getVarName("font-size", "default"),
),
// dot
"dot": (
"default": getVarName("badge-dot-size", "default"),
)
);
@return $badge;
}
// switch输入框组件配置
@function switchVar($type: "") {
$switch: (
/** 关闭状态(默认状态) **********************************/ // 文字颜色
"text-color": (
"default": getVarName("text-color", "primary"),
"type": getVarName("color", "white"),
),
// 背景颜色
"bg-color": (
"default": getVarName("neutral-color", "light-6"),
"type": getVarName("color", $type),
),
/** 禁用状态 **********************************/ // 文字颜色
"disabled-text-color": (
"default": getVarName("text-color", "disabled"),
"type": getVarName("color", "white"),
),
// 背景颜色
"disabled-bg-color": (
"default": getVarName("color", "disabled-bg"),
"type": getVarName("color", $type),
),
/** 打开状态(勾选状态) **********************************/ // 文字颜色
"checked-text-color": (
"default": getVarName("color", "white"),
"type": getVarName("color", "white"),
),
// 背景颜色
"checked-bg-color": (
"default": getVarName("color", "primary"),
"type": getVarName("color", $type),
),
// 开关宽度
"width": (
"default": getVarName("switch-width", "default"),
),
// 开关高度
"height": (
"default": getVarName("switch-height", "default"),
),
// label文本尺寸
"font-size": (
"default": getVarName("font-size", "default"),
)
);
@return $switch;
}mixins.scss (最后theme汇总的)
scss
@use "sass:map";
// 转发资源
@forward "function";
// 转发资源
@forward "var";
@use "function" as *;
@use "var.scss" as *;
@use "config.scss" as *;
@mixin set-color() {
@each $type in $types {
$color: map.get($colors, $type, "base");
#{createVarName(('color', $type))}: #{$color};
}
}
@mixin set-color-light() {
@each $type in $types {
@for $i from 1 through 9 {
$color: map.get($colors, $type, "light-#{$i}");
#{createVarName(('color', $type, 'light', $i))}: #{$color};
}
}
}
@mixin set-global-var() {
@each $key, $data in $global {
@if $data {
// 判断是否存在数据
@each $type, $value in $data {
#{createVarName(($key, $type))}: #{$value};
}
}
}
}
@mixin set-private-var($block, $data) {
@each $key, $value in $data {
#{createVarName(($block, $key))}: #{$value};
}
}
// 设置组件的变量
@mixin set-component-var($name, $var, $varKey: "default") {
@each $key, $value in $var {
$varName: getVarName($name, $key);
$val: map.get($var, $key, $varKey);
@if ($val) {
#{$varName}: var(#{$val});
}
}
}
// 生成block
@mixin b($block) {
$b: "." + $namespace + $connect + $block;
#{$b} {
@content;
}
}
// 后代选择器
@mixin d($block) {
$name: "." + $namespace + $connect + $block;
#{$name} {
@content;
}
}
// 同级选择器
@mixin adn($elem) {
$name: "+ ." + $namespace + $connect + $elem;
#{$name} {
@content;
}
}
// 生成elem
@mixin e($elem, $root: true) {
$name: $b + $element-connect + $elem;
@if $root {
@at-root {
#{$name} {
@content;
}
}
} @else {
#{$name} {
@content;
}
}
}
// 生成修改器
@mixin m($attr, $value: "") {
$modifier: $b + $modifier-connect + $attr + $modifier-value-connect + $value;
@at-root {
#{$modifier} {
@content;
}
}
}
// 状态
@mixin s($attrs, $and: true) {
$state: "";
@each $attr in $attrs {
@if $and {
$state: "&" + "." + $state-prefix + $attr + "," + $state;
} @else {
$state: "" + "." + $state-prefix + $attr + "," + $state;
}
}
#{$state} {
@content;
}
}最外层汇总
- styles/common/initRoot.scss
scss
@use "./theme/var.scss" as *;
@use "./theme/mixins.scss" as *;
:root {
// 默认动画0.3S
--y-transition: 0.3s;
// 初始化
@include set-global-var();
// 主色、辅助色
@include set-color();
// 层次色
@include set-color-light();
}
@font-face {
font-family: "Aileron";
src: url("../font/Aileron.otf");
}
// body { font-family: 'Aileron'; }引用
- styles/index.scss
scss
@use "./common/initRoot.scss";
@use "./common/common.scss";
@use "./common/iconfont.scss";
@use "./common/isLoading.scss";
@use "./chooseButton/chooseButton.scss";
@use "./chooseButtonGroup/chooseButtonGroup.scss";