]> git.ipfire.org Git - thirdparty/bootstrap.git/commitdiff
Tidy up utilities map code
authorRomaric Pascal <hello@romaricpascal.is>
Fri, 17 Feb 2023 10:18:38 +0000 (10:18 +0000)
committerRomaric Pascal <hello@romaricpascal.is>
Fri, 17 Feb 2023 10:23:40 +0000 (10:23 +0000)
- use consistent variable name
- use consistent code structure for functions that do similar things
- remove code duplication for utilities-add

scss/mixins/_utilities-map.scss

index 74f4d9c402d582cf4a8e6c6dcac251f81f614ee2..6e46e6c23c7599a308f23b3ac449426a29907fcb 100644 (file)
@@ -2,6 +2,21 @@
 // Helper functions and mixins for manipulating the utilities map
 //
 
+// Helper function to create a map from a list or string
+// so we can manipulate utility values consistently
+@function map-from($list-or-string) {
+  $map: ();
+
+  @each $item in $list-or-string {
+    $map: map-merge(
+      $map,
+      ($item: $item)
+    );
+  }
+
+  @return $map;
+}
+
 // Gets the map of options for a given utility
 @function utilities-get-options($utility-name) {
   @return map-get($utilities, $utility-name);
 
 // Gets the value of a specific option for a given utility
 @function utilities-get-option($utility-name, $option-name) {
-  $options: utilities-get-options($utility-name);
+  $utility-options: utilities-get-options($utility-name);
 
-  @if ($options) {
+  @if ($utility-options) {
     @return map-get(
-      $options,
+      $utility-options,
       $option-name
     );
   }
 
 // Returns a map of the values for the utility
 @function utilities-get-values($utility-name) {
-  $values: utilities-get-option($utility-name, values);
-
-  @if (type-of($values) != map) {
-    @return map-from($values);
-  }
-
-  @return $values;
-}
+  $utility-values: utilities-get-option($utility-name, values);
 
-@function map-from($list-or-string) {
-  $map: ();
-
-  @each $item in $list-or-string {
-    $map: map-merge(
-      $map,
-      ($item: $item)
-    );
+  @if (type-of($utility-values) != map) {
+    @return map-from($utility-values);
   }
 
-  @return $map;
+  @return $utility-values;
 }
 
 // Gets a specific value for the given utility
 @function utilities-get-value($utility-name, $value-name) {
-  $values: utilities-get-values($utility-name);
+  $utility-values: utilities-get-values($utility-name);
 
   @return map-get(
-    $values,
+    $utility-values,
     $value-name
   );
 }
 // To completely replace the existing options, set the 3rd `$merge` parameter to false
 @mixin utilities-set-options($utility-name, $options, $merge: true) {
   @if ($merge) {
-    $existing-options: utilities-get-options($utility-name);
+    $utility-options: utilities-get-options($utility-name);
 
-    // Allow the mixin to create a new utility when setting an unknown utility name
+    // Allows the mixin to create a new utility when setting an unknown utility name
+    // Important: The variable needs to have the same name as the function argument
+    // here, so that we can have a single merge call at the end of the function
     $options: if(
-      $existing-options,
+      $utility-options,
       map-merge(
         utilities-get-options($utility-name),
         $options
 //
 // If you want to completely reconfigure the values use `utilities-set-option` to provide a new map for the `values` option
 @mixin utilities-add-values($utility-name, $values) {
-  $existing-values: utilities-get-values($utility-name);
+  $utility-values: utilities-get-values($utility-name);
 
   @include utilities-set-option(
     $utility-name,
     values,
     map-merge(
-      $existing-values,
+      $utility-values,
       $values
     )
   );
 
 // Remove specific values from a given utility
 @mixin utilities-remove-values($utility-name, $value-names...) {
-  $values: utilities-get-values($utility-name);
+  $utility-values: utilities-get-values($utility-name);
 
-  $updated-values: map-remove($values, $value-names...);
-
-  @include utilities-set-option($utility-name, values, $updated-values);
+  @include utilities-set-option(
+    $utility-name,
+    values,
+    map-remove(
+      $utility-values,
+      $value-names...
+    )
+  );
 }
 
 // Add a new utility to the utilities map
 @mixin utilities-add($utility-name, $utility) {
-  $utilities: map-merge(
-    $utilities,
-    ($utility-name: $utility)
-  ) !global;
+  @include utilities-set-options($utility-name, $utility, false);
 }
 
 // Remove a utility from the utilities map