Skip to content

Commit

Permalink
Drop color(), theme-color() & gray() functions in favor of vari…
Browse files Browse the repository at this point in the history
…ables

Also the `theme-color-level()` now accepts any color you want instead of only `$theme-color` colors.
  • Loading branch information
MartijnCuppens committed Jul 22, 2019
1 parent cd4768a commit 2b1e2be
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 77 deletions.
2 changes: 1 addition & 1 deletion scss/_alert.scss
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@

@each $color, $value in $theme-colors {
.alert-#{$color} {
@include alert-variant(theme-color-level($color, $alert-bg-level), theme-color-level($color, $alert-border-level), theme-color-level($color, $alert-color-level));
@include alert-variant(color-level($value, $alert-bg-level), color-level($value, $alert-border-level), color-level($value, $alert-color-level));
}
}
18 changes: 2 additions & 16 deletions scss/_functions.scss
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,8 @@
@return if($yiq >= $yiq-contrasted-threshold, $dark, $light);
}

// Retrieve color Sass maps
@function color($key: "blue") {
@return map-get($colors, $key);
}

@function theme-color($key: "primary") {
@return map-get($theme-colors, $key);
}

@function gray($key: "100") {
@return map-get($grays, $key);
}

// Request a theme color level
@function theme-color-level($color-name: "primary", $level: 0) {
$color: theme-color($color-name);
// Request a color level
@function color-level($color: $primary, $level: 0) {
$color-base: if($level > 0, $black, $white);
$level: abs($level);

Expand Down
2 changes: 1 addition & 1 deletion scss/_list-group.scss
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,5 @@
// Organizationally, this must come after the `:hover` states.

@each $color, $value in $theme-colors {
@include list-group-item-variant($color, theme-color-level($color, -9), theme-color-level($color, 6));
@include list-group-item-variant($color, color-level($value, -9), color-level($value, 6));
}
2 changes: 1 addition & 1 deletion scss/_tables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
// inheritance to nested tables.

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

@include table-row-variant(active, $table-active-bg);
Expand Down
10 changes: 5 additions & 5 deletions scss/_variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ $body-text-align: null !default;
//
// Style anchor elements.

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

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

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

$form-feedback-margin-top: $form-text-margin-top !default;
$form-feedback-font-size: $small-font-size !default;
$form-feedback-valid-color: theme-color("success") !default;
$form-feedback-invalid-color: theme-color("danger") !default;
$form-feedback-valid-color: $success !default;
$form-feedback-invalid-color: $danger !default;

$form-feedback-icon-valid-color: $form-feedback-valid-color !default;
$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;
Expand Down Expand Up @@ -985,7 +985,7 @@ $progress-bg: $gray-200 !default;
$progress-border-radius: $border-radius !default;
$progress-box-shadow: $box-shadow-inset !default;
$progress-bar-color: $white !default;
$progress-bar-bg: theme-color("primary") !default;
$progress-bar-bg: $primary !default;
$progress-bar-animation-timing: 1s linear infinite !default;
$progress-bar-transition: width .6s ease !default;

Expand Down
52 changes: 18 additions & 34 deletions site/content/docs/4.3/getting-started/theming.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,19 @@ Some of our Sass maps are merged into empty ones by default. This is done to all

#### Modify map

To modify an existing color in our `$theme-colors` map, add the following to your custom Sass file:
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:

{{< highlight scss >}}
$primary: #0074d9;
$danger: #ff4136;
{{< /highlight >}}

Later on, theses variables are set in Bootstrap's `$theme-colors` map:

{{< highlight scss >}}
$theme-colors: (
"primary": #0074d9,
"danger": #ff4136
"primary": $primary,
"danger": $danger
);
{{< /highlight >}}

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

### Functions

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:

{{< highlight scss >}}
@function color($key: "blue") {
@return map-get($colors, $key);
}

@function theme-color($key: "primary") {
@return map-get($theme-colors, $key);
}

@function gray($key: "100") {
@return map-get($grays, $key);
}
{{< /highlight >}}

These allow you to pick one color from a Sass map much like how you'd use a color variable from v3.
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.

{{< highlight scss >}}
.custom-element {
color: gray("100");
background-color: theme-color("dark");
color: $gray-100;
background-color: $dark;
}
{{< /highlight >}}

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.
We also have a function for getting a particular _level_ of color. Negative level values will lighten the color, while higher levels will darken.

{{< highlight scss >}}
@function theme-color-level($color-name: "primary", $level: 0) {
$color: theme-color($color-name);
@function color-level($color: $primary, $level: 0) {
$color-base: if($level > 0, #000, #fff);
$level: abs($level);

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

{{< highlight scss >}}
.custom-element {
color: theme-color-level(primary, -10);
color: color-level($primary, -10);
}
{{< /highlight >}}

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.

### Color contrast

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.
Expand All @@ -219,7 +207,7 @@ You can also specify a base color with our color map functions:

{{< highlight scss >}}
.custom-element {
color: color-yiq(theme-color("dark")); // returns `color: #fff`
color: color-yiq($dark); // returns `color: #fff`
}
{{< /highlight >}}

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

{{< highlight scss >}}
// With variable
.alpha { color: $purple; }

// From the Sass map with our `color()` function
.beta { color: color("purple"); }
{{< /highlight >}}

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

Expand Down
25 changes: 6 additions & 19 deletions site/content/docs/4.3/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Changes to our source Sass files and compiled CSS.
- **Todo:** Rearrange forms source files (under `scss/forms/`)
- **Todo:** Rearrange grid source files (under `scss/grid/`)
- Removed print styles and `$enable-print-styles` variable. Print display classes, however, have remained intact. [See #28339](https://github.com/twbs/bootstrap/pull/28339).
- Drop `color()`, `theme-color()` & `gray()` functions in favor of variables. [See #29083](https://github.com/twbs/bootstrap/pull/29083)
- 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)

## JavaScript

Expand All @@ -53,8 +55,7 @@ Changes to any layout tools and our grid system.
Changes to Reboot, typography, tables, and more.

- **Todo:** Make RFS enabled by default
- Reset default horizontal `padding-left` on `<ul>` and `<ol>` elements from browser default `40px` to `2rem`.
- Simplified table styles (no more 2px border on `thead > th` elements) and tightened cell padding.
- Reset default horizontal `padding` on `<ul>` and `<ol>` elements from browser default `40px` to `2rem`.

## Forms

Expand All @@ -64,17 +65,12 @@ Changes to Reboot, typography, tables, and more.
- Rearranged source Sass files under `scss/forms/`, including moving over input group styles.
- Combined native and custom checkboxes and radios into single `.form-check` class.
- New checks support sizing via `em`/`font-size` or explicit modifier classes now.
- New checks now appear larger by default for improved usability.
- Dropped `.custom-control` and associated classes.
- Renamed most `$custom-control` variables to `$form-control` ones.
- Combined native and custom selects into `.form-select`.
- Dropped `.custom-select` and associated classes.
- Renamed most `$custom-select` variables to `$form-select` ones.
- Updated file input component with same overall design, but improved HTML.
- Refactored `.form-file` markup to resolve some visual bugs while allowing translation and button text changes via HTML instead of CSS.
- Dropped native `.form-control-file` and `.form-control-range` components entirely.
- Renamed `.custom-file` to `.form-file` (including variables).
- Added support for `:focus` and `:disabled` styles.
- Dropped native `.form-control-file` and `.form-control-range` components.
- Renamed `.custom-file` to `.form-file` (including variables).
- Refactored `.form-file` markup to resolve some visual bugs while allowing translation and button text changes via HTML instead of CSS.
- Renamed `.custom-range` to `.form-range` (including variables).
- Dropped `.form-group` for margin utilities (we've replaced our docs examples with `.mb-3`).
- Dropped support for `.form-control-plaintext` inside `.input-group`s.
Expand All @@ -93,15 +89,6 @@ Badges were overhauled to better differentiate themselves from buttons and to be
- **Todo:** Removed `.badge-pill` for the `.rounded-pill` utility class
- **Todo:** Removed badge's hover and focus styles for `a.badge` and `button.badge`.

### Cards

- Removed the card columns in favor of a Masonry grid [See #28922](https://github.com/twbs/bootstrap/pull/28922).

### Icons (New!)

- Added new Bootstrap icons to the project for our documentation, form controls, and more.
- Removed Open Iconic icons from project source code for form controls.

### Jumbotron

- The jumbotron component is removed in favor of utility classes like `.bg-light` for the background color and `.p-*` classes to control padding.
Expand Down

0 comments on commit 2b1e2be

Please sign in to comment.