Skip to content

Commit ef34571

Browse files
committed
Add patch scrollToActive (active main navigation item)
1 parent 77491ab commit ef34571

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Patch of scrollToActive of sphinxbook theme
3+
* Scroll to active navigation item
4+
*/
5+
6+
/**
7+
* A helper function to load scripts when the DOM is loaded.
8+
* This waits for everything to be on the page first before running, since
9+
* some functionality doesn't behave properly until everything is ready.
10+
*/
11+
var sbRunWhenDOMLoaded = (cb) => {
12+
if (document.readyState != "loading") {
13+
cb();
14+
} else if (document.addEventListener) {
15+
document.addEventListener("DOMContentLoaded", cb);
16+
} else {
17+
document.attachEvent("onreadystatechange", function () {
18+
if (document.readyState == "complete") cb();
19+
});
20+
}
21+
};
22+
23+
/**
24+
* Sidebar scroll on load.
25+
*
26+
* Detect the active page in the sidebar, and scroll so that it is centered on
27+
* the screen.
28+
*/
29+
var scrollToActive = () => {
30+
var navbar = document.getElementById("site-navigation");
31+
var navbar_scrollable = navbar.children[0];
32+
var active_pages = navbar.querySelectorAll(".active");
33+
var active_page = active_pages[active_pages.length - 1];
34+
// Only scroll the navbar if the active link is lower than 50% of the page
35+
if (
36+
active_page !== undefined &&
37+
active_page.offsetTop > $(window).height() * 0.5
38+
) {
39+
navbar_scrollable.scrollTop =
40+
active_page.offsetTop - $(window).height() * 0.2;
41+
}
42+
};
43+
44+
45+
sbRunWhenDOMLoaded(scrollToActive);
46+
47+
$(document).ready(function() {
48+
console.debug("debug patch_scrollToActive")
49+
});

docs/conf.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@
127127
"plone.restapi/src",
128128
]
129129

130+
html_js_files = [
131+
"patch_scrollToActive.js",
132+
]
133+
130134
html_extra_path = [
131135
"robots.txt",
132136
]
@@ -291,11 +295,13 @@ def source_replace(app, docname, source):
291295
result = result.replace(key, app.config.source_replacements[key])
292296
source[0] = result
293297

298+
294299
# Dict of replacements.
295300
source_replacements = {
296301
"{PLONE_BACKEND_VERSION}": "6.0.0b2",
297302
}
298303

304+
299305
def setup(app):
300-
app.add_config_value('source_replacements', {}, True)
301-
app.connect('source-read', source_replace)
306+
app.add_config_value("source_replacements", {}, True)
307+
app.connect("source-read", source_replace)

0 commit comments

Comments
 (0)