Last active
August 29, 2015 14:06
-
-
Save whizark/5dd0db8d8c0d46391a59 to your computer and use it in GitHub Desktop.
Sass: Passing/Getting property value(s) into/inside mixins/functions #sass
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
<a class="button">An example link</a> | |
<a class="button--ghost">An example link</a> |
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.3) | |
// Compass (v1.0.1) | |
// ---- | |
// Sass: Passing/Getting property value(s) to/in mixins/functions | |
// Property dumper mixin | |
@mixin properties($properties) { | |
@each $property, $value in $properties { | |
@if str-index($property, '-my-') != 1 { | |
#{$property}: $value; | |
} | |
} | |
} | |
// Component mixins | |
// Button base mixin | |
@mixin my-button--base($propeties: ()) { | |
display: inline-block; | |
padding: .25em; | |
} | |
// Default button mixin | |
@mixin my-button--default($properties: ()) { | |
$defaults: ( | |
-my-color: null | |
); | |
$properties: map-merge($defaults, $properties); | |
$color : map-get($properties, '-my-color'); | |
// CSS Variables | |
// --color : $color; | |
color : #fff; | |
background: $color; | |
} | |
// Ghost button mixin | |
@mixin my-button--ghost($properties: ()) { | |
$defaults: ( | |
-my-color: null | |
); | |
$properties : map-merge($defaults, $properties); | |
$color : map-get($properties, '-my-color'); | |
$font-weight : normal; | |
$border-width: 1px; | |
// Get CSS font-weight value | |
@if map-has-key($properties, 'font-weight') { | |
$font-weight: map-get($properties, 'font-weight'); | |
} | |
@if $font-weight == 'bold' { | |
$border-width: 2px; | |
} | |
// CSS Variables | |
// --color : $color; | |
color : $color; | |
background: transparent; | |
border : $border-width solid $color; | |
} | |
// Component mixin (API) | |
@mixin my-button($properties: ()) { | |
$defaults: ( | |
-my-type: 'default' | |
); | |
$properties: map-merge($defaults, $properties); | |
$type : map-get($properties, '-my-type'); | |
@include my-button--base($properties); | |
@if $type == 'ghost' { | |
@include my-button--ghost($properties); | |
} @else { | |
@include my-button--default($properties); | |
} | |
} | |
// Component definitions | |
// Default button component | |
%my-button { | |
$properties: ( | |
-my-color : #f00, | |
font-weight: bold | |
); | |
@include my-button($properties); | |
@include properties($properties); | |
} | |
// Ghost button component | |
%my-button--ghost { | |
$properties: ( | |
-my-type : ghost, | |
-my-color : #f00, | |
font-weight: bold | |
); | |
@include my-button($properties); | |
@include properties($properties); | |
} | |
// Mapping components to concrete classes | |
.button { | |
@extend %my-button; | |
} | |
.button--ghost { | |
@extend %my-button--ghost; | |
} |
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
.button { | |
display: inline-block; | |
padding: .25em; | |
color: #fff; | |
background: #f00; | |
font-weight: bold; | |
} | |
.button--ghost { | |
display: inline-block; | |
padding: .25em; | |
color: #f00; | |
background: transparent; | |
border: 2px solid #f00; | |
font-weight: bold; | |
} |
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
<a class="button">An example link</a> | |
<a class="button--ghost">An example link</a> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment