Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color mode: proposal to handle color mode for components #38917

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions scss/_accordion.scss
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,8 @@
}

@if $enable-dark-mode {
@include color-mode(dark) {
.accordion-button::after {
--#{$prefix}accordion-btn-icon: #{escape-svg($accordion-button-icon-dark)};
--#{$prefix}accordion-btn-active-icon: #{escape-svg($accordion-button-active-icon-dark)};
}
@include color-mode(dark, ".accordion-button::after") {
--#{$prefix}accordion-btn-icon: #{escape-svg($accordion-button-icon-dark)};
--#{$prefix}accordion-btn-active-icon: #{escape-svg($accordion-button-active-icon-dark)};
}
}
13 changes: 2 additions & 11 deletions scss/_carousel.scss
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,7 @@
}

@if $enable-dark-mode {
@include color-mode(dark) {
@if $color-mode-type == "media-query" {
.carousel {
@include carousel-dark();
}
} @else {
.carousel,
&.carousel {
@include carousel-dark();
}
}
@include color-mode(dark, ".carousel") {
@include carousel-dark();
}
}
6 changes: 2 additions & 4 deletions scss/_close.scss
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@
}

@if $enable-dark-mode {
@include color-mode(dark) {
.btn-close {
@include btn-close-white();
}
@include color-mode(dark, ".btn-close") {
@include btn-close-white();
}
}
17 changes: 11 additions & 6 deletions scss/_navbar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,7 @@
@include deprecate("`.navbar-light`", "v5.2.0", "v6.0.0", true);
}

