Skip to content

Commit adf6bbb

Browse files
authored
fix(navbar_options): Apply attributes and theme (#1942)
1 parent 0973b52 commit adf6bbb

File tree

5 files changed

+52
-5
lines changed

5 files changed

+52
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020

2121
* The `.get_latest_stream_result()` method on `ui.MarkdownStream()` was deprecated in favor of the new `.latest_stream` property. Call `.result()` on the property to get the latest result, `.status` to check the status, and `.cancel()` to cancel the stream.
2222

23+
* `ui.page_navbar()` and `ui.navset_bar()` now correctly apply `theme` and additional attributes from `navbar_options` created with `ui.navbar_options()`. (#1942)
24+
2325
### Bug fixes
2426

2527
* Fixed an issue where the `<main>` areas of `ui.page_sidebar()` and `ui.page_navbar()` (with a `sidebar`) were made to be a fillable containers even when `fillable=False`. (#1816)
@@ -62,12 +64,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6264

6365
### Breaking changes
6466

65-
* The navbar-related style options of `ui.page_navbar()` and `ui.navset_bar()` have been consolidated into a single `navbar_options` argument that pairs with a new `ui.navbar_options()` helper. Using the direct `position`, `bg`, `inverse`, `collapsible`, and `underline` arguments will continue to work with a deprecation message.
67+
* The navbar-related style options of `ui.page_navbar()` and `ui.navset_bar()` have been consolidated into a single `navbar_options` argument that pairs with a new `ui.navbar_options()` helper. Using the direct `position`, `bg`, `inverse`, `collapsible`, and `underline` arguments will continue to work with a deprecation message. (#1822)
6668

6769
Related to this change, `ui.navset_bar()` now defaults to using `underline=True` so that it uses the same set of default `ui.navbar_options()` as the page variant. In `ui.navbar_options()`, `inverse` is replaced by `theme`, which takes values `"light"` (dark text on a **light** background), `"dark"` (light text on a **dark** background), or `"auto"` (follow page settings).
6870

69-
* The Shiny Core component `shiny.ui.Chat()` no longer has a `.ui()` method. This method
70-
was never intended to be used in Shiny Core (in that case, use `shiny.ui.chat_ui()`) to create the UI element. Note that the `shiny.express.ui.Chat()` class still has a `.ui()` method. (#1840)
71+
* The Shiny Core component `shiny.ui.Chat()` no longer has a `.ui()` method. This method was never intended to be used in Shiny Core (in that case, use `shiny.ui.chat_ui()`) to create the UI element. Note that the `shiny.express.ui.Chat()` class still has a `.ui()` method. (#1840)
7172

7273
### Bug fixes
7374

shiny/ui/_navs.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1320,7 +1320,12 @@ def layout(self, nav: Tag, content: Tag) -> TagList:
13201320
nav = div(nav, id=collapse_id, class_="collapse navbar-collapse")
13211321

13221322
nav_container.append(nav)
1323-
nav_final = tags.nav({"class": "navbar navbar-expand-md"}, nav_container)
1323+
nav_final = tags.nav(
1324+
{"class": "navbar navbar-expand-md"},
1325+
nav_container,
1326+
{"data-bs-theme": self.navbar_options.theme},
1327+
**self.navbar_options.attrs,
1328+
)
13241329

13251330
if self.navbar_options.position != "static-top":
13261331
nav_final.add_class(self.navbar_options.position)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from shiny import App, Inputs, Outputs, Session, ui
2+
3+
app_ui = ui.page_navbar(
4+
ui.nav_panel(
5+
"Page 1",
6+
ui.navset_bar(
7+
ui.nav_panel("Inner 1", "Inner content"),
8+
title="Inner navbar",
9+
id="inner_navset_bar",
10+
navbar_options=ui.navbar_options(class_="bg-light", theme="light"),
11+
),
12+
),
13+
title="Title",
14+
id="page_navbar",
15+
navbar_options=ui.navbar_options(class_="bg-primary", theme="dark"),
16+
)
17+
18+
19+
def server(input: Inputs, output: Outputs, session: Session):
20+
pass
21+
22+
23+
app = App(app_ui, server)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import re
2+
3+
from playwright.sync_api import Page, expect
4+
5+
from shiny.playwright import controller
6+
from shiny.run import ShinyAppProc
7+
8+
9+
def test_navset_bar_options(page: Page, local_app: ShinyAppProc) -> None:
10+
page.goto(local_app.url)
11+
12+
page_navbar = controller.PageNavbar(page, "page_navbar")
13+
expect(page_navbar._loc_navbar).to_have_class(re.compile(r"(^|\s)bg-primary(\s|$)"))
14+
expect(page_navbar._loc_navbar).to_have_attribute("data-bs-theme", "dark")
15+
16+
inner_navbar = controller.NavsetBar(page, "inner_navset_bar")
17+
expect(inner_navbar._loc_navbar).to_have_class(re.compile(r"(^|\s)bg-light(\s|$)"))
18+
expect(inner_navbar._loc_navbar).to_have_attribute("data-bs-theme", "light")

tests/pytest/test_navs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def test_navset_bar_markup():
157157

158158
assert TagList(x).render()["html"] == textwrap.dedent(
159159
"""\
160-
<nav class="navbar navbar-expand-md navbar-default">
160+
<nav class="navbar navbar-expand-md navbar-default" data-bs-theme="auto">
161161
<div class="container-fluid">
162162
<span class="navbar-brand">Page title</span><button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbar-collapse-795772" aria-controls="navbar-collapse-795772" aria-expanded="false" aria-label="Toggle navigation"><span class="navbar-toggler-icon"></span></button>
163163
<div id="navbar-collapse-795772" class="collapse navbar-collapse">

0 commit comments

Comments
 (0)