Created
October 22, 2014 15:48
-
-
Save samthurman/4b2d27dfd284f8fcf23a to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ---- | |
// 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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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