From: Romaric Pascal Date: Fri, 17 Feb 2023 10:18:38 +0000 (+0000) Subject: Tidy up utilities map code X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=5df18c3853539b292010565049ffc9e888549bb1;p=thirdparty%2Fbootstrap.git Tidy up utilities map code - use consistent variable name - use consistent code structure for functions that do similar things - remove code duplication for utilities-add --- diff --git a/scss/mixins/_utilities-map.scss b/scss/mixins/_utilities-map.scss index 74f4d9c402..6e46e6c23c 100644 --- a/scss/mixins/_utilities-map.scss +++ b/scss/mixins/_utilities-map.scss @@ -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); @@ -9,11 +24,11 @@ // 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 ); } @@ -23,34 +38,21 @@ // 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 ); } @@ -59,11 +61,13 @@ // 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 @@ -87,13 +91,13 @@ // // 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 ) ); @@ -101,19 +105,21 @@ // 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