|
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. |
9 | 9 | @function map-deep-get($map, $keys...) {
|
10 | 10 | @each $key in $keys {
|
11 | 11 | $map: map-get($map, $key);
|
12 | 12 | }
|
13 | 13 | @return $map;
|
14 | 14 | }
|
15 | 15 |
|
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. |
19 | 19 | @function getBreakpointValue($breakpointName) {
|
20 |
| - $breakpointValue: map-deep-get($grid-values, 'breakpoints', $breakpointName, 'breakpoint'); |
| 20 | + $breakpointValue: map-deep-get($allBreakpoints, $breakpointName, 'breakpoint'); |
| 21 | + |
21 | 22 | @return $breakpointValue * 1rem;
|
22 | 23 | }
|
23 | 24 |
|
24 | 25 | /// 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 | + } |
30 | 50 |
|
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 | + } |
32 | 54 |
|
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 | + } |
36 | 58 |
|
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 | + } |
41 | 62 |
|
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}); |
43 | 66 | }
|
44 | 67 |
|
45 |
| - @return $fluidWidth; |
| 68 | + @return calc((100vw - (#{$margin} * 2)) * #{$columnSpan} / #{$columnTotal}); |
46 | 69 | }
|
47 | 70 |
|
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 | +/// } |
51 | 86 | @function getFixedSize($fixedUnit) {
|
| 87 | + $rowHeight: map-get($grid-values, 'rowHeight'); |
52 | 88 |
|
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.'; |
57 | 91 | }
|
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.'; |
60 | 95 | }
|
61 | 96 |
|
62 |
| - @return $remValue; |
| 97 | + @return $fixedUnit * $rowHeight * 1rem; |
63 | 98 | }
|
64 | 99 |
|
65 | 100 | /// 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) { |
68 | 103 | @media screen and (min-width: #{getBreakpointValue($breakpointName)}) {
|
69 | 104 | @content;
|
70 | 105 | }
|
|
0 commit comments