1
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-08-22 05:03:16 +02:00

Issue #33861: New utl() mixin

Co-Authored-By: Andreas Hennings <andreas@dqxtech.net>
This commit is contained in:
Andreas Hennings
2021-05-25 00:00:44 +02:00
committed by Mark Otto
parent 208ba3d748
commit 0354fd27e4
6 changed files with 119 additions and 0 deletions

View File

@@ -73,6 +73,9 @@
@return $result; @return $result;
} }
// Some functions are defined in separate files.
@import "functions/utilities-map";
// Internal Bootstrap function to turn maps into its negative variant. // Internal Bootstrap function to turn maps into its negative variant.
// It prefixes the keys with `n` and makes the value negative. // It prefixes the keys with `n` and makes the value negative.
@function negativify-map($map) { @function negativify-map($map) {

View File

@@ -60,3 +60,4 @@ $utilities: map-get-multiple(
); );
@import "utilities/api"; @import "utilities/api";
@import "utilities/mixin";

View File

@@ -17,3 +17,4 @@
// Utilities // Utilities
@import "utilities/api"; @import "utilities/api";
@import "utilities/mixin";

1
scss/bootstrap.scss vendored
View File

@@ -49,4 +49,5 @@
// Utilities // Utilities
@import "utilities/api"; @import "utilities/api";
@import "utilities/mixin";
// scss-docs-end import-stack // scss-docs-end import-stack

View File

@@ -0,0 +1,89 @@
// Builds a map of utility classes.
@function build-utilities-map($grid-breakpoints: $grid-breakpoints, $utilities: $utilities) {
// Build a breakpoint map that does not include the zero breakpoint.
$breakpoints-map: ();
@each $name, $min in $grid-breakpoints {
@if $min != 0 {
$breakpoints-map: map-merge(
$breakpoints-map,
(
"-" + $name: $name,
)
);
}
}
$utilities-map: () !default;
@each $key, $utility in $utilities {
@if type-of($utility) == "map" {
$properties: map-get($utility, property);
// Some utilities set the value on more than one property.
@if type-of($properties) == "string" {
$properties: append((), $properties);
}
// Use custom class if present
$shortname: if(
map-has-key($utility, class),
map-get($utility, class),
nth($properties, 1)
);
$shortname: if($shortname == null, "", $shortname);
// Shortname with prepended dash, or empty string if empty.
$dashname: if($shortname == "", "", "-" + $shortname);
$values: map-get($utility, values);
// If the values are a list or string, convert it into a map
@if type-of($values) == "string" or type-of(nth($values, 1)) != "list" {
$values: zip($values, $values);
}
// $values could be a map or a list. @each covers both.
@each $k, $value in $values {
// Value key with prepended dash, or empty string if value key is null.
$dashkey: if($k, "-" + $k, "");
$property-value-map: ();
@each $property in $properties {
$property-value-map: map-merge(
$property-value-map,
(
$property: $value,
)
);
}
$dashclass: $dashname + $dashkey;
$class: str-slice($dashclass, 2);
$utilities-map: map-merge(
$utilities-map,
(
// Create a normalized version of the utility definition.
$class: (
breakpoint: null,
properties: $properties,
value: $value,
),
)
);
@if map-get($utility, responsive) {
@each $dashpoint, $breakpoint in $breakpoints-map {
$class: str-slice($dashname + $dashpoint + $dashkey, 2);
$utilities-map: map-merge(
$utilities-map,
(
$class: (
breakpoint: $breakpoint,
properties: $properties,
value: $value,
)
)
);
}
}
}
}
}
@return $utilities-map;
}

View File

@@ -0,0 +1,24 @@
$utilities-map: build-utilities-map(); // stylelint-disable-line scss/dollar-variable-default
@mixin utl($class) {
@if map-has-key($utilities-map, $class) {
$definition: map-get($utilities-map, $class);
$breakpoint: map-get($definition, breakpoint);
@if $breakpoint != null {
@include media-breakpoint-up($breakpoint) {
@each $property in map-get($definition, properties) {
#{$property}: map-get($definition, value);
}
}
}
@else {
@each $property in map-get($definition, properties) {
#{$property}: map-get($definition, value);
}
}
}
@else {
@debug "Unknown utility class " + $class;
}
}