Skip to content

Instantly share code, notes, and snippets.

@makenosound
Created May 3, 2013 02:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makenosound/5506752 to your computer and use it in GitHub Desktop.
Save makenosound/5506752 to your computer and use it in GitHub Desktop.
Modified version of the `spacing-classes` pattern from Stitch. Allows you to specify additional units for output.
// Generate inline class names for your elements
//
// <div class="actions inset-vert-small-large">
// <button type="submit">Totes click me!</button>
// </div>
//
// Padding
// ---------------
// pad-{item} : pad-small (padding: 10px)
// pad-vert-{item}-{item + 1} : pad-vert-small-large (padding: 10px 0 40px 0)
// pad-horz-{item}-{item + 1} : pad-horz-small-large (padding: 0 10px 0 40px)
//
// Margin
// ---------------
// inset-{item} : inset-small (margin: 10px)
// inset-vert-{item}-{item + 1} : inset-vert-small-large (margin: 10px 0 40px 0)
// inset-horz-{item}-{item + 1} : inset-horz-small-large (margin: 0 10px 0 40px)
//
// Offset margin
// ---------------
// offset-{item} : offset-small (margin: -10px)
// offset-vert-{item}-{item + 1} : offset-vert-small-large (margin: -10px 0 -40px 0)
// offset-horz-{item}-{item + 1} : offset-horz-small-large (margin: 0 -10px 0 -40px)
@mixin spacing-classes($base: 10, $list: small normal medium large, $units: px rem) {
@for $i from 1 through length($list) {
.pad-#{nth(unquote($list), $i)} {
@for $u from 1 through length($units) {
$unit: nth(unquote($units), $u);
$divisor: if($unit == rem, 10, 1);
padding: ($base * $i / $divisor)#{$unit};
}
}
.inset-#{nth(unquote($list), $i)} {
@for $u from 1 through length($units) {
$unit: nth(unquote($units), $u);
$divisor: if($unit == rem, 10, 1);
margin: ($base * $i / $divisor)#{$unit};
}
}
.offset-#{nth(unquote($list), $i)} {
@for $u from 1 through length($units) {
$unit: nth(unquote($units), $u);
$divisor: if($unit == rem, 10, 1);
margin: (-($base * $i / $divisor))#{$unit};
}
}
.pad-top-#{nth(unquote($list), $i)} {
@for $u from 1 through length($units) {
$unit: nth(unquote($units), $u);
$divisor: if($unit == rem, 10, 1);
padding-top: ($base * $i / $divisor)#{$unit};
}
}
.pad-right-#{nth(unquote($list), $i)} {
@for $u from 1 through length($units) {
$unit: nth(unquote($units), $u);
$divisor: if($unit == rem, 10, 1);
padding-right: ($base * $i / $divisor)#{$unit};
}
}
.pad-bottom-#{nth(unquote($list), $i)} {
@for $u from 1 through length($units) {
$unit: nth(unquote($units), $u);
$divisor: if($unit == rem, 10, 1);
padding-bottom: ($base * $i / $divisor)#{$unit};
}
}
.pad-left-#{nth(unquote($list), $i)} {
@for $u from 1 through length($units) {
$unit: nth(unquote($units), $u);
$divisor: if($unit == rem, 10, 1);
padding-left: ($base * $i / $divisor)#{$unit};
}
}
.inset-top-#{nth(unquote($list), $i)} {
@for $u from 1 through length($units) {
$unit: nth(unquote($units), $u);
$divisor: if($unit == rem, 10, 1);
margin-top: ($base * $i / $divisor)#{$unit};
}
}
.inset-right-#{nth(unquote($list), $i)} {
@for $u from 1 through length($units) {
$unit: nth(unquote($units), $u);
$divisor: if($unit == rem, 10, 1);
margin-right: ($base * $i / $divisor)#{$unit};
}
}
.inset-bottom-#{nth(unquote($list), $i)} {
@for $u from 1 through length($units) {
$unit: nth(unquote($units), $u);
$divisor: if($unit == rem, 10, 1);
margin-bottom: ($base * $i / $divisor)#{$unit};
}
}
.inset-left-#{nth(unquote($list), $i)} {
@for $u from 1 through length($units) {
$unit: nth(unquote($units), $u);
$divisor: if($unit == rem, 10, 1);
margin-left: ($base * $i / $divisor)#{$unit};
}
}
.outset-top-#{nth(unquote($list), $i)} {
@for $u from 1 through length($units) {
$unit: nth(unquote($units), $u);
$divisor: if($unit == rem, 10, 1);
margin-top: (-($base * $i / $divisor))#{$unit};
}
}
.outset-right-#{nth(unquote($list), $i)} {
@for $u from 1 through length($units) {
$unit: nth(unquote($units), $u);
$divisor: if($unit == rem, 10, 1);
margin-right: (-($base * $i / $divisor))#{$unit};
}
}
.outset-bottom-#{nth(unquote($list), $i)} {
@for $u from 1 through length($units) {
$unit: nth(unquote($units), $u);
$divisor: if($unit == rem, 10, 1);
margin-bottom: (-($base * $i / $divisor))#{$unit};
}
}
.outset-left-#{nth(unquote($list), $i)} {
@for $u from 1 through length($units) {
$unit: nth(unquote($units), $u);
$divisor: if($unit == rem, 10, 1);
margin-left: (-($base * $i / $divisor))#{$unit};
}
}
.inset-#{nth(unquote($list), $i)} {
@for $u from 1 through length($units) {
$unit: nth(unquote($units), $u);
$divisor: if($unit == rem, 10, 1);
margin: ($base * $i / $divisor)#{$unit};
}
}
.offset-#{nth(unquote($list), $i)} {
@for $u from 1 through length($units) {
$unit: nth(unquote($units), $u);
$divisor: if($unit == rem, 10, 1);
margin: (-($base * $i / $divisor))#{$unit};
}
}
.pad-vert-#{nth(unquote($list), $i)} {
@for $u from 1 through length($units) {
$unit: nth(unquote($units), $u);
$divisor: if($unit == rem, 10, 1);
padding-top: ($base * $i / $divisor)#{$unit};
padding-bottom: ($base * $i / $divisor)#{$unit};
}
}
.pad-horz-#{nth(unquote($list), $i)} {
@for $u from 1 through length($units) {
$unit: nth(unquote($units), $u);
$divisor: if($unit == rem, 10, 1);
padding-right: ($base * $i / $divisor)#{$unit};
padding-left: ($base * $i / $divisor)#{$unit};
}
}
.inset-vert-#{nth(unquote($list), $i)} {
@for $u from 1 through length($units) {
$unit: nth(unquote($units), $u);
$divisor: if($unit == rem, 10, 1);
margin-top: ($base * $i / $divisor)#{$unit};
margin-bottom: ($base * $i / $divisor)#{$unit};
}
}
.inset-horz-#{nth(unquote($list), $i)} {
@for $u from 1 through length($units) {
$unit: nth(unquote($units), $u);
$divisor: if($unit == rem, 10, 1);
margin-right: ($base * $i / $divisor)#{$unit};
margin-left: ($base * $i / $divisor)#{$unit};
}
}
.offset-vert-#{nth(unquote($list), $i)} {
@for $u from 1 through length($units) {
$unit: nth(unquote($units), $u);
$divisor: if($unit == rem, 10, 1);
margin-top: (-($base * $i / $divisor))#{$unit};
margin-bottom: (-($base * $i / $divisor))#{$unit};
}
}
.offset-horz-#{nth(unquote($list), $i)} {
@for $u from 1 through length($units) {
$unit: nth(unquote($units), $u);
$divisor: if($unit == rem, 10, 1);
margin-left: (-($base * $i / $divisor))#{$unit};
margin-right: (-($base * $i / $divisor))#{$unit};
}
}
@for $j from 1 through length($list) {
@if ($i != $j) {
.pad-vert-#{nth(unquote($list), $i)}-#{nth(unquote($list), ($j))} {
@for $u from 1 through length($units) {
$unit: nth(unquote($units), $u);
$divisor: if($unit == rem, 10, 1);
padding-top: ($base * $i / $divisor)#{$unit};
padding-bottom: ($base * $j)#{$unit};
}
}
.pad-horz-#{nth(unquote($list), $i)}-#{nth(unquote($list), ($j))} {
@for $u from 1 through length($units) {
$unit: nth(unquote($units), $u);
$divisor: if($unit == rem, 10, 1);
padding-right: ($base * $i / $divisor)#{$unit};
padding-left: ($base * $j)#{$unit};
}
}
.inset-vert-#{nth(unquote($list), $i)}-#{nth(unquote($list), ($j))} {
@for $u from 1 through length($units) {
$unit: nth(unquote($units), $u);
$divisor: if($unit == rem, 10, 1);
margin-top: ($base * $i / $divisor)#{$unit};
margin-bottom: ($base * $j)#{$unit};
}
}
.inset-horz-#{nth(unquote($list), $i)}-#{nth(unquote($list), ($j))} {
@for $u from 1 through length($units) {
$unit: nth(unquote($units), $u);
$divisor: if($unit == rem, 10, 1);
margin-right: ($base * $i / $divisor)#{$unit};
margin-left: ($base * $j)#{$unit};
}
}
.offset-vert-#{nth(unquote($list), $i)}-#{nth(unquote($list), ($j))} {
@for $u from 1 through length($units) {
$unit: nth(unquote($units), $u);
$divisor: if($unit == rem, 10, 1);
margin-top: (-($base * $i / $divisor))#{$unit};
margin-bottom: (-($base * $j))#{$unit};
}
}
.offset-horz-#{nth(unquote($list), $i)}-#{nth(unquote($list), ($j))} {
@for $u from 1 through length($units) {
$unit: nth(unquote($units), $u);
$divisor: if($unit == rem, 10, 1);
margin-right: (-($base * $i / $divisor))#{$unit};
margin-left: (-($base * $j))#{$unit};
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment