Skip to content

Instantly share code, notes, and snippets.

@samthurman
Created October 22, 2014 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samthurman/4b2d27dfd284f8fcf23a to your computer and use it in GitHub Desktop.
Save samthurman/4b2d27dfd284f8fcf23a to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
// ----
// Sass (v3.4.6)
// Compass (v1.0.1)
// ----
// THE SMART EXTEND
//
// If you have multiple selectors with the same
// mixin config "Smart Extend" will group their
// styles together.
//
// Modified from Lu Nelson's "Self Aware Mixin"
// http://lunelson.roughdraft.io/cc1b431a69360949dc01-the-self-aware-sass-mixin
// global map stores every mixin configuration
$mixin-configurations: ();
// define mixin with any format of arguments
@mixin smart-extend($mixin-configuration) {
// look up these arguments as a key in the global map
$id: map-get($mixin-configurations, $mixin-configuration);
// if they return an 'ID'...
@if $id {
// extend that ID
@extend %#{$id};
} @else {
// create the ID
$id: unique-id();
// merge the ID in to the mixin's map against the ARGS
@include add-configuration($mixin-configuration, $id);
//create a new placeholder
@include create-placeholder($id, $mixin-configuration);
// extend that placeholder
@extend %#{$id};
}
}
@mixin add-configuration($mixin-configuration, $id) {
$mixin-configurations: map-merge($mixin-configurations, ($mixin-configuration: $id)) !global;
}
@mixin create-placeholder($id, $args){
// create a placeholder named after the ID at root
@at-root {
%#{$id} {
@each $argument, $value in $args {
#{$argument}: $value;
}
}
}
}
// USAGE EXAMPLE
@mixin border-radius($radius: 0.5em) {
// pass self aware mixin an array of arguments
$args: (
-webkit-border-radius: $radius,
-moz-border-radius: $radius,
border-radius: $radius
);
@include smart-extend($args);
}
.shared1 {
@include border-radius(2px);
}
.shared2 {
@include border-radius(2px);
}
.unique {
@include border-radius(5px);
}
.shared1, .shared2 {
-webkit-border-radius: 2px;
-moz-border-radius: 2px;
border-radius: 2px;
}
.unique {
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment