Skip to content

Instantly share code, notes, and snippets.

@vivid-web
Last active May 16, 2023 10:22
Show Gist options
  • Save vivid-web/8f7a23fe8b172b4238b6 to your computer and use it in GitHub Desktop.
Save vivid-web/8f7a23fe8b172b4238b6 to your computer and use it in GitHub Desktop.
BEM Helper (SCSS, SASS, LESS, Stylus)
// Mixins
.has(@element; @content) {
&__@{element} {
@content();
}
}
.variant(@modifier; @content) {
&--@{modifier} {
@content();
}
}
// Example
.Alert {
padding: 20px;
position: relative;
width: 500px;
.variant(danger; {
background-color: #c00;
color: #300;
});
.has(close-button; {
display: inline-block;
position: absolute;
right: 20px;
top: 20px;
.variant(danger; {
color: #c00;
});
});
}
// Mixins
=has($element)
@at-root #{&}__#{$element}
@content
=variant($modifier)
@at-root #{&}--#{$modifier}
@content
// Example
.Alert
padding: 20px
position: relative
width: 500px
+variant(danger)
background-color: #c00
color: #300
+has(close-button)
display: inline-block
position: absolute
right: 20px
top: 20px
+variant(danger)
color: #c00;
// Mixins
@mixin has($element) {
&__#{$element} {
@content;
}
}
@mixin variant($modifier) {
&--#{$modifier} {
@content;
}
}
// Example
.Alert {
padding: 20px;
position: relative;
width: 500px;
@include variant(danger) {
background-color: #c00;
color: #300;
}
@include has(close-button) {
display: inline-block;
position: absolute;
right: 20px;
top: 20px;
@include variant(danger) {
color: #c00;
}
}
}
// Mixins
has($element)
/&__{$element}
{block}
variant($modifier)
/&--{$modifier}
{block}
// Example
.Alert
padding: 20px
position: relative
width: 500px
+variant(danger)
background-color: #c00
color: #300
+has(close-button)
display: inline-block
position: absolute
right: 20px
top: 20px
+has(danger)
color: #c00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment