Skip to content
This repository was archived by the owner on Jul 18, 2024. It is now read-only.

Commit 1f58e4e

Browse files
author
Jen Downs
committed
fix(grid): Updated comments with examples. updated error messages throughout.
1 parent 63876d8 commit 1f58e4e

File tree

7 files changed

+287
-144
lines changed

7 files changed

+287
-144
lines changed
Binary file not shown.

examples/bootstrap/css-gridish/scss/_utilities.scss

Lines changed: 74 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,105 @@
1-
@import "variables";
2-
@import "values";
3-
@debug $allBreakpoints;
4-
/// Traverse maps and retrieve deeply nested values.
5-
/// @author Hugo Giraudel
6-
/// @param {map} A sass map.
7-
/// @param {arglist} Any number of keys.
8-
/// @return {*} Nested values or nested map.
1+
@import 'variables';
2+
@import 'values';
3+
4+
/// Function from https://css-tricks.com/snippets/sass/deep-getset-maps/
5+
/// Helps you traverse maps and retrieve deeply nested values.
6+
/// @param {Map} $map A sass map and any number of keys.
7+
/// @param {*} $keys Nested values.
8+
/// @return Nested values or nested map.
99
@function map-deep-get($map, $keys...) {
1010
@each $key in $keys {
1111
$map: map-get($map, $key);
1212
}
1313
@return $map;
1414
}
1515

16-
/// Function returns the rem value of a given breakpoint.
17-
/// @param {string} An existing breakpoint name (sm, md, lg, amd so on).
18-
/// @return {length} The breakpoint value in rems.
16+
/// Function returns the value for a given breakpoint.
17+
/// @param {String} $breakpointName Breakpoint name (sm, md, lg, xlg, max).
18+
/// @return {Length} The breakpoint value in rems.
1919
@function getBreakpointValue($breakpointName) {
20-
$breakpointValue: map-deep-get($grid-values, 'breakpoints', $breakpointName, 'breakpoint');
20+
$breakpointValue: map-deep-get($allBreakpoints, $breakpointName, 'breakpoint');
21+
2122
@return $breakpointValue * 1rem;
2223
}
2324

