用 scss 练习编写一个样式库,有什么原则可以遵守呢?自己生成的 css 文件庞大无比

2018-03-06 08:53:00 +08:00
 vevlins

我有几个具体的问题: 1.这种在一个组件文件内分 mixin 的做法合适吗?与下一个对应,划分的一个原因是希望 mixin 内写代码,下面写嵌套结构。但是我看到有人说尽量少用 mixin。

2.从速度上来说不要嵌套结构会更快,但是嵌套的话我觉得更有助于开发的时候不乱章法,作为一个样式库,应该通过嵌套进行限制防止滥用还是样式不做嵌套,但是给出正确的示例代码,要求使用者按照示例代码来?

摘取一段代码,是用来实现右上角提醒框的。

scss:

@mixin notice(){
    padding: 1.6rem;
    border: 1px solid rgba($default-color,.3);
    width: 30rem;
    max-width: 90%;
    position: fixed;
    top: 2rem;
    right: 2rem;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
    overflow: hidden;
    border-radius: 5px;
    z-index: 2000;
}

@mixin notice-color($background-color:$white-color,$color:$white-color){
    background-color: $background-color;
    color: $color;
}

@mixin notice-head(){
    height: 1.8rem;
    margin: 0 0 1rem 0;
    position: relative;
}

@mixin notice-title(){
    height: 1.8rem;
    overflow: hidden;
    text-overflow: ellipsis;
    position: absolute;
    word-break: keep-all;
    left: 0;
    top:0;
    right: 2rem;
    font-weight: 600;
    margin: 0 2rem 0 0;
}

@mixin notice-close(){
    font-family: 'icomoon';
    vertical-align: middle;
    position: absolute;
    right: 0;
    opacity: .5;
    &:hover{
        cursor: pointer;
    }
    &:before{
        content: "\e91e";
    }
}

@mixin notice-content(){
    font-size: 1.4rem;
    overflow: hidden;
    margin: 0;
    word-break:break-all;
}

.#{$prefix}notice {
    & {
        @include notice();
        @include notice-color($background-color:$white-color,$color:$black-color);
    }
    &.#{$prefix}notice-primary{
        @include notice-color($primary-color);
    }
    &.#{$prefix}notice-success{
        @include notice-color($success-color);
    }
    &.#{$prefix}notice-warn{
        @include notice-color($warn-color);
    }
    &.#{$prefix}notice-danger{
        @include notice-color($danger-color);
    }
    & .#{$prefix}notice-head {
        @include notice-head();
    }
    & .#{$prefix}notice-title {
        @include notice-title();
    }
    & .#{$prefix}notice-close {
        @include notice-close();
    }
    & .#{$prefix}notice-content {
        @include notice-content();
    }
}

生成的 css

.p-notice {
  padding: 1.6rem;
  border: 1px solid rgba(212, 212, 212, 0.3);
  width: 30rem;
  max-width: 90%;
  position: fixed;
  top: 2rem;
  right: 2rem;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  overflow: hidden;
  border-radius: 5px;
  z-index: 2000;
  background-color: #ffffff;
  color: #000000; }

.p-notice.p-notice-primary {
  background-color: #29B6F6;
  color: #ffffff; }

.p-notice.p-notice-success {
  background-color: #66BB6A;
  color: #ffffff; }

.p-notice.p-notice-warn {
  background-color: #FFCA28;
  color: #ffffff; }

.p-notice.p-notice-danger {
  background-color: #EF5350;
  color: #ffffff; }

.p-notice .p-notice-head {
  height: 1.8rem;
  margin: 0 0 1rem 0;
  position: relative; }

.p-notice .p-notice-title {
  height: 1.8rem;
  overflow: hidden;
  text-overflow: ellipsis;
  position: absolute;
  word-break: keep-all;
  left: 0;
  top: 0;
  right: 2rem;
  font-weight: 600;
  margin: 0 2rem 0 0; }

.p-notice .p-notice-close {
  font-family: 'icomoon';
  vertical-align: middle;
  position: absolute;
  right: 0;
  opacity: .5; }
  .p-notice .p-notice-close:hover {
    cursor: pointer; }
  .p-notice .p-notice-close:before {
    content: "\e91e"; }

.p-notice .p-notice-content {
  font-size: 1.4rem;
  overflow: hidden;
  margin: 0;
  word-break: break-all; }

1442 次点击
所在节点    问与答
9 条回复
newbieo0O
2018-03-06 09:02:47 +08:00
CSS 除非较多的动画,其他对机器性能没啥影响。子类命名不用再添加主类的名字了。 .intro>.header / .intro>.desc
qiaobeier
2018-03-06 09:02:48 +08:00
yangg
2018-03-06 09:52:01 +08:00
为什么每个 notice 类型都要 color:#fff, 先学 css 模块化,而不是 scss

scss 还可以用 extend
vevlins
2018-03-06 09:57:01 +08:00
@yangg 这里是因为默认的背景色是白色,它的文字颜色需要黑色,其他颜色背景的文字需要白色。所以要么把默认的声明为额外的 p-notice-default,要么把其他的挨个声明#fff。 我刚才又看了一遍也注意到这个问题,但是没想到好的解决方案。
yangg
2018-03-06 09:57:39 +08:00
另外 mixin 是代码里的方法或函数,如果里面 的内容都最小单元了,别人也不引用,就不需要用了

推荐看下 bootstrap 的组件结构
vevlins
2018-03-06 10:00:55 +08:00
@yangg 谢谢!
kamal
2018-03-06 11:04:54 +08:00
这样写没什么问题,SCSS 就是为了方便写才出生的。
硬要挑问题的话,上面的代码抽象太多了。比如这里
& .#{$prefix}notice-head {
@include notice-head();
}
& .#{$prefix}notice-title {
@include notice-title();
}
& .#{$prefix}notice-close {
@include notice-close();
}
& .#{$prefix}notice-content {
@include notice-content();
}

这四个 include,如果没有别处用到,就不需要抽象 mixin 了吧。(这样做也没啥影响,就算是为了好看,也算理由吧)
kamal
2018-03-06 11:06:30 +08:00
以前翻译的一篇文章 https://yukun.im/css/607 《 Sass @extend 还是 Mixin 》
vevlins
2018-03-06 11:25:09 +08:00
@kamal 谢谢!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/435179

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX