Skip to content

Instantly share code, notes, and snippets.

@jakob-e
Last active August 29, 2015 14:10
Show Gist options
  • Save jakob-e/b4468c11e6f89d964dc1 to your computer and use it in GitHub Desktop.
Save jakob-e/b4468c11e6f89d964dc1 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com. http://sassmeister.com/gist/b4468c11e6f89d964dc1
<h1>Flexible mixin arguments</h1>
<p>A small helper function to make your mixin take almost anything thrown at it</p>
<ul>
<li>Keyword</li>
<li>Map</li>
<li>List</li>
<li>Arguments</li>
</ul>
// ----
// Sass (v3.4.7)
// Compass (v1.0.1)
// ----
//
// Map arguments helper
//
@function map-arguments($keys, $arglist){
$map: keywords($arglist);
@if inspect($map)=='()' and length($arglist) > 0 {
@if type-of(nth($arglist,1))==map{
$map:nth($arglist,1);
}
@else {
$list:if(type-of(nth($arglist,1))==list,nth($arglist,1),$arglist);
@for $i from 1 through length($list){
$map:map-merge($map,(nth($keys,$i):nth($list,$i)));
}
}
}
@return $map;
}
//
// Mixin sample
//
@mixin size($arglist...) {
$map : map-arguments(width height, $arglist);
$width : map-get($map, width);
$height: map-get($map, height);
$height: $width !default;
width : $width;
height : $height;
}
//
// Use examples
//
.keyword-arguments{
/* Only width */
@include size($width: width-value);
/* Only height */
@include size($height: height-value);
/* Normal order */
@include size($width: width-value, $height: height-value);
/* Reversed order */
@include size($height: height-value, $width: width-value);
}
.map-arguments {
/* Only width */
$map:(width: width-value);
@include size($map);
/* Only height */
$map:(width: width-value, height: height-value);
@include size($map);
/* Normal order */
$map:(width: width-value, height: height-value);
@include size($map);
/* Reversed order */
$map:(height: height-value, width: width-value);
@include size($map);
}
.list-arguments {
/* Only width */
$list: width-value;
@include size($list);
/* Comma */
$list: width-value, height-value;
@include size($list);
/* Space */
$list: width-value height-value;
@include size($list);
}
.arguments {
/* Only width */
@include size(width-value);
/* Width and height */
@include size(width-value, height-value);
}
*{ font-family:sans-serif;}
.keyword-arguments {
/* Only width */
width: width-value;
height: width-value;
/* Only height */
height: height-value;
/* Normal order */
width: width-value;
height: height-value;
/* Reversed order */
width: width-value;
height: height-value;
}
.map-arguments {
/* Only width */
width: width-value;
height: width-value;
/* Only height */
width: width-value;
height: height-value;
/* Normal order */
width: width-value;
height: height-value;
/* Reversed order */
width: width-value;
height: height-value;
}
.list-arguments {
/* Only width */
width: width-value;
height: width-value;
/* Comma */
width: width-value;
height: height-value;
/* Space */
width: width-value;
height: height-value;
}
.arguments {
/* Only width */
width: width-value;
height: width-value;
/* Width and height */
width: width-value;
height: height-value;
}
* {
font-family: sans-serif;
}
<h1>Flexible mixin arguments</h1>
<p>A small helper function to make your mixin take almost anything thrown at it</p>
<ul>
<li>Keyword</li>
<li>Map</li>
<li>List</li>
<li>Arguments</li>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment