Generate %placeholder utilities to @extend from
See original GitHub issueMotivation: !important in utiliites
My colleague likes to use SASS @extend
with bootstrap utility classes, e.g.
header {
@extend .py-1;
}
I personally tend to disagree, but I guess it is a valid technique.
One problem when doing this is we also inherit the !important
part, which is reasonable on actual utility classes, but not in most of the @extend
use cases.
Proposed solution: %placeholder
.
Generate placeholder utilities in addition to the regular utilities, using SASS placeholder syntax. https://sass-lang.com/documentation/style-rules/placeholder-selectors They could then be extended like so:
header {
@extend %py-1;
}
This would have the same effect, but without the !important
.
Implementation
In the generate-utility(..)
mixin it would look like this:
@mixin generate-utility(..) {
[..]
// Generate the real utility.
.#{$property-class + $infix + $property-class-modifier} {
@each $property in $properties {
#{$property}: $value if($enable-important-utilities, !important, null);
}
}
// Generate placeholder utility.
%#{$property-class + $infix + $property-class-modifier} {
@each $property in $properties {
#{$property}: $value;
}
}
I applied this as a quick hack, and it actually worked!
I was able to @extend
the generated placeholder utilities.
Userland workaround?
I was going to do this in custom code, but this would require to copy the entire generate-utility()
mixin.
Issue Analytics
- State:
- Created 2 years ago
- Comments:27 (12 by maintainers)
Top GitHub Comments
From my perspective, we will never use the utilities in our own source Sass.
Building on the first quoted comment here, this hits the same way for me. This isn’t for us. This is for people who want utility, but without the HTML pollution. This is what Tailwind does with
@apply
—building components out of atomic property-value pairs. This way you’re still using the approach and methodology of Bootstrap, but you’re still building components like us.I think there’s a very valid use case here of consuming utilities in a way that meets developers and designers where they are. Some are in the markup, some are still wanting to write component CSS without all the frills. Instead of creating your own
$component-*
variables that reassign existing Bootstrap values, you could use the utilities that are built on our own system wide rules (margin, padding, text, font, etc are all great examples of this).Or a hybrid approach of something like this…
Actually my first draft above is broken, because it does not support the case where the same short key is used in multiple utilities (e.g. margins and negative margins). I already have a different version in my own project:
This does not address your points, I m just posting it here because it works better than the other version.