From 795a5845619843040b5ab63ff7639fdf9ae86753 Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Fri, 5 Dec 2014 10:53:43 +0000 Subject: [PATCH 1/2] Refactor grid_framework A more idiomatic refactoring of the grid framework * Use %-placeholder instead of generating a class name list * Use if expression * Remove loop-grid-columns --- scss/mixins/_grid-framework.scss | 92 ++++++++++++++------------------ 1 file changed, 39 insertions(+), 53 deletions(-) diff --git a/scss/mixins/_grid-framework.scss b/scss/mixins/_grid-framework.scss index b75517d292..5ac62751fd 100644 --- a/scss/mixins/_grid-framework.scss +++ b/scss/mixins/_grid-framework.scss @@ -3,79 +3,65 @@ // Used only by Bootstrap to generate the correct number of grid classes given // any value of `$grid-columns`. -// [converter] This is defined recursively in LESS, but Sass supports real loops -@mixin make-grid-columns($i: 1, $list: ".col-xs-#{$i}, .col-sm-#{$i}, .col-md-#{$i}, .col-lg-#{$i}, .col-xl-#{$i}") { - @for $i from (1 + 1) through $grid-columns { - $list: "#{$list}, .col-xs-#{$i}, .col-sm-#{$i}, .col-md-#{$i}, .col-lg-#{$i}, .col-xl-#{$i}"; - } - #{$list} { - position: relative; - // Prevent columns from collapsing when empty - min-height: 1px; - // Inner gutter via padding - padding-left: ($grid-gutter-width / 2); - padding-right: ($grid-gutter-width / 2); +%twbs-grid-column { + position: relative; + // Prevent columns from collapsing when empty + min-height: 1px; + // Inner gutter via padding + padding-left: ($grid-gutter-width / 2); + padding-right: ($grid-gutter-width / 2); +} + +%twbs-grid-column-float { + float: left +} + +@mixin make-grid-columns($columns: $grid-columns) { + @for $i from 1 through $columns { + .col-xs-#{$i}, .col-sm-#{$i}, .col-md-#{$i}, .col-lg-#{$i}, .col-xl-#{$i} { + @extend %twbs-grid-column; + } } } - -// [converter] This is defined recursively in LESS, but Sass supports real loops -@mixin float-grid-columns($class, $i: 1, $list: ".col-#{$class}-#{$i}") { - @for $i from (1 + 1) through $grid-columns { - $list: "#{$list}, .col-#{$class}-#{$i}"; - } - #{$list} { - float: left; +@mixin float-grid-columns($class, $columns: $grid-columns) { + @for $i from 1 through $columns { + .col-#{$class}-#{$i} { + @extend %twbs-grid-column-float; + } } } - -@mixin calc-grid-column($index, $class, $type) { +@mixin calc-grid-column($index, $class, $type, $columns: $grid-columns) { @if ($type == width) and ($index > 0) { .col-#{$class}-#{$index} { - width: percentage(($index / $grid-columns)); + width: percentage($index / $columns); } } - @if ($type == push) and ($index > 0) { + @if $type == push { .col-#{$class}-push-#{$index} { - left: percentage(($index / $grid-columns)); + left: if($index > 0, percentage($index / $columns), auto); } } - @if ($type == push) and ($index == 0) { - .col-#{$class}-push-0 { - left: auto; - } - } - @if ($type == pull) and ($index > 0) { + @if $type == pull { .col-#{$class}-pull-#{$index} { - right: percentage(($index / $grid-columns)); + right: if($index > 0, percentage($index / $columns), auto); } } - @if ($type == pull) and ($index == 0) { - .col-#{$class}-pull-0 { - right: auto; - } - } - @if ($type == offset) { + @if $type == offset { .col-#{$class}-offset-#{$index} { - margin-left: percentage(($index / $grid-columns)); + margin-left: percentage($index / $columns); } } } -// [converter] This is defined recursively in LESS, but Sass supports real loops -@mixin loop-grid-columns($columns, $class, $type) { - @for $i from 0 through $columns { - @include calc-grid-column($i, $class, $type); - } -} - - // Create grid for specific class -@mixin make-grid($class) { +@mixin make-grid($class, $columns: $grid-columns) { @include float-grid-columns($class); - @include loop-grid-columns($grid-columns, $class, width); - @include loop-grid-columns($grid-columns, $class, pull); - @include loop-grid-columns($grid-columns, $class, push); - @include loop-grid-columns($grid-columns, $class, offset); -} \ No newline at end of file + @for $i from 0 through $columns { + @include calc-grid-column($i, $class, width, $columns); + @include calc-grid-column($i, $class, push, $columns); + @include calc-grid-column($i, $class, pull, $columns); + @include calc-grid-column($i, $class, offset, $columns); + } +} From 5fefe06c3d9d3e568d030be2612e709119c6739e Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Sun, 7 Dec 2014 14:52:44 +0000 Subject: [PATCH 2/2] Refactor grid-framework followup * Split up calc-grid-column, generate selectors in make-grid * Iterate over $grid-breakpoints and (pull, push, offset) --- scss/_variables.scss | 2 +- scss/mixins/_grid-framework.scss | 102 ++++++++++++++++--------------- 2 files changed, 54 insertions(+), 50 deletions(-) diff --git a/scss/_variables.scss b/scss/_variables.scss index 5f41979f0e..8f9e26d68d 100644 --- a/scss/_variables.scss +++ b/scss/_variables.scss @@ -294,7 +294,7 @@ $screen-xs-max: ($screen-sm-min - .1); //== Grid system // //## Define your custom responsive grid. - +$grid-breakpoints: (xs sm md lg xl); //** Number of columns in the grid. $grid-columns: 12; //** Padding between columns. Gets divided in half for the left and right. diff --git a/scss/mixins/_grid-framework.scss b/scss/mixins/_grid-framework.scss index 5ac62751fd..3eecbae8b2 100644 --- a/scss/mixins/_grid-framework.scss +++ b/scss/mixins/_grid-framework.scss @@ -3,65 +3,69 @@ // Used only by Bootstrap to generate the correct number of grid classes given // any value of `$grid-columns`. -%twbs-grid-column { - position: relative; - // Prevent columns from collapsing when empty - min-height: 1px; - // Inner gutter via padding - padding-left: ($grid-gutter-width / 2); - padding-right: ($grid-gutter-width / 2); -} - -%twbs-grid-column-float { - float: left -} - -@mixin make-grid-columns($columns: $grid-columns) { +// Common properties for all breakpoints +@mixin make-grid-columns($columns: $grid-columns, $breakpoints: $grid-breakpoints) { + %grid-column { + position: relative; + // Prevent columns from collapsing when empty + min-height: 1px; + // Inner gutter via padding + padding-left: ($grid-gutter-width / 2); + padding-right: ($grid-gutter-width / 2); + } @for $i from 1 through $columns { - .col-xs-#{$i}, .col-sm-#{$i}, .col-md-#{$i}, .col-lg-#{$i}, .col-xl-#{$i} { - @extend %twbs-grid-column; + @each $breakpoint in $breakpoints { + .col-#{$breakpoint}-#{$i} { + @extend %grid-column; + } } } } -@mixin float-grid-columns($class, $columns: $grid-columns) { +// Breakpoint-specific properties +@mixin make-grid($breakpoint, $columns: $grid-columns) { + // Work around cross-media @extend (https://github.com/sass/sass/issues/1050) + %grid-column-float-#{$breakpoint} { + float: left; + } @for $i from 1 through $columns { - .col-#{$class}-#{$i} { - @extend %twbs-grid-column-float; + .col-#{$breakpoint}-#{$i} { + @extend %grid-column-float-#{$breakpoint}; + @include grid-column-width($i, $columns); + } + } + @each $modifier in (pull, push, offset) { + @for $i from 0 through $columns { + .col-#{$breakpoint}-#{$modifier}-#{$i} { + @include grid-column-modifier($modifier, $i, $columns) + } } } } -@mixin calc-grid-column($index, $class, $type, $columns: $grid-columns) { - @if ($type == width) and ($index > 0) { - .col-#{$class}-#{$index} { - width: percentage($index / $columns); - } - } +@mixin grid-column-width($index, $columns) { + width: percentage($index / $columns); +} + +@mixin grid-column-push($index, $columns) { + left: if($index > 0, percentage($index / $columns), auto); +} + +@mixin grid-column-pull($index, $columns) { + right: if($index > 0, percentage($index / $columns), auto); +} + +@mixin grid-column-offset($index, $columns) { + margin-left: percentage($index / $columns); +} + +// Work around the lack of dynamic mixin @include support (https://github.com/sass/sass/issues/626) +@mixin grid-column-modifier($type, $index, $columns) { @if $type == push { - .col-#{$class}-push-#{$index} { - left: if($index > 0, percentage($index / $columns), auto); - } - } - @if $type == pull { - .col-#{$class}-pull-#{$index} { - right: if($index > 0, percentage($index / $columns), auto); - } - } - @if $type == offset { - .col-#{$class}-offset-#{$index} { - margin-left: percentage($index / $columns); - } - } -} - -// Create grid for specific class -@mixin make-grid($class, $columns: $grid-columns) { - @include float-grid-columns($class); - @for $i from 0 through $columns { - @include calc-grid-column($i, $class, width, $columns); - @include calc-grid-column($i, $class, push, $columns); - @include calc-grid-column($i, $class, pull, $columns); - @include calc-grid-column($i, $class, offset, $columns); + @include grid-column-push($index, $columns); + } @else if $type == pull { + @include grid-column-pull($index, $columns); + } @else if $type == offset { + @include grid-column-offset($index, $columns); } }