Skip to content

Commit 457d8a8

Browse files
Automatically collapse nested levels in TOC sidebar (pandas-dev#19)
1 parent 563e84a commit 457d8a8

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

pandas-docs/source/_themes/bootstrap_docs_theme/docs-toc.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
<nav id="bd-toc-nav">
44
<ul class="nav section-nav flex-column">
55
{% for item in page_toc.children recursive %}
6-
<li class="toc-entry toc-h{{ loop.depth + 1 }}">
7-
<a href="{{ item.url }}" class="nav-item nav-link">{{ item.title }}</a>
6+
<li class="nav-item toc-entry toc-h{{ loop.depth + 1 }}">
7+
<a href="{{ item.url }}" class="nav-link">{{ item.title }}</a>
88
{%- if item.children -%}
9-
<ul>
9+
<ul class="nav section-nav flex-column">
1010
{{ loop(item.children) }}
1111
</ul>
1212
{%- endif %}

pandas-docs/source/_themes/bootstrap_docs_theme/layout.html

+2-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454

5555
</div>
5656
</div>
57+
{%- include "scripts.html" %}
58+
{%- endblock %}
5759

58-
{# %- include "scripts.html" % #}
59-
</body>
60-
{%- endblock %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<script>
2+
// TOC sidebar - add "active" class to parent list
3+
//
4+
// Bootstrap's scrollspy adds the active class to the <a> link,
5+
// but for the automatic collapsing we need this on the parent list item.
6+
//
7+
// The event is triggered on "window" (and not the nav item as documented),
8+
// see https://github.com/twbs/bootstrap/issues/20086
9+
$(window).on("activate.bs.scrollspy", function(){
10+
var navLinks = document.querySelectorAll('#bd-toc-nav a');
11+
for (var i = 0; i < navLinks.length; i++) {
12+
var navLink = navLinks[i];
13+
navLink.parentElement.classList.remove('active');
14+
}
15+
var navLinks = document.querySelectorAll('#bd-toc-nav a.active');
16+
for (var i = 0; i < navLinks.length; i++) {
17+
var navLink = navLinks[i];
18+
navLink.parentElement.classList.add('active');
19+
}
20+
});
21+
</script>

pandas-docs/source/_themes/bootstrap_docs_theme/static/css/custom.css

+13-1
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,16 @@
223223

224224
.nav-link:hover {
225225
border-style: none;
226-
}
226+
}
227+
228+
229+
/* Collapsing of the TOC sidebar while scrolling */
230+
231+
/* Nav: hide second level (shown on .active) */
232+
.bd-toc .nav .nav {
233+
display: none;
234+
}
235+
236+
.bd-toc .nav > .active > ul {
237+
display: block;
238+
}

0 commit comments

Comments
 (0)