Skip to content

Commit 47bbd94

Browse files
mdoffooddXhmikosR
authored
Add maps for all colors, document how to extend color utilities (#32319)
* Add maps for all colors, document how to extend color utilities * Updates to make this more functional for v5 - Moves color maps into _variables.scss for now (felt weird to split them out when the variables are there) - Adds a new function, map-merge-multiple(), and updates docs that reference combo-map - Updates code snippet and docs guidance about how to extend the color utils to handle this * Update site/content/docs/5.0/customize/color.md Co-authored-by: Gaël Poupard <ffoodd@users.noreply.github.com> Co-authored-by: XhmikosR <xhmikosr@gmail.com>
1 parent 66ce774 commit 47bbd94

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed

scss/_functions.scss

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@
5555
@return $result;
5656
}
5757

58+
// Merge multiple maps
59+
@function map-merge-multiple($maps...) {
60+
$merged-maps: ();
61+
62+
@each $map in $maps {
63+
$merged-maps: map-merge($merged-maps, $map);
64+
}
65+
@return $merged-maps;
66+
}
67+
5868
// Replace `$search` with `$replace` in `$string`
5969
// Used on our SVG icon backgrounds for custom forms.
6070
//

scss/_variables.scss

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,126 @@ $cyan-600: shade-color($cyan, 20%) !default;
198198
$cyan-700: shade-color($cyan, 40%) !default;
199199
$cyan-800: shade-color($cyan, 60%) !default;
200200
$cyan-900: shade-color($cyan, 80%) !default;
201+
202+
$blues: (
203+
"blue-100": $blue-100,
204+
"blue-200": $blue-200,
205+
"blue-300": $blue-300,
206+
"blue-400": $blue-400,
207+
"blue-500": $blue-500,
208+
"blue-600": $blue-600,
209+
"blue-700": $blue-700,
210+
"blue-800": $blue-800,
211+
"blue-900": $blue-900
212+
) !default;
213+
214+
$indigos: (
215+
"indigo-100": $indigo-100,
216+
"indigo-200": $indigo-200,
217+
"indigo-300": $indigo-300,
218+
"indigo-400": $indigo-400,
219+
"indigo-500": $indigo-500,
220+
"indigo-600": $indigo-600,
221+
"indigo-700": $indigo-700,
222+
"indigo-800": $indigo-800,
223+
"indigo-900": $indigo-900
224+
) !default;
225+
226+
$purples: (
227+
"purple-100": $purple-200,
228+
"purple-200": $purple-100,
229+
"purple-300": $purple-300,
230+
"purple-400": $purple-400,
231+
"purple-500": $purple-500,
232+
"purple-600": $purple-600,
233+
"purple-700": $purple-700,
234+
"purple-800": $purple-800,
235+
"purple-900": $purple-900
236+
) !default;
237+
238+
$pinks: (
239+
"pink-100": $pink-100,
240+
"pink-200": $pink-200,
241+
"pink-300": $pink-300,
242+
"pink-400": $pink-400,
243+
"pink-500": $pink-500,
244+
"pink-600": $pink-600,
245+
"pink-700": $pink-700,
246+
"pink-800": $pink-800,
247+
"pink-900": $pink-900
248+
) !default;
249+
250+
$reds: (
251+
"red-100": $red-100,
252+
"red-200": $red-200,
253+
"red-300": $red-300,
254+
"red-400": $red-400,
255+
"red-500": $red-500,
256+
"red-600": $red-600,
257+
"red-700": $red-700,
258+
"red-800": $red-800,
259+
"red-900": $red-900
260+
) !default;
261+
262+
$oranges: (
263+
"orange-100": $orange-100,
264+
"orange-200": $orange-200,
265+
"orange-300": $orange-300,
266+
"orange-400": $orange-400,
267+
"orange-500": $orange-500,
268+
"orange-600": $orange-600,
269+
"orange-700": $orange-700,
270+
"orange-800": $orange-800,
271+
"orange-900": $orange-900
272+
) !default;
273+
274+
$yellows: (
275+
"yellow-100": $yellow-100,
276+
"yellow-200": $yellow-200,
277+
"yellow-300": $yellow-300,
278+
"yellow-400": $yellow-400,
279+
"yellow-500": $yellow-500,
280+
"yellow-600": $yellow-600,
281+
"yellow-700": $yellow-700,
282+
"yellow-800": $yellow-800,
283+
"yellow-900": $yellow-900
284+
) !default;
285+
286+
$greens: (
287+
"green-100": $green-100,
288+
"green-200": $green-200,
289+
"green-300": $green-300,
290+
"green-400": $green-400,
291+
"green-500": $green-500,
292+
"green-600": $green-600,
293+
"green-700": $green-700,
294+
"green-800": $green-800,
295+
"green-900": $green-900
296+
) !default;
297+
298+
$teals: (
299+
"teal-100": $teal-100,
300+
"teal-200": $teal-200,
301+
"teal-300": $teal-300,
302+
"teal-400": $teal-400,
303+
"teal-500": $teal-500,
304+
"teal-600": $teal-600,
305+
"teal-700": $teal-700,
306+
"teal-800": $teal-800,
307+
"teal-900": $teal-900
308+
) !default;
309+
310+
$cyans: (
311+
"cyan-100": $cyan-100,
312+
"cyan-200": $cyan-200,
313+
"cyan-300": $cyan-300,
314+
"cyan-400": $cyan-400,
315+
"cyan-500": $cyan-500,
316+
"cyan-600": $cyan-600,
317+
"cyan-700": $cyan-700,
318+
"cyan-800": $cyan-800,
319+
"cyan-900": $cyan-900
320+
) !default;
201321
// fusv-enable
202322

