Skip to content

Instantly share code, notes, and snippets.

@samthurman
Created October 22, 2014 15:12
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/fba484101ff72848c803 to your computer and use it in GitHub Desktop.
Save samthurman/fba484101ff72848c803 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
// ----
// Sass (v3.4.6)
// Compass (v1.0.1)
// ----
// THE SELF-AWARE MIXIN
//
// Should you use a mixin or an extend?
// This mixin makes that question moot.
// It checks to see if your mixin config exists
// if it does it extends a placeholder ID
// with that config.
//
// Modified from Lu Nelson's example
// 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 self-aware($my-args) {
// look up these arguments as a key in the global map
$id: map-get($mixin-configurations, $my-args);
// 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
$mixin-configurations: map-merge($mixin-configurations, ($my-args: $id)) !global;
//create a new placeholder
@include create-placeholder($id, $my-args);
// extend that placeholder
@extend %#{$id};
}
}
@mixin create-placeholder($id, $args){
// create a placeholder named after the ID at root
@at-root {
%#{$id} {
@each $argument, $value in $args {
#{$argument}: $value;
}
}
}
}
@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 self-aware($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