New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Utilities functions mixin #37398
base: main
Are you sure you want to change the base?
Utilities functions mixin #37398
Conversation
- get options, a specific option or a specific value for a given utility - set options, new values or remove values for a given utility - add or remove utilities
|
@julien-deramond @louismaximepiton @ffoodd Would love your thoughts on this! Kind of a few PRs worth of history to look through for the full context, but would love to add this for v5.3. |
|
Happy to answer any question about the code |
Just few questions, small things to change IMO and as global comments:
Should we introduce default values on each function/mixin ?
Should we check each time for existence of the map ?
What's the future for the maps.scss file ? I thought it was for this kind of purpose (redefine some maps before using them inside utilities API).
However, this sounds really great to have inside Bootstrap. This doesn't add compiled CSS, might not bring any breaking change, might be really useful when well used ! Great work :)
| @return $values; | ||
| } | ||
|
|
||
| @function map-from($list-or-string) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to be sure if I understand well: if we use a string here, there should be only 1 item in the map right ? or is there a way to do multiple item map from a string ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, @each won't split the string in any way and treat it as a list with 1 item in it.
|
|
||
| // 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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we're experiencing some troubles with some overriding Sass variables, should we use a nomenclature such as $utility-options: ...
| } | ||
|
|
||
| $utilities: map-merge( | ||
| $utilities, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose $utilities cannot and should not be null here ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$utilities is the global $utilities variable so is defined by Bootstrap. There's always a risk someone made it null. However, things will blow up in utilities/_api during generation if that's the case, so I'm no sure it's worth being more defensive here.
| @mixin utilities-remove-values($utility-name, $value-names...) { | ||
| $values: utilities-get-values($utility-name); | ||
|
|
||
| $updated-values: map-remove($values, $value-names...); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we check for values not to be null ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as in the previous function, values won't be null thanks to map-from inside utilities-get-values.
|
|
||
| // Remove specific values from a given utility | ||
| @mixin utilities-remove-values($utility-name, $value-names...) { | ||
| $values: utilities-get-values($utility-name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should use the same name as the previous function ?
|
|
||
| // Add a new utility to the utilities map | ||
| @mixin utilities-add($utility-name, $utility) { | ||
| $utilities: map-merge( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we use @include utilities-set-options($utility-name, $utility, false); ?
@louismaximepiton Thanks for the review
I'm not quite sure how to deal with the few amends as it's now @mdo's branch on the Bootstrap repo. A PR towards this branch feels a bit overkill, and it'll likely be much faster if someone with write access moves things around (especially as I don't have as much capacity to contribute nowadays, sorry).
As a side note, now #36029 has been merged, it's likely worth pulling in the tests I had set up on a separate branch for writing these functions and mixins.
| @return $values; | ||
| } | ||
|
|
||
| @function map-from($list-or-string) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, @each won't split the string in any way and treat it as a list with 1 item in it.
|
|
||
| // Set options for a given utility, merged with the existing ones by default. | ||
| // To completely replace the existing options, set the 3rd `$merge` parameter to false | ||
| @mixin utilities-set-options($utility-name, $options, $merge: true) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point on utilities-add being utilities-set-options with $merge: false. I agree it's likely worth refactoring so utilities-add just delegates to utilities-set-options to avoid duplication.
| } | ||
|
|
||
| $utilities: map-merge( | ||
| $utilities, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$utilities is the global $utilities variable so is defined by Bootstrap. There's always a risk someone made it null. However, things will blow up in utilities/_api during generation if that's the case, so I'm no sure it's worth being more defensive here.
| @mixin utilities-remove-values($utility-name, $value-names...) { | ||
| $values: utilities-get-values($utility-name); | ||
|
|
||
| $updated-values: map-remove($values, $value-names...); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as in the previous function, values won't be null thanks to map-from inside utilities-get-values.
| $utility-name, | ||
| values, | ||
| map-merge( | ||
| $existing-values, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utilities-get-values will return an empty map through map-from, so we can always run map-merge without worry here values field if it didn't exist already, but it feels fair as it fulfils what the function name says.


Replaces #37114 because I couldn't push to that PR.