Created
May 5, 2014 18:30
-
-
Save johnhunter/11544102 to your computer and use it in GitHub Desktop.
DRY sass example
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
// Allow mixins that share common properties in a single css rule | |
// store autogenerated selectors that map | |
$Placeholder-Selectors: (); | |
@mixin button($color, $extend: true) { | |
@include button-static($extend); | |
background-color: $color; | |
border-color: mix(black, $color, 25%); | |
&:hover { | |
background-color: mix(black, $color, 15%); | |
border-color: mix(black, $color, 40%); | |
} | |
} | |
@mixin button-static($extend: true) { | |
$button-selector: map-get($Placeholder-Selectors, 'button'); | |
@if $extend == true { | |
@if $button-selector == null { | |
// create a unique placeholder selector | |
$button-selector: unique-id(); | |
$Placeholder-Selectors: map-merge($Placeholder-Selectors, ('button': $button-selector)) !global; | |
// add a placeholder class that includes this mixin properties | |
@at-root %#{$button-selector} { | |
@include button-static(false); | |
} | |
} | |
// default behavior is to extend the placeholder | |
@extend %#{$button-selector}; | |
} | |
@else { | |
border: 1px solid; | |
border-radius: 5px; | |
padding: .25em .5em; | |
&:hover { | |
cursor: pointer; | |
} | |
} | |
} | |
// Use | |
.foo { | |
@include button(#f00); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment