Combine extend and mixin in with same rules

2020-02-16 04:01发布

Okey! I have couple of extends in sass like

%heading
%paragraph
%gutter

and so on...

I want to reuse thouse in media queries, but that doesnt work. I know that.

Then i came up with the idea to have all my extends as mixins too. So when i want them in a media query i simply use mixin. for example

.my-widget {
    @extend %gutter;
    @media.... {
        @include gutter-other;
    }
}

and because i dont want to write all my rules again. How do i write my sass then?

I tried

%my-extend, @mixin my-extend {
   ...
}

but that didnt work.

Any ideas how to work with this?

1条回答
叛逆
2楼-- · 2020-02-16 04:18

No, you can't combine them that way. You'll have to write a mixin that is invoked by your extend class and anything inside of a media query.

@mixin my-extend {
    background: yellow;
}

%my-extend {
    @include my-extend;
}

.foo {
    @extend %my-extend;
}

.bar {
    @extend %my-extend;
}

.baz {
    @media (min-width: 30em) {
        @include my-extend;
    }
}

Output:

.foo, .bar {
  background: yellow;
}

@media (min-width: 30em) {
  .baz {
    background: yellow;
  }
}
查看更多
登录 后发表回答