Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

calc()-grid #51

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion __test__/src/scss/test.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ body {

.block {
// * Behave like a grid-item
@include i;
background: $color-secondary;
border: 1px solid $color-border;
box-shadow: inset 0 0 0.5em 0.5em rgba($color-shade, 0.1);
Expand Down
25 changes: 13 additions & 12 deletions __test__/src/scss/tests/_modifiers.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
}

&-item {
$hagrid-gutter: 3rem;
@include i(1/3);

&:last-child {
Expand All @@ -21,6 +22,18 @@
@include g(right);
}

&-full {
@include g(full);
}

&-wide {
@include g;
}

&-narrow {
@include g;
}

&-center {
@include g(center);
}
Expand Down Expand Up @@ -49,18 +62,6 @@
}
}

&-narrow {
@include g(narrow);
}

&-full {
@include g(full);
}

&-wide {
@include g(wide);
}

&-xrev {
@include g(rev);
}
Expand Down
10 changes: 2 additions & 8 deletions scss/_hagrid.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,10 @@
////

/// Gutters
/// Set gutters usable as modifiers.
/// Set gutter used in grid calculation
/// @property {Number}

$hagrid-gutters:
(
default: 1.5rem,
full: 0,
narrow: 0.5rem,
wide: 3rem
) !default;
$hagrid-gutter: 1.5rem !default;

/// Breakpoints
/// Can be used for the grid e.g. @include i(breakpoint 2/3)
Expand Down
46 changes: 32 additions & 14 deletions scss/hagrid/_functions.scss
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@if $arg-length == 0 {
// * If no params are passed, default to mobile-first
@if $prop == width {
#{$prop}: 100%;
#{$prop}: get-calc(100%);
}

@else {
Expand All @@ -34,7 +34,8 @@

// * If a general width is passed in, use it
@if type-of($width) == number {
#{$prop}: calculate-width($width);
@include get-prop($prop, $width);

$width-detected: true;
}
// * If false is passed, assign no width
Expand Down Expand Up @@ -64,7 +65,7 @@
@if $bp != false {
@include bp(#{$key}) {
@if type-of($value) == number {
#{$prop}: calculate-width($value);
@include get-prop($prop, $value);
}

@else if $value == auto {
Expand All @@ -86,7 +87,7 @@

// * If only responsive params are passed, default to mobile-first
@if $width-detected == false and $prop == width {
#{$prop}: 100%;
#{$prop}: get-calc(100%);
}
}
}
Expand All @@ -105,20 +106,28 @@
}
}

/// Check if a reference exists in $hagrid-gutters
/// @param {String} $id
/// @return {Boolean}
/// Calculate a calc value based on a gutter
/// @param {Number} $width
/// @param {Number} $gutter
/// @return {Value}

@function hagrid-gutter-exists($id) {
@return map-has-key($hagrid-gutters, $id);
@function get-calc($width, $gutter: $hagrid-gutter) {
@return unquote("calc(#{calculate-width($width)} - #{$hagrid-gutter})");
}

/// Get a referenced item from $hagrid-gutters
/// @param {String} $id
/// @return {String}
/// Return calc() or width() based on prop
/// @param {String} $prop
/// @param {Number} $value
/// @return {Value}

@mixin get-prop($prop, $value) {
@if $prop == width {
#{$prop}: get-calc($value);
}

@function hagrid-gutter-get($id) {
@return map-get($hagrid-gutters, $id);
@else {
#{$prop}: calculate-width($value);
}
}

/// Validate a breakpoint-key and return itself, its value or false
Expand Down Expand Up @@ -197,3 +206,12 @@

@return str-slice($string, -1 * str-length($needle)) == $needle;
}

@function halves($num) {
@if type-of($num) != "number" {
@warn "`halves` function expecting a number; #{type-of($num)} given.";
@return false;
}

@return ($num / 2);
}
23 changes: 5 additions & 18 deletions scss/hagrid/_modifiers.scss
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/// @output Modifier-styles for a given call

@mixin grid-modifiers($modifiers...) {
$gutter: hagrid-gutter-get(default);
$gutter: $hagrid-gutter;
$is-y: false;
$is-wrap: false;
$is-not-spacing: true;
Expand All @@ -37,7 +37,7 @@
$is-wrap: true;
}

@elseif hagrid-gutter-exists($modifier) {
@elseif $modifier == full {
$is-not-spacing: false;
}

Expand All @@ -58,7 +58,7 @@

#{$hagrid-item-selector} {
@if $is-not-spacing == true {
padding-left: $gutter;
margin-left: $gutter;
}
}
}
Expand Down Expand Up @@ -108,20 +108,7 @@
}
}

@else {
// * Gutter Modifiers

@if hagrid-gutter-exists($modifier) {
$gutter: hagrid-gutter-get($modifier);
margin-left: -$gutter;

#{$hagrid-item-selector} {
padding-left: $gutter;
}
}

@else {
@error "Modifier #{$modifier} does not exist.";
}
@elseif $modifier != full {
@error "Modifier #{$modifier} does not exist.";
}
}