Skip to content

Instantly share code, notes, and snippets.

@vctrtvfrrr
Last active March 12, 2019 14:49
Show Gist options
  • Save vctrtvfrrr/e0576b755a49ef3682354091c6911dcb to your computer and use it in GitHub Desktop.
Save vctrtvfrrr/e0576b755a49ef3682354091c6911dcb to your computer and use it in GitHub Desktop.
Sass spacing helper
$sizeUnit: rem
$marginKey: "m"
$paddingKey: "p"
$separator: "-"
$sizes: ("none", 0), ("auto", auto), ("xxs", 0.125), ("xs", 0.25), ("sm", 0.5), ("md", 1), ("lg", 2), ("xl", 4), ("xxl", 8)
$positions: ("t", "top"), ("r", "right"), ("b", "bottom"), ("l", "left")
@function sizeValue($key, $value)
@return if($key == "none" or $key == "auto", $value, $value + $sizeUnit)
@each $size in $sizes
$sizeKey: nth($size, 1)
$sizeValue: nth($size, 2)
.#{$marginKey}#{$separator}#{$sizeKey}
margin: sizeValue($sizeKey, $sizeValue) !important
@if ($sizeKey != "auto")
.#{$paddingKey}#{$separator}#{$sizeKey}
padding: sizeValue($sizeKey, $sizeValue) !important
@each $position in $positions
$posKey: nth($position, 1)
$posValue: nth($position, 2)
.#{$marginKey}#{$posKey}#{$separator}#{$sizeKey}
margin-#{$posValue}: sizeValue($sizeKey, $sizeValue) !important
@if ($sizeKey != "auto")
.#{$paddingKey}#{$posKey}#{$separator}#{$sizeKey}
padding-#{$posValue}: sizeValue($sizeKey, $sizeValue) !important
// Alias for margin auto horizontaly and verticaly
.mh-auto
@extend .ml-auto
@extend .mr-auto
.mv-auto
@extend .mt-auto
@extend .mb-auto
@hellokvn
Copy link

hellokvn commented Aug 5, 2018

With Bulma naming style:

$sizeUnit: rem;
$marginKey: "has-margin";
$paddingKey: "has-padding";
$separator: "-";
$sizes: (
  ("none", 0),
  ("xxs", 0.125),
  ("xs", 0.25),
  ("sm", 0.5),
  ("md", 1),
  ("lg", 2),
  ("xl", 4),
  ("xxl", 8)
);
$positions: (
  ("top", "top"),
  ("right", "right"),
  ("bottom", "bottom"),
  ("left", "left")
);

@function sizeValue($key, $value) {
  @return if($key == "none", 0, $value + $sizeUnit);
}

@each $size in $sizes {
  $sizeKey: nth($size, 1);
  $sizeValue: nth($size, 2);
  .#{$marginKey}#{$separator}#{$sizeKey} {
    margin: sizeValue($sizeKey, $sizeValue);
  }
  .#{$paddingKey}#{$separator}#{$sizeKey} {
    padding: sizeValue($sizeKey, $sizeValue);
  }
  @each $position in $positions {
    $posKey: nth($position, 1);
    $posValue: nth($position, 2);
    .#{$marginKey}#{$separator}#{$posKey}#{$separator}#{$sizeKey} {
      margin-#{$posValue}: sizeValue($sizeKey, $sizeValue);
    }
    .#{$paddingKey}#{$separator}#{$posKey}#{$separator}#{$sizeKey} {
      padding-#{$posValue}: sizeValue($sizeKey, $sizeValue);
    }
  }
}

@vctrtvfrrr
Copy link
Author

Thanks, @hellokvn. I'll leave both versions here.

@AlexZeitler
Copy link

@VictorOtavio @hellokvn 👍 It would be awesome if it would also support responsive modifiers like -mobile, -desktop etc.

@cfofiu
Copy link

cfofiu commented Aug 26, 2018

FYI, $sizes variable messes with the rest of the Bulma sizes. I changed $sizes to $sizes-margin and works well now.

@riccardobevilacqua
Copy link

@VictorOtavio you might want to add some usage examples, although @furey outlined the output in the original thread.

@frederikhors
Copy link

frederikhors commented Sep 29, 2018

@VictorOtavio @hellokvn we should use !important?

@frederikhors
Copy link

@VictorOtavio @hellokvn how to use bulma's breakpoints?

@multiplehats
Copy link

Awesome. Thanks guys

@paolareyes
Copy link

Great, thanks!

@ro31337
Copy link

ro31337 commented Mar 9, 2019

WARNING

This code introduces the issue to Bulma, because it overrides $sizes variable.

jgthms/bulma#2255

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment