Skip to content

Commit 0354fd2

Browse files
donquixotemdo
authored andcommitted
Issue #33861: New utl() mixin
Co-Authored-By: Andreas Hennings <andreas@dqxtech.net>
1 parent 208ba3d commit 0354fd2

6 files changed

Lines changed: 119 additions & 0 deletions

File tree

scss/_functions.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@
7373
@return $result;
7474
}
7575

76+
// Some functions are defined in separate files.
77+
@import "functions/utilities-map";
78+
7679
// Internal Bootstrap function to turn maps into its negative variant.
7780
// It prefixes the keys with `n` and makes the value negative.
7881
@function negativify-map($map) {

scss/bootstrap-grid.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,4 @@ $utilities: map-get-multiple(
6060
);
6161

6262
@import "utilities/api";
63+
@import "utilities/mixin";

scss/bootstrap-utilities.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717

1818
// Utilities
1919
@import "utilities/api";
20+
@import "utilities/mixin";

scss/bootstrap.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@
4949

5050
// Utilities
5151
@import "utilities/api";
52+
@import "utilities/mixin";
5253
// scss-docs-end import-stack

scss/functions/_utilities-map.scss

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Builds a map of utility classes.
2+
@function build-utilities-map($grid-breakpoints: $grid-breakpoints, $utilities: $utilities) {
3+
// Build a breakpoint map that does not include the zero breakpoint.
4+
$breakpoints-map: ();
5+
@each $name, $min in $grid-breakpoints {
6+
@if $min != 0 {
7+
$breakpoints-map: map-merge(
8+
$breakpoints-map,
9+
(
10+
"-" + $name: $name,
11+
)
12+
);
13+
}
14+
}
15+
16+
$utilities-map: () !default;
17+
@each $key, $utility in $utilities {
18+
19+
@if type-of($utility) == "map" {
20+
$properties: map-get($utility, property);
21+
// Some utilities set the value on more than one property.
22+
@if type-of($properties) == "string" {
23+
$properties: append((), $properties);
24+
}
25+
26+
// Use custom class if present
27+
$shortname: if(
28+
map-has-key($utility, class),
29+
map-get($utility, class),
30+
nth($properties, 1)
31+
);
32+
$shortname: if($shortname == null, "", $shortname);
33+
34+
// Shortname with prepended dash, or empty string if empty.
35+
$dashname: if($shortname == "", "", "-" + $shortname);
36+
37+
$values: map-get($utility, values);
38+
// If the values are a list or string, convert it into a map
39+
@if type-of($values) == "string" or type-of(nth($values, 1)) != "list" {
40+
$values: zip($values, $values);
41+
}
42+
43+
// $values could be a map or a list. @each covers both.
44+
@each $k, $value in $values {
45+
// Value key with prepended dash, or empty string if value key is null.
46+
$dashkey: if($k, "-" + $k, "");
47+
$property-value-map: ();
48+
@each $property in $properties {
49+
$property-value-map: map-merge(
50+
$property-value-map,
51+
(
52+
$property: $value,
53+
)
54+
);
55+
}
56+
$dashclass: $dashname + $dashkey;
57+
$class: str-slice($dashclass, 2);
58+
$utilities-map: map-merge(
59+
$utilities-map,
60+
(
61+
// Create a normalized version of the utility definition.
62+
$class: (
63+
breakpoint: null,
64+
properties: $properties,
65+
value: $value,
66+
),
67+
)
68+
);
69+
@if map-get($utility, responsive) {
70+
@each $dashpoint, $breakpoint in $breakpoints-map {
71+
$class: str-slice($dashname + $dashpoint + $dashkey, 2);
72+
$utilities-map: map-merge(
73+
$utilities-map,
74+
(
75+
$class: (
76+
breakpoint: $breakpoint,
77+
properties: $properties,
78+
value: $value,
79+
)
80+
)
81+
);
82+
}
83+
}
84+
}
85+
}
86+
}
87+
88+
@return $utilities-map;
89+
}

scss/utilities/_mixin.scss

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
$utilities-map: build-utilities-map(); // stylelint-disable-line scss/dollar-variable-default
3+
4+
@mixin utl($class) {
5+
@if map-has-key($utilities-map, $class) {
6+
$definition: map-get($utilities-map, $class);
7+
$breakpoint: map-get($definition, breakpoint);
8+
@if $breakpoint != null {
9+
@include media-breakpoint-up($breakpoint) {
10+
@each $property in map-get($definition, properties) {
11+
#{$property}: map-get($definition, value);
12+
}
13+
}
14+
}
15+
@else {
16+
@each $property in map-get($definition, properties) {
17+
#{$property}: map-get($definition, value);
18+
}
19+
}
20+
}
21+
@else {
22+
@debug "Unknown utility class " + $class;
23+
}
24+
}

0 commit comments

Comments
 (0)