aboutsummaryrefslogtreecommitdiff
path: root/scss/functions/_utilities-map.scss
blob: 28c7380ce6375f43b36d4cb0ba121224b6210e12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Builds a map of utility classes.
@function build-utilities-map($grid-breakpoints: $grid-breakpoints, $utilities: $utilities) {
  // Build a breakpoint map that does not include the zero breakpoint.
  $breakpoints-map: ();
  @each $name, $min in $grid-breakpoints {
    @if $min != 0 {
      $breakpoints-map: map-merge(
        $breakpoints-map,
        (
          "-" + $name: $name,
        )
      );
    }
  }

  $utilities-map: () !default;
  @each $key, $utility in $utilities {

    @if type-of($utility) == "map" {
      $properties: map-get($utility, property);
      // Some utilities set the value on more than one property.
      @if type-of($properties) == "string" {
        $properties: append((), $properties);
      }

      // Use custom class if present
      $shortname: if(
        map-has-key($utility, class),
        map-get($utility, class),
        nth($properties, 1)
      );
      $shortname: if($shortname == null, "", $shortname);

      // Shortname with prepended dash, or empty string if empty.
      $dashname: if($shortname == "", "", "-" + $shortname);

      $values: map-get($utility, values);
      // If the values are a list or string, convert it into a map
      @if type-of($values) == "string" or type-of(nth($values, 1)) != "list" {
        $values: zip($values, $values);
      }

      // $values could be a map or a list. @each covers both.
      @each $k, $value in $values {
        // Value key with prepended dash, or empty string if value key is null.
        $dashkey: if($k, "-" + $k, "");
        $property-value-map: ();
        @each $property in $properties {
          $property-value-map: map-merge(
            $property-value-map,
            (
              $property: $value,
            )
          );
        }
        $dashclass: $dashname + $dashkey;
        $class: str-slice($dashclass, 2);
        $utilities-map: map-merge(
          $utilities-map,
          (
            // Create a normalized version of the utility definition.
            $class: (
              breakpoint: null,
              properties: $properties,
              value: $value,
            ),
          )
        );
        @if map-get($utility, responsive) {
          @each $dashpoint, $breakpoint in $breakpoints-map {
            $class: str-slice($dashname + $dashpoint + $dashkey, 2);
            $utilities-map: map-merge(
              $utilities-map,
              (
                $class: (
                  breakpoint: $breakpoint,
                  properties: $properties,
                  value: $value,
                )
              )
            );
          }
        }
      }
    }
  }

  @return $utilities-map;
}