Skip to content

Commit

Permalink
Do not generate dropdown in sidebar. (pydata#1771)
Browse files Browse the repository at this point in the history
* Do not generate dropdown in sidebar.

This should fix pydata#1735, it is complementary to pydata#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 <gabriel@fouasnon.com>

* apply review

* Add failing test for dropdown sidebar

---------

Co-authored-by: gabalafou <gabriel@fouasnon.com>
  • Loading branch information
2 people authored and ivanov committed Jun 4, 2024
1 parent 7b91264 commit 10d5d5b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
{% if theme_navbar_center %}
<div class="sidebar-header-items__center">
{% for navbar_item in theme_navbar_center %}
<div class="navbar-item">{% include navbar_item %}</div>
{#
In the mobile sidebar we do not want a dropdown, so set a large cutoff (999).
#}
{% with theme_header_links_before_dropdown=999 %}
<div class="navbar-item">{% include navbar_item %}</div>
{% endwith %}
{% endfor %}
</div>
{% endif %}
Expand Down
22 changes: 18 additions & 4 deletions src/pydata_sphinx_theme/toctree.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from functools import cache
from itertools import count
from typing import Iterator, List, Union
from typing import Iterator, List, Tuple, Union
from urllib.parse import urlparse

import sphinx
Expand Down Expand Up @@ -101,8 +101,22 @@ def unique_html_id(base_id: str):
return next(get_or_create_id_generator(base_id))

@cache
def generate_header_nav_before_dropdown(n_links_before_dropdown):
"""The cacheable part."""
def _generate_header_nav_before_dropdown(
n_links_before_dropdown,
) -> Tuple[str, List[str]]:
"""Return html for navbar and dropdown.
.. warning::
Private helper function, do not call this directly.
Given the number of links before the dropdown, return the html for the navbar,
as well as the list of links to put in a dropdown.
Returns:
- HTML str for the navbar
- list of HTML str for the dropdown
"""
try:
n_links_before_dropdown = int(n_links_before_dropdown)
except Exception:
Expand Down Expand Up @@ -226,7 +240,7 @@ def generate_header_nav_html(
n_links_before_dropdown:The number of links to show before nesting the remaining links in a Dropdown element.
dropdown_text:Text of the dropdown element button.
"""
out, links_dropdown = generate_header_nav_before_dropdown(
out, links_dropdown = _generate_header_nav_before_dropdown(
n_links_before_dropdown
)

Expand Down
9 changes: 9 additions & 0 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ def test_navbar_header_dropdown(sphinx_build_factory, n_links) -> None:
}
sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides).build()
index_html = sphinx_build.html_tree("index.html")

# classic navbar:
navbar = index_html.select("ul.bd-navbar-elements")[0]
dropdowns = navbar.select("li.dropdown")
standalone_links = navbar.select(".navbar-nav > li.nav-item:not(.dropdown)")
Expand All @@ -362,6 +364,13 @@ def test_navbar_header_dropdown(sphinx_build_factory, n_links) -> None:
# There should be no dropdown and only standalone links
assert standalone_links and not dropdowns

# sidebar nav should never have dropdown
navbar = index_html.select("ul.bd-navbar-elements")[1]
dropdowns = navbar.select("li.dropdown")
standalone_links = navbar.select(".navbar-nav > li.nav-item:not(.dropdown)")
assert len(standalone_links) == 7
assert len(dropdowns) == 0


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

0 comments on commit 10d5d5b

Please sign in to comment.