Skip to content

Commit 1bb47f6

Browse files
gadenbuiecpsievert
andauthored
docs(navbar_options): Describe how to set navbar styles to match Bootswatch themes (#1881)
Co-authored-by: Carson Sievert <[email protected]>
1 parent ee998f9 commit 1bb47f6

File tree

1 file changed

+72
-1
lines changed

1 file changed

+72
-1
lines changed

shiny/ui/_navs.py

+72-1
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,77 @@ def navbar_options(
10751075
"""
10761076
Configure the appearance and behavior of the navbar.
10771077
1078+
## Navbar style with Bootstrap 5 and Bootswatch themes
1079+
1080+
In Shiny v1.3.0, the default navbar colors for Bootswatch themes are less
1081+
opinionated by default and follow light or dark mode (see
1082+
:func:`~shiny.ui.input_dark_mode`).
1083+
1084+
You can use `ui.navbar_options()` to adjust the colors of the navbar when using a
1085+
Bootswatch preset theme with Bootstrap 5. For example, the [Bootswatch documentation
1086+
for the Flatly theme](https://bootswatch.com/flatly/) shows 4 navbar variations.
1087+
Inspecting the source code for the first example reveals the following markup:
1088+
1089+
```html
1090+
<nav class="navbar navbar-expand-lg bg-primary" data-bs-theme="dark">
1091+
<!-- all of the navbar html -->
1092+
</nav>
1093+
```
1094+
1095+
Note that this navbar uses the `bg-primary` class for a dark navy background. The
1096+
navbar's white text is controlled by the `data-bs-theme="dark"` attribute, which is
1097+
used by Bootstrap for light text on a _dark_ background. In Shiny, you can achieve
1098+
this look with:
1099+
1100+
```python
1101+
ui.page_navbar(
1102+
theme=ui.Theme(version=5, preset="flatly"),
1103+
navbar_options=ui.navbar_options(class="bg-primary", theme="dark")
1104+
)
1105+
```
1106+
1107+
This particular combination of `class="bg-primary"` and `theme="dark"` works well
1108+
for most Bootswatch presets. Note that in Shiny Express, `theme` and
1109+
`navbar_options` both are set using :func:`~shiny.express.ui.page_opts`.
1110+
1111+
Another variation from the Flatly documentation features a navbar with dark text on a
1112+
light background:
1113+
1114+
```python
1115+
ui.page_navbar(
1116+
theme = ui.Theme(version=5, preset="flatly"),
1117+
navbar_options = ui.navbar_options(class="bg-light", theme="light")
1118+
)
1119+
```
1120+
1121+
The above options set navbar foreground and background colors that are always the
1122+
same in both light and dark modes. To customize the navbar colors used in light or
1123+
dark mode, you can use the `$navbar-light-bg` and `$navbar-dark-bg` Sass variables.
1124+
When provided, Shiny will automatically choose to use light or dark text as the
1125+
foreground color.
1126+
1127+
```python
1128+
ui.page_navbar(
1129+
theme=(
1130+
ui.Theme(version=5, preset = "flatly")
1131+
.add_defaults(
1132+
navbar_light_bg="#18BC9C", # flatly's success color (teal)
1133+
navbar_dark_bg="#2C3E50" # flatly's primary color (navy)
1134+
)
1135+
)
1136+
)
1137+
)
1138+
```
1139+
1140+
Finally, you can also use the `$navbar-bg` Sass variable to set the navbar
1141+
background color for both light and dark modes:
1142+
1143+
```python
1144+
ui.page_navbar(
1145+
theme=ui.Theme(version=5, preset="flatly").add_defaults(navbar_bg="#E74C3C") # flatly's red
1146+
)
1147+
```
1148+
10781149
Parameters
10791150
-----------
10801151
position
@@ -1095,7 +1166,7 @@ def navbar_options(
10951166
collapsible
10961167
If `True`, automatically collapses the elements into an expandable menu on
10971168
mobile devices or narrow window widths.
1098-
**attrs : dict
1169+
attrs
10991170
Additional HTML attributes to apply to the navbar container element.
11001171
11011172
Returns:

0 commit comments

Comments
 (0)