Skip to content

Commit 1c05c1d

Browse files
MartijnCuppensXhmikosR
authored andcommitted
Drop color(), theme-color() & gray() functions (#29083)
Drop `color()`, `theme-color()` & `gray()` functions in favor of variables. The functions just called a `map-get()` of a map where just the variables were defined. Also the `theme-color-level()` now accepts any color you want instead of only `$theme-colors` colors. The first value now is a variable instead of the `$theme-colors` key.
1 parent 172d580 commit 1c05c1d

File tree

7 files changed

+30
-58
lines changed

7 files changed

+30
-58
lines changed

scss/_alert.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@
4646

4747
@each $color, $value in $theme-colors {
4848
.alert-#{$color} {
49-
@include alert-variant(theme-color-level($color, $alert-bg-level), theme-color-level($color, $alert-border-level), theme-color-level($color, $alert-color-level));
49+
@include alert-variant(color-level($value, $alert-bg-level), color-level($value, $alert-border-level), color-level($value, $alert-color-level));
5050
}
5151
}

scss/_functions.scss

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,22 +93,8 @@
9393
@return if($yiq >= $yiq-contrasted-threshold, $dark, $light);
9494
}
9595

96-
// Retrieve color Sass maps
97-
@function color($key: "blue") {
98-
@return map-get($colors, $key);
99-
}
100-
101-
@function theme-color($key: "primary") {
102-
@return map-get($theme-colors, $key);
103-
}
104-
105-
@function gray($key: "100") {
106-
@return map-get($grays, $key);
107-
}
108-
109-
// Request a theme color level
110-
@function theme-color-level($color-name: "primary", $level: 0) {
111-
$color: theme-color($color-name);
96+
// Request a color level
97+
@function color-level($color: $primary, $level: 0) {
11298
$color-base: if($level > 0, $black, $white);
11399
$level: abs($level);
114100

scss/_list-group.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,5 +155,5 @@
155155
// Organizationally, this must come after the `:hover` states.
156156

157157
@each $color, $value in $theme-colors {
158-
@include list-group-item-variant($color, theme-color-level($color, -9), theme-color-level($color, 6));
158+
@include list-group-item-variant($color, color-level($value, -9), color-level($value, 6));
159159
}

scss/_tables.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
// inheritance to nested tables.
107107

108108
@each $color, $value in $theme-colors {
109-
@include table-row-variant($color, theme-color-level($color, $table-bg-level), theme-color-level($color, $table-border-level));
109+
@include table-row-variant($color, color-level($value, $table-bg-level), color-level($value, $table-border-level));
110110
}
111111

112112
@include table-row-variant(active, $table-active-bg);

scss/_variables.scss

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ $body-text-align: null !default;
161161
//
162162
// Style anchor elements.
163163

164-
$link-color: theme-color("primary") !default;
164+
$link-color: $primary !default;
165165
$link-decoration: none !default;
166166
$link-hover-color: darken($link-color, 15%) !default;
167167
$link-hover-decoration: underline !default;
@@ -244,7 +244,7 @@ $box-shadow-lg: 0 1rem 3rem rgba($black, .175) !default;
244244
$box-shadow-inset: inset 0 1px 2px rgba($black, .075) !default;
245245

246246
$component-active-color: $white !default;
247-
$component-active-bg: theme-color("primary") !default;
247+
$component-active-bg: $primary !default;
248248

249249
$caret-width: .3em !default;
250250
$caret-vertical-align: $caret-width * .85 !default;
@@ -661,8 +661,8 @@ $form-file-button-bg: $input-group-addon-bg !default;
661661

662662
$form-feedback-margin-top: $form-text-margin-top !default;
663663
$form-feedback-font-size: $small-font-size !default;
664-
$form-feedback-valid-color: theme-color("success") !default;
665-
$form-feedback-invalid-color: theme-color("danger") !default;
664+
$form-feedback-valid-color: $success !default;
665+
$form-feedback-invalid-color: $danger !default;
666666

667667
$form-feedback-icon-valid-color: $form-feedback-valid-color !default;
668668
$form-feedback-icon-valid: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'><path fill='#{$form-feedback-icon-valid-color}' d='M2.3 6.73L.6 4.53c-.4-1.04.46-1.4 1.1-.8l1.1 1.4 3.4-3.8c.6-.63 1.6-.27 1.2.7l-4 4.6c-.43.5-.8.4-1.1.1z'/></svg>") !default;
@@ -985,7 +985,7 @@ $progress-bg: $gray-200 !default;
985985
$progress-border-radius: $border-radius !default;
986986
$progress-box-shadow: $box-shadow-inset !default;
987987
$progress-bar-color: $white !default;
988-
$progress-bar-bg: theme-color("primary") !default;
988+
$progress-bar-bg: $primary !default;
989989
$progress-bar-animation-timing: 1s linear infinite !default;
990990
$progress-bar-transition: width .6s ease !default;
991991

site/content/docs/4.3/getting-started/theming.md

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,19 @@ Some of our Sass maps are merged into empty ones by default. This is done to all
100100

101101
#### Modify map
102102

103-
To modify an existing color in our `$theme-colors` map, add the following to your custom Sass file:
103+
All variables in the `$theme-colors` map are defined as standalone variables. To modify an existing color in our `$theme-colors` map, add the following to your custom Sass file:
104+
105+
{{< highlight scss >}}
106+
$primary: #0074d9;
107+
$danger: #ff4136;
108+
{{< /highlight >}}
109+
110+
Later on, theses variables are set in Bootstrap's `$theme-colors` map:
104111

105112
{{< highlight scss >}}
106113
$theme-colors: (
107-
"primary": #0074d9,
108-
"danger": #ff4136
114+
"primary": $primary,
115+
"danger": $danger
109116
);
110117
{{< /highlight >}}
111118

@@ -146,36 +153,19 @@ For example, we use the `primary`, `success`, and `danger` keys from `$theme-col
146153

147154
### Functions
148155

149-
Bootstrap utilizes several Sass functions, but only a subset are applicable to general theming. We've included three functions for getting values from the color maps:
150-
151-
{{< highlight scss >}}
152-
@function color($key: "blue") {
153-
@return map-get($colors, $key);
154-
}
155-
156-
@function theme-color($key: "primary") {
157-
@return map-get($theme-colors, $key);
158-
}
159-
160-
@function gray($key: "100") {
161-
@return map-get($grays, $key);
162-
}
163-
{{< /highlight >}}
164-
165-
These allow you to pick one color from a Sass map much like how you'd use a color variable from v3.
156+
In Bootstrap 5, we've dropped the `color()`, `theme-color()` and `gray()` functions because the values are also available as standalone variables. So instead of using `theme-color("primary")`, you can now just use the `$primary` variable.
166157

167158
{{< highlight scss >}}
168159
.custom-element {
169-
color: gray("100");
170-
background-color: theme-color("dark");
160+
color: $gray-100;
161+
background-color: $dark;
171162
}
172163
{{< /highlight >}}
173164

174-
We also have another function for getting a particular _level_ of color from the `$theme-colors` map. Negative level values will lighten the color, while higher levels will darken.
165+
We also have a function for getting a particular _level_ of color. Negative level values will lighten the color, while higher levels will darken.
175166

176167
{{< highlight scss >}}
177-
@function theme-color-level($color-name: "primary", $level: 0) {
178-
$color: theme-color($color-name);
168+
@function color-level($color: $primary, $level: 0) {
179169
$color-base: if($level > 0, #000, #fff);
180170
$level: abs($level);
181171

@@ -187,12 +177,10 @@ In practice, you'd call the function and pass in two parameters: the name of the
187177

188178
{{< highlight scss >}}
189179
.custom-element {
190-
color: theme-color-level(primary, -10);
180+
color: color-level($primary, -10);
191181
}
192182
{{< /highlight >}}
193183

194-
Additional functions could be added in the future or your own custom Sass to create level functions for additional Sass maps, or even a generic one if you wanted to be more verbose.
195-
196184
### Color contrast
197185

198186
An additional function we include in Bootstrap is the color contrast function, `color-yiq`. It utilizes the [YIQ color space](https://en.wikipedia.org/wiki/YIQ) to automatically return a light (`#fff`) or dark (`#111`) contrast color based on the specified base color. This function is especially useful for mixins or loops where you're generating multiple classes.
@@ -219,7 +207,7 @@ You can also specify a base color with our color map functions:
219207

220208
{{< highlight scss >}}
221209
.custom-element {
222-
color: color-yiq(theme-color("dark")); // returns `color: #fff`
210+
color: color-yiq($dark); // returns `color: #fff`
223211
}
224212
{{< /highlight >}}
225213

@@ -272,11 +260,7 @@ All colors available in Bootstrap 4, are available as Sass variables and a Sass
272260
Here's how you can use these in your Sass:
273261

274262
{{< highlight scss >}}
275-
// With variable
276263
.alpha { color: $purple; }
277-
278-
// From the Sass map with our `color()` function
279-
.beta { color: color("purple"); }
280264
{{< /highlight >}}
281265

282266
[Color utility classes]({{< docsref "/utilities/colors" >}}) are also available for setting `color` and `background-color`.
@@ -349,7 +333,7 @@ Here are two examples of how we loop over the `$theme-colors` map to generate mo
349333
// Generate alert modifier classes
350334
@each $color, $value in $theme-colors {
351335
.alert-#{$color} {
352-
@include alert-variant(theme-color-level($color, -10), theme-color-level($color, -9), theme-color-level($color, 6));
336+
@include alert-variant(color-level($color, -10), color-level($color, -9), color-level($color, 6));
353337
}
354338
}
355339

site/content/docs/4.3/migration.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Changes to our source Sass files and compiled CSS.
3333
- **Todo:** Rearrange forms source files (under `scss/forms/`)
3434
- **Todo:** Rearrange grid source files (under `scss/grid/`)
3535
- Removed print styles and `$enable-print-styles` variable. Print display classes, however, have remained intact. [See #28339](https://github.com/twbs/bootstrap/pull/28339).
36+
- Dropped `color()`, `theme-color()` & `gray()` functions in favor of variables. [See #29083](https://github.com/twbs/bootstrap/pull/29083)
37+
- The `theme-color-level()` function is renamed to `color-level()` and now accepts any color you want instead of only `$theme-color` colors. [See #29083](https://github.com/twbs/bootstrap/pull/29083)
3638

3739
## JavaScript
3840

0 commit comments

Comments
 (0)