2425
/// Returns a calc() expression that represents a fluid width at a given breakpoint.
25-
/// @param {string} A valid breakpoint (sm, md, lg, xlg, or max)
26-
/// @param {number} The number of columns to span across.
27-
/// @return {length} A calc() expression representing fluid width.
28-
@function getFluidSize($breakpointName, $columns) {
29-
$fluidWidth: 0; // default
26+
/// @param {String} $breakpointName A valid breakpoint (sm, md, lg, xlg, or max).
27+
/// @param {Number} $columnSpan The number of columns to span across.
28+
/// @return {Length} A calc() expression representing fluid width.
29+
/// @example scss
30+
/// @media screen and (min-width: 20rem) {
31+
/// button {
32+
/// @include mediaQuery('sm') {
33+
/// max-width: getFluidSize('sm', 1);
34+
/// }
35+
/// }
36+
/// @output css
37+
/// @media screen and (min-width: 20rem) {
38+
/// button {
39+
/// max-width: 25vw;
40+
/// }
41+
/// }
42+
@function getFluidSize($breakpointName, $columnSpan) {
43+
$breakpoint: map-get($allBreakpoints, $breakpointName);
44+
$columnTotal: map-get($breakpoint, columns);
45+
$margin: map-get($breakpoint, margin);
46+
47+
@if ($breakpoint == null) {
48+
@error 'The provided breakpoint `#{$breakpointName}` is not a valid breakpoint found in breakpoints or extraArtboards in $grid-values. Please double-check your grid configuration.';
49+
}
3050

31-
$breakpoint: map-deep-get($grid-values, 'breakpoints', $breakpointName);
51+
@if ($columnTotal == null or type-of($columnTotal) != 'number') {
52+
@error 'The provided breakpoint `#{$breakpointName}` needs to have a total number of columns set in $grid-values. Please double-check your grid configuration.';
53+
}
3254

33-
@if (map-get($breakpoint, columns) != null) {
34-
$columnQuantity: map-get($breakpoint, columns);
35-
$fluidWidth: 100vw / $columnQuantity;
55+
@if ($columnSpan == null or type-of($columnSpan) != 'number') {
56+
@error 'The number of columns to span for the breakpoint `#{$breakpointName}` must be a valid number. The provided column span value `#{$columnSpan}` is not a number';
57+
}
3658

37-
$totalMargin: 0;
38-
@if (map-get($breakpoint, margin) != null) {
39-
$totalMargin: map-get($breakpoint, margin) * 2;
40-
}
59+
@if ($columnSpan > $columnTotal or $columnSpan <= 0) {
60+
@error 'The number of columns to span for the breakpoint `#{$breakpointName}` must be greater than 0 and less than or equal to the total number of columns for this breakpoint. The provided column value `#{$columnSpan}` does not meet this criteria';
61+
}
4162

42-
$fluidWidth: calc((100vw - #{$totalMargin}) * #{$columns} / #{$columnQuantity});
63+
// For IE, we can't have a 0 in the $fluidWidth calc().
64+
@if ($margin == 0) {
65+
@return calc((100vw) * #{$columnSpan} / #{$columnTotal});
4366
}
4467

45-
@return $fluidWidth;
68+
@return calc((100vw - (#{$margin} * 2)) * #{$columnSpan} / #{$columnTotal});
4669
}
4770

48-
/// Gets a calculated rem value for a fixed nondimensional unit size.
49-
/// @param {number} Fixed nondimensional units.
50-
/// @return {length} A calculated rem value.
71+
/// Function gets a calculated rem value for a fixed size.
72+
/// @param {Number} $fixedUnit Fixed nondimensional units.
73+
/// @return {Length} A calculated rem value.
74+
/// @example scss
75+
/// button {
76+
/// @include mediaQuery('sm') {
77+
/// max-width: getFixedSize(10);
78+
/// }
79+
/// }
80+
/// @output css
81+
/// @media screen and (min-width: 20rem) {
82+
/// button {
83+
/// max-width: 5rem;
84+
/// }
85+
/// }
5186
@function getFixedSize($fixedUnit) {
87+
$rowHeight: map-get($grid-values, 'rowHeight');
5288

53-
$remValue: 0; // default
54-
55-
@if $fixedUnit != null and $fixedUnit > 0 {
56-
$remValue: $fixedUnit * map-get($grid-values, 'rowHeight') * 1rem;
89+
@if ($rowHeight == null or type-of($rowHeight) != 'number') {
90+
@error 'The rowHeight in $grid-values needs be a valid number. Please check your grid configuration.';
5791
}
58-
@else {
59-
@error 'The provided value #{$fixedUnit} to getFixedSize() is not a number.'
92+
93+
@if ($fixedUnit == null or type-of($fixedUnit) != 'number') {
94+
@error 'The provided fixed value `#{$fixedUnit}` to get-fixed-size() needs to be a valid number greater than 0.';
6095
}
6196

62-
@return $remValue;
97+
@return $fixedUnit * $rowHeight * 1rem;
6398
}
6499

65100
/// Utility for declaring mobile-first media queries.
66-
/// @param {String} $breakpoint-name The name of the breakpoint to set its width value to media query.
67-
@mixin media-query($breakpointNme) {
101+
/// @param {String} $breakpointName The name of the breakpoint to set its width value to media query.
102+
@mixin mediaQuery($breakpointName) {
68103
@media screen and (min-width: #{getBreakpointValue($breakpointName)}) {
69104
@content;
70105
}
0 Bytes
Binary file not shown.

examples/carbon/css-gridish/scss/_utilities.scss

Lines changed: 74 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,105 @@
1-
@import "variables";
2-
@import "values";
3-
@debug $allBreakpoints;
4-
/// Traverse maps and retrieve deeply nested values.
5-
/// @author Hugo Giraudel
6-
/// @param {map} A sass map.
7-
/// @param {arglist} Any number of keys.
8-
/// @return {*} Nested values or nested map.
1+
@import 'variables';
2+
@import 'values';
3+
4+
/// Function from https://css-tricks.com/snippets/sass/deep-getset-maps/
5+
/// Helps you traverse maps and retrieve deeply nested values.
6+
/// @param {Map} $map A sass map and any number of keys.
7+
/// @param {*} $keys Nested values.
8+
/// @return Nested values or nested map.
99
@function map-deep-get($map, $keys...) {
1010
@each $key in $keys {
1111
$map: map-get($map, $key);
1212
}
1313
@return $map;
1414
}
1515

16-
/// Function returns the rem value of a given breakpoint.
17-
/// @param {string} An existing breakpoint name (sm, md, lg, amd so on).
18-
/// @return {length} The breakpoint value in rems.
16+
/// Function returns the value for a given breakpoint.
17+
/// @param {String} $breakpointName Breakpoint name (sm, md, lg, xlg, max).
18+
/// @return {Length} The breakpoint value in rems.
1919
@function getBreakpointValue($breakpointName) {
20-
$breakpointValue: map-deep-get($grid-values, 'breakpoints', $breakpointName, 'breakpoint');
20+
$breakpointValue: map-deep-get($allBreakpoints, $breakpointName, 'breakpoint');
21+
2122
@return $breakpointValue * 1rem;
2223
}
2324

2425
/// Returns a calc() expression that represents a fluid width at a given breakpoint.
25-
/// @param {string} A valid breakpoint (sm, md, lg, xlg, or max)
26-
/// @param {number} The number of columns to span across.
27-
/// @return {length} A calc() expression representing fluid width.
28-
@function getFluidSize($breakpointName, $columns) {
29-
$fluidWidth: 0; // default
26+
/// @param {String} $breakpointName A valid breakpoint (sm, md, lg, xlg, or max).
27+
/// @param {Number} $columnSpan The number of columns to span across.
28+
/// @return {Length} A calc() expression representing fluid width.
29+
/// @example scss
30+
/// @media screen and (min-width: 20rem) {
31+
/// button {
32+
/// @include mediaQuery('sm') {
33+
/// max-width: getFluidSize('sm', 1);
34+
/// }
35+
/// }
36+
/// @output css
37+
/// @media screen and (min-width: 20rem) {
38+
/// button {
39+
/// max-width: 25vw;
40+
/// }
41+
/// }
42+
@function getFluidSize($breakpointName, $columnSpan) {
43+
$breakpoint: map-get($allBreakpoints, $breakpointName);
44+
$columnTotal: map-get($breakpoint, columns);
45+
$margin: map-get($breakpoint, margin);
46+
47+
@if ($breakpoint == null) {
48+
@error 'The provided breakpoint `#{$breakpointName}` is not a valid breakpoint found in breakpoints or extraArtboards in $grid-values. Please double-check your grid configuration.';
49+
}
3050

31-
$breakpoint: map-deep-get($grid-values, 'breakpoints', $breakpointName);
51+
@if ($columnTotal == null or type-of($columnTotal) != 'number') {
52+
@error 'The provided breakpoint `#{$breakpointName}` needs to have a total number of columns set in $grid-values. Please double-check your grid configuration.';
53+
}
3254

33-
@if (map-get($breakpoint, columns) != null) {
34-
$columnQuantity: map-get($breakpoint, columns);
35-
$fluidWidth: 100vw / $columnQuantity;
55+
@if ($columnSpan == null or type-of($columnSpan) != 'number') {
56+
@error 'The number of columns to span for the breakpoint `#{$breakpointName}` must be a valid number. The provided column span value `#{$columnSpan}` is not a number';
57+
}
3658

37-
$totalMargin: 0;
38-
@if (map-get($breakpoint, margin) != null) {
39-
$totalMargin: map-get($breakpoint, margin) * 2;
40-
}
59+
@if ($columnSpan > $columnTotal or $columnSpan <= 0) {
60+
@error 'The number of columns to span for the breakpoint `#{$breakpointName}` must be greater than 0 and less than or equal to the total number of columns for this breakpoint. The provided column value `#{$columnSpan}` does not meet this criteria';
61+
}
4162

42-
$fluidWidth: calc((100vw - #{$totalMargin}) * #{$columns} / #{$columnQuantity});
63+
// For IE, we can't have a 0 in the $fluidWidth calc().
64+
@if ($margin == 0) {
65+
@return calc((100vw) * #{$columnSpan} / #{$columnTotal});
4366
}
4467

45-
@return $fluidWidth;
68+
@return calc((100vw - (#{$margin} * 2)) * #{$columnSpan} / #{$columnTotal});
4669
}
4770

48-
/// Gets a calculated rem value for a fixed nondimensional unit size.
49-
/// @param {number} Fixed nondimensional units.
50-
/// @return {length} A calculated rem value.
71+
/// Function gets a calculated rem value for a fixed size.
72+
/// @param {Number} $fixedUnit Fixed nondimensional units.
73+
/// @return {Length} A calculated rem value.
74+
/// @example scss
75+
/// button {
76+
/// @include mediaQuery('sm') {
77+
/// max-width: getFixedSize(10);
78+
/// }
79+
/// }
80+
/// @output css
81+
/// @media screen and (min-width: 20rem) {
82+
/// button {
83+
/// max-width: 5rem;
84+
/// }
85+
/// }
5186
@function getFixedSize($fixedUnit) {
87+
$rowHeight: map-get($grid-values, 'rowHeight');
5288

53-
$remValue: 0; // default
54-
55-
@if $fixedUnit != null and $fixedUnit > 0 {
56-
$remValue: $fixedUnit * map-get($grid-values, 'rowHeight') * 1rem;
89+
@if ($rowHeight == null or type-of($rowHeight) != 'number') {
90+
@error 'The rowHeight in $grid-values needs be a valid number. Please check your grid configuration.';
5791
}
58-
@else {
59-
@error 'The provided value #{$fixedUnit} to getFixedSize() is not a number.'
92+
93+
@if ($fixedUnit == null or type-of($fixedUnit) != 'number') {
94+
@error 'The provided fixed value `#{$fixedUnit}` to get-fixed-size() needs to be a valid number greater than 0.';
6095
}
6196

62-
@return $remValue;
97+
@return $fixedUnit * $rowHeight * 1rem;
6398
}
6499

65100
/// Utility for declaring mobile-first media queries.
66-
/// @param {String} $breakpoint-name The name of the breakpoint to set its width value to media query.
67-
@mixin media-query($breakpointNme) {
101+
/// @param {String} $breakpointName The name of the breakpoint to set its width value to media query.
102+
@mixin mediaQuery($breakpointName) {
68103
@media screen and (min-width: #{getBreakpointValue($breakpointName)}) {
69104
@content;
70105
}
Binary file not shown.

0 commit comments

Comments
 (0)