Skip to content

Commit 0bbcb63

Browse files
author
dddssw
committed
sass
2 parents ad21073 + 79e23a0 commit 0bbcb63

File tree

2 files changed

+262
-0
lines changed

2 files changed

+262
-0
lines changed

configure/component.md

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,206 @@ const ns = useNamespace('affix')
256256
background-color: var(--ep-c-bg-row);
257257
}
258258
```
259+
## 组件库 sass
260+
### 主题色
261+
```sass
262+
$colors: () !default;
263+
$colors: map.deep-merge(
264+
(
265+
'white': #ffffff,
266+
'black': #000000,
267+
'primary': (
268+
'base': #409eff,
269+
),
270+
'success': (
271+
'base': #67c23a,
272+
),
273+
'warning': (
274+
'base': #e6a23c,
275+
),
276+
'danger': (
277+
'base': #f56c6c,
278+
),
279+
'error': (
280+
'base': #f56c6c,
281+
),
282+
'info': (
283+
'base': #909399,
284+
),
285+
),
286+
$colors
287+
);
288+
```
289+
### 主题色层次
290+
也就是主题色的10%到90%的变化, 使用 `mix` 来混合颜色
291+
```scss
292+
@mixin set-color-mix-level(
293+
$type,
294+
$number,
295+
$mode: 'light',
296+
$mix-color: $color-white
297+
) {
298+
$colors: map.deep-merge(
299+
(
300+
$type: (
301+
'#{$mode}-#{$number}':
302+
color.mix(
303+
$mix-color,
304+
map.get($colors, $type, 'base'),
305+
math.percentage(math.div($number, 10))
306+
),
307+
),
308+
),
309+
$colors
310+
) !global;
311+
}
312+
313+
@each $type in $types {
314+
@for $i from 1 through 9 {
315+
@include set-color-mix-level($type, $i, 'light', $color-white);
316+
}
317+
}
318+
319+
// --el-color-primary-dark-2,暗黑模式
320+
@each $type in $types {
321+
@include set-color-mix-level($type, 2, 'dark', $color-black);
322+
}
323+
```
324+
### :root伪类
325+
通过这个伪类中自定义全局的 `css` 变量,一般以 `--` 开头, 可以确保他们在整个文档中全局可用
326+
```scss
327+
:root {
328+
--a:#fff
329+
}
330+
background-color:var(--a)
331+
```
332+
### 利用 sass 生成 :root 变量
333+
定义前缀,块,修改器变量,因为是BEM规范
334+
```scss
335+
$namespace: 'el' !default;
336+
$common-separator: '-' !default;
337+
$element-separator: '__' !default;
338+
$modifier-separator: '--' !default;
339+
$state-prefix: 'is-' !default;
340+
```
341+
将色彩映射到root选择器中
342+
343+
:::code-group
344+
```var.scss
345+
@use 'mixins/var' as *;
346+
@use '../common/var' as *;
347+
348+
:root {
349+
color-scheme: light;
350+
351+
// --el-color-#{$type}
352+
// --el-color-#{$type}-light-{$i}
353+
@each $type in (primary, success, warning, danger, error, info) {
354+
@include set-css-color-type($colors, $type);//$colors是一个map
355+
}
356+
}
357+
```
358+
```mixins/_var.scss
359+
@use 'function' as *;
360+
361+
@mixin set-css-color-type($colors, $type) {
362+
@include set-css-var-value(('color', $type), map.get($colors, $type, 'base'));
363+
364+
@each $i in (3, 5, 7, 8, 9) {
365+
@include set-css-var-value(
366+
('color', $type, 'light', $i),
367+
map.get($colors, $type, 'light-#{$i}')
368+
);
369+
}
370+
371+
@include set-css-var-value(
372+
('color', $type, 'dark-2'),
373+
map.get($colors, $type, 'dark-2')
374+
);
375+
}
376+
377+
// set css var value, because we need translate value to string
378+
// for example:
379+
// @include set-css-var-value(('color', 'primary'), red);
380+
// --el-color-primary: red;
381+
@mixin set-css-var-value($name, $value) {
382+
#{joinVarName($name)}: #{$value};
383+
}
384+
```
385+
```mixins/function.scss
386+
// join var name
387+
// joinVarName(('button', 'text-color')) => '--el-button-text-color'
388+
@function joinVarName($list) {
389+
$name: '--' + config.$namespace;
390+
@each $item in $list {
391+
@if $item != '' {
392+
$name: $name + '-' + $item;
393+
}
394+
}
395+
@return $name;
396+
}
397+
```
398+
:::
399+
400+
### 使用root里的变量名词
401+
:::code-group
402+
```mixins/function.scss
403+
// getCssVar('button', 'text-color') => var(--el-button-text-color)
404+
@function getCssVar($args...) {
405+
@return var(#{joinVarName($args)});
406+
}
407+
```
408+
```example
409+
color: getCssVar('color', 'primary')
410+
```
411+
:::
412+
413+
对于组件库的样式,可以通过class或者style来修改.比如el-row的justify属性,他会声明不同的样式
414+
:::code-group
415+
```row.scss
416+
@use 'common/var' as *;
417+
@use 'mixins/mixins' as *;
418+
@use 'mixins/utils' as *;
419+
420+
@include b(row) {
421+
display: flex;
422+
flex-wrap: wrap;
423+
position: relative;
424+
box-sizing: border-box;
425+
426+
@include when(justify-center) {
427+
justify-content: center;
428+
}
429+
@include when(justify-end) {
430+
justify-content: flex-end;
431+
}
432+
@include when(justify-space-between) {
433+
justify-content: space-between;
434+
}
435+
@include when(justify-space-around) {
436+
justify-content: space-around;
437+
}
438+
@include when(justify-space-evenly) {
439+
justify-content: space-evenly;
440+
}
441+
@include when(align-top) {
442+
align-items: flex-start;
443+
}
444+
@include when(align-middle) {
445+
align-items: center;
446+
}
447+
@include when(align-bottom) {
448+
align-items: flex-end;
449+
}
450+
}
451+
```
452+
```mixin.scss
453+
@mixin when($state) {
454+
@at-root {
455+
&.#{$state-prefix + $state} {
456+
@content;
457+
}
458+
}
459+
}
460+
```
461+
:::

packages/sass.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,31 @@ layout: doc
1212
| `$var: "A"; $var: "B"` | `$var = "B"` | 直接覆盖原值 |
1313
| `$var: "A"; $var: "B" !default` | `$var = "A"` | 保留原值(默认值不生效) |
1414
| `$var: null; $var: "B" !default` | `$var = "B"` | `null` 视为未定义,应用默认值 |
15+
16+
::: code-group
17+
```_library.scss
18+
$black: #000 !default;
19+
$border-radius: 0.25rem !default;
20+
$box-shadow: 0 0.5rem 1rem rgba($black, 0.15) !default;
21+
22+
code {
23+
border-radius: $border-radius;
24+
box-shadow: $box-shadow;
25+
}
26+
```
27+
```style.scss
28+
@use 'library' with (
29+
$black: #222,
30+
$border-radius: 0.1rem
31+
);
32+
```
33+
```res.css
34+
code {
35+
border-radius: 0.1rem;
36+
box-shadow: 0 0.5rem 1rem rgba(34, 34, 34, 0.15);
37+
}
38+
```
39+
:::
1540
## 插值
1641
`#{}`, 插值始终返回未加引号的 字符串
1742
```scss
@@ -140,3 +165,37 @@ a {
140165
}
141166
```
142167
:::
168+
169+
## 变量的数据类型
170+
### map
171+
::: code-group
172+
```查找
173+
@use "sass:map";
174+
$font-weights:() !default
175+
$font-weights: ("regular": 400, "medium": 500, "bold": 700);
176+
177+
@debug map.get($font-weights, "medium"); // 500
178+
@debug map.get($font-weights, "extra-bold"); // null
179+
```
180+
```循环
181+
$icons: ("eye": "\f112", "start": "\f12e", "stop": "\f12f");
182+
183+
@each $name, $glyph in $icons {
184+
.icon-#{$name}:before {
185+
display: inline-block;
186+
font-family: "Icon Font";
187+
content: $glyph;
188+
}
189+
}
190+
```
191+
```设置
192+
@use "sass:map";
193+
194+
$font-weights: ("regular": 400, "medium": 500, "bold": 700);
195+
196+
@debug map.set($font-weights, "extra-bold", 900);
197+
// ("regular": 400, "medium": 500, "bold": 700, "extra-bold": 900)
198+
@debug map.set($font-weights, "bold", 900);
199+
// ("regular": 400, "medium": 500, "bold": 900)
200+
```
201+
:::

0 commit comments

Comments
 (0)