Skip to content

Commit 9bb7785

Browse files
AnnMarieWtcbegley
andauthored
Color mode (#984)
* css and className changes for color mode * docs color mode theme switch * lint * Added more info on themes * Update docs/components_page/components/form.md Co-authored-by: Tom Begley <tomcbegley@gmail.com> * fixed stack style * align theme toggle to right & format as btn * lint * fixed stack examples * make examples pages always a light theme --------- Co-authored-by: Tom Begley <tomcbegley@gmail.com>
1 parent 5d4cd53 commit 9bb7785

File tree

13 files changed

+139
-50
lines changed

13 files changed

+139
-50
lines changed

docs/components_page/components/form.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ The `FormFeedback` component should be added to a `html.Div` containing the `Inp
4949

5050
`Form` and other components can also be used with _dash-core-components_.
5151

52+
Note that Bootstrap themes are not automatically applied to components such as _dash-core-components_, _dash-ag-grid_, or _DataTable_. For more information on styling other Dash components with a Bootstrap theme see the [Dash Bootstrap Theme Explorer](https://hellodash.pythonanywhere.com/) site.
53+
5254
{{example:components/form/dash_core.py:form}}
5355

5456
{{apidoc:src/components/form/Form.js}}

docs/components_page/components/jumbotron/custom.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
),
1313
dbc.Button("Example Button", color="light", outline=True),
1414
],
15-
className="h-100 p-5 text-white bg-dark rounded-3",
15+
className="h-100 p-5 text-white bg-primary rounded-3",
1616
),
1717
md=6,
1818
)
@@ -28,7 +28,7 @@
2828
),
2929
dbc.Button("Example Button", color="secondary", outline=True),
3030
],
31-
className="h-100 p-5 bg-light border rounded-3",
31+
className="h-100 p-5 bg-light text-dark border rounded-3",
3232
),
3333
md=6,
3434
)

docs/components_page/components/jumbotron/simple.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@
2222
fluid=True,
2323
className="py-3",
2424
),
25-
className="p-3 bg-light rounded-3",
25+
className="p-3 bg-body-secondary rounded-3",
2626
)

docs/components_page/components/layout/horizontal_stack.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
[
66
dbc.Stack(
77
[
8-
html.Div("Horizontal", className="bg-light border"),
9-
html.Div("Stack", className="bg-light border"),
10-
html.Div("Without", className="bg-light border"),
11-
html.Div("Gaps", className="bg-light border"),
8+
html.Div("Horizontal"),
9+
html.Div("Stack"),
10+
html.Div("Without"),
11+
html.Div("Gaps"),
1212
],
1313
direction="horizontal",
1414
),
1515
html.Hr(),
1616
dbc.Stack(
1717
[
18-
html.Div("Horizontal", className="bg-light border"),
19-
html.Div("Stack", className="bg-light border"),
20-
html.Div("With", className="bg-light border"),
21-
html.Div("Gaps", className="bg-light border"),
18+
html.Div("Horizontal"),
19+
html.Div("Stack"),
20+
html.Div("With"),
21+
html.Div("Gaps"),
2222
],
2323
direction="horizontal",
2424
gap=3,

docs/components_page/components/layout/simple_stack.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,17 @@
55
[
66
dbc.Stack(
77
[
8-
html.Div(
9-
"This stack has no gaps", className="bg-light border"
10-
),
11-
html.Div("Next item", className="bg-light border"),
12-
html.Div("Last item", className="bg-light border"),
8+
html.Div("This stack has no gaps"),
9+
html.Div("Next item"),
10+
html.Div("Last item"),
1311
]
1412
),
1513
html.Hr(),
1614
dbc.Stack(
1715
[
18-
html.Div("This stack has gaps", className="bg-light border"),
19-
html.Div("Next item", className="bg-light border"),
20-
html.Div("Last item", className="bg-light border"),
16+
html.Div("This stack has gaps"),
17+
html.Div("Next item"),
18+
html.Div("Last item"),
2119
],
2220
gap=3,
2321
),

docs/components_page/components/layout/stack_spacers.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
import dash_bootstrap_components as dbc
22
from dash import html
33

4+
demo_div = "bg-primary-subtle border border-primary-subtle p-2"
5+
46
stack = html.Div(
57
[
68
dbc.Stack(
79
[
8-
html.Div("Start", className="bg-light border"),
10+
html.Div("Start"),
911
html.Div(
10-
"Middle (ms-auto)", className="ms-auto bg-light border"
12+
"Middle (ms-auto)",
13+
className="ms-auto",
1114
),
12-
html.Div("End", className="bg-light border"),
15+
html.Div("End"),
1316
],
1417
direction="horizontal",
1518
gap=3,
1619
),
1720
html.Hr(),
1821
dbc.Stack(
1922
[
20-
html.Div("Start", className="bg-light border"),
21-
html.Div(
22-
"Middle (mx-auto)", className="mx-auto bg-light border"
23-
),
24-
html.Div("End", className="bg-light border"),
23+
html.Div("Start"),
24+
html.Div("Middle (mx-auto)", className="mx-auto"),
25+
html.Div("End"),
2526
],
2627
direction="horizontal",
2728
gap=3,

docs/content/docs/themes.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,45 @@ To start with, we recommend experimenting with some of the Bootswatch themes ava
5959

6060
Check out the [theme explorer](/docs/themes/explorer/) for a live demo of dash-bootstrap-components using all of the different available themes. You may also like to check out the [dash-bootstrap-css](https://github.com/tcbegley/dash-bootstrap-css) project which contains Bootstrap stylesheets that apply consistent styling to various components from dash-core-components.
6161

62+
## Light and Dark Color Modes
63+
64+
_Available in dash-bootstrap-components>=1.5.0_
65+
66+
To toggle between a light and dark mode in your app, create a component to switch the theme. For example, add a component like this to the layout:
67+
68+
```python
69+
app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP, dbc.icons.FONT_AWESOME])
70+
71+
color_mode_switch = html.Span(
72+
[
73+
dbc.Label(className="fa fa-moon", html_for="switch"),
74+
dbc.Switch( id="switch", value=True, className="d-inline-block ms-1", persistence=True),
75+
dbc.Label(className="fa fa-sun", html_for="switch"),
76+
]
77+
)
78+
```
79+
80+
Here's a callback to change the theme:
81+
82+
```python
83+
clientside_callback(
84+
" " "
85+
(switchOn) => {
86+
switchOn
87+
? document.documentElement.setAttribute('data-bs-theme', 'light')
88+
: document.documentElement.setAttribute('data-bs-theme', 'dark')
89+
return window.dash_clientside.no_update
90+
}
91+
" " ",
92+
Output("switch", "id"),
93+
Input("switch", "value"),
94+
)
95+
```
96+
97+
See this example live in the [Dash Bootstrap Theme Explorer](https://hellodash.pythonanywhere.com/adding-themes/color-modes)
98+
See more information in the [Bootstrap Docs](https://getbootstrap.com/docs/5.3/customize/color-modes/)
99+
100+
62101
[dash-docs-external]: https://dash.plotly.com/external-resources/
63102
[bootstrap]:https://getbootstrap.com/
64103
[bootstrap-download]: https://getbootstrap.com/docs/5.0/getting-started/download/

docs/static/docs.css

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ span.hljs-meta {
3131
color: #66d9ef;
3232
}
3333

34+
.highlight {
35+
border: var(--bs-border-color) var(--bs-border-style) var(--bs-border-width);
36+
border-radius: 6px;
37+
}
38+
3439
.demo-layout-container {
3540
height: 600px;
3641
margin-right: 0rem;
@@ -60,18 +65,17 @@ span.hljs-meta {
6065
}
6166

6267
.docs-sidebar .nav-link {
63-
color: #888;
68+
color: var(--bs-body-color)
6469
padding: 0.3rem 0.5rem 0.3rem 1rem;
6570
border-left: 2px solid transparent;
6671
}
6772

6873
.docs-sidebar .nav-link:hover {
69-
color: #777;
7074
border-left: 2px solid #aaa;
7175
}
7276

7377
.docs-sidebar .nav-link.active {
74-
color: #666;
78+
color: var(--bs-emphasis-color);
7579
border-left: 2px solid #d9534f;
7680
}
7781

@@ -80,7 +84,7 @@ span.hljs-meta {
8084
}
8185

8286
.example-container {
83-
border: 1px solid #d8d8d8;
87+
border: 1px solid var(--bs-border-color);
8488
margin: 2rem 0 0;
8589
border-radius: 6px;
8690
}
@@ -96,6 +100,7 @@ span.hljs-meta {
96100

97101
.example-source-container pre {
98102
margin-bottom: 0;
103+
border: 1px solid var(--bs-border-color);
99104
}
100105

101106
.source-container pre.hljs {
@@ -108,14 +113,7 @@ span.hljs-meta {
108113

109114
.layout-demo .row {
110115
margin-bottom: 10px;
111-
background: linear-gradient(
112-
to right,
113-
rgba(0, 0, 0, 0),
114-
rgba(0, 0, 0, 0) calc(100% - 0.75rem),
115-
#fff calc(100% - 0.75rem)
116-
),
117-
linear-gradient(to right, #fff, #fff 0.75rem, rgba(0, 0, 0, 0) 0.75rem),
118-
linear-gradient(to right, #eeeeee, #eeeeee);
116+
background: var(--bs-secondary-bg-subtle)
119117
}
120118

121119
.layout-demo .row.g-0 {
@@ -127,10 +125,12 @@ span.hljs-meta {
127125
}
128126

129127
.layout-demo .col > div,
130-
.layout-demo [class*='col-'] > div {
128+
.layout-demo [class*='col-'] > div,
129+
.layout-demo .vstack > div,
130+
.layout-demo .hstack > div {
131131
padding: 0.75rem;
132-
background-color: rgba(86, 61, 124, 0.15);
133-
border: 1px solid rgba(86, 61, 124, 0.2);
132+
background-color: var(--bs-primary-bg-subtle);
133+
border: 1px solid var(--bs-primary-border-subtle);
134134
}
135135

136136
._dash-undo-redo {
@@ -218,17 +218,18 @@ span.hljs-meta {
218218
padding: 0.25rem 0rem;
219219
font-weight: 500;
220220
font-size: 1.25rem;
221-
color: rgba(0, 0, 0, 0.65);
221+
color: var(--bs-body-color);
222222
text-decoration: none;
223223
}
224224

225225
.dbcd-nav-link:hover {
226-
color: rgba(0, 0, 0, 0.75);
227226
text-decoration: none;
227+
font-weight: bold;
228228
}
229229

230230
.dbcd-nav-item.active > .dbcd-nav-link {
231-
color: rgba(0, 0, 0, 0.85);
231+
color: var(--bs-emphasis-color);
232+
font-weight: bold;
232233
}
233234

234235
.dbcd-nav-links ul > li {
@@ -237,16 +238,17 @@ span.hljs-meta {
237238

238239
.dbcd-nav-links ul > li > a {
239240
padding: 0.25rem 0rem;
240-
color: rgba(0, 0, 0, 0.65);
241+
color: var(--bs-body-color);
241242
text-decoration: none;
242243
}
243244

244245
.dbcd-nav-links ul > li.active > a {
245-
color: rgba(0, 0, 0, 0.85);
246+
color: var(--bs-emphasis-color);
247+
font-weight: bold;
246248
}
247249

248250
.dbcd-nav-links ul > li > a:hover {
249-
color: rgba(0, 0, 0, 0.75);
251+
font-weight: bold;
250252
text-decoration: none;
251253
}
252254

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const getStoredTheme = () => localStorage.getItem('theme')
2+
const lightIcon = "bi bi-sun-fill"
3+
const darkIcon = "bi bi-moon-fill"
4+
5+
6+
const setIcon = theme => {
7+
icon = theme == "light" ? lightIcon : darkIcon
8+
document.getElementById('theme-icon').className = icon
9+
}
10+
11+
const handleThemeChange = () => {
12+
getStoredTheme() == 'dark' ? setTheme("light") : setTheme("dark")
13+
}
14+
15+
const setTheme = theme => {
16+
document.documentElement.setAttribute('data-bs-theme', theme)
17+
localStorage.setItem('theme', theme)
18+
setIcon(theme)
19+
}
20+
21+
// icon needs to be set after page is loaded
22+
const setInitialIcon = () => {
23+
getStoredTheme() == 'dark' ? setIcon("dark") : setIcon("light")
24+
}
25+
window.onload = (event) => {
26+
// example pages should always be light theme
27+
if (window.location.pathname.includes("/examples") ) {
28+
return document.documentElement.setAttribute('data-bs-theme', "light")
29+
}
30+
setInitialIcon()
31+
32+
};
33+
34+
35+

docs/templates/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ <h2>Get started quickly</h2>
5555
<a href="/docs/quickstart" class="btn btn-secondary">Quickstart »</a>
5656
</div>
5757
<div class="col-12 col-lg-6 mt-4 mt-lg-0">
58-
<pre><code class="language-sh">pip install dash-bootstrap-components</code></pre>
59-
<pre><code class="language-python">import dash
58+
<pre><code class="language-sh border">pip install dash-bootstrap-components</code></pre>
59+
<pre><code class="language-python border">import dash
6060
import dash_bootstrap_components as dbc
6161

6262
app = dash.Dash(

0 commit comments

Comments
 (0)