0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-02-26 23:54:23 +01:00

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
This commit is contained in:
Romaric Pascal 2023-02-17 10:18:38 +00:00
parent dc93f6ad19
commit 5df18c3853

View File

@ -2,36 +2,8 @@
// Helper functions and mixins for manipulating the utilities 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);
@if ($options) {
@return map-get(
$options,
$option-name
);
}
@return null;
}
// 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;
}
// 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: ();
@ -45,12 +17,42 @@
@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) {
$utility-options: utilities-get-options($utility-name);
@if ($utility-options) {
@return map-get(
$utility-options,
$option-name
);
}
@return null;
}
// Returns a map of the values for the utility
@function utilities-get-values($utility-name) {
$utility-values: utilities-get-option($utility-name, values);
@if (type-of($utility-values) != map) {
@return map-from($utility-values);
}
@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