Skip to content

Commit 9409824

Browse files
committed
navbar align is a string and right is now an option
1 parent 8a64c55 commit 9409824

File tree

6 files changed

+53
-12
lines changed

6 files changed

+53
-12
lines changed

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"twitter_url": "https://twitter.com/pandas_dev",
7171
"use_edit_page_button": True,
7272
"show_toc_level": 1,
73-
# "navbar_align_with_content": False, # For testing that the navbar items snap to left properly
73+
# "navbar_align": "right", # For testing that the navbar items align properly
7474
}
7575

7676
html_context = {

docs/user_guide/configuring.rst

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,17 +216,37 @@ use this pattern:
216216
For information about configuring the sidebar's contents, see :ref:`configure-sidebar`.
217217

218218

219-
Make navbar menu items snap to the left
220-
=======================================
219+
Configure navbar menu item alignment
220+
====================================
221221

222222
By default, the navigation bar menu items will align with the content on your
223-
page. If instead you'd like these items to snap to the left (closer to the logo), then
224-
use the following configuration:
223+
page. This equals the following default configuration:
224+
225+
.. code-block:: python
226+
227+
html_theme_options = {
228+
...
229+
"navbar_align_with_content": "content"
230+
...
231+
}
232+
233+
If instead you'd like these items to snap to the left (closer to the logo), use this
234+
configuration:
235+
236+
.. code-block:: python
237+
238+
html_theme_options = {
239+
...
240+
"navbar_align_with_content": "left"
241+
...
242+
}
243+
244+
If you'd like these items to snap to the right of the page, use this configuration:
225245

226246
.. code-block:: python
227247
228248
html_theme_options = {
229249
...
230-
"navbar_align_with_content": False
250+
"navbar_align_with_content": "right"
231251
...
232252
}

pydata_sphinx_theme/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,26 @@ def get_page_toc_object():
7575
except Exception:
7676
return {}
7777

78+
def navbar_align_class():
79+
"""Return the class that aligns the navbar based on config."""
80+
align = context.get("theme_navbar_align", "content")
81+
align_options = {
82+
"content": ("col-lg-9", "mr-auto"),
83+
"left": ("", "mr-auto"),
84+
"right": ("", "ml-auto"),
85+
}
86+
if align not in align_options:
87+
raise ValueError(
88+
(
89+
"Theme optione navbar_align must be one of"
90+
f"{align_options.keys()}, got: {align}"
91+
)
92+
)
93+
return align_options[align]
94+
7895
context["get_nav_object"] = get_nav_object
7996
context["get_page_toc_object"] = get_page_toc_object
97+
context["navbar_align_class"] = navbar_align_class
8098

8199

82100
def docutils_node_to_jinja(list_item, only_pages=False):

pydata_sphinx_theme/docs-navbar.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbar-menu" aria-controls="navbar-menu" aria-expanded="false" aria-label="Toggle navigation">
1212
<span class="navbar-toggler-icon"></span>
1313
</button>
14-
<div id="navbar-menu" class="{% if theme_navbar_align_with_content in [True, 'True'] %}col-lg-9 {% endif %}collapse navbar-collapse">
15-
<ul id="navbar-main-elements" class="navbar-nav mr-auto">
14+
{% set navbar_class, navbar_align = navbar_align_class() %}
15+
<div id="navbar-menu" class="{{ navbar_class }} collapse navbar-collapse">
16+
<ul id="navbar-main-elements" class="navbar-nav {{ navbar_align }}">
1617
{% set nav = get_nav_object(maxdepth=1, collapse=True) %}
1718
{% for main_nav_item in nav %}
1819
<li class="nav-item {% if main_nav_item.active%}active{% endif %}">

pydata_sphinx_theme/theme.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ search_bar_text = Search the docs ...
1616
search_bar_position = sidebar
1717
navigation_with_keys = True
1818
show_toc_level = 1
19-
navbar_align_with_content = True
19+
navbar_align = content

tests/test_build.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,17 @@ def test_sidebar_visible(sphinx_build):
113113
sphinx_build.clean()
114114

115115

116-
def test_navbar_align_with_content(sphinx_build):
117-
"""The sidebar is shrunk when no sidebars specified in html_sidebars."""
116+
def test_navbar_align(sphinx_build):
117+
"""The navbar items align with the proper part of the page."""
118118
sphinx_build.copy()
119119

120120
sphinx_build.build()
121121
index_html = sphinx_build.get("index.html")
122122
assert "col-lg-9" in index_html.select("div#navbar-menu")[0].attrs["class"]
123123

124-
sphinx_build.build(["-D", "html_theme_options.navbar_align_with_content=False"])
124+
# Both the column alignment and the margin should be changed
125+
sphinx_build.build(["-D", "html_theme_options.navbar_align=right"])
125126
index_html = sphinx_build.get("index.html")
126127
assert "col-lg-9" not in index_html.select("div#navbar-menu")[0].attrs["class"]
128+
assert "ml-auto" in index_html.select("ul#navbar-main-elements")[0].attrs["class"]
127129
sphinx_build.clean()

0 commit comments

Comments
 (0)