203323
// Characters which are escaped by the escape-svg function

site/content/docs/5.0/customize/color.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,45 @@ Here's how you can use these in your Sass:
106106
```
107107

108108
[Color]({{< docsref "/utilities/colors" >}}) and [background]({{< docsref "/utilities/background" >}}) utility classes are also available for setting `color` and `background-color` using the `500` color values.
109+
110+
## Generating utilities
111+
112+
<small class="d-inline-flex px-2 py-1 font-monospace text-muted border rounded-3">Added in v5.1.0</small>
113+
114+
Bootstrap doesn't include `color` and `background-color` utilities for every color variable, but you can generate these yourself with our [utility API]({{< docsref "/utilities/api" >}}) and our extended Sass maps added in v5.1.0.
115+
116+
1. To start, make sure you've imported our functions, variables, mixins, and utilities.
117+
2. Use our `map-merge-multiple()` function to quickly merge multiple Sass maps together in a new map.
118+
3. Merge this new combined map to extend any utility with a `{color}-{level}` class name.
119+
120+
Here's an example that generates text color utilities (e.g., `.text-purple-500`) using the above steps.
121+
122+
```scss
123+
@import "bootstrap/scss/functions";
124+
@import "bootstrap/scss/variables";
125+
@import "bootstrap/scss/mixins";
126+
@import "bootstrap/scss/utilities";
127+
128+
$all-colors: map-merge-multiple($blues, $indigos, $purples, $pinks, $reds, $oranges, $yellows, $greens, $teals, $cyans);
129+
130+
$utilities: map-merge(
131+
$utilities,
132+
(
133+
"color": map-merge(
134+
map-get($utilities, "color"),
135+
(
136+
values: map-merge(
137+
map-get(map-get($utilities, "color"), "values"),
138+
(
139+
$all-colors
140+
),
141+
),
142+
),
143+
),
144+
)
145+
);
146+
147+
@import "bootstrap/scss/utilities/api";
148+
```
149+
150+
This will generate new `.text-{color}-{level}` utilities for every color and level. You can do the same for any other utility and property as well.

0 commit comments

Comments
 (0)