]> git.ipfire.org Git - thirdparty/bootstrap.git/commitdiff
Issue #33861: New utl() mixin
authorAndreas Hennings <andreas@dqxtech.net>
Mon, 24 May 2021 22:00:44 +0000 (00:00 +0200)
committerMark Otto <markdotto@gmail.com>
Sat, 25 Mar 2023 03:10:12 +0000 (22:10 -0500)
Co-Authored-By: Andreas Hennings <andreas@dqxtech.net>
scss/_functions.scss
scss/bootstrap-grid.scss
scss/bootstrap-utilities.scss
scss/bootstrap.scss
scss/functions/_utilities-map.scss [new file with mode: 0644]
scss/utilities/_mixin.scss [new file with mode: 0644]

index 90296586b3c8cb64e3105ed61ca44773f815ef6a..522f8ad2ffb3cf9b7a591f97381fe046d35fbcc9 100644 (file)
@@ -73,6 +73,9 @@
   @return $result;
 }
 
+// Some functions are defined in separate files.
+@import "functions/utilities-map";
+
 // Internal Bootstrap function to turn maps into its negative variant.
 // It prefixes the keys with `n` and makes the value negative.
 @function negativify-map($map) {
index 52bd577e3ae57f75bf42c1c58cb210da9cd78203..0f5ea5a05c5254014f309e0970bf6c86b2a0a6bd 100644 (file)
@@ -60,3 +60,4 @@ $utilities: map-get-multiple(
 );
 
 @import "utilities/api";
+@import "utilities/mixin";
index 99c4a3595ca5744931f4f26f3166e391c6137921..c3618a58e0b5d32c5c08d9eb55066e6f3c3b72a8 100644 (file)
@@ -17,3 +17,4 @@
 
 // Utilities
 @import "utilities/api";
+@import "utilities/mixin";
index 449d704878fa210c85e09a2af91723eee9f14ad6..a38a5c6c79cf7da62d2dc23c17f4d211f5d47335 100644 (file)
@@ -49,4 +49,5 @@
 
 // Utilities
 @import "utilities/api";
+@import "utilities/mixin";
 // scss-docs-end import-stack
diff --git a/scss/functions/_utilities-map.scss b/scss/functions/_utilities-map.scss
new file mode 100644 (file)
index 0000000..28c7380
--- /dev/null
@@ -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;
+}
diff --git a/scss/utilities/_mixin.scss b/scss/utilities/_mixin.scss
new file mode 100644 (file)
index 0000000..175affe
--- /dev/null
@@ -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;
+  }
+}