.navbar-dark,
.navbar[data-bs-theme="dark"] {
@mixin navbar-dark() {
// scss-docs-start navbar-dark-css-vars
--#{$prefix}navbar-color: #{$navbar-dark-color};
--#{$prefix}navbar-hover-color: #{$navbar-dark-hover-color};
Expand All @@ -280,10 +279,16 @@
// scss-docs-end navbar-dark-css-vars
}

.navbar-dark {
@include navbar-dark();
}

@if $enable-dark-mode {
@include color-mode(dark) {
.navbar-toggler-icon {
--#{$prefix}navbar-toggler-icon-bg: #{escape-svg($navbar-dark-toggler-icon-bg)};
}
@include color-mode(dark, ".navbar") {
@include navbar-dark();
}

@include color-mode(dark, ".navbar-toggler-icon") {
--#{$prefix}navbar-toggler-icon-bg: #{escape-svg($navbar-dark-toggler-icon-bg)};
}
}
2 changes: 1 addition & 1 deletion scss/_root.scss
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
}

@if $enable-dark-mode {
@include color-mode(dark, true) {
@include color-mode(dark) {
color-scheme: dark;

// scss-docs-start root-dark-mode-vars
Expand Down
6 changes: 2 additions & 4 deletions scss/forms/_form-check.scss
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,7 @@
}

@if $enable-dark-mode {
@include color-mode(dark) {
.form-switch .form-check-input:not(:checked):not(:focus) {
--#{$prefix}form-switch-bg: #{escape-svg($form-switch-bg-image-dark)};
}
@include color-mode(dark, ".form-switch .form-check-input:not(:checked):not(:focus)") {
--#{$prefix}form-switch-bg: #{escape-svg($form-switch-bg-image-dark)};
}
}
6 changes: 2 additions & 4 deletions scss/forms/_form-select.scss
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@
}

@if $enable-dark-mode {
@include color-mode(dark) {
.form-select {
--#{$prefix}form-select-bg-img: #{escape-svg($form-select-indicator-dark)};
}
@include color-mode(dark, ".form-select") {
--#{$prefix}form-select-bg-img: #{escape-svg($form-select-indicator-dark)};
}
}
35 changes: 29 additions & 6 deletions scss/mixins/_color-mode.scss
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
// scss-docs-start color-mode-mixin
@mixin color-mode($mode: light, $root: false) {
@if $color-mode-type == "media-query" {
@if $root == true {
@mixin color-mode($mode: light, $selector: null) {
@if $color-mode-type == media-query {
@if $selector == null {
@media (prefers-color-scheme: $mode) {
:root {
@content;
}
}
} @else {
@media (prefers-color-scheme: $mode) {
@content;
#{$selector} {
@content;
}
}
}
} @else {
[data-bs-theme="#{$mode}"] {
@content;
@if $selector == null {
[data-bs-theme="#{$mode}"] {
@content;
}
} @else {
$tmp: str-replace($selector, " ", "[data-bs-theme=\"#{$mode}\"] ");
@if $tmp != $selector {
#{$tmp},
[data-bs-theme="#{$mode}"] #{$selector},
#{$selector}[data-bs-theme="#{$mode}"] {
@content;
}
} @else if str-index($selector, $substring: "::") != null {
[data-bs-theme="#{$mode}"] #{$selector},
#{str-replace($selector, "::", "[data-bs-theme=\"#{$mode}\"]::")} {
@content;
}
} @else {
[data-bs-theme="#{$mode}"] #{$selector},
#{$selector}[data-bs-theme="#{$mode}"] {
@content;
}
}
}
}
}
Expand Down
23 changes: 10 additions & 13 deletions scss/tests/mixins/_color-modes.test.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@
@include it("generates data attribute selectors for dark mode") {
@include assert() {
@include output() {
@include color-mode(dark) {
.element {
color: var(--bs-primary-text-emphasis);
background-color: var(--bs-primary-bg-subtle);
}
@include color-mode(dark, ".element") {
color: var(--bs-primary-text-emphasis);
background-color: var(--bs-primary-bg-subtle);
}
@include color-mode(dark, true) {
@include color-mode(dark) {
--custom-color: #{mix($indigo, $blue, 50%)};
}
}
@include expect() {
[data-bs-theme=dark] .element {
[data-bs-theme=dark] .element,
.element[data-bs-theme=dark] {
color: var(--bs-primary-text-emphasis);
background-color: var(--bs-primary-bg-subtle);
}
Expand All @@ -39,13 +38,11 @@

@include assert() {
@include output() {
@include color-mode(dark) {
.element {
color: var(--bs-primary-text-emphasis);
background-color: var(--bs-primary-bg-subtle);
}
@include color-mode(dark, ".element") {
color: var(--bs-primary-text-emphasis);
background-color: var(--bs-primary-bg-subtle);
}
@include color-mode(dark, true) {
@include color-mode(dark) {
--custom-color: #{mix($indigo, $blue, 50%)};
}
}
Expand Down
6 changes: 2 additions & 4 deletions site/assets/scss/_navbar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@
}
}

@include color-mode(dark) {
.bd-navbar {
box-shadow: 0 .5rem 1rem rgba($black, .15), inset 0 -1px 0 rgba($white, .15);
}
@include color-mode(dark, ".bd-navbar") {
box-shadow: 0 .5rem 1rem rgba($black, .15), inset 0 -1px 0 rgba($white, .15);
}
2 changes: 1 addition & 1 deletion site/assets/scss/_search.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
--docsearch-logo-color: var(--bd-violet);
}

@include color-mode(dark, true) {
@include color-mode(dark) {
// From here, the values are copied from https://cdn.jsdelivr.net/npm/@docsearch/css@3
// in html[data-theme="dark"] selector
// and are slightly modified for formatting purpose
Expand Down
2 changes: 1 addition & 1 deletion site/assets/scss/_syntax.scss
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
--base0F: #333;
}

@include color-mode(dark, true) {
@include color-mode(dark) {
// --base00: #282c34;
// --base01: #353b45;
--base02: #3e4451;
Expand Down
2 changes: 1 addition & 1 deletion site/assets/scss/_variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ $bd-callout-variants: info, warning, danger !default;
--bd-pre-bg: var(--bs-tertiary-bg);
}

@include color-mode(dark, true) {
@include color-mode(dark) {
--bd-violet: #{mix($bd-violet, $white, 75%)};
--bd-violet-bg: #{$bd-violet};
--bd-toc-color: var(--#{$prefix}emphasis-color);
Expand Down