@@ -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+ :::
0 commit comments