Skip to content

Commit edc2a0b

Browse files
authored
✨ NEW: hide sidebar + navbar_align option (#263)
1 parent 04898f3 commit edc2a0b

File tree

11 files changed

+148
-13
lines changed

11 files changed

+148
-13
lines changed

docs/conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151

5252
html_sidebars = {
5353
"contributing": ["sidebar-search-bs.html", "custom-template.html"],
54+
"changelog": [],
5455
}
5556

5657
# -- Options for HTML output -------------------------------------------------
@@ -69,6 +70,7 @@
6970
"twitter_url": "https://twitter.com/pandas_dev",
7071
"use_edit_page_button": True,
7172
"show_toc_level": 1,
73+
# "navbar_align": "right", # For testing that the navbar items align properly
7274
}
7375

7476
html_context = {

docs/user_guide/configuring.rst

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ an external site. You can add external links to the nav bar like so:
4848
]
4949
}
5050
51+
.. _configure-sidebar:
52+
5153
Configure the sidebar
5254
=====================
5355

@@ -180,3 +182,71 @@ You can show deeper levels by default by using the following configuration, indi
180182
181183
All headings up to and including the level specified will now be shown
182184
regardless of what is displayed on the page.
185+
186+
187+
Remove the sidebar from some pages
188+
==================================
189+
190+
If you'd like the left sidebar to be removed from a page, you can use the
191+
following configuration in ``conf.py``:
192+
193+
.. code-block:: python
194+
195+
html_sidebars = {
196+
"pagename": []
197+
}
198+
199+
This works for glob-style patterns as well. For example:
200+
201+
.. code-block:: python
202+
203+
html_sidebars = {
204+
"folder/*": []
205+
}
206+
207+
If you'd like to remove the left sidebar from **all** pages of your documentation,
208+
use this pattern:
209+
210+
.. code-block:: python
211+
212+
html_sidebars = {
213+
"**": []
214+
}
215+
216+
For information about configuring the sidebar's contents, see :ref:`configure-sidebar`.
217+
218+
219+
Configure navbar menu item alignment
220+
====================================
221+
222+
By default, the navigation bar menu items will align with the content on your
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:
245+
246+
.. code-block:: python
247+
248+
html_theme_options = {
249+
...
250+
"navbar_align_with_content": "right"
251+
...
252+
}

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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +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-
15-
<div id="navbar-menu" class="col-lg-9 collapse navbar-collapse">
16-
<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 }}">
1717
{% set nav = get_nav_object(maxdepth=1, collapse=True) %}
1818
{% for main_nav_item in nav %}
1919
<li class="nav-item {% if main_nav_item.active%}active{% endif %}">

pydata_sphinx_theme/layout.html

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,16 @@
4545
<div class="container-xl">
4646
<div class="row">
4747
{% block docs_sidebar %}
48-
<div class="col-12 col-md-3 bd-sidebar">
49-
{%- for sidebartemplate in sidebars %}
50-
{%- include sidebartemplate %}
51-
{%- endfor %}
52-
</div>
48+
{% if sidebars %}
49+
<!-- Only show if we have sidebars configured, else just a small margin -->
50+
<div class="col-12 col-md-3 bd-sidebar">
51+
{%- for sidebartemplate in sidebars %}
52+
{%- include sidebartemplate %}
53+
{%- endfor %}
54+
</div>
55+
{% else %}
56+
<div class="col-12 col-md-1 col-xl-2 bd-sidebar no-sidebar"></div>
57+
{% endif %}
5358
{% endblock %}
5459

5560
{% block docs_toc %}
@@ -61,7 +66,12 @@
6166
{% endblock %}
6267

6368
{% block docs_main %}
64-
<main class="col-12 col-md-9 col-xl-7 py-md-5 pl-md-5 pr-md-4 bd-content" role="main">
69+
{% if sidebars %}
70+
{% set content_col_class = "col-md-9 col-xl-7" %}
71+
{% else %}
72+
{% set content_col_class = "col-md-11 col-xl-8" %}
73+
{% endif %}
74+
<main class="col-12 {{ content_col_class }} py-md-5 pl-md-5 pr-md-4 bd-content" role="main">
6575
{% block docs_body %}
6676
<div>
6777
{% block body %} {% endblock %}

pydata_sphinx_theme/static/css/index.8bae7b2b42fe3a8dc942de8a456fc9f2.css renamed to pydata_sphinx_theme/static/css/index.c78d4f2b1f8277c2fa0830b4506d5cfe.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pydata_sphinx_theme/static/webpack-macros.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717

1818
{% macro head_pre_bootstrap() %}
1919
<link href="{{ pathto('_static/css/theme.css', 1) }}" rel="stylesheet" />
20-
<link href="{{ pathto('_static/css/index.8bae7b2b42fe3a8dc942de8a456fc9f2.css', 1) }}" rel="stylesheet" />
20+
<link href="{{ pathto('_static/css/index.c78d4f2b1f8277c2fa0830b4506d5cfe.css', 1) }}" rel="stylesheet" />
2121
{% endmacro %}
2222

2323
{% macro head_js_preload() %}
24-
<link rel="preload" as="script" href="{{ pathto('_static/js/index.c3e2d73f727876cf7323.js', 1) }}">
24+
<link rel="preload" as="script" href="{{ pathto('_static/js/index.8636327e669f6dcffc22.js', 1) }}">
2525
{% endmacro %}
2626

2727
{% macro body_post() %}
28-
<script src="{{ pathto('_static/js/index.c3e2d73f727876cf7323.js', 1) }}"></script>
28+
<script src="{{ pathto('_static/js/index.8636327e669f6dcffc22.js', 1) }}"></script>
2929
{% endmacro %}

pydata_sphinx_theme/theme.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +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 = content

src/scss/index.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ table.field-list {
210210
height: calc(100vh - 4rem);
211211
}
212212
}
213+
214+
&.no-sidebar {
215+
border-right: 0;
216+
}
213217
}
214218

215219
.bd-links {

tests/test_build.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,33 @@ def test_logo_name(file_regression, sphinx_build):
9797
sphinx_build.build(["-D", "html_logo="])
9898
index_html = sphinx_build.get("index.html")
9999
assert "PyData Tests" in index_html.select(".navbar-brand")[0].text.strip()
100+
101+
102+
def test_sidebar_visible(sphinx_build):
103+
"""The sidebar is shrunk when no sidebars specified in html_sidebars."""
104+
sphinx_build.copy()
105+
106+
sphinx_build.build()
107+
index_html = sphinx_build.get("page1.html")
108+
assert "col-md-3" in index_html.select(".bd-sidebar")[0].attrs["class"]
109+
110+
sphinx_build.build(["-D", "html_sidebars.page1="])
111+
index_html = sphinx_build.get("page1.html")
112+
assert "col-md-1" in index_html.select(".bd-sidebar")[0].attrs["class"]
113+
sphinx_build.clean()
114+
115+
116+
def test_navbar_align(sphinx_build):
117+
"""The navbar items align with the proper part of the page."""
118+
sphinx_build.copy()
119+
120+
sphinx_build.build()
121+
index_html = sphinx_build.get("index.html")
122+
assert "col-lg-9" in index_html.select("div#navbar-menu")[0].attrs["class"]
123+
124+
# Both the column alignment and the margin should be changed
125+
sphinx_build.build(["-D", "html_theme_options.navbar_align=right"])
126+
index_html = sphinx_build.get("index.html")
127+
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"]
129+
sphinx_build.clean()

0 commit comments

Comments
 (0)