Skip to content

Instantly share code, notes, and snippets.

@whizark
Created September 6, 2014 09:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whizark/e2281eadfa6b3a22caf0 to your computer and use it in GitHub Desktop.
Save whizark/e2281eadfa6b3a22caf0 to your computer and use it in GitHub Desktop.
Sass: Dependency Injection into Mixins #sass
<div class="container--micro">
<!-- The style attributes only for the purpose of the demo -->
<div style="float: left; height: 100px; background: #0f0;">
content
</div>
<div style="float: left; height: 100px; background: #00f;">
content
</div>
</div>
<div class="container--overflow">
<!-- The style attributes only for the purpose of the demo -->
<div style="float: left; height: 100px; background: #0f0;">
content
</div>
<div style="float: left; height: 100px; background: #00f;">
content
</div>
</div>
// ----
// Sass (v3.4.3)
// Compass (v1.0.1)
// ----
// Sass: Dependency Injection into Mixins
// Component mixins
// Micro clearfix mixin
@mixin my-clearfix--micro() {
&:before,
&:after {
content: " ";
display: table;
}
&:after {
clear: both;
}
}
// Legacy overflow clearfix mixin
@mixin my-clearfix--overflow() {
overflow: hidden;
}
// Default button mixin
@mixin my-container($properties: ()) {
$defaults: (
// The default dependency (compoent name)
-my-clearfix: 'my-clearfix--micro'
);
$properties: map-merge($defaults, $properties);
$clearfix : map-get($properties, '-my-clearfix');
@extend %#{$clearfix};
margin : 1em;
padding : 1em;
background: #f00;
}
// Component definitions
// Micro clearfix component
%my-clearfix--micro {
@include my-clearfix--micro();
}
// Overflow clearfix component
%my-clearfix--overflow {
@include my-clearfix--overflow();
}
// Micro clearfix container component
%my-container--micro {
$properties: ();
@include my-container($properties);
}
// Overflow clearfix container component
%my-container--overflow {
$properties: (
// The dependant (component name)
-my-clearfix: 'my-clearfix--overflow'
);
@include my-container($properties);
}
// Mapping components to concrete classes
.container--micro {
@extend %my-container--micro;
}
.container--overflow {
@extend %my-container--overflow;
}
.container--micro:before, .container--micro:after {
content: " ";
display: table;
}
.container--micro:after {
clear: both;
}
.container--overflow {
overflow: hidden;
}
.container--micro {
margin: 1em;
padding: 1em;
background: #f00;
}
.container--overflow {
margin: 1em;
padding: 1em;
background: #f00;
}
<div class="container--micro">
<!-- The style attributes only for the purpose of the demo -->
<div style="float: left; height: 100px; background: #0f0;">
content
</div>
<div style="float: left; height: 100px; background: #00f;">
content
</div>
</div>
<div class="container--overflow">
<!-- The style attributes only for the purpose of the demo -->
<div style="float: left; height: 100px; background: #0f0;">
content
</div>
<div style="float: left; height: 100px; background: #00f;">
content
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment