Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not generate dropdown in sidebar. #1771

Merged
merged 3 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 %}
Carreau marked this conversation as resolved.
Show resolved Hide resolved
<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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be a simple refactor that might help make this function easier to understand if it returns

  • list of HTML str for navbar
  • list of HTML str for the dropdown

Copy link
Collaborator Author

@Carreau Carreau Apr 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think better than that, the cached function should not return html but the data necessary to build the html. But that require some refactor.

I'll make an issue to suggest doing a refactor for later.

Edit: for completeness this was done as issue #1773

- list of HTML str for the dropdown
"""
drammock marked this conversation as resolved.
Show resolved Hide resolved
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
Loading