Skip to content

Commit a31db4c

Browse files
Carreaugabalafou
andauthored
Do not generate dropdown in sidebar. (#1771)
* Do not generate dropdown in sidebar. This should fix #1735, it is complementary to #1564 but with a different approach. Instead of generating the same navbar as the fullpage width one, it generate one with no dropdowns, with this, any css that tries to make downtown looks like normal links in html becomes unnecessary. Co-authored-by: gabalafou <[email protected]> * apply review * Add failing test for dropdown sidebar --------- Co-authored-by: gabalafou <[email protected]>
1 parent cf0824a commit a31db4c

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

src/pydata_sphinx_theme/theme/pydata_sphinx_theme/sections/sidebar-primary.html

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
{% if theme_navbar_center %}
77
<div class="sidebar-header-items__center">
88
{% for navbar_item in theme_navbar_center %}
9-
<div class="navbar-item">{% include navbar_item %}</div>
9+
{#
10+
In the mobile sidebar we do not want a dropdown, so set a large cutoff (999).
11+
#}
12+
{% with theme_header_links_before_dropdown=999 %}
13+
<div class="navbar-item">{% include navbar_item %}</div>
14+
{% endwith %}
1015
{% endfor %}
1116
</div>
1217
{% endif %}

src/pydata_sphinx_theme/toctree.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from functools import cache
44
from itertools import count
5-
from typing import Iterator, List, Union
5+
from typing import Iterator, List, Tuple, Union
66
from urllib.parse import urlparse
77

88
import sphinx
@@ -101,8 +101,22 @@ def unique_html_id(base_id: str):
101101
return next(get_or_create_id_generator(base_id))
102102

103103
@cache
104-
def generate_header_nav_before_dropdown(n_links_before_dropdown):
105-
"""The cacheable part."""
104+
def _generate_header_nav_before_dropdown(
105+
n_links_before_dropdown,
106+
) -> Tuple[str, List[str]]:
107+
"""Return html for navbar and dropdown.
108+
109+
.. warning::
110+
111+
Private helper function, do not call this directly.
112+
113+
Given the number of links before the dropdown, return the html for the navbar,
114+
as well as the list of links to put in a dropdown.
115+
116+
Returns:
117+
- HTML str for the navbar
118+
- list of HTML str for the dropdown
119+
"""
106120
try:
107121
n_links_before_dropdown = int(n_links_before_dropdown)
108122
except Exception:
@@ -226,7 +240,7 @@ def generate_header_nav_html(
226240
n_links_before_dropdown:The number of links to show before nesting the remaining links in a Dropdown element.
227241
dropdown_text:Text of the dropdown element button.
228242
"""
229-
out, links_dropdown = generate_header_nav_before_dropdown(
243+
out, links_dropdown = _generate_header_nav_before_dropdown(
230244
n_links_before_dropdown
231245
)
232246

tests/test_build.py

+9
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,8 @@ def test_navbar_header_dropdown(sphinx_build_factory, n_links) -> None:
349349
}
350350
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build()
351351
index_html = sphinx_build.html_tree("index.html")
352+
353+
# classic navbar:
352354
navbar = index_html.select("ul.bd-navbar-elements")[0]
353355
dropdowns = navbar.select("li.dropdown")
354356
standalone_links = navbar.select(".navbar-nav > li.nav-item:not(.dropdown)")
@@ -362,6 +364,13 @@ def test_navbar_header_dropdown(sphinx_build_factory, n_links) -> None:
362364
# There should be no dropdown and only standalone links
363365
assert standalone_links and not dropdowns
364366

367+
# sidebar nav should never have dropdown
368+
navbar = index_html.select("ul.bd-navbar-elements")[1]
369+
dropdowns = navbar.select("li.dropdown")
370+
standalone_links = navbar.select(".navbar-nav > li.nav-item:not(.dropdown)")
371+
assert len(standalone_links) == 7
372+
assert len(dropdowns) == 0
373+
365374

366375
@pytest.mark.parametrize("dropdown_text", (None, "Other")) # None -> default "More"
367376
def test_navbar_header_dropdown_button(sphinx_build_factory, dropdown_text) -> None:

0 commit comments

Comments
 (0)