|
| 1 | +// |
| 2 | +// Copyright IBM Corp. 2018, 2018 |
| 3 | +// |
| 4 | +// This source code is licensed under the Apache-2.0 license found in the |
| 5 | +// LICENSE file in the root directory of this source tree. |
| 6 | +// |
| 7 | + |
| 8 | +// Helpers for defining columns, rows, and containers are heavily inspired by, |
| 9 | +// and often derived from, bootstrap: |
| 10 | +// https://github.com/twbs/bootstrap/blob/v4-dev/scss/mixins/_grid.scss |
| 11 | + |
| 12 | +@import '../vendor/@carbon/layout/breakpoint'; |
| 13 | +@import 'prefix'; |
| 14 | + |
| 15 | +// ----------------------------------------------------------------------------- |
| 16 | +// Columns |
| 17 | +// ----------------------------------------------------------------------------- |
| 18 | + |
| 19 | +/// Used to initialize the default properties for a column class, most notably |
| 20 | +/// for setting width and default gutters when a column's breakpoint has not been |
| 21 | +/// hit yet. |
| 22 | +/// @param {Number} $gutter [$carbon--grid-gutter] - The gutter for the grid system |
| 23 | +/// @param {Number} $collapsed-gutter [$carbon--grid-gutter--condensed] - The condensed mode gutter |
| 24 | +/// @access private |
| 25 | +/// @group @carbon/grid |
| 26 | +@mixin carbon--make-col-ready( |
| 27 | + $gutter: $carbon--grid-gutter, |
| 28 | + $condensed-gutter: $carbon--grid-gutter--condensed |
| 29 | +) { |
| 30 | + // Prevent columns from becoming too narrow when at smaller grid tiers by |
| 31 | + // always setting `width: 100%;`. This works because we use `flex` values |
| 32 | + // later on to override this initial width. |
| 33 | + width: 100%; |
| 34 | + padding-right: ($gutter / 2); |
| 35 | + padding-left: ($gutter / 2); |
| 36 | + |
| 37 | + // For our condensed use-case, our gutters collapse to 2px solid, 1px on each |
| 38 | + // side. |
| 39 | + .#{$prefix}--row--condensed &, |
| 40 | + .#{$prefix}--grid--condensed & { |
| 41 | + padding-right: ($condensed-gutter / 2); |
| 42 | + padding-left: ($condensed-gutter / 2); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/// Define the width of the column for a given span and column count. |
| 47 | +/// A width of 0 will hide the column entirely. |
| 48 | +/// @param {Number} $span - The number of columns covered |
| 49 | +/// @param {Number} $columns - The total number of columns available |
| 50 | +/// @access private |
| 51 | +/// @group @carbon/grid |
| 52 | +@mixin carbon--make-col($span, $columns) { |
| 53 | + @if $span == 0 { |
| 54 | + display: none; |
| 55 | + } @else { |
| 56 | + // Explicitly include `display: block` to override |
| 57 | + display: block; |
| 58 | + flex: 0 0 percentage($span / $columns); |
| 59 | + // Add a `max-width` to ensure content within each column does not blow out |
| 60 | + // the width of the column. Applies to IE10+ and Firefox. Chrome and Safari |
| 61 | + // do not appear to require this. |
| 62 | + max-width: percentage($span / $columns); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +/// Create a column offset for a given span and column count. |
| 67 | +/// @param {Number} $span - The number of columns the offset should cover |
| 68 | +/// @param {Number} $columns - The total number of columns available |
| 69 | +/// @access private |
| 70 | +/// @group @carbon/grid |
| 71 | +@mixin carbon--make-col-offset($span, $columns) { |
| 72 | + $offset: $span / $columns; |
| 73 | + @if $offset == 0 { |
| 74 | + margin-left: 0; |
| 75 | + } @else { |
| 76 | + margin-left: percentage($offset); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/// Output the CSS required for all the columns in a given grid system. |
| 81 | +/// @param {Map} $breakpoints [$carbon--grid-breakpoints] - The breakpoints in the grid system |
| 82 | +/// @param {Number} $gutter [$carbon--grid-gutter] - The gutter for the grid system |
| 83 | +/// @access private |
| 84 | +/// @group @carbon/grid |
| 85 | +@mixin carbon--make-grid-columns( |
| 86 | + $breakpoints: $carbon--grid-breakpoints, |
| 87 | + $gutter: $carbon--grid-gutter |
| 88 | +) { |
| 89 | + .#{$prefix}--col { |
| 90 | + @include carbon--make-col-ready(); |
| 91 | + } |
| 92 | + |
| 93 | + @each $breakpoint in map-keys($breakpoints) { |
| 94 | + $infix: carbon--breakpoint-infix($breakpoint); |
| 95 | + $columns: map-get(map-get($breakpoints, $breakpoint), columns); |
| 96 | + |
| 97 | + // Allow columns to stretch full width below their breakpoints |
| 98 | + @for $i from 0 through $columns { |
| 99 | + .#{$prefix}--col#{$infix}-#{$i} { |
| 100 | + @include carbon--make-col-ready(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + .#{$prefix}--col#{$infix}, |
| 105 | + .#{$prefix}--col#{$infix}--auto { |
| 106 | + @include carbon--make-col-ready(); |
| 107 | + } |
| 108 | + |
| 109 | + @include carbon--breakpoint($breakpoint, $breakpoints) { |
| 110 | + // Provide basic `.col-{bp}` classes for equal-width flexbox columns |
| 111 | + .#{$prefix}--col, |
| 112 | + .#{$prefix}--col#{$infix} { |
| 113 | + flex-basis: 0; |
| 114 | + flex-grow: 1; |
| 115 | + max-width: 100%; |
| 116 | + } |
| 117 | + |
| 118 | + .#{$prefix}--col--auto, |
| 119 | + .#{$prefix}--col#{$infix}--auto { |
| 120 | + flex: 1 0 0%; |
| 121 | + width: auto; |
| 122 | + // Reset earlier grid tiers |
| 123 | + max-width: 100%; |
| 124 | + } |
| 125 | + |
| 126 | + @for $i from 0 through $columns { |
| 127 | + .#{$prefix}--col#{$infix}-#{$i} { |
| 128 | + @include carbon--make-col($i, $columns); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + @for $i from 0 through ($columns - 1) { |
| 133 | + @if not($infix == '') { |
| 134 | + .#{$prefix}--offset#{$infix}-#{$i} { |
| 135 | + @include carbon--make-col-offset($i, $columns); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +// ----------------------------------------------------------------------------- |
| 144 | +// Rows |
| 145 | +// ----------------------------------------------------------------------------- |
| 146 | + |
| 147 | +/// Define the properties for a selector assigned to a row in the grid system. |
| 148 | +/// @param {Number} $gutter [$carbon--grid-gutter] - The gutter in the grid system |
| 149 | +/// @access private |
| 150 | +/// @group @carbon/grid |
| 151 | +@mixin carbon--make-row($gutter: $carbon--grid-gutter) { |
| 152 | + display: flex; |
| 153 | + flex-wrap: wrap; |
| 154 | + margin-right: -1 * $gutter / 2; |
| 155 | + margin-left: -1 * $gutter / 2; |
| 156 | +} |
| 157 | + |
| 158 | +// ----------------------------------------------------------------------------- |
| 159 | +// No gutter |
| 160 | +// ----------------------------------------------------------------------------- |
| 161 | + |
| 162 | +/// Add `no-gutter` and `no-gutter--{left,right}` classes to the output CSS. These |
| 163 | +/// classes are useful for dropping the gutter in fluid situations. |
| 164 | +/// @access private |
| 165 | +/// @group @carbon/grid |
| 166 | +@mixin carbon--no-gutter { |
| 167 | + .#{$prefix}--no-gutter, |
| 168 | + .#{$prefix}--row.#{$prefix}--no-gutter [class*='#{$prefix}--col'] { |
| 169 | + padding-left: 0; |
| 170 | + padding-right: 0; |
| 171 | + } |
| 172 | + |
| 173 | + .#{$prefix}--no-gutter--left, |
| 174 | + .#{$prefix}--row.#{$prefix}--no-gutter--left [class*='#{$prefix}--col'] { |
| 175 | + padding-left: 0; |
| 176 | + } |
| 177 | + |
| 178 | + .#{$prefix}--no-gutter--right, |
| 179 | + .#{$prefix}--row.#{$prefix}--no-gutter--right [class*='#{$prefix}--col'] { |
| 180 | + padding-right: 0; |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +// ----------------------------------------------------------------------------- |
| 185 | +// Hang |
| 186 | +// ----------------------------------------------------------------------------- |
| 187 | + |
| 188 | +/// Add `hang--left` and `hang--right` classes for a given gutter. These classes are |
| 189 | +/// used alongside `no-gutter--left` and `no-gutter--right` to "hang" type. |
| 190 | +/// @param {Number} $gutter [$carbon--grid-gutter] - The gutter in the grid system |
| 191 | +/// @access private |
| 192 | +/// @group @carbon/grid |
| 193 | +@mixin carbon--hang($gutter: $carbon--grid-gutter) { |
| 194 | + .#{$prefix}--hang--left { |
| 195 | + padding-left: ($gutter / 2); |
| 196 | + } |
| 197 | + |
| 198 | + .#{$prefix}--hang--right { |
| 199 | + padding-right: ($gutter / 2); |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +// ----------------------------------------------------------------------------- |
| 204 | +// Aspect ratio |
| 205 | +// ----------------------------------------------------------------------------- |
| 206 | + |
| 207 | +/// The aspect ratios that are used to generate corresponding aspect ratio |
| 208 | +/// classes in code |
| 209 | +/// @type List |
| 210 | +/// @access public |
| 211 | +/// @group @carbon/grid |
| 212 | +$carbon--aspect-ratios: ((16, 9), (2, 1), (4, 3), (1, 1), (1, 2)); |
| 213 | + |
| 214 | +/// Output the CSS classes for generating aspect ratio classes |
| 215 | +/// @param {List} $aspect-ratios [$carbon--aspect-ratios] - A list of aspect ratios to generate |
| 216 | +/// @access private |
| 217 | +/// @group @carbon/grid |
| 218 | +@mixin carbon--aspect-ratio($aspect-ratios: $carbon--aspect-ratios) { |
| 219 | + .#{$prefix}--aspect-ratio { |
| 220 | + height: 0; |
| 221 | + position: relative; |
| 222 | + } |
| 223 | + |
| 224 | + .#{$prefix}--aspect-ratio--object { |
| 225 | + position: absolute; |
| 226 | + top: 0; |
| 227 | + right: 0; |
| 228 | + bottom: 0; |
| 229 | + left: 0; |
| 230 | + width: 100%; |
| 231 | + height: 100%; |
| 232 | + z-index: 100; |
| 233 | + } |
| 234 | + |
| 235 | + @each $ratio in $aspect-ratios { |
| 236 | + $width: nth($ratio, 1); |
| 237 | + $height: nth($ratio, 2); |
| 238 | + |
| 239 | + .#{$prefix}--aspect-ratio--#{$width}x#{$height} { |
| 240 | + padding-bottom: percentage($height / $width); |
| 241 | + } |
| 242 | + } |
| 243 | +} |
| 244 | + |
| 245 | +// ----------------------------------------------------------------------------- |
| 246 | +// Grid |
| 247 | +// ----------------------------------------------------------------------------- |
| 248 | + |
| 249 | +/// Create the container for a grid. Will cause full-bleed for the grid unless |
| 250 | +/// max-width properties are added with `make-container-max-widths` |
| 251 | +/// @param {Map} $breakpoints [$carbon--grid-breakpoints] - A map of breakpoints where the key is the name |
| 252 | +/// @access private |
| 253 | +/// @group @carbon/grid |
| 254 | +@mixin carbon--make-container($breakpoints: $carbon--grid-breakpoints) { |
| 255 | + margin-right: auto; |
| 256 | + margin-left: auto; |
| 257 | + |
| 258 | + @include carbon--set-largest-breakpoint(); |
| 259 | + |
| 260 | + @each $name, $value in $breakpoints { |
| 261 | + $prev-breakpoint: map-get($breakpoints, carbon--breakpoint-prev($name)); |
| 262 | + $margin: map-get($value, margin); |
| 263 | + |
| 264 | + @if $prev-breakpoint { |
| 265 | + $prev-margin: map-get($prev-breakpoint, margin); |
| 266 | + @if $prev-margin != $margin { |
| 267 | + @include carbon--breakpoint($name) { |
| 268 | + padding-left: #{($carbon--grid-gutter / 2) + $margin}; |
| 269 | + padding-right: #{($carbon--grid-gutter / 2) + $margin}; |
| 270 | + } |
| 271 | + } |
| 272 | + } @else { |
| 273 | + @include carbon--breakpoint($name) { |
| 274 | + padding-left: #{($carbon--grid-gutter / 2) + $margin}; |
| 275 | + padding-right: #{($carbon--grid-gutter / 2) + $margin}; |
| 276 | + } |
| 277 | + } |
| 278 | + } |
| 279 | +} |
| 280 | + |
| 281 | +/// Get the last breakpoint width and set max-width to its value |
| 282 | +/// @param {Map} $breakpoints [$carbon--grid-breakpoints] - A map of breakpoints where the key is the name |
| 283 | +/// @access private |
| 284 | +/// @group @carbon/grid |
| 285 | +@mixin carbon--set-largest-breakpoint($breakpoints: $carbon--grid-breakpoints) { |
| 286 | + $largest-breakpoint: last-map-item($breakpoints); |
| 287 | + |
| 288 | + max-width: map-get($largest-breakpoint, 'width'); |
| 289 | +} |
| 290 | + |
| 291 | +/// Add in the max-widths for each breakpoint to the container |
| 292 | +/// @param {Map} $breakpoints [$carbon--grid-breakpoints] - A map of breakpoints where the key is the name |
| 293 | +/// @access private |
| 294 | +/// @group @carbon/grid |
| 295 | +@mixin carbon--make-container-max-widths( |
| 296 | + $breakpoints: $carbon--grid-breakpoints |
| 297 | +) { |
| 298 | + @each $name, $value in $breakpoints { |
| 299 | + @include carbon--breakpoint($name) { |
| 300 | + max-width: map-get($value, width); |
| 301 | + } |
| 302 | + } |
| 303 | +} |
| 304 | + |
| 305 | +/// Generate the CSS for a grid for the given breakpoints and gutters |
| 306 | +/// @param {Map} $breakpoints [$carbon--grid-breakpoints] - The default breakpoints |
| 307 | +/// @param {Number} $grid-gutter [$carbon--grid-gutter] - The default gutters |
| 308 | +/// @param {Number} $condensed-gutter [$carbon--grid-gutter--condensed] - The condensed mode gutter |
| 309 | +/// @access public |
| 310 | +/// @group @carbon/grid |
| 311 | +@mixin carbon--grid( |
| 312 | + $breakpoints: $carbon--grid-breakpoints, |
| 313 | + $grid-gutter: $carbon--grid-gutter, |
| 314 | + $condensed-gutter: $carbon--grid-gutter--condensed |
| 315 | +) { |
| 316 | + .#{$prefix}--grid { |
| 317 | + @include carbon--make-container($breakpoints); |
| 318 | + } |
| 319 | + |
| 320 | + @include carbon--largest-breakpoint($breakpoints) { |
| 321 | + .#{$prefix}--grid--full-width { |
| 322 | + max-width: 100%; |
| 323 | + } |
| 324 | + } |
| 325 | + |
| 326 | + .#{$prefix}--row { |
| 327 | + @include carbon--make-row(); |
| 328 | + } |
| 329 | + |
| 330 | + .#{$prefix}--grid--condensed [class*='#{$prefix}--col'] { |
| 331 | + padding-top: $condensed-gutter / 2; |
| 332 | + padding-bottom: $condensed-gutter / 2; |
| 333 | + } |
| 334 | + |
| 335 | + @include carbon--make-grid-columns($breakpoints, $grid-gutter); |
| 336 | + @include carbon--no-gutter(); |
| 337 | + @include carbon--hang($grid-gutter); |
| 338 | + @include carbon--aspect-ratio(); |
| 339 | +} |
0 commit comments