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

Add thorough implementation of utilities functions and mixins

This commit is contained in:
Romaric Pascal 2022-03-13 16:41:41 +00:00
parent aa06a202b2
commit c30bd63981

View File

@ -105,16 +105,50 @@
// Gets the value of a specific option for a given utility // Gets the value of a specific option for a given utility
@function utilities-get-option($utility-name, $option-name) { @function utilities-get-option($utility-name, $option-name) {
$options: utilities-get-options($utility-name);
@if ($options) {
@return map-get( @return map-get(
utilities-get-options($utility-name), $options,
$option-name $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;
}
@function map-from($list-or-string) {
$map: ();
@each $item in $list-or-string {
$map: map-merge(
$map,
($item: $item)
);
}
@return $map;
} }
// Gets a specific value for the given utility // Gets a specific value for the given utility
@function utilities-get-value($utility-name, $value-name) { @function utilities-get-value($utility-name, $value-name) {
$values: utilities-get-values($utility-name);
@return map-get( @return map-get(
utility-get-option($utility-name, values), $values,
$value-name $value-name
); );
} }
@ -124,16 +158,26 @@
// To completely replace the existing options, set the 3rd `$merge` parameter to false // To completely replace the existing options, set the 3rd `$merge` parameter to false
@mixin utilities-set-options($utility-name, $options, $merge: true) { @mixin utilities-set-options($utility-name, $options, $merge: true) {
@if($merge) { @if ($merge) {
$options: map-merge(
$existing-options: utilities-get-options($utility-name);
// Allow the mixin to create a new utility
// when setting an unknown utility name
$options: if(
$existing-options,
map-merge(
utilities-get-options($utility-name), utilities-get-options($utility-name),
$options $options
) ),
$options
);
} }
$utilities: map-merge($utilities, $utilities: map-merge(
$utility-name: $options $utilities,
) ($utility-name: $options)
) !global; // !global ensures we modify the global $utilities map
} }
// Set a specific option to a new value for the given utility // Set a specific option to a new value for the given utility
@ -143,37 +187,41 @@
// Add new values to a given utility. // Add new values to a given utility.
// If you want to completely reconfigure the values // If you want to completely reconfigure the values
// use `utility-set-option` to provide a new map // use `utilities-set-option` to provide a new map
// for the `values` option // for the `values` option
@mixin utilities-add-values($utility-name, $values) { @mixin utilities-add-values($utility-name, $values) {
$existing-values: utilities-get-values($utility-name);
@include utilities-set-option( @include utilities-set-option(
$utility-name, $utility-name,
values, values,
map-merge( map-merge(
utility-get-option($utility-name, values), $existing-values,
$values $values
) )
) );
} }
// Remove specific values from a given utility // Remove specific values from a given utility
@mixin utilities-remove-values($utility-name, $value-names...) { @mixin utilities-remove-values($utility-name, $value-names...) {
$values: utility-get-option($utility-name, values); $values: utilities-get-values($utility-name);
$updated-values: map-remove($values, $value-names); $updated-values: map-remove($values, $value-names...);
@include utility-set-option($utility-name, values, $updated-values); @include utilities-set-option($utility-name, values, $updated-values);
} }
// Add a new utility to the utilities map // Add a new utility to the utilities map
@mixin utilities-add($utility-name, $utility) { @mixin utilities-add($utility-name, $utility) {
$utilities: map-merge($utilities, $utilities: map-merge(
$utilities,
($utility-name: $utility) ($utility-name: $utility)
); ) !global;
} }
// Remove a utility from the utilities map // Remove a utility from the utilities map
@mixin utilities-remove($utility-names...) { @mixin utilities-remove($utility-names...) {
$utilities: map-remove($utility, $utility-names); $utilities: map-remove($utilities, $utility-names...) !global;
} }