|
| 1 | +// Builds a map of utility classes. |
| 2 | +@function build-utilities-map($grid-breakpoints: $grid-breakpoints, $utilities: $utilities) { |
| 3 | + // Build a breakpoint map that does not include the zero breakpoint. |
| 4 | + $breakpoints-map: (); |
| 5 | + @each $name, $min in $grid-breakpoints { |
| 6 | + @if $min != 0 { |
| 7 | + $breakpoints-map: map-merge( |
| 8 | + $breakpoints-map, |
| 9 | + ( |
| 10 | + "-" + $name: $name, |
| 11 | + ) |
| 12 | + ); |
| 13 | + } |
| 14 | + } |
| 15 | + |
| 16 | + $utilities-map: () !default; |
| 17 | + @each $key, $utility in $utilities { |
| 18 | + |
| 19 | + @if type-of($utility) == "map" { |
| 20 | + $properties: map-get($utility, property); |
| 21 | + // Some utilities set the value on more than one property. |
| 22 | + @if type-of($properties) == "string" { |
| 23 | + $properties: append((), $properties); |
| 24 | + } |
| 25 | + |
| 26 | + // Use custom class if present |
| 27 | + $shortname: if( |
| 28 | + map-has-key($utility, class), |
| 29 | + map-get($utility, class), |
| 30 | + nth($properties, 1) |
| 31 | + ); |
| 32 | + $shortname: if($shortname == null, "", $shortname); |
| 33 | + |
| 34 | + // Shortname with prepended dash, or empty string if empty. |
| 35 | + $dashname: if($shortname == "", "", "-" + $shortname); |
| 36 | + |
| 37 | + $values: map-get($utility, values); |
| 38 | + // If the values are a list or string, convert it into a map |
| 39 | + @if type-of($values) == "string" or type-of(nth($values, 1)) != "list" { |
| 40 | + $values: zip($values, $values); |
| 41 | + } |
| 42 | + |
| 43 | + // $values could be a map or a list. @each covers both. |
| 44 | + @each $k, $value in $values { |
| 45 | + // Value key with prepended dash, or empty string if value key is null. |
| 46 | + $dashkey: if($k, "-" + $k, ""); |
| 47 | + $property-value-map: (); |
| 48 | + @each $property in $properties { |
| 49 | + $property-value-map: map-merge( |
| 50 | + $property-value-map, |
| 51 | + ( |
| 52 | + $property: $value, |
| 53 | + ) |
| 54 | + ); |
| 55 | + } |
| 56 | + $dashclass: $dashname + $dashkey; |
| 57 | + $class: str-slice($dashclass, 2); |
| 58 | + $utilities-map: map-merge( |
| 59 | + $utilities-map, |
| 60 | + ( |
| 61 | + // Create a normalized version of the utility definition. |
| 62 | + $class: ( |
| 63 | + breakpoint: null, |
| 64 | + properties: $properties, |
| 65 | + value: $value, |
| 66 | + ), |
| 67 | + ) |
| 68 | + ); |
| 69 | + @if map-get($utility, responsive) { |
| 70 | + @each $dashpoint, $breakpoint in $breakpoints-map { |
| 71 | + $class: str-slice($dashname + $dashpoint + $dashkey, 2); |
| 72 | + $utilities-map: map-merge( |
| 73 | + $utilities-map, |
| 74 | + ( |
| 75 | + $class: ( |
| 76 | + breakpoint: $breakpoint, |
| 77 | + properties: $properties, |
| 78 | + value: $value, |
| 79 | + ) |
| 80 | + ) |
| 81 | + ); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @return $utilities-map; |
| 89 | +} |
0 commit comments